KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
52 import java.util.Properties JavaDoc;
53 import java.io.InputStream JavaDoc;
54 import java.io.IOException JavaDoc;
55
56 import org.apache.torque.util.Criteria;
57 import org.apache.torque.om.Persistent;
58 import org.apache.torque.manager.MethodResultCache;
59 import org.apache.fulcrum.localization.Localization;
60
61 import org.tigris.scarab.services.cache.ScarabCache;
62 import org.tigris.scarab.tools.localization.L10NKeySet;
63 import org.tigris.scarab.om.Module;
64 import org.tigris.scarab.om.IssuePeer;
65 import org.tigris.scarab.util.ScarabException;
66 import org.tigris.scarab.util.ScarabConstants;
67 import org.tigris.scarab.util.Log;
68 import org.tigris.scarab.workflow.WorkflowFactory;
69
70 /**
71  * This class represents an IssueType.
72  *
73  * @author <a HREF="mailto:elicia@collab.net">Elicia David</a>
74  * @author <a HREF="mailto:jon@collab.net">Jon S. Stevens</a>
75  * @version $Id: IssueType.java 9628 2005-04-18 19:25:32Z jorgeuriarte $
76  */

77 public class IssueType
78     extends org.tigris.scarab.om.BaseIssueType
79     implements Persistent
80 {
81     private static final String JavaDoc ISSUE_TYPE =
82         "IssueType";
83     private static final String JavaDoc GET_TEMPLATE_ISSUE_TYPE =
84         "getTemplateIssueType";
85     private static final String JavaDoc GET_INSTANCE =
86         "getInstance";
87     protected static final String JavaDoc GET_ATTRIBUTE_GROUPS =
88         "getAttributeGroups";
89     protected static final String JavaDoc GET_ATTRIBUTE_GROUP =
90         "getAttributeGroup";
91     protected static final String JavaDoc GET_R_ISSUETYPE_ATTRIBUTES =
92         "getRIssueTypeAttributes";
93     protected static final String JavaDoc GET_R_ISSUETYPE_OPTIONS =
94         "getRIssueTypeOptions";
95     protected static final String JavaDoc GET_ALL_R_ISSUETYPE_OPTIONS =
96         "getAllRIssueTypeOptions";
97     protected static final String JavaDoc GET_DEFAULT_TEXT_ATTRIBUTE =
98         "getDefaultTextAttribute";
99     protected static final String JavaDoc GET_QUICK_SEARCH_ATTRIBUTES =
100         "getQuickSearchAttributes";
101     protected static final String JavaDoc GET_REQUIRED_ATTRIBUTES =
102         "getRequiredAttributes";
103     protected static final String JavaDoc GET_ACTIVE_ATTRIBUTES =
104         "getActiveAttributes";
105
106     static final String JavaDoc USER = "user";
107     static final String JavaDoc NON_USER = "non-user";
108
109     private static final Properties JavaDoc SYSTEM_CONFIG = new Properties JavaDoc();
110
111     //loads the properties file that specifies system defined issue types
112
static
113     {
114         InputStream JavaDoc in = IssueType.class
115                             .getResourceAsStream("IssueTypeConfig.properties");
116         if (in != null)
117         {
118             try
119             {
120                 SYSTEM_CONFIG.load(in);
121             }
122             catch(IOException JavaDoc ioe)
123             {
124                 Log.get().warn("Exception while loading the file: IssueTypeConfig.properties", ioe);
125             }
126         }
127     }
128
129
130     // this will not change, so only look it up once.
131
private IssueType templateIssueType;
132
133     // this will not change, so only look it up once.
134
private IssueType parentIssueType;
135
136     /**
137      * Gets the IssueType template for this IssueType. The template
138      * is a special type of IssueType.
139      */

140     public IssueType getTemplateIssueType()
141         throws Exception JavaDoc
142     {
143         if (templateIssueType == null)
144         {
145             Criteria crit = new Criteria();
146             crit.add(IssueTypePeer.PARENT_ID, getIssueTypeId());
147             List JavaDoc results = IssueTypePeer.doSelect(crit);
148             if (results.isEmpty())
149             {
150                 throw new ScarabException(L10NKeySet.ExceptionTemplateTypeForIssueType);
151             }
152             else
153             {
154                 templateIssueType = (IssueType)results.get(0);
155             }
156         }
157         return templateIssueType;
158     }
159
160     /**
161      * Gets the parent IssueType for this template IssueType. The template
162      * is a special type of IssueType.
163      */

164     public IssueType getIssueTypeForTemplateType()
165         throws Exception JavaDoc
166     {
167         if (parentIssueType == null)
168         {
169             parentIssueType = getIssueTypeRelatedByParentId();
170         }
171         return parentIssueType;
172     }
173
174
175     /**
176      * Gets the id of the template that corresponds to the issue type.
177      */

178     public Integer JavaDoc getTemplateId()
179         throws Exception JavaDoc
180     {
181         return getTemplateIssueType().getIssueTypeId();
182     }
183
184     /**
185      * Returns true if the issue type has issues associated with it.
186      */

187     public boolean hasIssues()
188         throws Exception JavaDoc
189     {
190         return hasIssues((Module) null);
191     }
192
193    /**
194     * If module name is identical to global name, return the global
195     * name. Otherwise return the module name followed by a space
196     * and the global name in parentheses.
197     * @return a <code>String</code> representation of the display name.
198     */

199     public String JavaDoc getDisplayName(Module module)
200         throws Exception JavaDoc
201     {
202         String JavaDoc moduleName = module.getRModuleIssueType(this).getDisplayName();
203         String JavaDoc displayName = getName();
204         if (!moduleName.equals(displayName))
205         {
206             displayName = moduleName +" (" + displayName + ")";
207         }
208         return displayName;
209     }
210
211     /**
212      * Returns true if the issue type/module has issues associated with it.
213      */

214     public boolean hasIssues(Module module)
215         throws Exception JavaDoc
216     {
217         Criteria crit = new Criteria();
218         crit.add(IssuePeer.TYPE_ID, getIssueTypeId());
219         if (module != null)
220         {
221             crit.add(IssuePeer.MODULE_ID, module.getModuleId());
222         }
223         return (IssuePeer.count(crit) > 0);
224     }
225
226     /**
227      * Get the IssueType using a issue type name
228      */

229     public static IssueType getInstance(String JavaDoc issueTypeName)
230         throws Exception JavaDoc
231     {
232         IssueType result = null;
233         Object JavaDoc obj = ScarabCache.get(ISSUE_TYPE, GET_INSTANCE, issueTypeName);
234         if (obj == null)
235         {
236             Criteria crit = new Criteria();
237             crit.add(IssueTypePeer.NAME, issueTypeName);
238             List JavaDoc issueTypes = IssueTypePeer.doSelect(crit);
239             if(issueTypes == null || issueTypes.size() == 0)
240             {
241                 throw new ScarabException(L10NKeySet.ExceptionInvalidIssueType,
242                                           issueTypeName);
243             }
244             result = (IssueType)issueTypes.get(0);
245             ScarabCache.put(result, ISSUE_TYPE, GET_INSTANCE, issueTypeName);
246         }
247         else
248         {
249             result = (IssueType)obj;
250         }
251         return result;
252     }
253
254     /**
255      * Copy the IssueType and its corresponding template type
256      */

257     public IssueType copyIssueType()
258         throws Exception JavaDoc
259     {
260         IssueType newIssueType = new IssueType();
261         newIssueType.setName(getName() + " (copy)");
262         newIssueType.setDescription(getDescription());
263         newIssueType.setParentId(ScarabConstants.INTEGER_0);
264         newIssueType.save();
265         Integer JavaDoc newId = newIssueType.getIssueTypeId();
266
267         // Copy template type
268
IssueType template = IssueTypePeer
269               .retrieveByPK(getTemplateId());
270         IssueType newTemplate = new IssueType();
271         newTemplate.setName(template.getName());
272         newTemplate.setParentId(newId);
273         newTemplate.save();
274
275         // Copy user attributes
276
List JavaDoc userRIAs = getRIssueTypeAttributes(false, USER);
277         for (int m=0; m<userRIAs.size(); m++)
278         {
279             RIssueTypeAttribute userRia = (RIssueTypeAttribute)userRIAs.get(m);
280             RIssueTypeAttribute newUserRia = userRia.copyRia();
281             newUserRia.setIssueTypeId(newId);
282             newUserRia.save();
283         }
284
285         // Copy attribute groups
286
List JavaDoc attrGroups = getAttributeGroups(false);
287         for (int i = 0; i<attrGroups.size(); i++)
288         {
289             AttributeGroup group = (AttributeGroup)attrGroups.get(i);
290             AttributeGroup newGroup = group.copyGroup();
291             newGroup.setIssueTypeId(newId);
292             newGroup.save();
293
294             // add attributes
295
List JavaDoc attrs = group.getAttributes();
296             if (attrs != null)
297             {
298                 for (int j=0; j<attrs.size(); j++)
299                 {
300                     // save attribute-attribute group maps
301
Attribute attr = (Attribute)attrs.get(j);
302                     RAttributeAttributeGroup raag = group.getRAttributeAttributeGroup(attr);
303                     RAttributeAttributeGroup newRaag = new RAttributeAttributeGroup();
304                     newRaag.setAttributeId(raag.getAttributeId());
305                     newRaag.setOrder(raag.getOrder());
306                     newRaag.setGroupId(newGroup.getAttributeGroupId());
307                     newRaag.save();
308
309                     // save attribute-issueType maps
310
RIssueTypeAttribute ria = getRIssueTypeAttribute(attr);
311                     RIssueTypeAttribute newRia = ria.copyRia();
312                     newRia.setIssueTypeId(newId);
313                     newRia.save();
314
315                     // save options
316
List JavaDoc rios = getRIssueTypeOptions(attr, false);
317                     if (rios != null)
318                     {
319                         for (int k=0; k<rios.size(); k++)
320                         {
321                             RIssueTypeOption rio = (RIssueTypeOption)rios.get(k);
322                             RIssueTypeOption newRio = rio.copyRio();
323                             newRio.setIssueTypeId(newId);
324                             newRio.save();
325                         }
326                     }
327                 }
328             }
329         }
330
331         // add workflow
332
WorkflowFactory.getInstance().copyIssueTypeWorkflows(this, newIssueType);
333
334         return newIssueType;
335     }
336
337     /**
338      * Delete mappings with all modules
339      */

340     public void deleteModuleMappings(ScarabUser user)
341         throws Exception JavaDoc
342     {
343         Criteria crit = new Criteria();
344         crit.add(RModuleIssueTypePeer.ISSUE_TYPE_ID,
345                  getIssueTypeId());
346         List JavaDoc rmits = RModuleIssueTypePeer.doSelect(crit);
347         for (int i=0; i<rmits.size(); i++)
348         {
349             RModuleIssueType rmit = (RModuleIssueType)rmits.get(i);
350             rmit.delete(user);
351         }
352         ScarabCache.clear();
353     }
354
355
356     /**
357      * Create default groups upon issue type creation.
358      */

359     public void createDefaultGroups()
360         throws Exception JavaDoc
361     {
362         AttributeGroup ag = createNewGroup();
363         ag.setOrder(1);
364         ag.setDedupe(true);
365         ag.setDescription(null);
366         ag.save();
367         AttributeGroup ag2 = createNewGroup();
368         ag2.setOrder(3);
369         ag2.setDedupe(false);
370         ag2.setDescription(null);
371         ag2.save();
372     }
373
374     public List JavaDoc getAttributeGroups(Module module)
375         throws Exception JavaDoc
376     {
377         return getAttributeGroups(module, false);
378     }
379
380     public List JavaDoc getAttributeGroups(boolean activeOnly)
381         throws Exception JavaDoc
382     {
383         return getAttributeGroups(null, activeOnly);
384     }
385
386     /**
387      * List of attribute groups associated with this module).
388      */

389     public List JavaDoc getAttributeGroups(Module module, boolean activeOnly)
390         throws Exception JavaDoc
391     {
392         List JavaDoc groups = null;
393         Boolean JavaDoc activeBool = activeOnly ? Boolean.TRUE : Boolean.FALSE;
394         Object JavaDoc obj = getMethodResult().get(this, GET_ATTRIBUTE_GROUPS,
395                                            module, activeBool);
396         if (obj == null)
397         {
398             Criteria crit = new Criteria()
399                 .add(AttributeGroupPeer.ISSUE_TYPE_ID, getIssueTypeId())
400                 .addAscendingOrderByColumn(AttributeGroupPeer.PREFERRED_ORDER);
401             if (activeOnly)
402             {
403                 crit.add(AttributeGroupPeer.ACTIVE, true);
404             }
405             if (module != null)
406             {
407                 crit.add(AttributeGroupPeer.MODULE_ID, module.getModuleId());
408             }
409             else
410             {
411                 // TODO Change this to be crit.add(AttributeGroupPeer.MODULE_ID, Criteria.ISNULL) when torque is fixed
412
crit.add(AttributeGroupPeer.MODULE_ID,
413                          (Object JavaDoc)(AttributeGroupPeer.MODULE_ID + " IS NULL"),
414                          Criteria.CUSTOM);
415             }
416             groups = AttributeGroupPeer.doSelect(crit);
417             getMethodResult().put(groups, this, GET_ATTRIBUTE_GROUPS,
418                                   module, activeBool);
419         }
420         else
421         {
422             groups = (List JavaDoc)obj;
423         }
424         return groups;
425     }
426
427
428     public AttributeGroup createNewGroup()
429         throws Exception JavaDoc
430     {
431         return createNewGroup(null);
432     }
433
434     /**
435      * Creates new attribute group.
436      */

437     public AttributeGroup createNewGroup(Module module)
438         throws Exception JavaDoc
439     {
440         List JavaDoc groups = getAttributeGroups(module, false);
441         AttributeGroup ag = new AttributeGroup();
442
443         // Make default group name 'new attribute group'
444
ag.setName(Localization.getString("ScarabBundle",
445                 ScarabConstants.DEFAULT_LOCALE, "NewAttributeGroup"));
446         ag.setActive(true);
447         ag.setIssueTypeId(getIssueTypeId());
448         if (module != null)
449         {
450             ag.setModuleId(module.getModuleId());
451         }
452         if (groups.size() == 0)
453         {
454             ag.setDedupe(true);
455             ag.setOrder(groups.size() +1);
456         }
457         else
458         {
459             ag.setDedupe(false);
460             ag.setOrder(groups.size() +2);
461         }
462         ag.save();
463         groups.add(ag);
464         return ag;
465     }
466
467     /**
468      * Gets the sequence where the dedupe screen fits between groups.
469      *
470      * @see #getDedupeSequence(Module)
471      */

472     public int getDedupeSequence()
473         throws Exception JavaDoc
474     {
475         return getDedupeSequence(null);
476     }
477
478     /**
479      * Gets the sequence where the dedupe screen fits between groups.
480      *
481      * @param module A specific Module to retrieve AttributeGroup
482      * associations for, or <code>null</code> for groups associated
483      * with the global issue type.
484      */

485     int getDedupeSequence(Module module)
486         throws Exception JavaDoc
487     {
488         List JavaDoc groups = getAttributeGroups(module, false);
489         int sequence = groups.size() + 1;
490         for (int i = 1; i <= groups.size(); i++)
491         {
492             int order;
493             int previousOrder;
494             try
495             {
496                 order = ((AttributeGroup) groups.get(i)).getOrder();
497                 previousOrder = ((AttributeGroup) groups.get(i - 1)).getOrder();
498             }
499             catch (Exception JavaDoc e)
500             {
501                 Log.get().warn("Error accessing dedupe sequence for issue "
502                                + "type '" + this + '\'', e);
503                 return sequence;
504             }
505
506             if (order != previousOrder + 1)
507             {
508                 sequence = order - 1;
509                 break;
510             }
511         }
512         return sequence;
513     }
514
515     /**
516      * Gets associated attributes.
517      */

518     public List JavaDoc getRIssueTypeAttributes()
519     {
520         List JavaDoc rias = null;
521         try
522         {
523             rias = getRIssueTypeAttributes(false);
524         }
525         catch (Exception JavaDoc e)
526         {
527             Log.get().warn("Could not get RIA records for " + getName(), e);
528         }
529         return rias;
530     }
531
532     /**
533      * Gets associated attributes.
534      */

535     public List JavaDoc getRIssueTypeAttributes(boolean activeOnly)
536         throws Exception JavaDoc
537     {
538         return getRIssueTypeAttributes(activeOnly, "all");
539     }
540
541
542     /**
543      * Gets associated attributes.
544      */

545     public List JavaDoc getRIssueTypeAttributes(boolean activeOnly,
546                                         String JavaDoc attributeType)
547         throws Exception JavaDoc
548     {
549         List JavaDoc rias = null;
550         Boolean JavaDoc activeBool = (activeOnly ? Boolean.TRUE : Boolean.FALSE);
551         Object JavaDoc obj = getMethodResult().get(this, GET_R_ISSUETYPE_ATTRIBUTES,
552                                            activeBool, attributeType);
553         if (obj == null)
554         {
555             Criteria crit = new Criteria();
556             crit.add(RIssueTypeAttributePeer.ISSUE_TYPE_ID,
557                      getIssueTypeId());
558             crit.addAscendingOrderByColumn(
559                 RIssueTypeAttributePeer.PREFERRED_ORDER);
560             
561             if (activeOnly)
562             {
563                 crit.add(RIssueTypeAttributePeer.ACTIVE, true);
564             }
565             
566             if (USER.equals(attributeType))
567             {
568                 crit.add(AttributePeer.ATTRIBUTE_TYPE_ID,
569                          AttributeTypePeer.USER_TYPE_KEY);
570                 crit.addJoin(AttributePeer.ATTRIBUTE_ID,
571                          RIssueTypeAttributePeer.ATTRIBUTE_ID);
572             }
573             else if (NON_USER.equals(attributeType))
574             {
575                 crit.addJoin(AttributePeer.ATTRIBUTE_ID,
576                          RIssueTypeAttributePeer.ATTRIBUTE_ID);
577                 crit.add(AttributePeer.ATTRIBUTE_TYPE_ID,
578                          AttributeTypePeer.USER_TYPE_KEY,
579                          Criteria.NOT_EQUAL);
580             }
581             
582             rias = RIssueTypeAttributePeer.doSelect(crit);
583             getMethodResult().put(rias, this, GET_R_ISSUETYPE_ATTRIBUTES,
584                                   activeBool, attributeType);
585         }
586         else
587         {
588             rias = (List JavaDoc)obj;
589         }
590         return rias;
591     }
592
593     /**
594      * Gets associated activeattributes.
595      */

596     public List JavaDoc getAttributes(String JavaDoc attributeType)
597         throws Exception JavaDoc
598     {
599         ArrayList JavaDoc attrs = new ArrayList JavaDoc();
600         List JavaDoc rias = getRIssueTypeAttributes(true, attributeType);
601         for (int i=0; i<rias.size(); i++)
602         {
603             attrs.add(((RIssueTypeAttribute)rias.get(i)).getAttribute());
604         }
605         return attrs;
606     }
607          
608     /**
609      * Adds issuetype-attribute mapping to issue type.
610      */

611     public RIssueTypeAttribute addRIssueTypeAttribute(Attribute attribute)
612         throws Exception JavaDoc
613     {
614         String JavaDoc attributeType = null;
615         attributeType = (attribute.isUserAttribute() ? USER : NON_USER);
616
617         RIssueTypeAttribute ria = new RIssueTypeAttribute();
618         ria.setIssueTypeId(getIssueTypeId());
619         ria.setAttributeId(attribute.getAttributeId());
620         ria.setOrder(getLastAttribute(attributeType) + 1);
621         ria.save();
622         getRIssueTypeAttributes(false, attributeType).add(ria);
623         return ria;
624     }
625
626     public RIssueTypeAttribute getRIssueTypeAttribute(Attribute attribute)
627         throws Exception JavaDoc
628     {
629         RIssueTypeAttribute ria = null;
630         List JavaDoc rias = null;
631         if (attribute.isUserAttribute())
632         {
633             rias = getRIssueTypeAttributes(false, USER);
634         }
635         else
636         {
637             rias = getRIssueTypeAttributes(false, NON_USER);
638         }
639         Iterator JavaDoc i = rias.iterator();
640         while (i.hasNext())
641         {
642             RIssueTypeAttribute tempRia = (RIssueTypeAttribute)i.next();
643             if (tempRia.getAttribute().equals(attribute))
644             {
645                 ria = tempRia;
646                 break;
647             }
648         }
649         return ria;
650     }
651
652     /**
653      * gets a list of all of the User Attributes in an issue type.
654      */

655     public List JavaDoc getUserAttributes()
656         throws Exception JavaDoc
657     {
658         return getUserAttributes(true);
659     }
660
661     /**
662      * gets a list of all of the User Attributes in an issue type.
663      */

664     public List JavaDoc getUserAttributes(boolean activeOnly)
665         throws Exception JavaDoc
666     {
667         List JavaDoc rIssueTypeAttributes = getRIssueTypeAttributes(activeOnly, USER);
668         List JavaDoc userAttributes = new ArrayList JavaDoc();
669
670         for (int i=0; i<rIssueTypeAttributes.size(); i++)
671         {
672             Attribute att = ((RIssueTypeAttribute)rIssueTypeAttributes.get(i)).getAttribute();
673             userAttributes.add(att);
674         }
675         return userAttributes;
676     }
677
678     /**
679      * FIXME: can this be done more efficently?
680      * gets highest sequence number for issueType-attribute map
681      * so that a new RIssueTypeAttribute can be added at the end.
682      */

683     public int getLastAttribute(String JavaDoc attributeType)
684         throws Exception JavaDoc
685     {
686         List JavaDoc itAttributes = getRIssueTypeAttributes(false, attributeType);
687         int last = 0;
688
689         for (int i=0; i<itAttributes.size(); i++)
690         {
691                int order = ((RIssueTypeAttribute) itAttributes.get(i))
692                          .getOrder();
693                if (order > last)
694                {
695                    last = order;
696                }
697         }
698         return last;
699     }
700
701
702     /**
703      * FIXME: can this be done more efficently?
704      * gets highest sequence number for module-attribute map
705      * so that a new RIssueTypeOption can be added at the end.
706      */

707     public int getLastAttributeOption(Attribute attribute)
708         throws Exception JavaDoc
709     {
710         List JavaDoc issueTypeOptions = getRIssueTypeOptions(attribute);
711         int last = 0;
712
713         for (int i=0; i<issueTypeOptions.size(); i++)
714         {
715                int order = ((RIssueTypeOption) issueTypeOptions.get(i))
716                          .getOrder();
717                if (order > last)
718                {
719                    last = order;
720                }
721         }
722         return last;
723     }
724
725     /**
726      * Adds issuetype-attribute-option mapping to module.
727      */

728     public RIssueTypeOption addRIssueTypeOption(AttributeOption option)
729         throws Exception JavaDoc
730     {
731         RIssueTypeOption rio = new RIssueTypeOption();
732         rio.setIssueTypeId(getIssueTypeId());
733         rio.setOptionId(option.getOptionId());
734         rio.setOrder(getLastAttributeOption(option.getAttribute()) + 1);
735         rio.save();
736         getRIssueTypeOptions(option.getAttribute(), false).add(rio);
737         return rio;
738     }
739
740     /**
741      * Gets associated attribute options.
742      */

743     public List JavaDoc getRIssueTypeOptions(Attribute attribute)
744         throws Exception JavaDoc
745     {
746         return getRIssueTypeOptions(attribute, true);
747     }
748
749     /**
750      * Gets associated attribute options.
751      */

752     public List JavaDoc getRIssueTypeOptions(Attribute attribute, boolean activeOnly)
753         throws Exception JavaDoc
754     {
755         List JavaDoc allRIssueTypeOptions = null;
756         allRIssueTypeOptions = getAllRIssueTypeOptions(attribute);
757
758         if (allRIssueTypeOptions != null)
759         {
760             if (activeOnly)
761             {
762                 List JavaDoc activeRIssueTypeOptions =
763                     new ArrayList JavaDoc(allRIssueTypeOptions.size());
764                 for (int i=0; i<allRIssueTypeOptions.size(); i++)
765                 {
766                     RIssueTypeOption rio =
767                         (RIssueTypeOption)allRIssueTypeOptions.get(i);
768                     if (rio.getActive())
769                     {
770                         activeRIssueTypeOptions.add(rio);
771                     }
772                 }
773                 allRIssueTypeOptions = activeRIssueTypeOptions;
774             }
775         }
776         return allRIssueTypeOptions;
777     }
778     
779
780     private List JavaDoc getAllRIssueTypeOptions(Attribute attribute)
781         throws Exception JavaDoc
782     {
783         List JavaDoc rIssueTypeOpts;
784         Object JavaDoc obj = ScarabCache.get(this, GET_ALL_R_ISSUETYPE_OPTIONS,
785                                      attribute);
786         if (obj == null)
787         {
788             List JavaDoc options = attribute.getAttributeOptions(false);
789             Integer JavaDoc[] optIds = null;
790             if (options == null)
791             {
792                 optIds = new Integer JavaDoc[0];
793             }
794             else
795             {
796                 optIds = new Integer JavaDoc[options.size()];
797             }
798             for (int i=optIds.length-1; i>=0; i--)
799             {
800                 optIds[i] = ((AttributeOption)options.get(i)).getOptionId();
801             }
802             
803             if (optIds.length > 0)
804             {
805                 Criteria crit = new Criteria();
806                 crit.add(RIssueTypeOptionPeer.ISSUE_TYPE_ID, getIssueTypeId());
807                 crit.addIn(RIssueTypeOptionPeer.OPTION_ID, optIds);
808                 crit.addJoin(RIssueTypeOptionPeer.OPTION_ID, AttributeOptionPeer.OPTION_ID);
809                 crit.addAscendingOrderByColumn(RIssueTypeOptionPeer.PREFERRED_ORDER);
810                 crit.addAscendingOrderByColumn(AttributeOptionPeer.OPTION_NAME);
811                 rIssueTypeOpts = RIssueTypeOptionPeer.doSelect(crit);
812             }
813             else
814             {
815                 rIssueTypeOpts = new ArrayList JavaDoc(0);
816             }
817             ScarabCache.put(rIssueTypeOpts, this, GET_ALL_R_ISSUETYPE_OPTIONS,
818                             attribute);
819         }
820         else
821         {
822             rIssueTypeOpts = (List JavaDoc)obj;
823         }
824         return rIssueTypeOpts;
825     }
826
827     public RIssueTypeOption getRIssueTypeOption(AttributeOption option)
828         throws Exception JavaDoc
829     {
830         RIssueTypeOption rio = null;
831         List JavaDoc rios = getRIssueTypeOptions(option.getAttribute(), false);
832         Iterator JavaDoc i = rios.iterator();
833         while (i.hasNext())
834         {
835             rio = (RIssueTypeOption)i.next();
836             if (rio.getAttributeOption().equals(option))
837             {
838                 break;
839             }
840         }
841
842         return rio;
843     }
844
845     /**
846      * Gets a list of all of the global Attributes that are not
847      * Associated with this issue type
848      */

849     public List JavaDoc getAvailableAttributes(String JavaDoc attributeType)
850         throws Exception JavaDoc
851     {
852         List JavaDoc allAttributes = AttributePeer.getAttributes(attributeType);
853         List JavaDoc availAttributes = new ArrayList JavaDoc();
854         List JavaDoc rIssueTypeAttributes = getRIssueTypeAttributes(false,
855                                                             attributeType);
856         List JavaDoc attrs = new ArrayList JavaDoc();
857         for (int i=0; i<rIssueTypeAttributes.size(); i++)
858         {
859             attrs.add(
860                ((RIssueTypeAttribute) rIssueTypeAttributes.get(i)).getAttribute());
861         }
862         for (int i=0; i<allAttributes.size(); i++)
863         {
864             Attribute att = (Attribute)allAttributes.get(i);
865             if (!attrs.contains(att))
866             {
867                 availAttributes.add(att);
868             }
869         }
870         return availAttributes;
871     }
872
873
874     /**
875      * Gets a list of all of the global attributes options
876      * that are not associated with this issue type
877      */

878     public List JavaDoc getAvailableAttributeOptions(Attribute attribute)
879         throws Exception JavaDoc
880     {
881         List JavaDoc rIssueTypeOptions = getRIssueTypeOptions(attribute, false);
882         List JavaDoc issueTypeOptions = new ArrayList JavaDoc();
883         if (rIssueTypeOptions != null)
884         {
885             for (int i=0; i<rIssueTypeOptions.size(); i++)
886             {
887                 issueTypeOptions.add(
888                    ((RIssueTypeOption) rIssueTypeOptions.get(i)).getAttributeOption());
889             }
890         }
891
892         List JavaDoc allOptions = attribute.getAttributeOptions(false);
893         List JavaDoc availOptions = new ArrayList JavaDoc();
894
895         for (int i=0; i<allOptions.size(); i++)
896         {
897             AttributeOption option = (AttributeOption)allOptions.get(i);
898             if (!issueTypeOptions.contains(option))
899             {
900                 availOptions.add(option);
901             }
902         }
903         return availOptions;
904     }
905
906     private MethodResultCache getMethodResult()
907     {
908         return IssueTypeManager.getMethodResult();
909     }
910
911     public String JavaDoc toString()
912     {
913         return '{' + super.toString() + ": name=" + getName() + '}';
914     }
915
916     /**
917      * Gets a list of non-user AttributeValues which match a given Module.
918      * It is used in the MoveIssue2.vm template
919      */

920     public List JavaDoc getMatchingAttributeValuesList(Module oldModule, Module newModule,
921                                                IssueType newIssueType)
922           throws Exception JavaDoc
923     {
924         List JavaDoc matchingAttributes = new ArrayList JavaDoc();
925         List JavaDoc srcActiveAttrs = getActiveAttributes(oldModule);
926         List JavaDoc destActiveAttrs = newIssueType.getActiveAttributes(newModule);
927         for (int i = 0; i<srcActiveAttrs.size(); i++)
928         {
929             Attribute attr = (Attribute)srcActiveAttrs.get(i);
930                 
931             if (destActiveAttrs.contains(attr))
932             {
933                 matchingAttributes.add(attr);
934             }
935         }
936         return matchingAttributes;
937     }
938
939     /**
940      * Gets a list of Attributes which do not match a given Module.
941      * It is used in the MoveIssue2.vm template
942      */

943     public List JavaDoc getOrphanAttributeValuesList(Module oldModule, Module newModule,
944                                              IssueType newIssueType)
945           throws Exception JavaDoc
946     {
947         List JavaDoc orphanAttributes = new ArrayList JavaDoc();
948         List JavaDoc srcActiveAttrs = getActiveAttributes(oldModule);
949         List JavaDoc destActiveAttrs = newIssueType.getActiveAttributes(newModule);
950         for (int i = 0; i<srcActiveAttrs.size(); i++)
951         {
952             Attribute attr = (Attribute)srcActiveAttrs.get(i);
953             if (!destActiveAttrs.contains(attr))
954             {
955                 orphanAttributes.add(attr);
956             }
957         }
958         return orphanAttributes;
959     }
960
961     /**
962      * Checks if this Issue Type is system defined.
963      * Such Issue types are specified in "IssueTypeConfig.properties"
964      * file in the format "<SCARAB_ISSUE_TYPE.NAME>=system"
965      *
966      * @return True if this Issue Type is System defined. False otherwise
967      */

968     public boolean isSystemDefined()
969         throws Exception JavaDoc
970     {
971         boolean systemDefined = false;
972         String JavaDoc name = getName();
973         if (name != null)
974         {
975             systemDefined = "system".equalsIgnoreCase(SYSTEM_CONFIG.getProperty(name));
976         }
977         return systemDefined;
978     }
979
980
981     /**
982      * if an RMA is the chosen attribute for email subjects then return it.
983      * if not explicitly chosen, choose the highest ordered text attribute.
984      *
985      * @return the Attribute to use as the email subject,
986      * or null if no suitable Attribute could be found.
987      */

988     public Attribute getDefaultTextAttribute(Module module)
989         throws Exception JavaDoc
990     {
991         Attribute result = null;
992         Object JavaDoc obj = ScarabCache.get(this, GET_DEFAULT_TEXT_ATTRIBUTE);
993         if (obj == null)
994         {
995             // get related RMAs
996
Criteria crit = new Criteria()
997                 .add(RModuleAttributePeer.MODULE_ID,
998                      module.getModuleId());
999             crit.addAscendingOrderByColumn(
1000                RModuleAttributePeer.PREFERRED_ORDER);
1001            List JavaDoc rmas = getRModuleAttributes(crit);
1002            
1003            // the code to find the correct attribute could be quite simple by
1004
// looping and calling RMA.isDefaultText(). The code from
1005
// that method can be restructured here to more efficiently
1006
// answer this question.
1007
Iterator JavaDoc i = rmas.iterator();
1008            while (i.hasNext())
1009            {
1010                RModuleAttribute rma = (RModuleAttribute)i.next();
1011                if (rma.getDefaultTextFlag())
1012                {
1013                    result = rma.getAttribute();
1014                    break;
1015                }
1016            }
1017            
1018            if (result == null)
1019            {
1020                // locate the highest ranked text attribute
1021
i = rmas.iterator();
1022                while (i.hasNext())
1023                {
1024                    RModuleAttribute rma = (RModuleAttribute)i.next();
1025                    Attribute testAttr = rma.getAttribute();
1026                    if (testAttr.isTextAttribute() &&
1027                         getAttributeGroup(module, testAttr).getActive())
1028                    {
1029                        result = testAttr;
1030                        break;
1031                    }
1032                }
1033            }
1034            ScarabCache.put(result, this, GET_DEFAULT_TEXT_ATTRIBUTE);
1035        }
1036        else
1037        {
1038            result = (Attribute)obj;
1039        }
1040        return result;
1041    }
1042
1043    /**
1044     * Array of Attributes used for quick search.
1045     *
1046     * @return an <code>List</code> of Attribute objects
1047     */

1048    public List JavaDoc getQuickSearchAttributes(Module module)
1049        throws Exception JavaDoc
1050    {
1051        List JavaDoc attributes = null;
1052        Object JavaDoc obj = ScarabCache.get(this, GET_QUICK_SEARCH_ATTRIBUTES,
1053                                     module);
1054        if (obj == null)
1055        {
1056            Criteria crit = new Criteria(3)
1057                .add(RModuleAttributePeer.QUICK_SEARCH, true);
1058            addOrderByClause(crit, module);
1059            attributes = getAttributes(crit);
1060            ScarabCache.put(attributes, this, GET_QUICK_SEARCH_ATTRIBUTES,
1061                            module);
1062        }
1063        else
1064        {
1065            attributes = (List JavaDoc)obj;
1066        }
1067        return attributes;
1068    }
1069
1070    /**
1071     * gets a list of all of the Attributes in a Module based on the Criteria.
1072     */

1073    private List JavaDoc getAttributes(Criteria criteria)
1074        throws Exception JavaDoc
1075    {
1076        List JavaDoc moduleAttributes = getRModuleAttributes(criteria);
1077        List JavaDoc attributes = new ArrayList JavaDoc(moduleAttributes.size());
1078        for (int i=0; i<moduleAttributes.size(); i++)
1079        {
1080            attributes.add(
1081               ((RModuleAttribute) moduleAttributes.get(i)).getAttribute());
1082        }
1083        return attributes;
1084    }
1085
1086    /**
1087     * Array of Attributes which are active and required by this module.
1088     * Whose attribute group's are also active.
1089     * @return an <code>List</code> of Attribute objects
1090     */

1091    public List JavaDoc getRequiredAttributes(Module module)
1092        throws Exception JavaDoc
1093    {
1094
1095        List JavaDoc attributes = null;
1096        Object JavaDoc obj = ScarabCache.get(this, GET_REQUIRED_ATTRIBUTES,
1097                                     module);
1098        if (obj == null)
1099        {
1100            Criteria crit = new Criteria(3)
1101                .add(RModuleAttributePeer.REQUIRED, true);
1102            crit.add(RModuleAttributePeer.ACTIVE, true);
1103            addOrderByClause(crit, module);
1104            List JavaDoc temp = getAttributes(crit);
1105            List JavaDoc requiredAttributes = new ArrayList JavaDoc();
1106            for (int i=0; i <temp.size(); i++)
1107            {
1108                Attribute att = (Attribute)temp.get(i);
1109                AttributeGroup group = getAttributeGroup(module, att);
1110                if (group != null && group.getActive())
1111                {
1112                    requiredAttributes.add(att);
1113                }
1114            }
1115            attributes = requiredAttributes;
1116            ScarabCache.put(attributes, this, GET_REQUIRED_ATTRIBUTES,
1117                            module);
1118        }
1119        else
1120        {
1121            attributes = (List JavaDoc)obj;
1122        }
1123        return attributes;
1124
1125    }
1126
1127    /**
1128     * Array of active Attributes for an issue type.
1129     *
1130     * @return an <code>List</code> of Attribute objects
1131     */

1132    public List JavaDoc getActiveAttributes(Module module)
1133        throws Exception JavaDoc
1134    {
1135        List JavaDoc attributes = null;
1136        Object JavaDoc obj = ScarabCache.get(this, GET_ACTIVE_ATTRIBUTES, module);
1137        if (obj == null)
1138        {
1139            Criteria crit = new Criteria(2);
1140            crit.add(RModuleAttributePeer.ACTIVE, true);
1141            addOrderByClause(crit, module);
1142            attributes = getAttributes(crit);
1143            ScarabCache.put(attributes, this, GET_ACTIVE_ATTRIBUTES,
1144                            module);
1145        }
1146        else
1147        {
1148            attributes = (List JavaDoc)obj;
1149        }
1150        return attributes;
1151    }
1152
1153    private void addOrderByClause(Criteria crit, Module module)
1154    {
1155        crit.addAscendingOrderByColumn(RModuleAttributePeer.PREFERRED_ORDER);
1156        crit.addAscendingOrderByColumn(RModuleAttributePeer.DISPLAY_VALUE);
1157        crit.add(RModuleAttributePeer.MODULE_ID, module.getModuleId());
1158    }
1159
1160    private AttributeGroup getAttributeGroup(Module module,
1161                                             Attribute attribute)
1162        throws Exception JavaDoc
1163    {
1164        AttributeGroup group = null;
1165        Object JavaDoc obj = ScarabCache.get(this, GET_ATTRIBUTE_GROUP,
1166                                     module, attribute);
1167        if (obj == null)
1168        {
1169            Criteria crit = new Criteria()
1170                .add(AttributeGroupPeer.ISSUE_TYPE_ID, getIssueTypeId())
1171                .add(AttributeGroupPeer.MODULE_ID,
1172                     module.getModuleId())
1173                .addJoin(RAttributeAttributeGroupPeer.GROUP_ID,
1174                   AttributeGroupPeer.ATTRIBUTE_GROUP_ID)
1175                .add(RAttributeAttributeGroupPeer.ATTRIBUTE_ID,
1176                     attribute.getAttributeId());
1177            List JavaDoc results = AttributeGroupPeer.doSelect(crit);
1178            if (results.size() > 0)
1179            {
1180                group = (AttributeGroup)results.get(0);
1181                ScarabCache.put(group, this, GET_ATTRIBUTE_GROUP,
1182                                module, attribute);
1183            }
1184        }
1185        else
1186        {
1187            group = (AttributeGroup)obj;
1188        }
1189        return group;
1190    }
1191}
1192
Popular Tags