1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
35
36
37
38
39 public class PPPMDatabaseReader implements IModelReader {
40
41
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
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
74
75
76
77
78
79
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 }