KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.tigris.scarab.om;
2
3 /* ================================================================
4  * Copyright (c) 2000-2004 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.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.ArrayList JavaDoc;
52 import java.sql.Connection JavaDoc;
53
54 // Turbine classes
55
import org.apache.torque.TorqueException;
56 import org.apache.torque.om.Persistent;
57 import org.apache.torque.util.Criteria;
58 import org.apache.fulcrum.localization.Localization;
59
60 import org.tigris.scarab.services.cache.ScarabCache;
61 import org.tigris.scarab.tools.localization.L10NKeySet;
62 import org.tigris.scarab.om.Module;
63 import org.tigris.scarab.om.ModuleManager;
64 import org.tigris.scarab.om.IssueTypePeer;
65 import org.tigris.scarab.util.ScarabException;
66 import org.tigris.scarab.workflow.WorkflowFactory;
67
68 /**
69  * This class represents a RModuleAttribute relationship.
70  *
71  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
72  * @version $Id: RModuleAttribute.java 9495 2005-03-19 23:14:36Z jorgeuriarte $
73  */

74 public class RModuleAttribute
75     extends BaseRModuleAttribute
76     implements Persistent, Conditioned
77 {
78     private static final String JavaDoc R_MODULE_ATTTRIBUTE =
79         "RModuleAttribute";
80     private static final String JavaDoc GET_RMAS =
81         "getRMAs";
82
83     public void save(Connection JavaDoc con) throws TorqueException
84     {
85         if (isModified())
86         {
87             if (isNew())
88             {
89                 super.save(con);
90             }
91             else
92             {
93                 RIssueTypeAttribute ria = null;
94                 try
95                 {
96                     ria = getIssueType().getRIssueTypeAttribute(getAttribute());
97                     if ((ria != null && ria.getLocked()))
98                     {
99                     throw new TorqueException(getAttribute().getName() + " is locked"); //EXCEPTION
100
}
101                     else
102                     {
103                         super.save(con);
104                     }
105                 }
106                 catch (Exception JavaDoc e)
107                 {
108                     throw new TorqueException("An error has occurred.", e); //EXCEPTION
109
}
110             }
111         }
112     }
113
114     /**
115      * Throws UnsupportedOperationException. Use
116      * <code>getModule()</code> instead.
117      *
118      * @return a <code>ScarabModule</code> value
119      */

120     public ScarabModule getScarabModule()
121     {
122         throw new UnsupportedOperationException JavaDoc(
123             "Should use getModule"); //EXCEPTION
124
}
125
126     /**
127      * Throws UnsupportedOperationException. Use
128      * <code>setModule(Module)</code> instead.
129      *
130      */

131     public void setScarabModule(ScarabModule module)
132     {
133         throw new UnsupportedOperationException JavaDoc(
134             "Should use setModule(Module). Note module cannot be new."); //EXCEPTION
135
}
136
137     /**
138      * Use this instead of setScarabModule. Note: module cannot be new.
139      */

140     public void setModule(Module me)
141         throws TorqueException
142     {
143         Integer JavaDoc id = me.getModuleId();
144         if (id == null)
145         {
146             throw new TorqueException("Modules must be saved prior to " +
147                                       "being associated with other objects."); //EXCEPTION
148
}
149         setModuleId(id);
150     }
151
152     /**
153      * Module getter. Use this method instead of getScarabModule().
154      *
155      * @return a <code>Module</code> value
156      */

157     public Module getModule()
158         throws TorqueException
159     {
160         Module module = null;
161         Integer JavaDoc id = getModuleId();
162         if ( id != null )
163         {
164             module = ModuleManager.getInstance(id);
165         }
166         
167         return module;
168     }
169
170     /**
171      * Get the Display Value for the attribute. In the event that this
172      * is a new RModuleAttribute that has not been assigned a Display
173      * Value, this method will return the Attribute Name.
174      */

175     public String JavaDoc getDisplayValue()
176     {
177         String JavaDoc dispVal = super.getDisplayValue();
178         if (dispVal == null)
179         {
180             try
181             {
182                 dispVal = getAttribute().getName();
183             }
184             catch (Exception JavaDoc e)
185             {
186                 getLog().error(e);
187                 dispVal = "!Error-Check Logs!";
188             }
189         }
190         return dispVal;
191     }
192
193     public void delete()
194          throws Exception JavaDoc
195     {
196          delete(false);
197     }
198
199     protected void delete(boolean overrideLock)
200          throws Exception JavaDoc
201     {
202         Module module = getModule();
203
204             IssueType issueType = IssueTypeManager
205                .getInstance(getIssueTypeId(), false);
206             if (issueType.getLocked() && !overrideLock)
207             {
208                 throw new ScarabException(L10NKeySet.CannotDeleteAttributeFromLockedIssueType);
209             }
210             else
211             {
212                 Criteria c = new Criteria()
213                     .add(RModuleAttributePeer.MODULE_ID, getModuleId())
214                     .add(RModuleAttributePeer.ISSUE_TYPE_ID, getIssueTypeId())
215                     .add(RModuleAttributePeer.ATTRIBUTE_ID, getAttributeId());
216                 RModuleAttributePeer.doDelete(c);
217                 Attribute attr = getAttribute();
218                 String JavaDoc attributeType = null;
219                 attributeType = (attr.isUserAttribute() ? Module.USER : Module.NON_USER);
220                 module.getRModuleAttributes(getIssueType(), false, attributeType)
221                                             .remove(this);
222                 WorkflowFactory.getInstance().deleteWorkflowsForAttribute(
223                                               attr, module, getIssueType());
224
225                 // delete module-user-attribute mappings
226
Criteria crit = new Criteria()
227                     .add(RModuleUserAttributePeer.ATTRIBUTE_ID,
228                          attr.getAttributeId())
229                     .add(RModuleUserAttributePeer.MODULE_ID,
230                          getModuleId())
231                     .add(RModuleUserAttributePeer.ISSUE_TYPE_ID,
232                          getIssueTypeId());
233                 RModuleUserAttributePeer.doDelete(crit);
234
235                 // delete module-option mappings
236
if (attr.isOptionAttribute())
237                 {
238                     List JavaDoc optionList = module.getRModuleOptions(attr,
239                                       IssueTypePeer.retrieveByPK(getIssueTypeId()),
240                                       false);
241                     if (optionList != null && !optionList.isEmpty())
242                     {
243                         ArrayList JavaDoc optionIdList =
244                             new ArrayList JavaDoc(optionList.size());
245                         for (int i = 0; i < optionList.size(); i++)
246                         {
247                             optionIdList.add(((RModuleOption)
248                                               optionList.get(i))
249                                              .getOptionId());
250                         }
251                         Criteria c2 = new Criteria()
252                             .add(RModuleOptionPeer.MODULE_ID, getModuleId())
253                             .add(RModuleOptionPeer.ISSUE_TYPE_ID,
254                                  getIssueTypeId())
255                             .addIn(RModuleOptionPeer.OPTION_ID, optionIdList);
256                         RModuleOptionPeer.doDelete(c2);
257                     }
258                 }
259             }
260
261             RModuleAttributeManager.removeInstanceFromCache(this);
262     }
263
264
265     private static List JavaDoc getRMAs(Integer JavaDoc moduleId, Integer JavaDoc issueTypeId)
266         throws Exception JavaDoc
267     {
268         List JavaDoc result = null;
269         Object JavaDoc obj = ScarabCache.get(R_MODULE_ATTTRIBUTE, GET_RMAS,
270                                      moduleId, issueTypeId);
271         if (obj == null)
272         {
273             Criteria crit = new Criteria()
274                 .add(RModuleAttributePeer.MODULE_ID, moduleId)
275                 .add(RModuleAttributePeer.ISSUE_TYPE_ID, issueTypeId);
276             crit.addAscendingOrderByColumn(
277                 RModuleAttributePeer.PREFERRED_ORDER);
278             result = RModuleAttributePeer.doSelect(crit);
279             ScarabCache.put(result, R_MODULE_ATTTRIBUTE, GET_RMAS,
280                             moduleId, issueTypeId);
281         }
282         else
283         {
284             result = (List JavaDoc)obj;
285         }
286         return result;
287     }
288
289     /**
290      * if this RMA is the chosen attribute for email subjects then return
291      * true. if not explicitly chosen, check the other RMA's for this module
292      * and if none is chosen as the email attribute, choose the highest
293      * ordered text attribute.
294      *
295      * @return a <code>boolean</code> value
296      */

297     public boolean getIsDefaultText()
298         throws Exception JavaDoc
299     {
300         boolean isDefault = getDefaultTextFlag();
301         if (!isDefault && getAttribute().isTextAttribute())
302         {
303             // get related RMAs
304
List JavaDoc rmas = getRMAs(getModuleId(), getIssueTypeId());
305             
306             // check if another is chosen
307
boolean anotherIsDefault = false;
308             for (int i=0; i<rmas.size(); i++)
309             {
310                 RModuleAttribute rma = (RModuleAttribute)rmas.get(i);
311                 if (rma.getDefaultTextFlag())
312                 {
313                     anotherIsDefault = true;
314                     break;
315                 }
316             }
317             
318             if (!anotherIsDefault)
319             {
320                 // locate the default text attribute
321
for (int i=0; i<rmas.size(); i++)
322                 {
323                     RModuleAttribute rma = (RModuleAttribute)rmas.get(i);
324                     if (rma.getAttribute().isTextAttribute())
325                     {
326                         if (rma.getAttributeId().equals(getAttributeId()))
327                         {
328                             isDefault = true;
329                         }
330                         else
331                         {
332                             anotherIsDefault = true;
333                         }
334                         
335                         break;
336                     }
337                 }
338             }
339         }
340         return isDefault;
341     }
342
343     /**
344      * This method sets the defaultTextFlag property and also makes sure
345      * that no other related RMA is defined as the default. It should be
346      * used instead of setDefaultTextFlag in application code.
347      *
348      * @param b a <code>boolean</code> value
349      */

350     public void setIsDefaultText(boolean b)
351         throws Exception JavaDoc
352     {
353         if (b && !getDefaultTextFlag())
354         {
355             // get related RMAs
356
List JavaDoc rmas = getRMAs(getModuleId(), getIssueTypeId());
357             
358             // make sure no other rma is selected
359
for (int i=0; i<rmas.size(); i++)
360             {
361                 RModuleAttribute rma = (RModuleAttribute)rmas.get(i);
362                 if (rma.getDefaultTextFlag())
363                 {
364                     rma.setDefaultTextFlag(false);
365                     rma.save();
366                     break;
367                 }
368             }
369         }
370         setDefaultTextFlag(b);
371     }
372     
373     public List JavaDoc getConditions() throws TorqueException
374     {
375         if (collConditions == null)
376         {
377             Criteria crit = new Criteria();
378             crit.add(ConditionPeer.ATTRIBUTE_ID, this.getAttributeId());
379             crit.add(ConditionPeer.MODULE_ID, this.getModuleId());
380             crit.add(ConditionPeer.ISSUE_TYPE_ID, this.getIssueTypeId());
381             crit.add(ConditionPeer.TRANSITION_ID, null);
382             collConditions = getConditions(crit);
383         }
384         return collConditions;
385     }
386     
387     /**
388      * Returns the array of attributeOptionIds that will force the requiment of this
389      * attribute if set. Used by templates to load the combo.
390      * @return
391      */

392     public Integer JavaDoc[] getConditionsArray()
393     {
394         List JavaDoc conditions = new ArrayList JavaDoc();
395         Integer JavaDoc[] aIDs = null;
396         try
397         {
398             conditions = this.getConditions();
399             aIDs = new Integer JavaDoc[conditions.size()];
400             int i=0;
401             for (Iterator JavaDoc iter = conditions.iterator(); iter.hasNext(); i++)
402             {
403                 aIDs[i] = ((Condition)iter.next()).getOptionId();
404             }
405         }
406         catch (TorqueException e)
407         {
408             this.getLog().error("getConditionsArray: " + e);
409         }
410         return aIDs;
411     }
412     /**
413      * Load the attribute options' IDs from the template combo.
414      * @param aOptionId
415      * @throws Exception
416      */

417     public void setConditionsArray(Integer JavaDoc aOptionId[]) throws Exception JavaDoc
418     {
419         Criteria crit = new Criteria();
420         crit.add(ConditionPeer.ATTRIBUTE_ID, this.getAttributeId());
421         crit.add(ConditionPeer.MODULE_ID, this.getModuleId());
422         crit.add(ConditionPeer.ISSUE_TYPE_ID, this.getIssueTypeId());
423         crit.add(ConditionPeer.TRANSITION_ID, null);
424         ConditionPeer.doDelete(crit);
425         this.getConditions().clear();
426         ConditionManager.clear();
427         if (aOptionId != null)
428         {
429             for (int i=0; i<aOptionId.length; i++)
430             {
431                 if (aOptionId[i].intValue() != 0)
432                 {
433                     Condition cond = new Condition();
434                     cond.setAttribute(this.getAttribute());
435                     cond.setOptionId(aOptionId[i]);
436                     cond.setTransitionId(null);
437                     cond.setIssueTypeId(this.getIssueTypeId());
438                     cond.setModuleId(this.getModuleId());
439                     this.addCondition(cond);
440                     cond.save();
441                 }
442             }
443         }
444     }
445     /**
446      * Return true if the given attributeOptionId will make the current
447      * attribute required.
448      * @param optionID
449      * @return
450      * @throws TorqueException
451      */

452     public boolean isRequiredIf(Integer JavaDoc optionID) throws TorqueException
453     {
454         Condition condition = new Condition();
455         condition.setAttribute(this.getAttribute());
456         condition.setModuleId(this.getModuleId());
457         condition.setIssueTypeId(this.getIssueTypeId());
458         condition.setTransitionId(new Integer JavaDoc(0));
459         condition.setOptionId(optionID);
460         return this.getConditions().contains(condition);
461     }
462     
463     /**
464      * Returns true if this object is conditioned (has related conditions)
465      * @return
466      */

467     public boolean isConditioned()
468     {
469         boolean bRdo = false;
470         try {
471             bRdo = this.getConditions().size()>0;
472         } catch (TorqueException te)
473         {
474             // Nothing to do
475
}
476         return bRdo;
477     }
478 }
479
Popular Tags