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 james.SimSystem;
19  import james.core.data.DBConnectionData;
20  import james.core.data.model.IModelReader;
21  import james.core.model.IModel;
22  import james.core.model.symbolic.ISymbolicModel;
23  import james.core.util.misc.Pair;
24  
25  import java.net.URI;
26  import java.util.Map;
27  
28  import p3j.database.IP3MDatabase;
29  import p3j.database.hibernate.P3MDatabase;
30  import p3j.pppm.ProjectionModel;
31  import p3j.pppm.SymbolicProjectionModel;
32  
33  /**
34   * Model reader to access the PPP model database.
35   * 
36   * @author Christina Bohk
37   * @author Roland Ewald
38   */
39  public class PPPMDatabaseReader implements IModelReader {
40  
41  	/** Default scheme for the database URL. */
42  	public static final String DEFAULT_DB_URL_SCHEME = "jdbc:mysql://";
43  
44  	@Override
45  	public ISymbolicModel<?> read(URI ident) {
46  
47  		ProjectionModel model = null;
48  
49  		// Analyse model location, open DB connection
50  		Pair<DBConnectionData, Integer> readerInfo = resolveModelDBURI(ident);
51  		IP3MDatabase sqlDatabase = new P3MDatabase();
52  		sqlDatabase.init(readerInfo.getFirstValue());
53  		try {
54  			sqlDatabase.open();
55  			model = sqlDatabase.getProjectionByID(readerInfo.getSecondValue());
56  		} catch (Exception ex) {
57  			SimSystem.report(ex);
58  			return null;
59  		}
60  		if (model == null) {
61  			throw new IllegalArgumentException("PPP Model with ID '"
62  			    + readerInfo.getSecondValue() + "' was not found.");
63  		}
64  		return new SymbolicProjectionModel(model);
65  	}
66  
67  	@Override
68  	public IModel read(URI source, Map<String, ?> parameters) {
69  		return (IModel) read(source).getAsDataStructure();
70  	}
71  
72  	/**
73  	 * Returns important information for the reader, extracted from the
74  	 * {@link URI}.
75  	 * 
76  	 * @param modelURI
77  	 *          the URI of the model
78  	 * @return a tuple containing DB connection information and the ID of the
79  	 *         {@link ProjectionModel} to be read
80  	 */
81  	protected static Pair<DBConnectionData, Integer> resolveModelDBURI(
82  	    URI modelURI) {
83  		String[] userInfo = modelURI.getUserInfo().split(":");
84  		String userName = "";
85  		String passwd = "";
86  		if (userInfo.length >= 1) {
87  			userName = userInfo[0];
88  		}
89  		if (userInfo.length >= 2) {
90  			passwd = userInfo[1];
91  		}
92  		String dbURL = modelURI.getHost() + modelURI.getPath();
93  		DBConnectionData dbConnData = new DBConnectionData(DEFAULT_DB_URL_SCHEME
94  		    + dbURL, userName, passwd, null);
95  		return new Pair<DBConnectionData, Integer>(dbConnData,
96  		    Integer.parseInt(modelURI.getQuery()));
97  	}
98  }