KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > util > common > Search


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.util.common;
21
22 import org.apache.commons.lang.StringUtils;
23
24 import net.sf.hibernate.HibernateException;
25 import net.sf.hibernate.Query;
26 import net.sf.hibernate.Session;
27 import net.sf.hibernate.util.HibernateUtil;
28
29 import za.org.coefficient.util.ejb.*;
30
31 import java.util.ArrayList JavaDoc;
32
33 /**
34  * @pojo2ejb.class
35  * name="Search"
36  * jndi-prefix="za/org/coefficient/util/"
37  *
38  *
39  * @web.resource-env-ref
40  * name="za/org/coefficient/util/Search"
41  * type="za.org.coefficient.util.common.Search"
42  * @web.resource-env-ref
43  * name="Search"
44  * type="za.org.coefficient.util.common.Search"
45  */

46 public class Search {
47     //~ Methods ================================================================
48

49     public int getPage(String JavaDoc query, int pageSize, int startElement,
50         Object JavaDoc[] vals, ArrayList JavaDoc page) {
51
52         int retVal = 0;
53         Session sess = null;
54         page.clear();
55
56         try {
57             sess = HibernateUtil.getUniqueSession();
58             net.sf.hibernate.Query quer = sess.createQuery(query);
59
60             for (int j = 0; j < vals.length; j++) {
61                 quer.setParameter(j, vals[j]);
62             }
63             quer.setMaxResults(pageSize);
64             quer.setFirstResult(startElement);
65             page.addAll(quer.list());
66
67             // Now select the count
68
int idx = query.indexOf("order by");
69             String JavaDoc temp = null;
70             if (idx != -1) {
71                 temp = query.substring(0, query.indexOf("order by"));
72             } else {
73                 temp = query;
74             }
75
76             // added this block to subtring query to get the search object.
77
//Search object used to get a distinct list for the countQuery
78
String JavaDoc name = "*";
79             String JavaDoc [] qryWords = StringUtils.split(query);
80             int word = 0;
81             if(!qryWords[0].trim().equalsIgnoreCase("from")) {
82                 if(qryWords[word].trim().equalsIgnoreCase("select")) {
83                     word++;
84                 }
85                 if(qryWords[word].trim().equalsIgnoreCase("distinct")) {
86                     word++;
87                 }
88                 name = "distinct " + qryWords[word];
89             }
90
91             temp = temp.replaceFirst("[Ff][Rr][Oo][Mm]", "from");
92             int fromIdx = temp.indexOf("from");
93             int commaIdx = temp.indexOf(",");
94             int replaceIdx = 0;
95             if ((fromIdx > commaIdx) && (commaIdx != -1)) {
96                 replaceIdx = commaIdx;
97             } else {
98                 replaceIdx = fromIdx;
99             }
100             String JavaDoc countQuery =
101                 "select count(" + name + ") "
102                 + temp.substring(replaceIdx, temp.length());
103
104             net.sf.hibernate.Query quer2 = sess.createQuery(countQuery);
105             System.arraycopy(vals, 0, vals, 0, vals.length);
106             for (int j = 0; j < vals.length; j++) {
107                 quer2.setParameter(j, vals[j]);
108             }
109             retVal = ((Integer JavaDoc) quer2.iterate()
110                                      .next()).intValue();
111         } catch (HibernateException he) {
112             he.printStackTrace();
113         } finally {
114             try {
115                 HibernateUtil.closeSession(sess);
116             } catch (HibernateException hee) {
117                 hee.printStackTrace();
118             }
119         }
120
121         return retVal;
122     }
123 }
124
Popular Tags