|
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 org.apache.log4j.Logger; |
|
25 |
|
26 import freemarker.template.SimpleNumber; |
|
27 import freemarker.template.SimpleScalar; |
|
28 import freemarker.template.TemplateModel; |
|
29 import freemarker.template.TemplateSequenceModel; |
|
30 |
|
31 class ORMSequenceModel implements TemplateSequenceModel { |
|
32 public static final int PAGE_SIZE = 750; |
|
33 private static Logger log = Logger.getLogger(ORMSequenceModel.class); |
|
34 private Query query; |
|
35 private int currentPage; |
|
36 private List<Object> data; |
|
37 private int size; |
|
38 private EntityManager entityManager; |
|
39 private String queryString; |
|
40 |
|
41 @SuppressWarnings("unchecked") |
|
42 public ORMSequenceModel(EntityManager entityManager, String queryString) { |
|
43 log.debug("ORMSequenceModel: " + queryString); |
|
44 this.queryString = queryString; |
|
45 this.entityManager = entityManager; |
|
46 // Caching the full size |
|
47 int page = 0; |
|
48 do { |
|
49 query = entityManager.createQuery(queryString); |
|
50 query.setFirstResult(PAGE_SIZE * page); |
|
51 query.setMaxResults(PAGE_SIZE); |
|
52 data = query.getResultList(); |
|
53 // incrementing the size and page. |
|
54 size += data.size(); |
|
55 page++; |
|
56 } while (data.size() == PAGE_SIZE); |
|
57 |
|
58 // First query |
|
59 currentPage = 0; |
|
60 query = entityManager.createQuery(queryString); |
|
61 query.setFirstResult(currentPage); |
|
62 query.setMaxResults(PAGE_SIZE); |
|
63 data = query.getResultList(); |
|
64 } |
|
65 |
|
66 /** |
|
67 * {@inheritDoc} |
|
68 */ |
|
69 public int size() { |
|
70 return size; |
|
71 } |
|
72 |
|
73 /** |
|
74 * {@inheritDoc} |
|
75 */ |
|
76 @SuppressWarnings("unchecked") |
|
77 public synchronized TemplateModel get(int index) { |
|
78 // Calculating the requested page |
|
79 int page = index / PAGE_SIZE; |
|
80 // Shall we load a new page |
|
81 if (page != currentPage) { |
|
82 query = entityManager.createQuery(queryString); |
|
83 query.setFirstResult(page * PAGE_SIZE); |
|
84 query.setMaxResults(PAGE_SIZE); |
|
85 data = query.getResultList(); |
|
86 currentPage = page; |
|
87 if (data.size() == 0) { |
|
88 return null; |
|
89 } |
|
90 } |
|
91 // Are we out of bound. |
|
92 if (data.size() <= index % PAGE_SIZE) { |
|
93 return null; |
|
94 } |
|
95 // Let's get the object. |
|
96 Object obj = data.get(index % PAGE_SIZE); |
|
97 if (obj instanceof String) { |
|
98 return new SimpleScalar((String)obj); |
|
99 } else if (obj instanceof Number) { |
|
100 return new SimpleNumber((Number)obj); |
|
101 } else if (obj == null) { |
|
102 return null; |
|
103 } else { |
|
104 return new ORMObjectModel(obj); |
|
105 } |
|
106 } |
|
107 |
|
108 } |