KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Serializable JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.ArrayList JavaDoc;
52 import java.util.Comparator JavaDoc;
53 import java.util.Collections JavaDoc;
54
55 import org.apache.torque.util.Criteria;
56 import org.tigris.scarab.services.cache.ScarabCache;
57
58 /**
59  * This class represents an AttributePeer.
60  *
61  * @author <a HREF="mailto:jon@collab.net">Jon S. Stevens</a>
62  * @version $Id: AttributePeer.java 8693 2003-10-14 04:59:24Z jmcnally $
63  */

64 public class AttributePeer
65     extends org.tigris.scarab.om.BaseAttributePeer
66 {
67     public static final Integer JavaDoc ASSIGNED_TO__PK = new Integer JavaDoc(2);
68     public static final Integer JavaDoc STATUS__PK = new Integer JavaDoc(3);
69     public static final Integer JavaDoc RESOLUTION__PK = new Integer JavaDoc(4);
70     public static final Integer JavaDoc TOTAL_VOTES__PK = new Integer JavaDoc(13);
71     public static final String JavaDoc EMAIL_TO = "to";
72     public static final String JavaDoc CC_TO = "cc";
73     public static final String JavaDoc USER = "user";
74     public static final String JavaDoc NON_USER = "non-user";
75
76     private static final String JavaDoc ATTRIBUTE_PEER =
77         "AttributePeer";
78
79     /**
80      * Gets a List of all of the Attribute objects in the database.
81      */

82     public static List JavaDoc getAttributes(String JavaDoc attributeType, boolean includeDeleted,
83                                      String JavaDoc sortColumn, String JavaDoc sortPolarity)
84         throws Exception JavaDoc
85     {
86         List JavaDoc result = null;
87         Boolean JavaDoc deletedBool = (includeDeleted ? Boolean.TRUE : Boolean.FALSE);
88         // 4th element is ignored due to bug in torque that is being
89
// Matched in ScarabCache
90
Serializable JavaDoc[] keys = {ATTRIBUTE_PEER, attributeType, deletedBool,
91                                sortColumn, null, sortPolarity};
92         Object JavaDoc obj = ScarabCache.get(keys);
93         if (obj == null)
94         {
95             Criteria crit = new Criteria();
96             crit.add(AttributePeer.ATTRIBUTE_ID, 0, Criteria.NOT_EQUAL);
97             if (!includeDeleted)
98             {
99                 crit.add(AttributePeer.DELETED, 0);
100             }
101             // add user type criteria - user or non-user
102
if (attributeType.equals("user"))
103             {
104                 crit.add(AttributePeer.ATTRIBUTE_TYPE_ID,
105                          AttributeTypePeer.USER_TYPE_KEY);
106             }
107             else
108             {
109                 crit.add(AttributePeer.ATTRIBUTE_TYPE_ID,
110                          AttributeTypePeer.USER_TYPE_KEY, Criteria.NOT_EQUAL);
111             }
112             // sort criteria
113
if (sortColumn.equals("desc"))
114             {
115                 addSortOrder(crit, AttributePeer.DESCRIPTION,
116                              sortPolarity);
117             }
118             else if (sortColumn.equals("date"))
119             {
120                 addSortOrder(crit, AttributePeer.CREATED_DATE,
121                              sortPolarity);
122             }
123             else if (sortColumn.equals("type"))
124             {
125                 crit.addJoin(AttributePeer.ATTRIBUTE_TYPE_ID,
126                              AttributeTypePeer.ATTRIBUTE_TYPE_ID);
127                 addSortOrder(crit, AttributeTypePeer .ATTRIBUTE_TYPE_NAME,
128                              sortPolarity);
129             }
130             else if (!sortColumn.equals("user"))
131             {
132                 // sort by name
133
addSortOrder(crit, AttributePeer.ATTRIBUTE_NAME,
134                              sortPolarity);
135             }
136             result = doSelect(crit);
137         }
138         else
139         {
140             result = (List JavaDoc)obj;
141         }
142         if (sortColumn.equals("user"))
143         {
144             result = sortAttributesByCreatingUser(result, sortPolarity);
145         }
146                 
147         ScarabCache.put(result, keys);
148         return result;
149     }
150
151     /**
152      * Gets a List of all of the Attribute objects filtered
153      * on name or description
154      */

155     public static List JavaDoc getFilteredAttributes(String JavaDoc name, String JavaDoc description,
156                                              String JavaDoc searchField)
157         throws Exception JavaDoc
158     {
159         List JavaDoc result = null;
160         List JavaDoc allAttributes = getAttributes();
161         if (allAttributes == null || allAttributes.size() == 0)
162         {
163             result = Collections.EMPTY_LIST;
164         }
165         else
166         {
167             List JavaDoc attrIds = new ArrayList JavaDoc();
168             for (int i = 0; i < allAttributes.size(); i++)
169             {
170                 attrIds.add(((Attribute)allAttributes.get(i)).getAttributeId());
171             }
172             Criteria crit = new Criteria();
173             crit.addIn(AttributePeer.ATTRIBUTE_ID, attrIds);
174             Criteria.Criterion c = null;
175             Criteria.Criterion c1 = null;
176             Criteria.Criterion c2 = null;
177             
178             if (name != null)
179             {
180                 c1 = crit.getNewCriterion(AttributePeer.ATTRIBUTE_NAME,
181                          addWildcards(name), Criteria.LIKE);
182             }
183             if (description != null)
184             {
185                 c2 = crit.getNewCriterion(AttributePeer.DESCRIPTION,
186                          addWildcards(description), Criteria.LIKE);
187             }
188             if (searchField.equals("Name"))
189             {
190                 c = c1;
191             }
192             else if (searchField.equals("Description"))
193             {
194                 c = c2;
195             }
196             else if (searchField.equals("Any"))
197             {
198                 c = c1.or(c2);
199             }
200             crit.and(c);
201             result = AttributePeer.doSelect(crit);
202         }
203         return result;
204     }
205
206     private static Object JavaDoc addWildcards(String JavaDoc s)
207     {
208         return new StringBuffer JavaDoc(s.length() + 2)
209             .append('%').append(s).append('%').toString();
210     }
211
212     /**
213      * Gets a List of all of the Attribute objects in the database.
214      * Sorts on selected column.
215      */

216     public static List JavaDoc getAttributes()
217         throws Exception JavaDoc
218     {
219         return getAttributes(NON_USER, false, AttributePeer.ATTRIBUTE_NAME, "asc");
220     }
221
222     /**
223      * Gets a List of Attribute objects in the database.
224      */

225     public static List JavaDoc getAttributes(String JavaDoc attributeType)
226         throws Exception JavaDoc
227     {
228         return getAttributes(attributeType, false, AttributePeer.ATTRIBUTE_NAME,
229                              "asc");
230     }
231
232     /**
233      * Gets a List of Attribute objects in the database.
234      */

235     public static List JavaDoc getAttributes(String JavaDoc attributeType, boolean includeDeleted)
236         throws Exception JavaDoc
237     {
238         return getAttributes(attributeType, includeDeleted, AttributePeer.ATTRIBUTE_NAME,
239                              "asc");
240     }
241
242     /**
243      * Gets a List of data Attribute objects in the database.
244      * Sorts on selected column.
245      */

246     public static List JavaDoc getAttributes(String JavaDoc sortColumn,
247                                      String JavaDoc sortPolarity)
248         throws Exception JavaDoc
249     {
250         return getAttributes(NON_USER, false, sortColumn, sortPolarity);
251     }
252
253     private static List JavaDoc sortAttributesByCreatingUser(List JavaDoc result,
254                                                      String JavaDoc sortPolarity)
255         throws Exception JavaDoc
256     {
257         final int polarity = ("asc".equals(sortPolarity)) ? 1 : -1;
258         Comparator JavaDoc c = new Comparator JavaDoc()
259         {
260             public int compare(Object JavaDoc o1, Object JavaDoc o2)
261             {
262                 int i = 0;
263                 try
264                 {
265                     i = polarity *
266                         ((Attribute)o1).getCreatedUserName()
267                          .compareTo(((Attribute)o2).getCreatedUserName());
268                 }
269                 catch (Exception JavaDoc e)
270                 {
271                     //
272
}
273                 return i;
274              }
275         };
276         Collections.sort(result, c);
277         return result;
278     }
279
280     private static Criteria addSortOrder(Criteria crit,
281                        String JavaDoc sortColumn, String JavaDoc sortPolarity)
282     {
283         if (sortPolarity.equals("desc"))
284         {
285             crit.addDescendingOrderByColumn(sortColumn);
286         }
287         else
288         {
289             crit.addAscendingOrderByColumn(sortColumn);
290         }
291         return crit;
292     }
293
294 }
295
Popular Tags