View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
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   * Represent the database type to be used.
26   * 
27   * Created on 27.10.2012
28   * 
29   * @author Christina Bohk
30   * @author Roland Ewald
31   */
32  public enum DatabaseType {
33  
34    /** HyperSQL data storage (default, file-based). */
35    HSQLDB,
36  
37    /** MySQL-based data storage. */
38    MYSQL,
39  
40    /** Generic data storage. */
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     * Prefix to associate certain configuration values with a database type.
59     * Should be unique per type.
60     * 
61     * @return the prefix
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 }