628
|
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.fmpp;
|
|
18 |
|
|
19 |
import java.util.List;
|
|
20 |
|
|
21 |
import javax.persistence.EntityManager;
|
|
22 |
import javax.persistence.Query;
|
|
23 |
|
|
24 |
import freemarker.template.SimpleScalar;
|
|
25 |
import freemarker.template.TemplateModel;
|
|
26 |
import freemarker.template.TemplateModelIterator;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* Internal Iterator class which provides data as collection.
|
|
30 |
*/
|
|
31 |
class ORMTemplateModelIterator implements TemplateModelIterator {
|
|
32 |
|
|
33 |
private static final int PAGE_SIZE = 2000;
|
|
34 |
private String queryString;
|
|
35 |
private Query query;
|
|
36 |
private int index;
|
|
37 |
private String returnType;
|
|
38 |
private boolean nativeQuery;
|
|
39 |
private EntityManager entityManager;
|
|
40 |
private List<Object> data;
|
|
41 |
private int currentPage;
|
|
42 |
private Object next;
|
|
43 |
|
|
44 |
public ORMTemplateModelIterator(EntityManager entityManager, String queryString, String type, String retType) {
|
|
45 |
this.entityManager = entityManager;
|
|
46 |
this.queryString = queryString;
|
|
47 |
returnType = retType;
|
|
48 |
if (type.startsWith("native")) {
|
|
49 |
nativeQuery = true;
|
|
50 |
}
|
|
51 |
index = 0;
|
|
52 |
}
|
|
53 |
|
|
54 |
/**
|
|
55 |
* {@inheritDoc}
|
|
56 |
*/
|
|
57 |
public TemplateModel next() {
|
|
58 |
Object value = next;
|
|
59 |
next = null;
|
|
60 |
if (value != null) {
|
|
61 |
if (nativeQuery && returnType.equals("java.lang.String")) {
|
|
62 |
return new SimpleScalar((String)value);
|
|
63 |
} else {
|
|
64 |
return new ORMObjectModel(value);
|
|
65 |
}
|
|
66 |
}
|
|
67 |
return null;
|
|
68 |
}
|
|
69 |
|
|
70 |
/**
|
|
71 |
* {@inheritDoc}
|
|
72 |
*/
|
|
73 |
@SuppressWarnings("unchecked")
|
|
74 |
public boolean hasNext() {
|
|
75 |
int page = index / PAGE_SIZE;
|
|
76 |
if (query == null || page != currentPage) {
|
|
77 |
if (nativeQuery) {
|
|
78 |
query = entityManager.createNativeQuery(queryString);
|
|
79 |
} else {
|
|
80 |
query = entityManager.createQuery(queryString);
|
|
81 |
}
|
|
82 |
query.setFirstResult(page * PAGE_SIZE);
|
|
83 |
query.setMaxResults(PAGE_SIZE);
|
|
84 |
data = query.getResultList();
|
|
85 |
currentPage = page;
|
|
86 |
}
|
|
87 |
if (next == null && index < data.size()) {
|
|
88 |
next = data.get(index);
|
|
89 |
index++;
|
|
90 |
}
|
|
91 |
return next != null;
|
|
92 |
}
|
|
93 |
}
|