KickJava   Java API By Example, From Geeks To Geeks.

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


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
50
51 import java.util.List JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.LinkedList JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import org.apache.torque.TorqueException;
56 import org.apache.torque.om.SimpleKey;
57 import org.apache.torque.om.Persistent;
58 import org.apache.torque.util.Criteria;
59
60 import org.tigris.scarab.services.security.ScarabSecurity;
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.ModuleManager;
65 import org.tigris.scarab.util.ScarabException;
66 import org.tigris.scarab.util.Log;
67 import org.tigris.scarab.workflow.WorkflowFactory;
68
69 /**
70  * You should add additional methods to this class to meet the
71  * application requirements. This class will only be generated as
72  * long as it does not already exist in the output directory.
73  */

74 public class AttributeGroup
75     extends org.tigris.scarab.om.BaseAttributeGroup
76     implements Persistent
77 {
78     // the following Strings are method names that are used in caching results
79
public static final String JavaDoc GET_ATTRIBUTES =
80         "getAttributes";
81     private static final String JavaDoc GET_R_ATTRIBUTE_ATTRGROUP =
82         "getRAttributeAttributeGroup";
83
84     public boolean isGlobal()
85         throws TorqueException
86     {
87         return (getModule() == null);
88     }
89
90     /**
91      * Throws UnsupportedOperationException. Use
92      * <code>getModule()</code> instead.
93      *
94      * @return a <code>ScarabModule</code> value
95      */

96     public ScarabModule getScarabModule()
97     {
98         throw new UnsupportedOperationException JavaDoc(
99             "Should use getModule"); //EXCEPTION
100
}
101
102     /**
103      * Throws UnsupportedOperationException. Use
104      * <code>setModule(Module)</code> instead.
105      *
106      */

107     public void setScarabModule(ScarabModule module)
108     {
109         throw new UnsupportedOperationException JavaDoc(
110             "Should use setModule(Module). Note module cannot be new."); //EXCEPTION
111
}
112
113     /**
114      * Use this instead of setScarabModule. Note: module cannot be new.
115      */

116     public void setModule(Module me)
117         throws TorqueException
118     {
119         Integer JavaDoc id = me.getModuleId();
120         if ( id == null)
121         {
122             throw new TorqueException("Modules must be saved prior to " +
123                                       "being associated with other objects."); //EXCEPTION
124
}
125         setModuleId(id);
126     }
127
128     /**
129      * Module getter. Use this method instead of getScarabModule().
130      *
131      * @return a <code>Module</code> value
132      */

133     public Module getModule()
134         throws TorqueException
135     {
136         Module module = null;
137         Integer JavaDoc id = getModuleId();
138         if ( id != null )
139         {
140             module = ModuleManager.getInstance(id);
141         }
142         
143         return module;
144     }
145
146     private static org.apache.torque.manager.MethodResultCache
147         getMethodResult()
148     {
149         return AttributeGroupManager.getMethodResult();
150     }
151
152     public boolean hasAnyOptionAttributes()
153         throws Exception JavaDoc
154     {
155         boolean result = false;
156         for (Iterator JavaDoc i = getAttributes().iterator(); i.hasNext() && !result;)
157         {
158             result = ((Attribute)i.next()).isOptionAttribute();
159         }
160         return result;
161     }
162
163     /**
164      * List of Attributes in this group.
165      */

166     public List JavaDoc getAttributes()
167         throws Exception JavaDoc
168     {
169         List JavaDoc result = null;
170         Object JavaDoc obj = getMethodResult().get(this, GET_ATTRIBUTES);
171         if (obj == null)
172         {
173             Log.get().debug("getAttributes() not cached, getting from db");
174             Criteria crit = new Criteria()
175                 .add(RAttributeAttributeGroupPeer.GROUP_ID,
176                      getAttributeGroupId())
177                 .addJoin(RAttributeAttributeGroupPeer.ATTRIBUTE_ID,
178                          AttributePeer.ATTRIBUTE_ID)
179                 .add(AttributePeer.ATTRIBUTE_TYPE_ID,
180                      AttributeTypePeer.USER_TYPE_KEY, Criteria.NOT_EQUAL)
181                 .add(AttributePeer.DELETED, false)
182                 .addAscendingOrderByColumn(RAttributeAttributeGroupPeer
183                                            .PREFERRED_ORDER);
184             List JavaDoc raags = RAttributeAttributeGroupPeer.doSelect(crit);
185             result = new LinkedList JavaDoc();
186             Iterator JavaDoc i = raags.iterator();
187             while (i.hasNext())
188             {
189                 Integer JavaDoc id = ((RAttributeAttributeGroup)i.next())
190                     .getAttributeId();
191                 result.add(AttributeManager.getInstance(SimpleKey.keyFor(id)));
192                 if (Log.get().isDebugEnabled())
193                 {
194                     Log.get().debug("attribute id=" + id +
195                                     "'s relationship to group was in db");
196                 }
197             }
198             getMethodResult().put(result, this, GET_ATTRIBUTES);
199         }
200         else
201         {
202             result = (List JavaDoc)obj;
203             if (Log.get().isDebugEnabled())
204             {
205                 Log.get().debug("getAttributes() returning cached value");
206                 for (Iterator JavaDoc i = result.iterator(); i.hasNext();)
207                 {
208                     Log.get().debug("attribute id=" +
209                                     ((Attribute)i.next()).getAttributeId() +
210                                     "'s relationship to group was in cache");
211                 }
212             }
213         }
214         return result;
215     }
216
217     /**
218      * @param attribute The attribute to check for the existance of in
219      * this group.
220      * @return Whether this attribute group has <code>attribute</code>
221      * as an attribute.
222      */

223     public boolean hasAttribute(Attribute attribute)
224         throws Exception JavaDoc
225     {
226         List JavaDoc raags = getAttributes();
227         return (raags != null && raags.contains(attribute));
228     }
229
230     /**
231      * Retrieves the attribute in this group with the highest preferred order.
232      */

233     public int getHighestOrderedAttribute ()
234         throws Exception JavaDoc
235     {
236         int highest = 0;
237         Criteria crit = new Criteria()
238             .add(RAttributeAttributeGroupPeer.GROUP_ID,
239                  getAttributeGroupId())
240             .addAscendingOrderByColumn(RAttributeAttributeGroupPeer
241                                        .PREFERRED_ORDER);
242         List JavaDoc raags = RAttributeAttributeGroupPeer.doSelect(crit);
243         if (raags.size() > 0)
244         {
245             RAttributeAttributeGroup raag = (RAttributeAttributeGroup)
246                                              raags.get(raags.size()-1);
247             highest = raag.getOrder();
248         }
249         return highest + 1;
250     }
251        
252     /**
253      * Retrieves R_ATTRIBUTE_ATTRIBUTEGROUP mapping object for this group
254      * And the given attribute.
255      */

256     public RAttributeAttributeGroup getRAttributeAttributeGroup
257         (Attribute attribute)
258         throws Exception JavaDoc
259     {
260         RAttributeAttributeGroup result = null;
261         Object JavaDoc obj = ScarabCache.get(this, GET_R_ATTRIBUTE_ATTRGROUP,
262                                      attribute);
263         if (obj == null)
264         {
265             Criteria crit = new Criteria()
266                 .add(RAttributeAttributeGroupPeer.GROUP_ID,
267                      getAttributeGroupId())
268                 .add(RAttributeAttributeGroupPeer.ATTRIBUTE_ID,
269                      attribute.getAttributeId());
270             
271             result = (RAttributeAttributeGroup)RAttributeAttributeGroupPeer
272                 .doSelect(crit).get(0);
273             ScarabCache.put(result, this, GET_R_ATTRIBUTE_ATTRGROUP,
274                             attribute);
275         }
276         else
277         {
278             result = (RAttributeAttributeGroup)obj;
279         }
280         return result;
281     }
282
283     /**
284      * List of module-attribute mappings in this group. If there
285      * are no attributes, this method returns an empty list.
286      */

287     public List JavaDoc getRModuleAttributes()
288         throws Exception JavaDoc
289     {
290         List JavaDoc attrs = getAttributes();
291         Iterator JavaDoc i = attrs.iterator();
292         List JavaDoc rmas = new ArrayList JavaDoc(attrs.size());
293         while (i.hasNext())
294         {
295             Attribute attr = (Attribute)i.next();
296             RModuleAttribute rma = getModule().getRModuleAttribute(attr, getIssueType());
297             if (rma != null)
298             {
299                 rmas.add(rma);
300             }
301         }
302         return rmas;
303     }
304             
305         
306     /**
307      * List of global issuetype-attribute mappings in this group.
308      * If there are no attributes in this group, then this returns
309      * an empty list.
310      */

311     public List JavaDoc getRIssueTypeAttributes()
312         throws Exception JavaDoc
313     {
314         List JavaDoc attrs = getAttributes();
315         Iterator JavaDoc i = attrs.iterator();
316         List JavaDoc rias = new ArrayList JavaDoc(attrs.size());
317         while (i.hasNext())
318         {
319             Attribute attr = (Attribute)i.next();
320             RIssueTypeAttribute ria =
321                 getIssueType().getRIssueTypeAttribute(attr);
322             
323             if (ria != null)
324             {
325                 rias.add(ria);
326             }
327         }
328         
329         return rias;
330     }
331
332     public void delete()
333          throws Exception JavaDoc
334     {
335         int dupeSequence = 0;
336         Integer JavaDoc issueTypeId = getIssueTypeId();
337         IssueType issueType = IssueTypeManager
338             .getInstance(SimpleKey.keyFor(issueTypeId), false);
339         List JavaDoc attrGroups = null;
340
341         if (isGlobal())
342         {
343             attrGroups = issueType.getAttributeGroups(null, false);
344             dupeSequence = issueType.getDedupeSequence();
345             // Delete issuetype-attribute mapping
346
Criteria crit = new Criteria()
347                 .addJoin(RIssueTypeAttributePeer.ATTRIBUTE_ID,
348                          RAttributeAttributeGroupPeer.ATTRIBUTE_ID)
349                 .add(RAttributeAttributeGroupPeer.GROUP_ID,
350                      getAttributeGroupId())
351                 .add(RIssueTypeAttributePeer.ISSUE_TYPE_ID, issueTypeId);
352             List JavaDoc results = RIssueTypeAttributePeer.doSelect(crit);
353             for (Iterator JavaDoc i = results.iterator(); i.hasNext();)
354             {
355                 ((RIssueTypeAttribute)i.next()).delete();
356             }
357         }
358         else
359         {
360             if (issueType.getLocked())
361             {
362                 
363                 throw new ScarabException(L10NKeySet.ExceptionGroupDeleteForbidden,
364                           this.getName(),
365                           issueType.getName());
366             }
367             else
368             {
369                 Module module = getModule();
370                 attrGroups = getIssueType().getAttributeGroups(module, false);
371                 dupeSequence = module.getDedupeSequence(issueType);
372                 // Delete module-attribute mapping
373
Criteria crit = new Criteria()
374                     .addJoin(RModuleAttributePeer.ATTRIBUTE_ID,
375                              RAttributeAttributeGroupPeer.ATTRIBUTE_ID)
376                     .add(RAttributeAttributeGroupPeer.GROUP_ID,
377                              getAttributeGroupId())
378                     .add(RModuleAttributePeer.MODULE_ID,
379                          getModuleId());
380                 Criteria.Criterion critIssueType = crit.getNewCriterion(
381                 RModuleAttributePeer.ISSUE_TYPE_ID,
382                 getIssueTypeId(), Criteria.EQUAL);
383                 critIssueType.or(crit.getNewCriterion(
384                 RModuleAttributePeer.ISSUE_TYPE_ID,
385                 issueType.getTemplateId(), Criteria.EQUAL));
386                 crit.and(critIssueType);
387                 List JavaDoc results = RModuleAttributePeer.doSelect(crit);
388                 for (int i=0; i<results.size(); i++)
389                 {
390                     RModuleAttribute rma = (RModuleAttribute)results.get(i);
391                     rma.delete();
392                 }
393             }
394         }
395
396         // Delete attribute - attribute group mapping
397
Criteria crit2 = new Criteria()
398             .add(RAttributeAttributeGroupPeer.GROUP_ID, getAttributeGroupId());
399         RAttributeAttributeGroupPeer.doDelete(crit2);
400
401
402         // Delete the attribute group
403
int order = getOrder();
404         crit2 = new Criteria()
405             .add(AttributeGroupPeer.ATTRIBUTE_GROUP_ID, getAttributeGroupId());
406         AttributeGroupPeer.doDelete(crit2);
407         IssueTypeManager.getManager().refreshedObject(this);
408             
409         // Adjust the orders for the other attribute groups
410
for (int i=0; i<attrGroups.size(); i++)
411         {
412             AttributeGroup tempGroup = (AttributeGroup)attrGroups.get(i);
413             int tempOrder = tempGroup.getOrder();
414             if (tempGroup.getOrder() > order)
415             {
416                 if (tempOrder == dupeSequence + 1 && tempOrder == 3)
417                 {
418                     tempGroup.setOrder(tempOrder - 2);
419                 }
420                 else
421                 {
422                     tempGroup.setOrder(tempOrder - 1);
423                 }
424                 tempGroup.save();
425             }
426         }
427     }
428
429     public void addAttribute(Attribute attribute)
430          throws Exception JavaDoc
431     {
432         IssueType issueType = getIssueType();
433         Module module = getModule();
434
435         // add attribute group-attribute mapping
436
RAttributeAttributeGroup raag =
437             addRAttributeAttributeGroup(attribute);
438         raag.save();
439         if (Log.get().isDebugEnabled())
440         {
441             Log.get().debug("Calling getAttributes() from addAttribute in order to add attribute id="
442                             + attribute.getAttributeId() + " to the List");
443         }
444         // FIXME! Distributed cache buster, cache should be invalidated.
445
List JavaDoc attributes = getAttributes();
446         if (!attributes.contains(attribute))
447         {
448             attributes.add(attribute);
449         }
450         
451         List JavaDoc allOptions = attribute.getAttributeOptions(false);
452         // remove duplicate options
453
// FIXME! why would we ever want Attribute.getAttributeOptions to
454
// return dupes, should this code be in that method?
455
ArrayList JavaDoc options = new ArrayList JavaDoc();
456         for (int i=0; i<allOptions.size(); i++)
457         {
458             AttributeOption option = (AttributeOption)allOptions.get(i);
459             if (!options.contains(option))
460             {
461                 options.add(option);
462             }
463         }
464
465         if (isGlobal())
466         {
467             // this is a global attribute group
468
// add issuetype-attribute groupings
469
issueType.addRIssueTypeAttribute(attribute);
470
471             // add issueType-attributeoption mappings
472
for (int j=0;j < options.size();j++)
473             {
474                 AttributeOption option = (AttributeOption)options.get(j);
475                 List JavaDoc roos = option.getROptionOptionsRelatedByOption2Id();
476                 ROptionOption roo = (ROptionOption)roos.get(0);
477                 RIssueTypeOption rio = new RIssueTypeOption();
478                 rio.setIssueTypeId(issueType.getIssueTypeId());
479                 rio.setOptionId(option.getOptionId());
480                 rio.setOrder(roo.getPreferredOrder());
481                 rio.setWeight(roo.getWeight());
482                 // making sure error is recorded in same log as our other
483
// debugging for pcn 17683.
484
try
485                 {
486                     rio.save();
487                 }
488                 catch (Exception JavaDoc e)
489                 {
490                     Log.get().error("Exception saving rio", e);
491                     throw e; //EXCEPTION
492
}
493             }
494         }
495         else
496         {
497             // add module-attribute groupings
498
module.addRModuleAttribute(issueType, attribute);
499
500             // add module-attributeoption mappings
501
for (int j=0;j < options.size();j++)
502             {
503                 AttributeOption option = (AttributeOption)options.get(j);
504                 List JavaDoc roos = option.getROptionOptionsRelatedByOption2Id();
505                 ROptionOption roo = (ROptionOption)roos.get(0);
506                 RModuleOption rmo = new RModuleOption();
507                 rmo.setModuleId(getModuleId());
508                 rmo.setIssueTypeId(issueType.getIssueTypeId());
509                 rmo.setOptionId(option.getOptionId());
510                 rmo.setDisplayValue(option.getName());
511                 rmo.setOrder(roo.getPreferredOrder());
512                 rmo.setWeight(roo.getWeight());
513                 rmo.save();
514
515                 // add module-attributeoption mappings to template type
516
IssueType templateType = IssueTypeManager
517                     .getInstance(issueType.getTemplateId(), false);
518                 RModuleOption rmo2 = module.
519                      addRModuleOption(templateType, option);
520                 rmo2.save();
521             }
522         }
523         getMethodResult().remove(this, AttributeGroup.GET_ATTRIBUTES);
524     }
525
526     public void deleteAttribute(Attribute attribute, ScarabUser user,
527                                  Module module)
528          throws Exception JavaDoc
529     {
530         String JavaDoc permission = null;
531         if (isGlobal())
532         {
533             permission = (ScarabSecurity.DOMAIN__EDIT);
534         }
535         else
536         {
537             permission = (ScarabSecurity.MODULE__CONFIGURE);
538         }
539         if (user.hasPermission(permission, module))
540         {
541             IssueType issueType = getIssueType();
542             IssueType template = IssueTypeManager.getInstance
543                                  (issueType.getTemplateId());
544             boolean success = true;
545
546             RIssueTypeAttribute ria = issueType.getRIssueTypeAttribute(attribute);
547             if (isGlobal())
548             {
549                 // This is a global attribute group
550
// Remove attribute - issue type mapping
551
List JavaDoc rias = issueType.getRIssueTypeAttributes
552                                      (false, AttributePeer.NON_USER);
553                 if (ria != null)
554                 {
555                     ria.delete();
556                 }
557                 if (rias != null)
558                 {
559                     rias.remove(ria);
560                 }
561             }
562             else
563             {
564                 // Check if attribute is locked
565
if (ria != null && ria.getLocked())
566                 {
567                     success = false;
568                     throw new TorqueException(attribute.getName() + "is locked"); //EXCEPTION
569
}
570                 else
571                 {
572                     // Remove attribute - module mapping
573
List JavaDoc rmas = module.getRModuleAttributes(issueType, false,
574                                                             AttributePeer.NON_USER);
575                     RModuleAttribute rma = module
576                         .getRModuleAttribute(attribute, issueType);
577                     rma.delete();
578                     WorkflowFactory.getInstance().deleteWorkflowsForAttribute
579                                                   (attribute, module, issueType);
580                     rmas.remove(rma);
581
582                     // Remove attribute - module mapping from template type
583
RModuleAttribute rma2 = module
584                                    .getRModuleAttribute(attribute, template);
585                     rma2.delete();
586                     rmas.remove(rma2);
587                 }
588             }
589
590             if (success)
591             {
592                 // Remove attribute - group mapping
593
RAttributeAttributeGroup raag =
594                     getRAttributeAttributeGroup(attribute);
595                 raag.delete();
596
597                 if (attribute.isOptionAttribute())
598                 {
599                     if (isGlobal())
600                     {
601                         // global attributeGroup; remove issuetype-option maps
602
List JavaDoc rios = issueType.getRIssueTypeOptions(attribute);
603                         if (rios != null)
604                         {
605                             for (int i = 0; i<rios.size();i++)
606                             {
607                                 ((RIssueTypeOption) rios.get(i))
608                                     .delete(user, module);
609                             }
610                         }
611                     }
612                     else
613                     {
614                         // Remove module-option mapping
615
List JavaDoc rmos = module.getRModuleOptions(attribute, issueType);
616                         if (rmos != null)
617                         {
618                             rmos.addAll(module.getRModuleOptions(attribute, template));
619                             for (int j = 0; j<rmos.size();j++)
620                             {
621                                 RModuleOption rmo = (RModuleOption)rmos.get(j);
622                                  // rmo's may be inherited by the parent module.
623
// don't delete unless they are directly associated
624
// with this module. Will know by the first one.
625
if (rmo.getModuleId().equals(module.getModuleId()))
626                                  {
627                                      rmo.delete();
628                                  }
629                                  else
630                                  {
631                                      break;
632                                  }
633                              }
634                          }
635                      }
636                  }
637              }
638          }
639          else
640          {
641              throw new ScarabException(L10NKeySet.YouDoNotHavePermissionToAction);
642          }
643         getMethodResult().remove(this, AttributeGroup.GET_ATTRIBUTES);
644     }
645
646     private RAttributeAttributeGroup addRAttributeAttributeGroup(Attribute attribute)
647          throws Exception JavaDoc
648     {
649         RAttributeAttributeGroup raag = new RAttributeAttributeGroup();
650         raag.setGroupId(getAttributeGroupId());
651         raag.setAttributeId(attribute.getAttributeId());
652         if (Log.get().isDebugEnabled())
653         {
654             Log.get().debug("Calling getAttributes() from addRAttributeAttributeGroup "
655                             + "in order to determine order for attribute id=" +
656                             attribute.getAttributeId());
657         }
658         raag.setOrder(getAttributes().size() +1);
659         return raag;
660     }
661
662     public AttributeGroup copyGroup()
663          throws Exception JavaDoc
664     {
665         AttributeGroup newGroup = new AttributeGroup();
666         newGroup.setName(getName());
667         newGroup.setDescription(getDescription());
668         newGroup.setDedupe(getDedupe());
669         newGroup.setActive(getActive());
670         newGroup.setOrder(getOrder());
671         return newGroup;
672     }
673 }
674
Popular Tags