KickJava   Java API By Example, From Geeks To Geeks.

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


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 // JDK classes
50
import java.util.Collections JavaDoc;
51 import java.util.HashMap JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.List JavaDoc;
54 import java.util.ArrayList JavaDoc;
55 import java.util.Date JavaDoc;
56 import com.workingdogs.village.Record;
57
58 // Turbine classes
59
import org.apache.torque.NoRowsException;
60 import org.apache.torque.TooManyRowsException;
61 import org.apache.torque.TorqueException;
62 import org.apache.torque.om.Persistent;
63 import org.apache.torque.om.ObjectKey;
64 import org.apache.torque.om.SimpleKey;
65 import org.apache.torque.util.Criteria;
66
67 // Scarab classes
68
import org.tigris.scarab.om.ScarabUserManager;
69
70 import org.tigris.scarab.services.cache.ScarabCache;
71
72 /**
73   * This class represents the SCARAB_R_OPTION_OPTION table.
74   * Please note that this class caches several pieces of data depending
75   * on the methods called. If you would like to clear these caches,
76   * it is a good idea to call the doRemoveCaches() method after making
77   * any modifications to the ROptionOption, ParentChildAttributeOption,
78   * and AttributeOption objects.
79   *
80   * @author <a HREF="mailto:jon@collab.net">Jon S. Stevens</a>
81   * @version $Id: Attribute.java 9495 2005-03-19 23:14:36Z jorgeuriarte $
82   */

83 public class Attribute
84     extends BaseAttribute
85     implements Persistent, Conditioned
86 {
87     private static final String JavaDoc CLASS_NAME = "Attribute";
88     
89     /** Class name used as part of a cache key when the method is static */
90     private static final String JavaDoc ATTRIBUTE =
91         CLASS_NAME;
92     /** Method name used as part of a cache key */
93     private static final String JavaDoc GET_INSTANCE =
94         "getInstance";
95     /** Method name used as part of a cache key */
96     private static final String JavaDoc GET_ALL_ATTRIBUTE_TYPES =
97         "getAllAttributeTypes";
98     /** Method name used as part of a cache key */
99     private static final String JavaDoc GET_COMPATIBLE_ATTRIBUTE_TYPES =
100         "getCompatibleAttributeTypes";
101     /** Method name used as part of a cache key */
102     private static final String JavaDoc GET_ATTRIBUTE_TYPE =
103         "getAttributeType";
104     /** Method name used as part of a cache key */
105     private static final String JavaDoc GET_ALL_ATTRIBUTES =
106         "getAllAttributes";
107     /** Method name used as part of a cache key */
108     private static final String JavaDoc GET_ALL_ATTRIBUTE_OPTIONS =
109         "getAllAttributeOptions";
110     /** Method name used as part of a cache key */
111     private static final String JavaDoc GET_ORDERED_ROPTIONOPTION_LIST =
112         "getOrderedROptionOptionList";
113
114     private static final String JavaDoc SELECT_ONE = "select-one";
115     private static final String JavaDoc USER_ATTRIBUTE = "user";
116     private static final String JavaDoc[] TEXT_TYPES = {"string", "email", "long-string", "date"};
117
118     private List JavaDoc orderedROptionOptionList = null;
119     private List JavaDoc orderedAttributeOptionList = null;
120     private List JavaDoc parentChildAttributeOptions = null;
121     
122     private HashMap JavaDoc optionsMap;
123     private List JavaDoc attributeOptionsWithDeleted;
124     private List JavaDoc attributeOptionsWithoutDeleted;
125
126     /**
127      * Must call getInstance()
128      */

129     protected Attribute()
130     {
131     }
132
133     static String JavaDoc getCacheKey(ObjectKey key)
134     {
135          String JavaDoc keyString = key.getValue().toString();
136          return new StringBuffer JavaDoc(CLASS_NAME.length() + keyString.length())
137              .append(CLASS_NAME).append(keyString).toString();
138     }
139
140
141     /**
142      * Return an instance based on the passed in attribute id as an int
143      * It will return a cached instance if possible.
144      */

145     public static Attribute getInstance(int id)
146         throws TorqueException
147     {
148         return AttributeManager.getInstance(new Integer JavaDoc(id));
149     }
150
151
152     /**
153      * Return an instance based on the passed in
154      * attribute name as a String. It will return
155      * the first match if the number of Attributes found > 0
156      * Note: The business logic dicates that there should
157      * never be duplicate Attributes. Therefore, the checkForDuplicate
158      * method will return true if the number of Attributes found
159      * is > 0
160      */

161     public static Attribute getInstance(String JavaDoc attributeName)
162         throws Exception JavaDoc
163     {
164         Attribute result = null;
165 // TODO Should attributes even be cached by name? What if the name is changed?
166
Object JavaDoc obj = ScarabCache.get(ATTRIBUTE, GET_INSTANCE, attributeName.toLowerCase());
167         if (obj == null)
168         {
169             Criteria crit = new Criteria();
170             crit.add (AttributePeer.ATTRIBUTE_NAME, attributeName);
171             crit.setIgnoreCase(true);
172             List JavaDoc attributes = AttributePeer.doSelect(crit);
173             if (attributes.size() > 0)
174             {
175                 result = (Attribute) attributes.get(0);
176                 ScarabCache.put(result, ATTRIBUTE, GET_INSTANCE, attributeName.toLowerCase());
177             }
178         }
179         else
180         {
181             result = (Attribute)obj;
182         }
183         return result;
184     }
185
186     /**
187      * Checks to see if there is another attribute with the same name
188      * already in the database. Returns true if there is another
189      * Attribute of the same name.
190      */

191     public static boolean checkForDuplicate(String JavaDoc attributeName)
192         throws Exception JavaDoc
193     {
194         return (getInstance(attributeName) != null);
195     }
196
197     public static boolean checkForDuplicate(String JavaDoc attributeName,
198                                             Attribute attribute)
199         throws Exception JavaDoc
200     {
201         return (checkForDuplicate(attributeName) &&
202                 !attributeName.equals(attribute.getName()));
203     }
204
205     /**
206      * Helper method that takes a Integer
207      */

208     public String JavaDoc getCreatedUserName() throws Exception JavaDoc
209     {
210         Integer JavaDoc userId = getCreatedBy();
211         String JavaDoc userName = null;
212         if (userId == null || userId.intValue() == 0)
213         {
214             // FIXME: l10n
215
userName = "Default";
216         }
217         else
218         {
219             ScarabUser su = ScarabUserManager
220                 .getInstance(SimpleKey.keyFor(userId));
221             userName = su.getName();
222         }
223         return userName;
224     }
225
226     /**
227      * Clears the internal caches for this object
228      */

229     void doRemoveCaches()
230     {
231         setOrderedROptionOptionList(null);
232         setOrderedAttributeOptionList(null);
233         setParentChildAttributeOptions(null);
234     }
235
236
237     /**
238      * Little method to return a List of all Attribute Type's.
239      * It is here for convenience with regards to needing this
240      * functionality from within a Template.
241      */

242     public static List JavaDoc getAllAttributeTypes()
243         throws Exception JavaDoc
244     {
245         List JavaDoc result = null;
246         Object JavaDoc obj = ScarabCache.get(ATTRIBUTE, GET_ALL_ATTRIBUTE_TYPES);
247         if (obj == null)
248         {
249             result = AttributeTypePeer.doSelect(new Criteria());
250             ScarabCache.put(result, ATTRIBUTE, GET_ALL_ATTRIBUTE_TYPES);
251         }
252         else
253         {
254             result = (List JavaDoc)obj;
255         }
256         return result;
257     }
258
259     /**
260      * Method to return compatible Attribute Type's.
261      * if the attribute has not been used at all, all types are
262      * compatible. if issues have been entered which use the
263      * attribute only text types are compatible with each other
264      * It is here for convenience with regards to needing this
265      * functionality from within a Template.
266      */

267     public List JavaDoc getCompatibleAttributeTypes()
268         throws Exception JavaDoc
269     {
270         List JavaDoc result = null;
271         Object JavaDoc obj = ScarabCache.get(this, GET_COMPATIBLE_ATTRIBUTE_TYPES);
272         if (obj == null)
273         {
274             boolean inUse = !isNew();
275             if (inUse)
276             {
277                 // check to see if attribute really has been used
278
Criteria crit = new Criteria();
279                 crit.add(AttributeValuePeer.ATTRIBUTE_ID, getAttributeId());
280                 inUse = AttributeValuePeer.count(crit) > 0;
281             }
282             if (inUse)
283             {
284                 if (isTextAttribute())
285                 {
286                     Criteria crit = new Criteria();
287                     crit.addIn(AttributeTypePeer.ATTRIBUTE_TYPE_ID, AttributeTypePeer.TEXT_PKS);
288                     result = AttributeTypePeer.doSelect(crit);
289                 }
290                 else
291                 {
292                     result = Collections.EMPTY_LIST;
293                 }
294             }
295             else
296             {
297                 result = getAllAttributeTypes();
298             }
299             ScarabCache.put(result, this, GET_COMPATIBLE_ATTRIBUTE_TYPES);
300         }
301         else
302         {
303             result = (List JavaDoc)obj;
304         }
305         return result;
306     }
307
308     /**
309      * Override the base class to provide caching of AttributeType objects
310      * and save a hit to the database.
311      */

312     public AttributeType getAttributeType()
313         throws TorqueException
314     {
315         AttributeType result = null;
316         Object JavaDoc obj = ScarabCache.get(this, GET_ATTRIBUTE_TYPE);
317         if (obj == null)
318         {
319             result = super.getAttributeType();
320             ScarabCache.put(result, this, GET_ATTRIBUTE_TYPE);
321         }
322         else
323         {
324             result = (AttributeType)obj;
325         }
326         return result;
327     }
328
329     /**
330      * get a list of all of the Attributes in the database
331      */

332     public static List JavaDoc getAllAttributes()
333         throws Exception JavaDoc
334     {
335         List JavaDoc result = null;
336         Object JavaDoc obj = ScarabCache.get(ATTRIBUTE, GET_ALL_ATTRIBUTES);
337         if (obj == null)
338         {
339             result = AttributePeer.doSelect(new Criteria());
340             ScarabCache.put(result, ATTRIBUTE, GET_ALL_ATTRIBUTES);
341         }
342         else
343         {
344             result = (List JavaDoc)obj;
345         }
346         return result;
347     }
348
349     public boolean isOptionAttribute()
350         throws TorqueException
351     {
352         if (getTypeId() != null)
353         {
354             return getAttributeType().getAttributeClass().getName()
355                 .equals(SELECT_ONE);
356         }
357         return false;
358     }
359
360     public boolean isUserAttribute()
361         throws TorqueException
362     {
363         if (getTypeId() != null)
364         {
365             return getAttributeType().getAttributeClass().getName()
366                 .equals(USER_ATTRIBUTE);
367         }
368         return false;
369     }
370
371     public boolean isTextAttribute()
372         throws Exception JavaDoc
373     {
374         boolean isText = false;
375         if (getTypeId() != null)
376         {
377             for (int i=0; i<TEXT_TYPES.length && !isText; i++)
378             {
379                 isText = TEXT_TYPES[i].equals(getAttributeType().getName());
380             }
381         }
382         return isText;
383     }
384  
385     public boolean isDateAttribute()
386         throws Exception JavaDoc
387     {
388         boolean isDate = false;
389         if(getTypeId() != null)
390         {
391             isDate = "date".equals(getAttributeType().getName());
392         }
393         return isDate;
394      }
395
396     /**
397      * This method is special. Don't use it.
398      * It is used to generate the mappings for r_option_option
399      * table mappings because AO's have to have a mapping in
400      * there in order to be looked up using the getOrderedROptionOptionList()
401      * method. This method has already been run and the output was
402      * used to copy/paste into the scarab-default-data.sql file.
403      * It is being kept here in case it is needed again someday.
404     public static void createROptionOptionMapping()
405         throws Exception
406     {
407         List attributes = Attribute.getAllAttributes();
408         for (int i=0; i<attributes.size();i++)
409         {
410             Attribute attr = (Attribute) attributes.get(i);
411             if (attr.getName().equals("Operating System") ||
412                 attr.getName().equals("Null Attribute"))
413             {
414                 continue;
415             }
416             System.out.println ("Attribute: " + attr.getName());
417             List attributeOptions = attr.getAttributeOptions();
418             Iterator itr = attributeOptions.iterator();
419             int counter = 1;
420             while (itr.hasNext())
421             {
422                 AttributeOption ao = (AttributeOption) itr.next();
423                 System.out.println ("\tAttribute Option: " + ao.getName());
424                 ROptionOption roo = ROptionOption.getInstance();
425                 roo.setOption1Id(new NumberKey(0));
426                 roo.setOption2Id(ao.getOptionId());
427                 roo.setRelationshipId(new NumberKey(1));
428                 roo.setPreferredOrder(counter++);
429                 roo.setDeleted(false);
430                 roo.save();
431             }
432         }
433     }
434      */

435
436 /****************************************************************************/
437 /* Attribute Option Methods */
438 /****************************************************************************/
439
440     /**
441      * Gets one of the options belonging to this attribute. if the
442      * PrimaryKey does not belong to an option in this attribute
443      * null is returned.
444      *
445      * @param pk a <code>Integer</code> value
446      * @return an <code>AttributeOption</code> value
447      */

448     public AttributeOption getAttributeOption(Integer JavaDoc pk)
449         throws TorqueException
450     {
451         if (optionsMap == null)
452         {
453             buildOptionsMap();
454         }
455         return (AttributeOption)optionsMap.get(pk);
456     }
457
458     /**
459      * Get an option by String id.
460      * @throws TorqueException if optionId is empty
461      */

462     public AttributeOption getAttributeOption(String JavaDoc optionID)
463         throws TorqueException
464     {
465         if (optionID == null || optionID.length() == 0)
466         {
467             throw new TorqueException("optionId is empty"); //EXCEPTION
468
}
469         return getAttributeOption(new Integer JavaDoc(optionID));
470     }
471
472
473     /**
474      * Used internally to get a list of Attribute Options
475      */

476     private List JavaDoc getAllAttributeOptions()
477         throws TorqueException
478     {
479         List JavaDoc result = null;
480         Object JavaDoc obj = ScarabCache.get(this, GET_ALL_ATTRIBUTE_OPTIONS);
481         if (obj == null)
482         {
483             Criteria crit = new Criteria();
484             crit.addJoin(AttributeOptionPeer.OPTION_ID,
485                          ROptionOptionPeer.OPTION2_ID);
486             crit.add(AttributeOptionPeer.ATTRIBUTE_ID, this.getAttributeId());
487             crit.addAscendingOrderByColumn(ROptionOptionPeer.PREFERRED_ORDER);
488             result = AttributeOptionPeer.doSelect(crit);
489             ScarabCache.put(result, this, GET_ALL_ATTRIBUTE_OPTIONS);
490         }
491         else
492         {
493             result = (List JavaDoc)obj;
494         }
495         return result;
496     }
497
498     /**
499      * package protected method to set the value of the cached
500      * list. Generally, this is used to set it to null.
501      */

502     void setParentChildAttributeOptions(List JavaDoc value)
503     {
504         parentChildAttributeOptions = value;
505     }
506
507     /**
508      * This returns a list of ParentChildAttributeOption objects
509      * which have been populated with combined join data from
510      * ROptionOption and the AttributeOption table.
511      *
512      * @return a List of ParentChildAttributeOption objects
513      */

514     public List JavaDoc getParentChildAttributeOptions()
515         throws TorqueException
516     {
517         if (parentChildAttributeOptions == null)
518         {
519             List JavaDoc rooList = getOrderedROptionOptionList();
520             List JavaDoc aoList = getOrderedAttributeOptionList();
521             parentChildAttributeOptions = new ArrayList JavaDoc(rooList.size());
522             for (int i=0; i<rooList.size();i++)
523             {
524                 ROptionOption roo = (ROptionOption)rooList.get(i);
525                 AttributeOption ao = (AttributeOption)aoList.get(i);
526     
527                 ParentChildAttributeOption pcao = ParentChildAttributeOption
528                         .getInstance(roo.getOption1Id(), roo.getOption2Id());
529                 pcao.setParentId(roo.getOption1Id());
530                 pcao.setOptionId(roo.getOption2Id());
531                 pcao.setPreferredOrder(roo.getPreferredOrder());
532                 pcao.setWeight(roo.getWeight());
533                 pcao.setName(ao.getName());
534                 pcao.setDeleted(ao.getDeleted());
535                 pcao.setAttributeId(this.getAttributeId());
536                 parentChildAttributeOptions.add(pcao);
537             }
538         }
539         return parentChildAttributeOptions;
540     }
541
542     /**
543      * package protected method to set the value of the cached
544      * list. Generally, this is used to set it to null.
545      */

546     void setOrderedROptionOptionList(List JavaDoc value)
547     {
548         orderedROptionOptionList = value;
549     }
550
551
552     /**
553      * Creates an ordered List of ROptionOption which are
554      * children within this Attribute. The list is ordered according
555      * to the preferred order.
556      *
557      * @return a List of ROptionOption's
558      */

559     public List JavaDoc getOrderedROptionOptionList()
560         throws TorqueException
561     {
562         List JavaDoc result = null;
563         Object JavaDoc obj = ScarabCache.get(this, GET_ORDERED_ROPTIONOPTION_LIST);
564         if (obj == null)
565         {
566             if (orderedROptionOptionList == null)
567             {
568                 Criteria crit = new Criteria();
569                 crit.addJoin(AttributeOptionPeer.OPTION_ID,
570                              ROptionOptionPeer.OPTION2_ID);
571                 crit.add(AttributeOptionPeer.ATTRIBUTE_ID, getAttributeId());
572                 crit.addAscendingOrderByColumn(
573                     ROptionOptionPeer.PREFERRED_ORDER);
574                 orderedROptionOptionList = ROptionOptionPeer.doSelect(crit);
575             }
576             result = orderedROptionOptionList;
577             ScarabCache.put(result, this, GET_ORDERED_ROPTIONOPTION_LIST);
578         }
579         else
580         {
581             result = (List JavaDoc)obj;
582         }
583         return result;
584     }
585
586     /**
587      * package protected method to set the value of the cached
588      * list. Generally, this is used to set it to null.
589      */

590     void setOrderedAttributeOptionList(List JavaDoc value)
591     {
592         orderedAttributeOptionList = value;
593     }
594
595     /**
596      * Creates an ordered List of AttributeOptions which are
597      * children within this Attribute. The list is ordered according
598      * to the preferred order.
599      *
600      * @return a List of AttributeOption's
601      */

602     public List JavaDoc getOrderedAttributeOptionList()
603         throws TorqueException
604     {
605         if (orderedAttributeOptionList == null)
606         {
607             Criteria crit = new Criteria();
608             crit.addJoin(AttributeOptionPeer.OPTION_ID, ROptionOptionPeer.OPTION2_ID);
609             crit.add(AttributeOptionPeer.ATTRIBUTE_ID, this.getAttributeId());
610             crit.addAscendingOrderByColumn(ROptionOptionPeer.PREFERRED_ORDER);
611             orderedAttributeOptionList = AttributeOptionPeer.doSelect(crit);
612         }
613         return orderedAttributeOptionList;
614     }
615
616     /**
617      * Get a list of all attribute options or just the ones
618      * that have not been marked as deleted.
619      */

620     public List JavaDoc getAttributeOptions(boolean includeDeleted)
621         throws TorqueException
622     {
623         List JavaDoc allOptions = getAllAttributeOptions();
624         List JavaDoc nonDeleted = new ArrayList JavaDoc(allOptions.size());
625         if (includeDeleted)
626         {
627             return allOptions;
628         }
629         else
630         {
631             for (int i=0; i<allOptions.size(); i++)
632             {
633                 AttributeOption option = (AttributeOption)allOptions.get(i);
634                 if (!option.getDeleted())
635                 {
636                     nonDeleted.add(option);
637                 }
638             }
639             return nonDeleted;
640         }
641     }
642
643     /**
644      * Build a list of options.
645      */

646     public synchronized void buildOptionsMap()
647         throws TorqueException
648     {
649         if (getAttributeType().getAttributeClass().getName()
650              .equals(SELECT_ONE))
651         {
652             // synchronized method due to getattributeOptionsWithDeleted, this needs
653
// further investigation !FIXME!
654
attributeOptionsWithDeleted = this.getAllAttributeOptions();
655             optionsMap = new HashMap JavaDoc((int)(1.25*attributeOptionsWithDeleted.size()+1));
656     
657             attributeOptionsWithoutDeleted = new ArrayList JavaDoc(attributeOptionsWithDeleted.size());
658             for (int i=0; i<attributeOptionsWithDeleted.size(); i++)
659             {
660                 AttributeOption option = (AttributeOption)attributeOptionsWithDeleted.get(i);
661                 optionsMap.put(option.getOptionId(), option);
662                 if (!option.getDeleted())
663                 {
664                     attributeOptionsWithoutDeleted.add(attributeOptionsWithDeleted.get(i));
665                 }
666             }
667         }
668     }
669
670     /**
671      * Override autogenerated base class method
672      * There should be no actitivites being returned
673      */

674     public List JavaDoc getActivitys() throws TorqueException
675     {
676         return null;
677     }
678
679     /**
680      * Copy the Attribute and its options
681      * Make sure the new options have a row in the option join table
682      */

683     public Attribute copyAttribute(ScarabUser user)
684         throws Exception JavaDoc
685     {
686         Attribute newAttribute = new Attribute();
687         newAttribute.setName(getName() + " (copy)");
688         newAttribute.setDescription(getDescription());
689         newAttribute.setTypeId(getTypeId());
690         newAttribute.setPermission(getPermission());
691         newAttribute.setRequiredOptionId(getRequiredOptionId());
692         newAttribute.setConditionsArray(getConditionsArray());
693         newAttribute.setAction(getAction());
694         newAttribute.setCreatedBy(user.getUserId());
695         newAttribute.setCreatedDate(new Date JavaDoc());
696         newAttribute.setDeleted(getDeleted());
697         newAttribute.save();
698
699         List JavaDoc attributeOptions = getAttributeOptions();
700         for (int i=0;i<attributeOptions.size();i++)
701         {
702             AttributeOption option = (AttributeOption)attributeOptions.get(i);
703             AttributeOption newOption = new AttributeOption();
704             newOption.setOptionId(option.getOptionId());
705             newOption.setAttributeId(newAttribute.getAttributeId());
706             newOption.setName(option.getName());
707             newOption.setDeleted(option.getDeleted());
708             newOption.save();
709
710             // Copy options's record in R_OPTION_OPTION table
711
List JavaDoc roos = option.getROptionOptionsRelatedByOption2Id();
712             for (int j=0;j<roos.size();j++)
713             {
714                 ROptionOption roo = (ROptionOption)roos.get(j);
715                 ROptionOption newRoo = new ROptionOption();
716                 newRoo.setOption2Id(newOption.getOptionId());
717                 newRoo.setOption1Id(roo.getOption1Id());
718                 newRoo.setRelationshipId(roo.getRelationshipId());
719                 newRoo.setWeight(roo.getWeight());
720                 newRoo.setPreferredOrder(roo.getPreferredOrder());
721                 newRoo.save();
722              }
723         }
724         return newAttribute;
725     }
726             
727     /**
728      * @return Whether this attribute is mapped to any modules.
729      */

730     public boolean hasModuleMappings()
731         throws Exception JavaDoc
732     {
733         return hasMapping((Module) null, (IssueType) null);
734     }
735
736     /**
737      * @param module <code>null</code> to ignore this criterion.
738      * @param issueType <code>null</code> to ignore this criterion.
739      * @return Whether this attribute is already mapped to the
740      * specified {@link Module} and {@link IssueType}.
741      */

742     public boolean hasMapping(Module module, IssueType issueType)
743         throws Exception JavaDoc
744     {
745         Criteria crit = new Criteria();
746         crit.add(RModuleAttributePeer.ATTRIBUTE_ID,
747                  getAttributeId());
748         if (module != null)
749         {
750             crit.add(RModuleAttributePeer.MODULE_ID,
751                      module.getModuleId());
752         }
753         if (issueType != null)
754         {
755             crit.add(RModuleAttributePeer.ISSUE_TYPE_ID,
756                      issueType.getIssueTypeId());
757         }
758         crit.addSelectColumn("count(" + RModuleAttributePeer.ATTRIBUTE_ID + ")");
759         return ((Record)IssuePeer.doSelectVillageRecords(crit).get(0))
760             .getValue(1).asInt() > 0;
761     }
762  
763     /**
764      * Refers to global issue types.
765      *
766      * @return Whether this attribute is mapped to any issue types.
767      * @see #hasGlobalMapping(IssueType)
768      */

769     public boolean hasGlobalIssueTypeMappings()
770         throws Exception JavaDoc
771     {
772         return hasGlobalMapping((IssueType) null);
773     }
774
775     /**
776      * Refers to global issue types.
777      *
778      * @param issueType A specific {@link IssueType} to find
779      * associated attributes for, or <code>null</code> to ignore this
780      * criterion.
781      * @return Whether there are any mappings for this attribute.
782      */

783     public boolean hasGlobalMapping(IssueType issueType)
784         throws Exception JavaDoc
785     {
786         Criteria crit = new Criteria();
787         crit.add(RIssueTypeAttributePeer.ATTRIBUTE_ID,
788                  getAttributeId());
789         if (issueType != null)
790         {
791             crit.add(RIssueTypeAttributePeer.ISSUE_TYPE_ID,
792                      issueType.getIssueTypeId());
793         }
794         crit.addSelectColumn("count(" + RIssueTypeAttributePeer.ATTRIBUTE_ID
795                              + ')');
796         return ((Record)IssuePeer.doSelectVillageRecords(crit).get(0))
797             .getValue(1).asInt() > 0;
798     }
799
800     /**
801      * Delete mappings with all modules and issue types.
802      */

803     public void deleteModuleMappings()
804         throws Exception JavaDoc
805     {
806         Criteria crit = new Criteria();
807         crit.add(RAttributeAttributeGroupPeer.ATTRIBUTE_ID,
808                  getAttributeId());
809         crit.addJoin(RAttributeAttributeGroupPeer.GROUP_ID,
810                      AttributeGroupPeer.ATTRIBUTE_GROUP_ID);
811         crit.add(AttributeGroupPeer.MODULE_ID, (Object JavaDoc)null, Criteria.NOT_EQUAL);
812         List JavaDoc raags = RAttributeAttributeGroupPeer.doSelect(crit);
813         for (Iterator JavaDoc i = raags.iterator(); i.hasNext();)
814         {
815             ((RAttributeAttributeGroup)i.next()).delete();
816         }
817
818         crit = new Criteria();
819         crit.add(RModuleAttributePeer.ATTRIBUTE_ID,
820                  getAttributeId());
821         List JavaDoc rmas = RModuleAttributePeer.doSelect(crit);
822         for (int i=0; i<rmas.size(); i++)
823         {
824             RModuleAttribute rma = (RModuleAttribute)rmas.get(i);
825             rma.delete(true);
826         }
827         ScarabCache.clear();
828     }
829
830     /**
831      * Delete mappings with global issue types.
832      */

833     public void deleteIssueTypeMappings()
834         throws Exception JavaDoc
835     {
836         Criteria crit = new Criteria();
837         crit.add(RAttributeAttributeGroupPeer.ATTRIBUTE_ID,
838                  getAttributeId());
839         crit.addJoin(RAttributeAttributeGroupPeer.GROUP_ID,
840                      AttributeGroupPeer.ATTRIBUTE_GROUP_ID);
841         crit.add(AttributeGroupPeer.MODULE_ID, null);
842         List JavaDoc raags = RAttributeAttributeGroupPeer.doSelect(crit);
843         for (Iterator JavaDoc i = raags.iterator(); i.hasNext();)
844         {
845             ((RAttributeAttributeGroup)i.next()).delete();
846         }
847
848         crit = new Criteria();
849         crit.add(RIssueTypeAttributePeer.ATTRIBUTE_ID,
850                  getAttributeId());
851         List JavaDoc rias = RIssueTypeAttributePeer.doSelect(crit);
852         for (Iterator JavaDoc i = rias.iterator(); i.hasNext();)
853         {
854             ((RIssueTypeAttribute)i.next()).delete();
855         }
856         
857         ScarabCache.clear();
858     }
859
860     /**
861      * Refers to Global Issue Types
862      * @return A list of global Issue Types, this attribute is associated with.
863      */

864     private List JavaDoc getAssociatedIssueTypes()
865         throws Exception JavaDoc
866     {
867         Criteria crit = new Criteria();
868         crit.add(RIssueTypeAttributePeer.ATTRIBUTE_ID,
869                  getAttributeId());
870         crit.addJoin(RIssueTypeAttributePeer.ISSUE_TYPE_ID,
871                  IssueTypePeer.ISSUE_TYPE_ID);
872         List JavaDoc issueTypeList = IssueTypePeer.doSelect(crit);
873         return issueTypeList;
874     }
875
876     /**
877      * Checks if this attribute is associated with atleast one of the
878      * global issue types that is system defined.
879      *
880      * @return True if the attribute is associated with a System defined
881      * global Issue Type.False otherwise.
882      */

883
884     public boolean isSystemDefined()
885         throws Exception JavaDoc
886     {
887         boolean systemDefined = false;
888         List JavaDoc issueTypeList = getAssociatedIssueTypes();
889         for (Iterator JavaDoc i = issueTypeList.iterator(); i.hasNext();)
890         {
891             if (((IssueType)i.next()).isSystemDefined())
892             {
893                 systemDefined = true;
894                 break;
895             }
896         }
897         return systemDefined;
898     }
899
900     /**
901      * Gets the attributeOption that will force this attribute to be required, in case of being set.
902      * @return
903      */

904     public AttributeOption getRequiredOption()
905     {
906         AttributeOption option = null;
907         try
908         {
909             option = AttributeOptionPeer.retrieveByPK(this.getRequiredOptionId());
910         }
911         catch (NoRowsException e)
912         {
913             // Nothing to do. Ignore.
914
}
915         catch (TooManyRowsException e)
916         {
917             // Nothing to do. Ignore.
918
}
919         catch (TorqueException e)
920         {
921             e.printStackTrace();
922         }
923         return option;
924     }
925     
926     /**
927      * Returns the array of attributeOptionIds that will force the requiment of this
928      * attribute if set. Used by templates to load the combo.
929      * @return
930      */

931     public Integer JavaDoc[] getConditionsArray()
932     {
933         List JavaDoc conditions = new ArrayList JavaDoc();
934         Integer JavaDoc[] aIDs = null;
935         try
936         {
937             conditions = this.getConditions();
938             aIDs = new Integer JavaDoc[conditions.size()];
939             int i=0;
940             for (Iterator JavaDoc iter = conditions.iterator(); iter.hasNext(); i++)
941             {
942                 Condition cond = (Condition)iter.next();
943                 aIDs[i] = cond.getOptionId();
944             }
945         }
946         catch (TorqueException e)
947         {
948             this.getLog().error("getConditionsArray: " + e);
949         }
950         return aIDs;
951     }
952     
953     public List JavaDoc getConditions() throws TorqueException
954     {
955         if (collConditions == null)
956         {
957             Criteria crit = new Criteria();
958             crit.add(ConditionPeer.ATTRIBUTE_ID, this.getAttributeId());
959             crit.add(ConditionPeer.MODULE_ID, null);
960             crit.add(ConditionPeer.TRANSITION_ID, null);
961             crit.add(ConditionPeer.ISSUE_TYPE_ID, null);
962             collConditions = getConditions(crit);
963         }
964         return collConditions;
965     }
966     
967     /**
968      * Load the attribute options' IDs from the template combo.
969      * @param aOptionId
970      * @throws Exception
971      */

972     public void setConditionsArray(Integer JavaDoc aOptionId[]) throws Exception JavaDoc
973     {
974         Criteria crit = new Criteria();
975         crit.add(ConditionPeer.ATTRIBUTE_ID, this.getAttributeId());
976         crit.add(ConditionPeer.MODULE_ID, null);
977         crit.add(ConditionPeer.ISSUE_TYPE_ID, null);
978         crit.add(ConditionPeer.TRANSITION_ID, null);
979         ConditionPeer.doDelete(crit);
980         this.save();
981         this.getConditions().clear();
982         ConditionManager.clear();
983         if (aOptionId != null)
984             for (int i=0; i<aOptionId.length; i++)
985             {
986                 if (aOptionId[i].intValue() != 0)
987                 {
988                     Condition cond = new Condition();
989                     cond.setAttributeId(this.getAttributeId());
990                     cond.setOptionId(aOptionId[i]);
991                     cond.setModuleId(null);
992                     cond.setIssueTypeId(null);
993                     cond.setTransitionId(null);
994                     this.addCondition(cond);
995                     cond.save();
996                 }
997             }
998     }
999     /**
1000     * Return true if the given attributeOptionId will make the current
1001     * attribute required.
1002     * @param optionID
1003     * @return
1004     * @throws TorqueException
1005     */

1006    public boolean isRequiredIf(Integer JavaDoc optionID) throws TorqueException
1007    {
1008        Condition cond = new Condition();
1009        cond.setAttributeId(this.getAttributeId());
1010        cond.setOptionId(optionID);
1011        cond.setModuleId(null);
1012        cond.setIssueTypeId(null);
1013        cond.setTransitionId(null);
1014        return this.getConditions().contains(cond);
1015    }
1016    
1017    public boolean isConditioned()
1018    {
1019        boolean bRdo = false;
1020        try {
1021            bRdo = this.getConditions().size()>0;
1022        } catch (TorqueException te)
1023        {
1024            // Nothing to do
1025
}
1026        return bRdo;
1027    }
1028
1029}
1030
Popular Tags