KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > om > QueryPeer


1 package org.tigris.scarab.om;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 import java.util.List JavaDoc;
50 import java.util.ArrayList JavaDoc;
51 import java.io.Serializable JavaDoc;
52 import org.apache.torque.util.Criteria;
53
54 // Local classes
55
import org.tigris.scarab.om.Module;
56
57 /**
58  * This is the QueryPeer class
59  *
60  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
61  * @version $Id: QueryPeer.java 9400 2005-01-16 01:56:12Z dabbous $
62  */

63 public class QueryPeer
64     extends org.tigris.scarab.om.BaseQueryPeer
65 {
66
67     static final String JavaDoc GET_QUERIES =
68         "getQueries";
69     static final String JavaDoc GET_USER_QUERIES =
70         "getUserQueries";
71     static final String JavaDoc GET_MODULE_QUERIES =
72         "getModuleQueries";
73     static final String JavaDoc QUERY_PEER =
74         "QueryPeer";
75
76     // query types
77
public static final String JavaDoc TYPE_ALL_USER = "allPrivate";
78     public static final String JavaDoc TYPE_PRIVATE = "private";
79     public static final String JavaDoc TYPE_GLOBAL = "global";
80     public static final String JavaDoc TYPE_ALL = "all";
81
82     // sort columns
83
public static final String JavaDoc SORT_NAME = "name";
84     public static final String JavaDoc SORT_DESCRIPTION = "desc";
85     public static final String JavaDoc SORT_AVAILABILITY = "avail";
86     public static final String JavaDoc SORT_USER = "user";
87
88     /**
89      * List of queries associated with the module or created by the user.
90      * valid type is private, global, or all.
91      * if issueType == null, ignore issue type, otherwise include queries
92      * that only use that issueType or use multiple issue types.
93      */

94     public static List JavaDoc getQueries(Module module, IssueType issueType,
95                                   ScarabUser user, String JavaDoc sortColumn,
96                                   String JavaDoc sortPolarity, String JavaDoc type)
97         throws Exception JavaDoc
98     {
99         List JavaDoc queries = null;
100         if (module == null)
101         {
102             queries = new ArrayList JavaDoc();
103         }
104         else
105         {
106             // FIXME: 4th element is ignored due to bug in torque
107
// not yet fixed in torque-3.0
108
Serializable JavaDoc[] key = {QUERY_PEER, GET_QUERIES, module, null,
109                 issueType, user, sortColumn, sortPolarity, type};
110             Object JavaDoc obj = QueryManager.getMethodResult().get(key);
111             if (obj == null)
112             {
113
114                 Criteria crit = new Criteria()
115                 .add(QueryPeer.DELETED, 0);
116
117                 Criteria.Criterion moduleCrit = crit.getNewCriterion(
118                         QueryPeer.MODULE_ID, module.getModuleId(), Criteria.EQUAL);
119                 Criteria.Criterion crossModule = crit.getNewCriterion(
120                         QueryPeer.MODULE_ID, null, Criteria.EQUAL);
121                 moduleCrit.or(crossModule);
122
123                 if (issueType != null)
124             {
125                 Criteria.Criterion issueTypeCrit = crit.getNewCriterion(
126                     QueryPeer.ISSUE_TYPE_ID, issueType.getIssueTypeId(),
127                     Criteria.EQUAL);
128                 Criteria.Criterion nullIssueTypeCrit = crit.getNewCriterion(
129                     QueryPeer.ISSUE_TYPE_ID, null, Criteria.EQUAL);
130                 moduleCrit.and(issueTypeCrit.or(nullIssueTypeCrit));
131             }
132             
133
134
135
136             
137             if (TYPE_PRIVATE.equals(type))
138             {
139                 crit.add(userPrivateQueriesCrits(user, crit, moduleCrit));
140             }
141             else if (TYPE_GLOBAL.equals(type))
142             {
143                 crit.add(allGlobalQueriesCrit(crit, moduleCrit));
144             }
145             else if (TYPE_ALL_USER.equals(type))
146             {
147                 Criteria.Criterion cuGlob = userUnapprovedQueriesCrits(user, crit, moduleCrit);
148                 Criteria.Criterion cPriv = userPrivateQueriesCrits(user, crit, moduleCrit);
149                 cuGlob.or(cPriv);
150                 crit.add(cuGlob);
151             }
152             else
153             {
154                 // All queries
155
Criteria.Criterion cGlob = allGlobalQueriesCrit(crit, moduleCrit);
156                 Criteria.Criterion cPriv = userPrivateQueriesCrits(user, crit, moduleCrit);
157                 cGlob.or(cPriv);
158                 crit.add(cGlob);
159             }
160
161             // Add sort criteria
162
if (SORT_DESCRIPTION.equals(sortColumn))
163             {
164                 addSortOrder(crit, QueryPeer.DESCRIPTION,
165                              sortPolarity);
166             }
167             else if (SORT_AVAILABILITY.equals(sortColumn))
168             {
169                 crit.addJoin(QueryPeer.SCOPE_ID,
170                              ScopePeer.SCOPE_ID);
171                 addSortOrder(crit, ScopePeer.SCOPE_NAME, sortPolarity);
172             }
173             else if (SORT_USER.equals(sortColumn))
174             {
175                 addSortOrder(crit, QueryPeer.USER_ID, sortPolarity);
176             }
177             else
178             {
179                 // sort by name
180
addSortOrder(crit, QueryPeer.NAME, sortPolarity);
181             }
182             String JavaDoc tmp = crit.toString();
183             queries = QueryPeer.doSelect(crit);
184             QueryManager.getMethodResult().put(queries, key);
185         }
186         else
187         {
188             queries = (List JavaDoc)obj;
189         }
190         }
191         return queries;
192     }
193
194     /**
195      * Return all user private queries.
196      * @param user
197      * @param crit
198      * @param moduleCrit
199      * @return
200      */

201     private static Criteria.Criterion userPrivateQueriesCrits(ScarabUser user, Criteria crit, Criteria.Criterion moduleCrit)
202     {
203         Criteria.Criterion cPriv = crit.getNewCriterion(
204                 QueryPeer.USER_ID, user.getUserId(), Criteria.EQUAL);
205         cPriv.and(crit.getNewCriterion(
206                 QueryPeer.SCOPE_ID, Scope.PERSONAL__PK,
207                 Criteria.EQUAL));
208         // need to be careful here, we are adding moduleCrit to
209
// two different criterion. if we switched the order of
210
// the OR below we would screw up cGlob.
211

212         // [HD] I think the following lines do not make sense,
213
// because as fas as i can see, every Query has a
214
// LIST_ID attached to it. Hence the OR below is
215
// always true and we get all user queries of the
216
// current user here.
217

218         //Criteria.Criterion notNullListCrit = crit.getNewCriterion(
219
// QueryPeer.LIST_ID, null, Criteria.NOT_EQUAL);
220
//cPriv.and(notNullListCrit.or(moduleCrit));
221

222         cPriv.and(moduleCrit);
223         return cPriv;
224     }
225
226     /**
227      * Return the user's private queries AND all queries, which
228      * have scope "module", but have not yet been approved by the
229      * module owner (thus they are still in scope private).
230      * @param user
231      * @param crit
232      * @param moduleCrit
233      * @return
234      */

235     private static Criteria.Criterion userUnapprovedQueriesCrits(ScarabUser user, Criteria crit, Criteria.Criterion moduleCrit)
236     {
237         Criteria.Criterion cUserPendingCrit = crit.getNewCriterion(
238                 QueryPeer.USER_ID, user.getUserId(), Criteria.EQUAL);
239         cUserPendingCrit.and(crit.getNewCriterion(
240                 QueryPeer.SCOPE_ID, Scope.MODULE__PK,
241                 Criteria.EQUAL));
242         cUserPendingCrit.and(crit.getNewCriterion(QueryPeer.APPROVED,
243                 Boolean.FALSE, Criteria.EQUAL));
244
245         // need to be careful here, we are adding moduleCrit to
246
// two different criterion. if we switched the order of
247
// the OR below we would screw up cGlob.
248

249         // [HD] I think the following lines do not make sense,
250
// because as fas as i can see, every Query has a
251
// LIST_ID attached to it. Hence the OR below is
252
// always true and we get all user queries of the
253
// current user here.
254

255         //Criteria.Criterion notNullListCrit = crit.getNewCriterion(
256
// QueryPeer.LIST_ID, null, Criteria.NOT_EQUAL);
257
//cUserPendingCrit.and(notNullListCrit.or(moduleCrit));
258

259         cUserPendingCrit.and(moduleCrit);
260         
261         return cUserPendingCrit;
262     }
263
264     /**
265      * Return all queries, whith scope "module"
266      * @param crit
267      * @param moduleCrit
268      * @return
269      */

270     private static Criteria.Criterion allGlobalQueriesCrit(Criteria crit, Criteria.Criterion moduleCrit)
271     {
272         Criteria.Criterion cGlob = crit.getNewCriterion(
273                 QueryPeer.SCOPE_ID,
274                 Scope.MODULE__PK,
275                 Criteria.EQUAL);
276
277         cGlob.and(crit.getNewCriterion(QueryPeer.APPROVED,
278                                            Boolean.TRUE, Criteria.EQUAL));
279         cGlob.and(moduleCrit);
280         return cGlob;
281     }
282
283     
284     
285     public static List JavaDoc getUserQueries(ScarabUser user)
286         throws Exception JavaDoc
287     {
288         List JavaDoc queries = null;
289         Object JavaDoc obj = QueryManager.getMethodResult()
290             .get(QUERY_PEER, GET_USER_QUERIES, user);
291         if (obj == null)
292         {
293             Criteria crit = new Criteria()
294                 .add(QueryPeer.DELETED, 0);
295             crit.add(QueryPeer.USER_ID, user.getUserId());
296             queries = QueryPeer.doSelect(crit);
297             QueryManager.getMethodResult()
298                 .put(queries, QUERY_PEER, GET_USER_QUERIES, user);
299         }
300         else
301         {
302             queries = (List JavaDoc)obj;
303         }
304         return queries;
305     }
306
307     public static List JavaDoc getModuleQueries(Module module)
308         throws Exception JavaDoc
309     {
310         List JavaDoc queries = null;
311         Object JavaDoc obj = QueryManager.getMethodResult()
312             .get(QUERY_PEER, GET_MODULE_QUERIES, module);
313         if (obj == null)
314         {
315             Criteria crit = new Criteria()
316                 .add(QueryPeer.DELETED, 0);
317             crit.add(QueryPeer.MODULE_ID, module.getModuleId());
318             crit.add(QueryPeer.SCOPE_ID, Scope.MODULE__PK);
319             queries = QueryPeer.doSelect(crit);
320             QueryManager.getMethodResult()
321                 .put(queries, QUERY_PEER, GET_MODULE_QUERIES, module);
322          }
323         else
324         {
325             queries = (List JavaDoc)obj;
326         }
327         return queries;
328     }
329
330     public static List JavaDoc getQueries(Module module, IssueType issueType,
331                                      ScarabUser user)
332         throws Exception JavaDoc
333     {
334         return getQueries(module, issueType, user, SORT_AVAILABILITY, "asc",
335                           TYPE_ALL);
336     }
337
338     public static List JavaDoc getQueries(Module module, IssueType issueType,
339                                      ScarabUser user, String JavaDoc sortColumn,
340                                      String JavaDoc sortPolarity)
341         throws Exception JavaDoc
342     {
343         return getQueries(module, issueType, user, sortColumn,
344                           sortPolarity, TYPE_ALL);
345     }
346
347     private static Criteria addSortOrder(Criteria crit,
348                        String JavaDoc sortColumn, String JavaDoc sortPolarity)
349     {
350         if (sortPolarity.equals("desc"))
351         {
352             crit.addDescendingOrderByColumn(sortColumn);
353         }
354         else
355         {
356             crit.addAscendingOrderByColumn(sortColumn);
357         }
358         return crit;
359     }
360
361 }
362
Popular Tags