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.pppm.readerwriter.database;
17  
18  import java.net.URI;
19  
20  import org.jamesii.core.data.DBConnectionData;
21  import org.jamesii.core.data.model.IModelReader;
22  import org.jamesii.core.data.model.read.plugintype.IMIMEType;
23  import org.jamesii.core.data.model.read.plugintype.ModelReaderFactory;
24  import org.jamesii.core.model.IModel;
25  import org.jamesii.core.model.symbolic.ISymbolicModel;
26  import org.jamesii.core.parameters.ParameterBlock;
27  import org.jamesii.core.util.misc.Pair;
28  
29  import p3j.pppm.ProjectionModel;
30  import p3j.pppm.SymbolicProjectionModel;
31  
32  /**
33   * Factory for reader of PPPM files.
34   * 
35   * @author Christina Bohk
36   * @author Roland Ewald
37   * 
38   */
39  public class PPPModelDatabaseReaderFactory extends ModelReaderFactory {
40  
41    /** Serialization ID. */
42    private static final long serialVersionUID = 2423298767414579313L;
43  
44    /** Scheme for all supported database URIs. */
45    private static final String GENERAL_DB_URI_SCHEME = "db-p3j";
46  
47    /**
48     * The actual parameters of the {@link PPPMDatabaseReader} are encoded in the
49     * query of this default URL.
50     */
51    public static final String DEFAULT_URI = GENERAL_DB_URI_SCHEME
52        + "://localhost";
53  
54    /** The parameter block name to store the url in. */
55    private static final String KEY_CONN_DATA_URL = "url";
56  
57    /** The parameter block name to store the user name for the database in. */
58    private static final String KEY_CONN_DATA_USER = "user";
59  
60    /** The parameter block name to store the password for the database in. */
61    private static final String KEY_CONN_DATA_PWD = "pwd";
62  
63    /**
64     * The parameter block name to store the name of the database driver class in.
65     */
66    private static final String KEY_CONN_DATA_DRIVER = "driver";
67  
68    /** The parameter block name to store the projection ID in. */
69    private static final String KEY_PROJECT_ID = "projId";
70  
71    @Override
72    public IModelReader create(ParameterBlock params) {
73      Pair<DBConnectionData, Integer> readerParams = retrieveReaderParams(params);
74      return new PPPMDatabaseReader(readerParams.getFirstValue(),
75          readerParams.getSecondValue());
76    }
77  
78    @Override
79    public boolean supportsModel(IModel model) {
80      return model instanceof ProjectionModel;
81    }
82  
83    @Override
84    public boolean supportsModel(ISymbolicModel<?> model) {
85      return model instanceof SymbolicProjectionModel;
86    }
87  
88    @Override
89    public boolean supportsURI(URI uri) {
90      return uri.getScheme().equals(GENERAL_DB_URI_SCHEME);
91    }
92  
93    @Override
94    public boolean supportsMIMEType(IMIMEType mime) {
95      return false;
96    }
97  
98    /**
99     * Creates a parameter block that contains all relevant data for a database
100    * model reader.
101    * 
102    * @param connData
103    *          the connection data
104    * @param projectionID
105    *          the projection id
106    * @return the parameter block all relevant parameters for the database model
107    *         reader
108    */
109   public static ParameterBlock createReaderParams(DBConnectionData connData,
110       int projectionID) {
111     ParameterBlock readerParams = new ParameterBlock();
112     readerParams.addSubBlock(KEY_CONN_DATA_URL, connData.getURL());
113     readerParams.addSubBlock(KEY_CONN_DATA_USER, connData.getUser());
114     readerParams.addSubBlock(KEY_CONN_DATA_PWD, connData.getPassword());
115     readerParams.addSubBlock(KEY_CONN_DATA_DRIVER, connData.getDriver());
116     readerParams.addSubBlock(KEY_PROJECT_ID, projectionID);
117     return readerParams;
118   }
119 
120   /**
121    * Retrieve reader parameters from parameter block.
122    * 
123    * @param params
124    *          the parameter block
125    * @return the connection data and the projection id
126    */
127   public static Pair<DBConnectionData, Integer> retrieveReaderParams(
128       ParameterBlock params) {
129     return new Pair<DBConnectionData, Integer>(new DBConnectionData(
130         params.getSubBlockValue(KEY_CONN_DATA_URL, ""),
131         params.getSubBlockValue(KEY_CONN_DATA_USER, ""),
132         params.getSubBlockValue(KEY_CONN_DATA_PWD, ""),
133         params.getSubBlockValue(KEY_CONN_DATA_DRIVER, "")),
134         params.getSubBlockValue(KEY_PROJECT_ID, -1));
135   }
136 
137 }