1 /*
2 * This file is part of Waarp Project (named also Waarp or GG).
3 *
4 * Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5 * tags. See the COPYRIGHT.txt in the distribution for a full listing of
6 * individual contributors.
7 *
8 * All Waarp Project is free software: you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * Waarp . If not, see <http://www.gnu.org/licenses/>.
19 */
20 package org.waarp.common.database.model;
21
22 import org.waarp.common.database.DbAdmin;
23 import org.waarp.common.database.exception.WaarpDatabaseNoConnectionException;
24
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Set;
29
30 /**
31 * Factory to store the Database Model object
32 */
33 public class DbModelFactory {
34
35 /**
36 * Info on JDBC Class is already loaded or not
37 */
38 public static final Set<String> classLoaded = new HashSet<String>();
39 /**
40 * Database Model Object list
41 */
42 public static final List<DbModel> dbModels = new ArrayList<DbModel>();
43
44 /**
45 * Initialize the Database Model according to arguments.
46 *
47 * @param dbdriver
48 * @param dbserver
49 * @param dbuser
50 * @param dbpasswd
51 * @param write
52 *
53 * @throws WaarpDatabaseNoConnectionException
54 */
55 public static DbAdmin initialize(final String dbdriver, final String dbserver,
56 final String dbuser, final String dbpasswd,
57 final boolean write)
58 throws WaarpDatabaseNoConnectionException {
59 final DbType type = DbType.getFromDriver(dbdriver);
60 final DbModel dbModel = null;
61 switch (type) {
62 case H2:
63 // dbModel = new DbModelH2(dbserver, dbuser, dbpasswd)
64 break;
65 case Oracle:
66 // dbModel = new DbModelOracle(dbserver, dbuser, dbpasswd)
67 break;
68 case PostGreSQL:
69 // dbModel = new DbModelPostgresql()
70 break;
71 case MySQL:
72 // dbModel = new DbModelMysql(dbserver, dbuser, dbpasswd)
73 break;
74 case MariaDB:
75 // dbModel = new DbModelMariadb(dbserver, dbuser, dbpasswd)
76 break;
77 default:
78 throw new WaarpDatabaseNoConnectionException(
79 "TypeDriver unknown: " + type);
80 }
81 if (dbModel == null) {
82 throw new WaarpDatabaseNoConnectionException(
83 "TypeDriver not allocated: " + type);
84 }
85 dbModels.add(dbModel);
86 return new DbAdmin(dbModel, dbserver, dbuser, dbpasswd, write);
87 }
88
89 /**
90 * Check if current DbModels are one of given DbType
91 *
92 * @param types
93 *
94 * @return True if present
95 */
96 public static boolean containsDbType(final DbType... types) {
97 for (final DbModel dbModel : dbModels) {
98 for (final DbType dbType : types) {
99 if (dbModel.getDbType() == dbType) {
100 return true;
101 }
102 }
103 }
104 return false;
105 }
106 }