1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.database;
17
18 import org.jamesii.core.data.DBConnectionData;
19 import org.jamesii.core.util.misc.Pair;
20
21 import p3j.gui.misc.P3JConfigFile;
22 import p3j.misc.Misc;
23
24
25
26
27
28
29
30
31
32 public enum DatabaseType {
33
34
35 HSQLDB,
36
37
38 MYSQL,
39
40
41 GENERIC;
42
43 @Override
44 public String toString() {
45 switch (this) {
46 case HSQLDB:
47 return "File-based (default, relatively slow)";
48 case MYSQL:
49 return "MySQL (requires access to MySQL server)";
50 case GENERIC:
51 return "Generic (requires manual configuration)";
52 default:
53 throw new UnsupportedOperationException();
54 }
55 }
56
57
58
59
60
61
62
63 public String asPrefix() {
64 switch (this) {
65 case HSQLDB:
66 return "HSQLDB";
67 case MYSQL:
68 return "MySQL";
69 case GENERIC:
70 return "Generic";
71 default:
72 throw new UnsupportedOperationException();
73 }
74 }
75
76 public Pair<DBConnectionData, String> readPreferences(P3JConfigFile conf) {
77 if (conf.get(asPrefix() + Misc.PREF_DB_URL) == null)
78 return getDefaults();
79 return readPreferences(asPrefix(), conf);
80 }
81
82 public void writePreferences(P3JConfigFile conf,
83 Pair<DBConnectionData, String> connData) {
84 writePreferences("", conf, connData);
85 writePreferences(asPrefix(), conf, readPreferences("", conf));
86 }
87
88 public Pair<DBConnectionData, String> getDefaults() {
89 return new Pair<>(new DBConnectionData(Misc.DEFAULT_DB_URLS.get(this),
90 Misc.DEFAULT_DB_USERS.get(this), Misc.DEFAULT_DB_PWDS.get(this),
91 Misc.JDBC_DRIVERS.get(this)), Misc.HIBERNATE_DIALECTS.get(this));
92 }
93
94 private Pair<DBConnectionData, String> readPreferences(String prefix,
95 P3JConfigFile conf) {
96 return new Pair<>(new DBConnectionData(conf.get(prefix + Misc.PREF_DB_URL)
97 .toString(), conf.get(prefix + Misc.PREF_DB_USER).toString(), conf.get(
98 prefix + Misc.PREF_DB_PWD).toString(), conf.get(
99 prefix + Misc.PREF_HIBERNATE_DRIVER_PROPERTY).toString()), conf.get(
100 prefix + Misc.PREF_HIBERNATE_DIALECT_PROPERTY).toString());
101 }
102
103 private void writePreferences(String prefix, P3JConfigFile conf,
104 Pair<DBConnectionData, String> connData) {
105 conf.put(prefix + Misc.PREF_DB_URL, connData.getFirstValue().getURL());
106 conf.put(prefix + Misc.PREF_DB_USER, connData.getFirstValue().getUser());
107 conf.put(prefix + Misc.PREF_DB_PWD, connData.getFirstValue().getPassword());
108 conf.put(prefix + Misc.PREF_HIBERNATE_DRIVER_PROPERTY, connData
109 .getFirstValue().getDriver());
110 conf.put(prefix + Misc.PREF_HIBERNATE_DIALECT_PROPERTY,
111 connData.getSecondValue());
112 }
113
114 public IPreferencesUIProvider getPreferencesUIProvider() {
115 switch (this) {
116 case HSQLDB:
117 return new HSQLDBPreferencesUIProvider();
118 case MYSQL:
119 return new MySQLPreferencesUIProvider();
120 case GENERIC:
121 return new GenericPreferencesUIProvider();
122 default:
123 throw new UnsupportedOperationException();
124 }
125 }
126
127 }