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