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 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   * Represent the database type to be used.
25   * 
26   * Created on 27.10.2012
27   * 
28   * @author Christina Bohk
29   * @author Roland Ewald
30   */
31  public enum DatabaseType {
32  
33    /** HyperSQL data storage (default, file-based). */
34    HSQLDB,
35  
36    /** MySQL-based data storage. */
37    MYSQL,
38  
39    /** Generic data storage. */
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     * Prefix to associate certain configuration values with a database type.
58     * Should be unique per type.
59     * 
60     * @return the prefix
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 }