KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.tigris.scarab.om;
2
3 /* ================================================================
4  * Copyright (c) 2000-2003 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by CollabNet <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of CollabNet.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of CollabNet.
47  */

48
49 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.ArrayList JavaDoc;
52
53 import org.apache.torque.TorqueException;
54 import org.apache.torque.om.Persistent;
55 import org.apache.torque.util.Criteria;
56 import org.tigris.scarab.tools.localization.L10NKeySet;
57
58 import org.tigris.scarab.services.security.ScarabSecurity;
59 import org.tigris.scarab.util.ScarabException;
60 import org.tigris.scarab.om.Module;
61 import org.tigris.scarab.om.ModuleManager;
62 import org.tigris.scarab.om.QueryPeer;
63 import org.tigris.scarab.om.MITListItem;
64 import org.tigris.scarab.om.MITListItemPeer;
65 import org.tigris.scarab.workflow.WorkflowFactory;
66
67 /**
68  * This class represents a RModuleIssueType
69  *
70  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
71  * @version $Id: RModuleIssueType.java 9766 2005-07-04 16:44:17Z jorgeuriarte $
72  */

73 public class RModuleIssueType
74     extends org.tigris.scarab.om.BaseRModuleIssueType
75     implements Persistent, Conditioned
76 {
77
78     /**
79      * Throws UnsupportedOperationException. Use
80      * <code>getModule()</code> instead.
81      *
82      * @return a <code>ScarabModule</code> value
83      */

84     public ScarabModule getScarabModule()
85     {
86         throw new UnsupportedOperationException JavaDoc(
87             "Should use getModule"); //EXCEPTION
88
}
89
90     /**
91      * Throws UnsupportedOperationException. Use
92      * <code>setModule(Module)</code> instead.
93      *
94      */

95     public void setScarabModule(ScarabModule module)
96     {
97         throw new UnsupportedOperationException JavaDoc(
98             "Should use setModule(Module). Note module cannot be new."); //EXCEPTION
99
}
100
101     /**
102      * Use this instead of setScarabModule. Note: module cannot be new.
103      */

104     public void setModule(Module me)
105         throws TorqueException
106     {
107         Integer JavaDoc id = me.getModuleId();
108         if (id == null)
109         {
110             throw new TorqueException("Modules must be saved prior to " +
111                                       "being associated with other objects."); //EXCEPTION
112
}
113         setModuleId(id);
114     }
115
116     /**
117      * Module getter. Use this method instead of getScarabModule().
118      *
119      * @return a <code>Module</code> value
120      */

121     public Module getModule()
122         throws TorqueException
123     {
124         Module module = null;
125         Integer JavaDoc id = getModuleId();
126         if ( id != null )
127         {
128             module = ModuleManager.getInstance(id);
129         }
130         
131         return module;
132     }
133
134
135     /**
136      * Checks if user has permission to delete module-issue type mapping.
137      */

138     public void delete(ScarabUser user)
139          throws Exception JavaDoc
140     {
141         Module module = getModule();
142         IssueType issueType = getIssueType();
143
144         if (user.hasPermission(ScarabSecurity.MODULE__CONFIGURE, module))
145         {
146             // Delete both active and inactive attribute groups first
147
List JavaDoc attGroups = issueType.getAttributeGroups(module, false);
148             for (int j=0; j<attGroups.size(); j++)
149             {
150                 // delete attribute-attribute group map
151
AttributeGroup attGroup =
152                               (AttributeGroup)attGroups.get(j);
153                 attGroup.delete();
154             }
155
156             // Delete mappings with user attributes
157
List JavaDoc rmas = module.getRModuleAttributes(issueType);
158             for (int i=0; i<rmas.size(); i++)
159             {
160                 ((RModuleAttribute)rmas.get(i)).delete();
161             }
162             // Delete mappings with user attributes for template type
163
IssueType templateType = issueType.getTemplateIssueType();
164             rmas = module.getRModuleAttributes(templateType);
165             for (int i=0; i<rmas.size(); i++)
166             {
167                 ((RModuleAttribute)rmas.get(i)).delete();
168             }
169  
170             // delete workflows
171
WorkflowFactory.getInstance().resetAllWorkflowsForIssueType(module,
172                                                                         issueType);
173             // delete templates
174
Criteria c = new Criteria()
175                 .add(IssuePeer.TYPE_ID, templateType.getIssueTypeId());
176             List JavaDoc result = IssuePeer.doSelect(c);
177             List JavaDoc issueIdList = new ArrayList JavaDoc(result.size());
178             for (int i=0; i < result.size(); i++)
179             {
180                 Issue issue = (Issue)result.get(i);
181                 issueIdList.add(issue.getIssueId());
182             }
183             IssuePeer.doDelete(c);
184             if (!issueIdList.isEmpty())
185             {
186                 c = new Criteria()
187                     .addIn(IssueTemplateInfoPeer.ISSUE_ID, issueIdList);
188                 IssueTemplateInfoPeer.doDelete(c);
189             }
190             
191             // mit list items
192
c = new Criteria()
193                 .add(MITListItemPeer.MODULE_ID, module.getModuleId())
194                 .add(MITListItemPeer.ISSUE_TYPE_ID, issueType.getIssueTypeId());
195             List JavaDoc listItems = MITListItemPeer.doSelect(c);
196             List JavaDoc listIds = new ArrayList JavaDoc(listItems.size());
197
198             for (int i=0; i < listItems.size(); i++)
199             {
200                 MITList list = ((MITListItem)listItems.get(i)).getMITList();
201                 Long JavaDoc listId = list.getListId();
202                 if (list.isSingleModuleIssueType() && !listIds.contains(listId))
203                 {
204                     listIds.add(listId);
205                 }
206             }
207             MITListItemPeer.doDelete(c);
208
209             if (!listIds.isEmpty())
210             {
211                 // delete single module-issuetype mit lists
212
c = new Criteria()
213                     .addIn(MITListPeer.LIST_ID, listIds);
214                 MITListPeer.doDelete(c);
215
216                 // delete queries
217
c = new Criteria()
218                     .addIn(QueryPeer.LIST_ID, listIds);
219                 QueryPeer.doDelete(c);
220             }
221
222             c = new Criteria()
223                 .add(RModuleIssueTypePeer.MODULE_ID, getModuleId())
224                 .add(RModuleIssueTypePeer.ISSUE_TYPE_ID, getIssueTypeId());
225             RModuleIssueTypePeer.doDelete(c);
226             RModuleIssueTypeManager.removeFromCache(this);
227             List JavaDoc rmits = module.getRModuleIssueTypes();
228             rmits.remove(this);
229         }
230         else
231         {
232             throw new ScarabException(L10NKeySet.YouDoNotHavePermissionToAction);
233         }
234     }
235
236     /**
237      * Not really sure why getDisplayText was created because
238      * it really should just be getDisplayName() (JSS)
239      *
240      * @see #getDisplayText()
241      */

242     public String JavaDoc getDisplayName()
243     {
244         String JavaDoc display = super.getDisplayName();
245         if (display == null)
246         {
247             try
248             {
249                 display = getIssueType().getName();
250             }
251             catch (TorqueException e)
252             {
253                 getLog().error("Error getting the issue type name: ", e);
254             }
255         }
256         return display;
257     }
258
259     /**
260      * Gets name to display. First tries to get the DisplayName
261      * for the RMIT, if that is null, then it will get the IssueType's
262      * name and use that.
263      *
264      * @deprecated use getDisplayName() instead
265      */

266     public String JavaDoc getDisplayText()
267     {
268         return this.getDisplayName();
269     }
270
271     public String JavaDoc getDisplayDescription()
272     {
273         String JavaDoc display = super.getDisplayDescription();
274         if (display == null)
275         {
276             try
277             {
278                 display = getIssueType().getDescription();
279             }
280             catch (TorqueException e)
281             {
282                 getLog().error("Error getting the issue type description: ", e);
283             }
284         }
285         return display;
286     }
287
288     /**
289      * Copies object.
290      */

291     public RModuleIssueType copy()
292          throws TorqueException
293     {
294         RModuleIssueType rmit2 = new RModuleIssueType();
295         rmit2.setModuleId(getModuleId());
296         rmit2.setIssueTypeId(getIssueTypeId());
297         rmit2.setActive(getActive());
298         rmit2.setDisplay(getDisplay());
299         rmit2.setDisplayDescription(getDisplayDescription());
300         rmit2.setOrder(getOrder());
301         rmit2.setDedupe(getDedupe());
302         rmit2.setHistory(getHistory());
303         rmit2.setComments(getComments());
304         return rmit2;
305     }
306
307     /**
308      * Forces the relationship to only retrieve IssueType level conditions (if ATTRIBUTE_ID is not
309      * asked to be NULL, this method could return conditions already defined for
310      * SCARAB_R_MODULE_ATTRIBUTE's records)
311      *
312      */

313     public List JavaDoc getConditions(Criteria criteria) throws TorqueException
314     {
315         criteria.add(ConditionPeer.ATTRIBUTE_ID, (Object JavaDoc)(ConditionPeer.ATTRIBUTE_ID + " IS NULL"), Criteria.CUSTOM);
316         return super.getConditions(criteria);
317     }
318     
319     /* (non-Javadoc)
320      * @see org.tigris.scarab.om.Conditioned#getConditionsArray()
321      */

322     public Integer JavaDoc[] getConditionsArray()
323     {
324         List JavaDoc conditions = new ArrayList JavaDoc();
325         Integer JavaDoc[] aIDs = null;
326         try
327         {
328             
329             conditions = this.getConditions();
330             aIDs = new Integer JavaDoc[conditions.size()];
331             int i=0;
332             for (Iterator JavaDoc iter = conditions.iterator(); iter.hasNext(); i++)
333             {
334                 aIDs[i] = (Integer JavaDoc)iter.next();
335             }
336         }
337         catch (TorqueException e)
338         {
339             this.getLog().error("getConditionsArray: " + e);
340         }
341         return aIDs;
342     }
343
344     /* (non-Javadoc)
345      * @see org.tigris.scarab.om.Conditioned#setConditionsArray(java.lang.Integer[])
346      */

347     public void setConditionsArray(Integer JavaDoc[] aOptionId) throws Exception JavaDoc
348     {
349         Criteria crit = new Criteria();
350         crit.add(ConditionPeer.ATTRIBUTE_ID, null);
351         crit.add(ConditionPeer.MODULE_ID, this.getModuleId());
352         crit.add(ConditionPeer.ISSUE_TYPE_ID, this.getIssueTypeId());
353         crit.add(ConditionPeer.TRANSITION_ID, null);
354         ConditionPeer.doDelete(crit);
355         this.save();
356         this.getConditions().clear();
357         ConditionManager.clear();
358         if (aOptionId != null)
359             for (int i = 0; i < aOptionId.length; i++)
360             {
361                 if (aOptionId[i].intValue() != 0)
362                 {
363                     Condition cond = new Condition();
364                     cond.setAttributeId(null);
365                     cond.setOptionId(aOptionId[i]);
366                     cond.setModuleId(this.getModuleId());
367                     cond.setIssueTypeId(this.getIssueTypeId());
368                     cond.setTransitionId(null);
369                     this.addCondition(cond);
370                     cond.save();
371                 }
372             }
373     }
374
375     /* (non-Javadoc)
376      * @see org.tigris.scarab.om.Conditioned#isRequiredIf(java.lang.Integer)
377      */

378     public boolean isRequiredIf(Integer JavaDoc optionID) throws TorqueException
379     {
380         Condition cond = new Condition();
381         cond.setAttributeId(null);
382         cond.setOptionId(optionID);
383         cond.setModuleId(this.getModuleId());
384         cond.setIssueTypeId(this.getIssueTypeId());
385         cond.setTransitionId(null);
386         return this.getConditions().contains(cond);
387
388     }
389
390     /* (non-Javadoc)
391      * @see org.tigris.scarab.om.Conditioned#isConditioned()
392      */

393     public boolean isConditioned()
394     {
395         boolean bRdo = false;
396         try {
397             bRdo = this.getConditions().size()>0;
398         } catch (TorqueException te)
399         {
400             // Nothing to do
401
}
402         return bRdo;
403     }
404     
405     /**
406      * Method called to associate a Condition object to this object
407      * through the Condition foreign key attribute
408      *
409      * @param l Condition
410      * @throws TorqueException
411      */

412     public void addCondition(Condition cond) throws TorqueException
413     {
414         getConditions().add(cond);
415         cond.setRModuleIssueType(this);
416     }
417 }
418
Popular Tags