587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package com.nokia.helium.metadata;
|
|
18 |
|
|
19 |
import java.io.File;
|
|
20 |
import java.util.List;
|
|
21 |
import java.util.Map;
|
|
22 |
import java.util.ArrayList;
|
|
23 |
import fmpp.Engine;
|
|
24 |
import fmpp.ProgressListener;
|
|
25 |
import fmpp.tdd.DataLoader;
|
|
26 |
import freemarker.template.TemplateCollectionModel;
|
|
27 |
import freemarker.template.TemplateModel;
|
|
28 |
import freemarker.template.TemplateHashModel;
|
|
29 |
import freemarker.template.TemplateSequenceModel;
|
|
30 |
import freemarker.template.SimpleScalar;
|
|
31 |
import freemarker.template.SimpleNumber;
|
|
32 |
import freemarker.template.TemplateModelIterator;
|
|
33 |
import com.nokia.helium.jpa.ORMReader;
|
|
34 |
import org.apache.log4j.Logger;
|
|
35 |
import freemarker.ext.beans.BeanModel;
|
|
36 |
import freemarker.ext.beans.BeansWrapper;
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
/**
|
|
41 |
* Utility class to access the data from the database and used by FMPP
|
|
42 |
* templates.
|
|
43 |
*/
|
|
44 |
public class ORMFMPPLoader implements DataLoader {
|
|
45 |
//private ResultSet rs;
|
|
46 |
private static final int READ_LIMIT = 20000;
|
|
47 |
|
|
48 |
private static Logger log = Logger.getLogger(ORMFMPPLoader.class);
|
|
49 |
|
|
50 |
private ORMReader reader;
|
|
51 |
|
|
52 |
|
|
53 |
/**
|
|
54 |
* @see fmpp.tdd.DataLoader#load(fmpp.Engine, java.util.List)
|
|
55 |
*/
|
|
56 |
public Object load(Engine engine, List args) throws Exception {
|
|
57 |
//log.debug("args.size:" + args.size());
|
|
58 |
java.util.ListIterator iter = args.listIterator();
|
|
59 |
int argsSize = args.size();
|
|
60 |
if (argsSize < 1) {
|
|
61 |
throw new Exception("Input DB path should be provided to load into FMPP.");
|
|
62 |
}
|
|
63 |
ORMQueryModeModel model = new ORMQueryModeModel((String) (args.get(0)));
|
|
64 |
engine.setAttribute(this.getClass().toString(), model);
|
|
65 |
return model;
|
|
66 |
}
|
|
67 |
|
|
68 |
/**
|
|
69 |
* QueryModel (which supports hash, sequence, containers)
|
|
70 |
* arg[0] - dbpath
|
|
71 |
*
|
|
72 |
*/
|
|
73 |
private class ORMQueryModeModel implements TemplateHashModel, ProgressListener {
|
|
74 |
|
|
75 |
private String dbPath;
|
|
76 |
public ORMQueryModeModel(String path) {
|
|
77 |
File actualPath = new File(path);
|
|
78 |
String fileName = actualPath.getName();
|
|
79 |
dbPath = new File(actualPath.getParent(), fileName.toLowerCase()).getPath();
|
|
80 |
}
|
|
81 |
|
|
82 |
/*
|
|
83 |
* Gets the template model for the corresponding query
|
|
84 |
* @param query for which the model is returned.
|
|
85 |
* @return returns the template model for the query
|
|
86 |
*/
|
|
87 |
public QueryTemplateModel get(String mode) {
|
|
88 |
String retType = null;
|
|
89 |
String actualMode = mode;
|
|
90 |
//log.debug("mode: " + mode);
|
|
91 |
if (mode.startsWith("native")) {
|
|
92 |
String [] splitString = mode.split(":");
|
|
93 |
if (splitString.length == 2 ) {
|
|
94 |
actualMode = splitString[0];
|
|
95 |
retType = splitString[1];
|
|
96 |
//log.debug("actualMode " + actualMode);
|
|
97 |
//log.debug("retType " + retType);
|
|
98 |
}
|
|
99 |
}
|
|
100 |
|
|
101 |
return new QueryTemplateModel(dbPath, actualMode, retType);
|
|
102 |
}
|
|
103 |
|
|
104 |
public boolean isEmpty() {
|
|
105 |
return false;
|
|
106 |
}
|
|
107 |
|
|
108 |
/**
|
|
109 |
* {@inheritDoc}
|
|
110 |
*/
|
|
111 |
@Override
|
|
112 |
public void notifyProgressEvent(Engine engine, int event, File src,
|
|
113 |
int pMode, Throwable error, Object param) throws Exception {
|
618
|
114 |
//if (event == ProgressListener.EVENT_END_PROCESSING_SESSION) {
|
|
115 |
// log.debug("notifyProgressEvent - finalizeORM");
|
|
116 |
// ORMUtil.finalizeORM(dbPath);
|
|
117 |
//}
|
587
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
/*
|
|
122 |
* Internal class to handle the sql query and returns the data in either
|
|
123 |
* hash or sequence or containers.
|
|
124 |
*/
|
|
125 |
private class QueryTemplateModel implements TemplateHashModel {
|
|
126 |
|
|
127 |
private String dbPath;
|
|
128 |
|
|
129 |
private String queryMode;
|
|
130 |
|
|
131 |
private String returnType;
|
|
132 |
|
|
133 |
private TemplateModel resultObject;
|
|
134 |
|
|
135 |
public QueryTemplateModel(String path, String mode, String retType) {
|
|
136 |
dbPath = path;
|
|
137 |
queryMode = mode;
|
|
138 |
returnType = retType;
|
|
139 |
}
|
|
140 |
|
|
141 |
public TemplateModel get(String query) {
|
|
142 |
//log.debug("QueryTemplateModel: query" + query);
|
|
143 |
if (queryMode.equals("jpasingle")) {
|
|
144 |
//log.debug("query executing with single result mode");
|
618
|
145 |
resultObject = getModel(dbPath, query, returnType);
|
|
146 |
|
587
|
147 |
} else {
|
|
148 |
//log.debug("query executing with multiple result mode");
|
618
|
149 |
resultObject = new ORMQueryModel(dbPath, query, queryMode, returnType);
|
587
|
150 |
}
|
|
151 |
return resultObject;
|
|
152 |
}
|
|
153 |
|
618
|
154 |
private TemplateModel getModel(String dbPath, String query,
|
|
155 |
String returnType) {
|
|
156 |
ORMReader reader = new ORMReader(dbPath);
|
|
157 |
ORMSequenceModel model = new ORMSequenceModel(reader.executeSingleResult(query, returnType));
|
|
158 |
reader.close();
|
|
159 |
return model;
|
587
|
160 |
}
|
|
161 |
|
|
162 |
public boolean isEmpty() {
|
|
163 |
//log.debug("query hash isempty:");
|
|
164 |
return false;
|
|
165 |
}
|
|
166 |
}
|
|
167 |
|
|
168 |
private class ORMSequenceModel implements TemplateSequenceModel {
|
|
169 |
private List ormList = new ArrayList();
|
|
170 |
|
|
171 |
@SuppressWarnings("unchecked")
|
|
172 |
public ORMSequenceModel(Object obj) {
|
|
173 |
ormList.add(obj);
|
|
174 |
}
|
|
175 |
public int size() {
|
|
176 |
return ormList.size();
|
|
177 |
}
|
|
178 |
|
|
179 |
public TemplateModel get(int index) {
|
|
180 |
//log.debug("ORMSequenceModel.get: index: " + index);
|
|
181 |
if (index < ormList.size()) {
|
|
182 |
Object obj = ormList.get(index);
|
|
183 |
if (obj instanceof String) {
|
|
184 |
return new SimpleScalar((String)obj);
|
|
185 |
} else if (obj instanceof Number) {
|
|
186 |
return new SimpleNumber((Number)obj);
|
|
187 |
} else if (obj == null) {
|
|
188 |
return null;
|
|
189 |
} else {
|
|
190 |
return new ORMObjectModel(obj);
|
|
191 |
}
|
|
192 |
}
|
|
193 |
return null;
|
|
194 |
}
|
|
195 |
}
|
|
196 |
|
|
197 |
private class ORMQueryModel implements TemplateCollectionModel
|
|
198 |
{
|
|
199 |
|
|
200 |
private ORMReader ormReader;
|
|
201 |
private String queryType;
|
|
202 |
private String query;
|
|
203 |
private String returnType;
|
|
204 |
|
618
|
205 |
public ORMQueryModel (String dbPath, String queryString, String type, String retType) {
|
|
206 |
ormReader = new ORMReader(dbPath);
|
587
|
207 |
queryType = type;
|
|
208 |
query = queryString;
|
|
209 |
returnType = retType;
|
|
210 |
//log.debug("ORMQueryModel: query" + query);
|
|
211 |
}
|
|
212 |
|
|
213 |
/*
|
|
214 |
* Provides data via collection interface.
|
|
215 |
* @return the iterator model from which the data is accessed.
|
|
216 |
*/
|
|
217 |
public TemplateModelIterator iterator() {
|
|
218 |
//log.debug("iterator constructor called");
|
|
219 |
return new ORMTemplateModelIterator(ormReader, query, queryType, returnType);
|
|
220 |
}
|
|
221 |
|
|
222 |
}
|
|
223 |
|
|
224 |
/*
|
|
225 |
* Internal Iterator class which provides data as collection.
|
|
226 |
*/
|
|
227 |
private class ORMTemplateModelIterator implements TemplateModelIterator {
|
|
228 |
|
|
229 |
private String query;
|
|
230 |
private List<Map<String, Object>> rowList;
|
|
231 |
private int currentOffsetIndex;
|
|
232 |
private boolean finished;
|
|
233 |
private String returnType;
|
|
234 |
private boolean nativeQuery;
|
|
235 |
private ORMReader ormReader;
|
|
236 |
|
|
237 |
public ORMTemplateModelIterator(ORMReader reader, String queryString, String type, String retType) {
|
|
238 |
ormReader = reader;
|
|
239 |
query = queryString;
|
|
240 |
returnType = retType;
|
|
241 |
if (type.startsWith("native")) {
|
|
242 |
nativeQuery = true;
|
|
243 |
}
|
|
244 |
//log.debug("ORMTemplateModelIterator: query" + query);
|
|
245 |
}
|
|
246 |
|
|
247 |
/**
|
|
248 |
* {@inheritDoc}
|
|
249 |
*/
|
|
250 |
@SuppressWarnings("unchecked")
|
|
251 |
public TemplateModel next() {
|
|
252 |
//log.debug("ORMTemplateModelIterator: next");
|
|
253 |
Object toRet = null;
|
|
254 |
if (rowList != null) {
|
|
255 |
toRet = rowList.remove(0);
|
|
256 |
}
|
|
257 |
if (nativeQuery && returnType.equals("java.lang.String")) {
|
|
258 |
return new SimpleScalar((java.lang.String)toRet);
|
|
259 |
}
|
|
260 |
return new ORMObjectModel(toRet);
|
|
261 |
}
|
|
262 |
|
|
263 |
/**
|
|
264 |
* {@inheritDoc}
|
|
265 |
*/
|
|
266 |
@SuppressWarnings("unchecked")
|
|
267 |
public boolean hasNext() {
|
|
268 |
if (rowList == null || rowList.size() == 0) {
|
|
269 |
if (!finished) {
|
|
270 |
if (nativeQuery) {
|
|
271 |
if (returnType.equals("java.lang.String")) {
|
|
272 |
rowList = ormReader.executeNativeQuery(query, null);
|
|
273 |
} else {
|
|
274 |
rowList = ormReader.executeNativeQuery(query, returnType);
|
|
275 |
}
|
|
276 |
} else {
|
|
277 |
rowList = ormReader.executeQuery(query);
|
|
278 |
}
|
|
279 |
if (rowList == null || rowList.size() == 0) {
|
|
280 |
finished = true;
|
618
|
281 |
ormReader.close();
|
587
|
282 |
}
|
|
283 |
}
|
|
284 |
}
|
|
285 |
return !finished;
|
|
286 |
}
|
|
287 |
}
|
|
288 |
|
|
289 |
private class ORMObjectModel extends BeanModel {
|
|
290 |
public ORMObjectModel(Object obj) {
|
|
291 |
super(obj, new BeansWrapper());
|
|
292 |
}
|
|
293 |
}
|
|
294 |
} |