1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.oodt.xmlps.profile;
19
20
21 import org.apache.oodt.xmlps.mapping.DatabaseTable;
22 import org.apache.oodt.xmlps.mapping.MappingReader;
23 import org.apache.oodt.xmlps.product.XMLPSProductHandler;
24 import org.apache.oodt.xmlps.profile.DBMSExecutor;
25 import org.apache.oodt.xmlps.queryparser.Expression;
26 import org.apache.oodt.xmlps.queryparser.HandlerQueryParser;
27
28
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.sql.SQLException;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Stack;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38
39
40 import org.apache.oodt.profile.Profile;
41 import org.apache.oodt.profile.ProfileException;
42 import org.apache.oodt.profile.handlers.ProfileHandler;
43 import org.apache.oodt.xmlquery.QueryElement;
44 import org.apache.oodt.xmlquery.XMLQuery;
45
46
47
48
49
50
51
52
53
54 public class XMLPSProfileHandler extends XMLPSProductHandler implements
55 ProfileHandler {
56
57 private DBMSExecutor executor;
58
59
60 private static final Logger LOG = Logger.getLogger(XMLPSProfileHandler.class
61 .getName());
62
63 private String resLocationSpec;
64
65 public XMLPSProfileHandler() throws InstantiationException {
66 super(null);
67 String mappingFilePath = System
68 .getProperty("org.apache.oodt.xmlps.profile.xml.mapFilePath");
69
70 if (mappingFilePath == null) {
71 throw new InstantiationException(
72 "Need to specify path to xml mapping file!");
73 }
74
75 try {
76 mapping = MappingReader.getMapping(mappingFilePath);
77 } catch (Exception e) {
78 throw new InstantiationException(
79 "Unable to parse profile mapping xml file: ["
80 + mappingFilePath + "]: reason: "
81 + e.getMessage());
82 }
83
84
85
86
87
88 String dbPropFilePath = System
89 .getProperty("org.apache.oodt.xmlps.profile.xml.dbPropFilePath");
90 if (dbPropFilePath != null) {
91 try {
92 System.getProperties()
93 .load(new FileInputStream(dbPropFilePath));
94 } catch (FileNotFoundException e) {
95
96 e.printStackTrace();
97 } catch (IOException e) {
98 e.printStackTrace();
99 throw new InstantiationException(e.getMessage());
100 }
101
102 executor = new DBMSExecutor();
103 }
104
105 this.resLocationSpec = System
106 .getProperty("org.apache.oodt.xmlps.profile.xml.resLocationSpec");
107 }
108
109
110
111
112
113
114 public List<Profile> findProfiles(XMLQuery query) throws ProfileException {
115 List<QueryElement> whereSet = query.getWhereElementSet();
116 List<QueryElement> selectSet = query.getSelectElementSet();
117 try {
118 translateToDomain(selectSet, true);
119 translateToDomain(whereSet, false);
120 } catch (Exception e) {
121 e.printStackTrace();
122 throw new ProfileException(e.getMessage());
123 }
124 List<Profile> profs = queryAndPackageProfiles(query);
125 return profs;
126 }
127
128
129
130
131
132
133 public Profile get(String id) throws ProfileException {
134 throw new ProfileException("Method not implemented!");
135 }
136
137
138
139
140
141
142 public String getID() {
143 return mapping.getId();
144 }
145
146 protected List<Profile> queryAndPackageProfiles(XMLQuery query) {
147 Stack<QueryElement> queryStack = HandlerQueryParser
148 .createQueryStack(query.getWhereElementSet());
149 Expression parsedQuery = HandlerQueryParser.parse(queryStack,
150 this.mapping);
151 List<Profile> profs = null;
152
153 StringBuffer sqlBuf = new StringBuffer("SELECT *");
154 sqlBuf.append(" FROM ");
155 sqlBuf.append(mapping.getDefaultTable());
156 sqlBuf.append(" ");
157
158 if (mapping.getNumTables() > 0) {
159 for (Iterator<String> i = mapping.getTableNames().iterator(); i
160 .hasNext();) {
161 String tableName = i.next();
162 if(tableName.equals(mapping.getDefaultTable())) continue;
163 DatabaseTable tbl = mapping.getTableByName(tableName);
164 sqlBuf.append("INNER JOIN ");
165 sqlBuf.append(tbl.getName());
166 sqlBuf.append(" ON ");
167 sqlBuf.append(tbl.getName());
168 sqlBuf.append(".");
169 sqlBuf.append(tbl.getJoinFieldName());
170 sqlBuf.append(" = ");
171 sqlBuf.append(tbl.getDefaultTableJoin());
172 sqlBuf.append(".");
173 sqlBuf.append(tbl.getDefaultTableJoinFieldName());
174 sqlBuf.append(" ");
175 }
176 }
177
178 if (parsedQuery != null) {
179 sqlBuf.append(" WHERE ");
180 sqlBuf.append(parsedQuery.evaluate());
181 }
182
183 LOG.log(Level.INFO, sqlBuf.toString());
184
185 if (executor != null) {
186 try {
187 profs = executor.executeLocalQuery(this.mapping, sqlBuf
188 .toString(), this.resLocationSpec);
189
190 } catch (SQLException e) {
191 e.printStackTrace();
192 LOG.log(Level.WARNING, "Error executing sql: ["
193 + sqlBuf.toString() + "]: Message: " + e.getMessage());
194 }
195 }
196
197 return profs;
198 }
199
200 protected void translateToDomain(List<QueryElement> elemSet,
201 boolean selectSet) throws Exception {
202 super.translateToDomain(elemSet, selectSet);
203 }
204
205 }