KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > da > AttributeAccess


1 package org.tigris.scarab.da;
2
3 /* ================================================================
4  * Copyright (c) 2000 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 CollabNet (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" name
28  * nor may "Tigris" appear in their names without prior written
29  * permission of CollabNet.
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 CollabNet.
47  */

48
49 import java.io.Serializable JavaDoc;
50 import java.util.ArrayList JavaDoc;
51 import java.util.Collection JavaDoc;
52 import java.util.HashSet JavaDoc;
53 import java.util.Iterator JavaDoc;
54 import java.util.List JavaDoc;
55 import java.util.Set JavaDoc;
56
57 import org.apache.torque.util.Criteria;
58 import com.workingdogs.village.Record;
59
60 import org.tigris.scarab.om.AttributePeer;
61 import org.tigris.scarab.om.AttributeGroupPeer;
62 import org.tigris.scarab.om.RAttributeAttributeGroupPeer;
63 import org.tigris.scarab.om.RModuleAttributePeer;
64 import org.tigris.scarab.om.RModuleUserAttributePeer;
65 import org.tigris.scarab.services.cache.ScarabCache;
66 import org.tigris.scarab.tools.localization.L10NKeySet;
67
68 /**
69  * Access to data relating to attributes.
70  *
71  * @see org.tigris.scarab.da.AttributeAccess
72  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
73  * @author <a HREF="mailto:dlr@collab.net">Daniel Rall</a>
74  * @since Scarab 0.17
75  */

76 public class AttributeAccess
77 {
78     /** Method name used as part of a cache key. */
79     private static final String JavaDoc RETRIEVE_QUERY_COLUMN_IDS =
80         "retrieveQueryColumnIDs";
81     private static final String JavaDoc RETRIEVE_QUICK_SEARCH_ATTRIBUTES =
82         "retrieveQuickSearchAttributeIDs";
83     private static final String JavaDoc RETRIEVE_REQUIRED_ATTRIBUTES =
84         "retrieveRequiredAttributeIDs";
85     private static final String JavaDoc RETRIEVE_ACTIVE_ATTRIBUTES =
86         "retrieveActiveAttributes";
87     private static final String JavaDoc RETRIEVE_DEFAULT_TEXT_ATTRIBUTE =
88         "retrieveDefaultTextAttributeID";
89     private static final String JavaDoc RETRIEVE_FIRST_ACTIVE_TEXT_ATTRIBUTE =
90         "retrieveFirstActiveTextAttributeID";
91
92     private Serializable JavaDoc thisKey = AttributeAccess.class;
93
94
95     /**
96      * Constructor used by {@link org.tigris.scarab.da.DAFactory}.
97      */

98     public AttributeAccess()
99     {
100     }
101
102     /**
103      * Retrieves a list of attribute identifiers for use in
104      * determining which columns to display for a user's query
105      * results.
106      *
107      * @param userID The associated user (must be
108      * non-<code>null</code>).
109      * @param listID The associated artifact type list (can be
110      * <code>null</code>).
111      * @param moduleID The associated module (ignored if <code>null</code>).
112      * @param artifactTypeID The associated artifact type (ignored if
113      * <code>null</code>).
114      * @return A list of attribute identifiers.
115      * @throws DAException If any problems are encountered.
116      */

117     public List JavaDoc retrieveQueryColumnIDs(String JavaDoc userID, String JavaDoc listID,
118                                        String JavaDoc moduleID, String JavaDoc artifactTypeID) throws DAException
119     {
120         List JavaDoc result = null;
121         Object JavaDoc obj = ScarabCache.get(thisKey,
122                                      RETRIEVE_QUERY_COLUMN_IDS,
123                                      userID, moduleID, artifactTypeID);
124         if (obj == null)
125         {
126             Criteria crit = new Criteria();
127             crit.addSelectColumn(RModuleUserAttributePeer.ATTRIBUTE_ID);
128             crit.add(RModuleUserAttributePeer.USER_ID, userID);
129             if (moduleID != null)
130             {
131                 crit.add(RModuleUserAttributePeer.MODULE_ID, moduleID);
132             }
133             if (artifactTypeID != null)
134             {
135                 crit.add(RModuleUserAttributePeer.ISSUE_TYPE_ID,
136                          artifactTypeID);
137             }
138             // null should be added to criteria for listID
139
crit.add(RModuleUserAttributePeer.LIST_ID, listID)
140                 .addAscendingOrderByColumn
141                      (RModuleUserAttributePeer.PREFERRED_ORDER);
142
143             try
144             {
145                 List JavaDoc records =
146                     RModuleUserAttributePeer.doSelectVillageRecords(crit);
147                 result = new ArrayList JavaDoc(records.size());
148                 for (Iterator JavaDoc i = records.iterator(); i.hasNext();)
149                 {
150                     result.add(((Record) i.next()).getValue(1).asString());
151                 }
152             }
153             catch (Exception JavaDoc e)
154             {
155                 throw new DAException(L10NKeySet.ExceptionFailedToReadIdentifierList,
156                                       e);
157             }
158             ScarabCache.put(result, AttributeAccess.class,
159                             RETRIEVE_QUERY_COLUMN_IDS,
160                             userID, moduleID, artifactTypeID);
161         }
162         else
163         {
164             result = (List JavaDoc) obj;
165         }
166         return result;
167     }
168
169     /**
170      * Deletes the persisted choice of issue list display columns for
171      * the given user and artifact type(s).
172      *
173      * @param userID The associated user (must be
174      * non-<code>null</code>).
175      * @param listID The associated artifact type list (can be
176      * <code>null</code>).
177      * @param moduleID The associated module (ignored if <code>null</code>).
178      * @param artifactTypeID The associated artifact type (ignored if
179      * <code>null</code>).
180      * @throws DAException If any problems are encountered.
181      */

182     public void deleteQueryColumnIDs(String JavaDoc userID, String JavaDoc listID,
183                                      String JavaDoc moduleID, String JavaDoc artifactTypeID)
184         throws DAException
185     {
186         Criteria crit = new Criteria();
187         crit.add(RModuleUserAttributePeer.USER_ID, userID);
188         if (moduleID != null)
189         {
190             crit.add(RModuleUserAttributePeer.MODULE_ID, moduleID);
191         }
192         if (artifactTypeID != null)
193         {
194             crit.add(RModuleUserAttributePeer.ISSUE_TYPE_ID,
195                      artifactTypeID);
196         }
197         crit.add(RModuleUserAttributePeer.LIST_ID, listID);
198         try
199         {
200             RModuleUserAttributePeer.doDelete(crit);
201         }
202         catch (Exception JavaDoc e)
203         {
204             throw new DAException(L10NKeySet.ExceptionFailedToDeleteIdentifierList,
205                                   e);
206         }
207     }
208
209
210     /**
211      * Set of attributeIDs which are active and required within the given
212      * module for the given issue type and whose attribute group's are
213      * also active.
214      *
215      * @param moduleID The associated module (must be
216      * non-<code>null</code>).
217      * @param artifactTypeID The associated artifact type (must be
218      * non-<code>null</code>).
219      * @return an <code>String</code> of String attribute ids
220      */

221     public Set JavaDoc retrieveRequiredAttributeIDs(String JavaDoc moduleID,
222                                             String JavaDoc artifactTypeID)
223     throws DAException
224     {
225         Set JavaDoc attributes = null;
226         Object JavaDoc obj = ScarabCache.get(thisKey, RETRIEVE_REQUIRED_ATTRIBUTES,
227                                      moduleID, artifactTypeID);
228         if (obj == null)
229         {
230             Criteria crit = new Criteria(7)
231                 .add(RModuleAttributePeer.REQUIRED, true)
232                 .add(RModuleAttributePeer.ACTIVE, true)
233                 .add(RModuleAttributePeer.MODULE_ID, moduleID)
234                 .add(RModuleAttributePeer.ISSUE_TYPE_ID, artifactTypeID);
235             addGroupCriteria(crit, moduleID, artifactTypeID);
236             attributes = getRMAAttributeIdSet(crit);
237             ScarabCache.put(attributes, thisKey, RETRIEVE_REQUIRED_ATTRIBUTES,
238                             moduleID, artifactTypeID);
239         }
240         else
241         {
242             attributes = (Set JavaDoc) obj;
243         }
244         return attributes;
245     }
246     
247     /**
248      * Set of attributeIDs which are active and marked for custom search
249      * within the given
250      * module for the given issue type and whose attribute group's are
251      * also active.
252      *
253      * @param moduleID The associated module (must be
254      * non-<code>null</code>).
255      * @param artifactTypeID The associated artifact type (must be
256      * non-<code>null</code>).
257      * @return an <code>Set</code> of String attribute ids
258      */

259     public Set JavaDoc retrieveQuickSearchAttributeIDs(String JavaDoc moduleID,
260                                                String JavaDoc artifactTypeID)
261     throws DAException
262     {
263         Set JavaDoc attributes = null;
264         Object JavaDoc obj = ScarabCache.get(thisKey, RETRIEVE_QUICK_SEARCH_ATTRIBUTES,
265                                      moduleID, artifactTypeID);
266         if (obj == null)
267         {
268             Criteria crit = new Criteria(3)
269                 .add(RModuleAttributePeer.QUICK_SEARCH, true)
270                 .add(RModuleAttributePeer.MODULE_ID, moduleID)
271                 .add(RModuleAttributePeer.ISSUE_TYPE_ID, artifactTypeID);
272             attributes = getRMAAttributeIdSet(crit);
273             ScarabCache.put(attributes, thisKey,
274                             RETRIEVE_QUICK_SEARCH_ATTRIBUTES,
275                             moduleID, artifactTypeID);
276         }
277         else
278         {
279             attributes = (Set JavaDoc) obj;
280         }
281         return attributes;
282     }
283
284     private Set JavaDoc getRMAAttributeIdSet(Criteria crit)
285         throws DAException
286     {
287         crit.addSelectColumn(RModuleAttributePeer.ATTRIBUTE_ID);
288         Set JavaDoc attributes = null;
289         try
290         {
291             List JavaDoc records = RModuleAttributePeer.doSelectVillageRecords(crit);
292             attributes = new HashSet JavaDoc(records.size());
293             for (Iterator JavaDoc i = records.iterator(); i.hasNext(); )
294             {
295                 attributes.add(((Record) i.next()).getValue(1).asString());
296             }
297         }
298         catch (Exception JavaDoc e)
299         {
300             throw new DAException(L10NKeySet.ExceptionFailedToReadIdentifierList, e);
301         }
302         return attributes;
303     }
304
305     private void addGroupCriteria(Criteria crit,
306                                   String JavaDoc moduleID, String JavaDoc artifactTypeID)
307     {
308         crit.addJoin(RAttributeAttributeGroupPeer.ATTRIBUTE_ID,
309                      RModuleAttributePeer.ATTRIBUTE_ID)
310             .addJoin(RAttributeAttributeGroupPeer.GROUP_ID,
311                      AttributeGroupPeer.ATTRIBUTE_GROUP_ID)
312             .add(AttributeGroupPeer.MODULE_ID, moduleID)
313             .add(AttributeGroupPeer.ISSUE_TYPE_ID, artifactTypeID)
314             .add(AttributeGroupPeer.ACTIVE, true);
315     }
316
317
318     /**
319      * Torque <code>Attribute</code>s which are active within the
320      * given module for the given issue type
321      * <strike>and whose attribute group's are also active</strike>.
322      *
323      * @param moduleID The associated module (must be
324      * non-<code>null</code>).
325      * @param artifactTypeID The associated artifact type (must be
326      * non-<code>null</code>).
327      * @param isOrdered indication whether an iterator over the Attributes
328      * should return them in their natural order.
329      * @return an <code>Collection</code> of torque Attribute objects. The
330      * collection will be a List if isOrdered is true, otherwise a Set is
331      * returned.
332      */

333     public Collection JavaDoc retrieveActiveAttributeOMs(String JavaDoc moduleID,
334                                                  String JavaDoc artifactTypeID,
335                                                  boolean isOrdered)
336     throws DAException
337     {
338         Collection JavaDoc attributes = null;
339         Boolean JavaDoc ordered = isOrdered ? Boolean.TRUE : Boolean.FALSE;
340         Object JavaDoc obj = ScarabCache.get(thisKey, RETRIEVE_ACTIVE_ATTRIBUTES,
341                                      moduleID, artifactTypeID, ordered);
342         if (obj == null)
343         {
344             Criteria crit = new Criteria(2);
345             crit.add(RModuleAttributePeer.ACTIVE, true);
346             crit.add(RModuleAttributePeer.MODULE_ID, moduleID);
347             crit.add(RModuleAttributePeer.ISSUE_TYPE_ID, artifactTypeID);
348             // FIXME! would like to eliminate attributes that exist in
349
// inactive groups, but user attributes are not in groups, so
350
// this cannot be as simple as the required attributes which
351
// never include user attributes. Will probably require a left
352
// join. Leaving as it was for now. An attribute is active
353
// even if it is in an inactive group.
354
//addGroupCriteria(crit, moduleID, artifactTypeID);
355

356             if (isOrdered)
357             {
358                 crit.addAscendingOrderByColumn(
359                     RModuleAttributePeer.PREFERRED_ORDER);
360                 crit.addAscendingOrderByColumn(
361                     RModuleAttributePeer.DISPLAY_VALUE);
362             }
363
364             crit.addJoin(AttributePeer.ATTRIBUTE_ID,
365                          RModuleAttributePeer.ATTRIBUTE_ID);
366             List JavaDoc records = null;
367             try
368             {
369                 records = AttributePeer.doSelect(crit);
370             }
371             catch (Exception JavaDoc e)
372             {
373                 throw new DAException(L10NKeySet.ExceptionFailedToReadIdentifierList, e);
374             }
375             if (isOrdered)
376             {
377                 attributes = new ArrayList JavaDoc(records.size());
378             }
379             else
380             {
381                 attributes = new HashSet JavaDoc(records.size());
382             }
383
384             for (Iterator JavaDoc i = records.iterator(); i.hasNext();)
385             {
386                 attributes.add(i.next());
387             }
388             
389             ScarabCache.put(attributes, thisKey, RETRIEVE_ACTIVE_ATTRIBUTES,
390                             moduleID, artifactTypeID, ordered);
391         }
392         else
393         {
394             attributes = (Collection JavaDoc)obj;
395         }
396         return attributes;
397     }
398
399
400     /**
401      * Retrieves the attribute ID which is active and marked as the
402      * default text attribute within the given
403      * module for the given issue type and whose attribute group is
404      * also active.
405      *
406      * @param moduleID The associated module (must be
407      * non-<code>null</code>).
408      * @param artifactTypeID The associated artifact type (must be
409      * non-<code>null</code>).
410      * @return an <code>String</code> attribute ID
411      */

412     public String JavaDoc retrieveDefaultTextAttributeID(String JavaDoc moduleID,
413                                                  String JavaDoc artifactTypeID)
414         throws DAException
415     {
416         String JavaDoc result = null;
417         Object JavaDoc obj = ScarabCache.get(thisKey, RETRIEVE_DEFAULT_TEXT_ATTRIBUTE,
418                                      moduleID, artifactTypeID);
419         if (obj == null)
420         {
421             Criteria crit = new Criteria(7);
422             crit.add(RModuleAttributePeer.DEFAULT_TEXT_FLAG, true)
423                 .add(RModuleAttributePeer.ACTIVE, true)
424                 .add(RModuleAttributePeer.MODULE_ID, moduleID)
425                 .add(RModuleAttributePeer.ISSUE_TYPE_ID, artifactTypeID);
426             addGroupCriteria(crit, moduleID, artifactTypeID);
427             result = getRMAAttributeId(crit);
428             ScarabCache.put(result, thisKey, RETRIEVE_DEFAULT_TEXT_ATTRIBUTE,
429                             moduleID, artifactTypeID);
430         }
431         else
432         {
433             result = (String JavaDoc) obj;
434         }
435         
436         return result.length() == 0 ? null : result;
437     }
438
439     private String JavaDoc getRMAAttributeId(Criteria crit)
440         throws DAException
441     {
442         String JavaDoc result = null;
443         crit.addSelectColumn(RModuleAttributePeer.ATTRIBUTE_ID);
444         try
445         {
446             List JavaDoc records = RModuleAttributePeer.doSelectVillageRecords(crit);
447             if (records.isEmpty())
448             {
449                 result = ""; // for caching
450
}
451             else
452             {
453                 result = ((Record) records.get(0)).getValue(1).asString();
454             }
455         }
456         catch (Exception JavaDoc e)
457         {
458             throw new DAException(L10NKeySet.ExceptionFailedToReadIdentifierList, e);
459         }
460         return result;
461     }
462
463
464     /**
465      * Retrieves the attribute ID which is active and is the first id returned
466      * when results are ordered based on numerical preferred order and/or
467      * alphabetical by name within the given
468      * module for the given issue type and whose attribute group is
469      * also active.
470      *
471      * @param moduleID The associated module (must be
472      * non-<code>null</code>).
473      * @param artifactTypeID The associated artifact type (must be
474      * non-<code>null</code>).
475      * @return An <code>String</code> attribute ID.
476      */

477     public String JavaDoc retrieveFirstActiveTextAttributeID(String JavaDoc moduleID,
478                                                      String JavaDoc artifactTypeID)
479         throws DAException
480     {
481         String JavaDoc result = null;
482
483         Object JavaDoc obj = ScarabCache.get(thisKey,
484                                      RETRIEVE_FIRST_ACTIVE_TEXT_ATTRIBUTE,
485                                      moduleID, artifactTypeID);
486         if (obj == null)
487         {
488             Criteria crit = new Criteria(7);
489             crit.add(RModuleAttributePeer.ACTIVE, true)
490                 .add(RModuleAttributePeer.MODULE_ID, moduleID)
491                 .add(RModuleAttributePeer.ISSUE_TYPE_ID, artifactTypeID);
492             addGroupCriteria(crit, moduleID, artifactTypeID);
493
494             crit.addAscendingOrderByColumn(
495                 RModuleAttributePeer.PREFERRED_ORDER);
496             crit.addAscendingOrderByColumn(
497                 RModuleAttributePeer.DISPLAY_VALUE);
498
499             result = getRMAAttributeId(crit);
500             ScarabCache.put(result, thisKey,
501                             RETRIEVE_FIRST_ACTIVE_TEXT_ATTRIBUTE,
502                             moduleID, artifactTypeID);
503         }
504         else
505         {
506             result = (String JavaDoc) obj;
507         }
508         
509         return result.length() == 0 ? null : result;
510     }
511 }
512
Popular Tags