KickJava   Java API By Example, From Geeks To Geeks.

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


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 Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 // JDK classes
50
import java.util.Comparator JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.sql.Connection JavaDoc;
54
55 // Turbine classes
56
import org.apache.torque.Torque;
57 import org.apache.torque.TorqueException;
58 import org.apache.torque.om.Persistent;
59 import org.apache.torque.util.Criteria;
60
61 import org.tigris.scarab.om.ModuleManager;
62 import org.tigris.scarab.om.Module;
63 import org.tigris.scarab.tools.localization.L10NKeySet;
64 import org.tigris.scarab.util.ScarabException;
65 import org.tigris.scarab.workflow.WorkflowFactory;
66
67 /**
68  * This class represents a RModuleOption
69  *
70  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
71  * @version $Id: RModuleOption.java 9104 2004-05-10 21:04:51Z dabbous $
72  */

73 public class RModuleOption
74     extends BaseRModuleOption
75     implements Persistent
76 {
77
78     private int level;
79
80     private static final Comparator JavaDoc COMPARATOR = new Comparator JavaDoc()
81         {
82             public int compare(Object JavaDoc obj1, Object JavaDoc obj2)
83             {
84                 int result = 1;
85                 RModuleOption opt1 = (RModuleOption)obj1;
86                 RModuleOption opt2 = (RModuleOption)obj2;
87                 if (opt1.getOrder() < opt2.getOrder())
88                 {
89                     result = -1;
90                 }
91                 else if (opt1.getOrder() == opt2.getOrder())
92                 {
93                     result = opt1.getDisplayValue()
94                         .compareTo(opt2.getDisplayValue());
95                 }
96                 return result;
97             }
98         };
99
100
101     /**
102      * Throws UnsupportedOperationException. Use
103      * <code>getModule()</code> instead.
104      *
105      * @return a <code>ScarabModule</code> value
106      */

107     public ScarabModule getScarabModule()
108     {
109         throw new UnsupportedOperationException JavaDoc(
110             "Should use getModule"); //EXCEPTION
111
}
112
113     /**
114      * Throws UnsupportedOperationException. Use
115      * <code>setModule(Module)</code> instead.
116      *
117      */

118     public void setScarabModule(ScarabModule module)
119     {
120         throw new UnsupportedOperationException JavaDoc(
121             "Should use setModule(Module). Note module cannot be new."); //EXCEPTION
122
}
123
124     /**
125      * Use this instead of setScarabModule. Note: module cannot be new.
126      */

127     public void setModule(Module me)
128         throws TorqueException
129     {
130         Integer JavaDoc id = me.getModuleId();
131         if (id == null)
132         {
133             throw new TorqueException("Modules must be saved prior to " +
134                                       "being associated with other objects."); //EXCEPTION
135
}
136         setModuleId(id);
137     }
138
139     /**
140      * Module getter. Use this method instead of getScarabModule().
141      *
142      * @return a <code>Module</code> value
143      */

144     public Module getModule()
145         throws TorqueException
146     {
147         Module module = null;
148         Integer JavaDoc id = getModuleId();
149         if ( id != null )
150         {
151             module = ModuleManager.getInstance(id);
152         }
153         
154         return module;
155     }
156
157     /**
158      * Compares numeric value and in cases where the numeric value
159      * is the same it compares the display values.
160      */

161     public static Comparator JavaDoc getComparator()
162     {
163         return COMPARATOR;
164     }
165
166     /**
167      * A convenience method for getting the option name. It is
168      * preferred over using getAttributeOption().getName() as it
169      * leaves open the possibility of per module display values.
170      */

171     public String JavaDoc getDisplayValue()
172     {
173         String JavaDoc dispVal = super.getDisplayValue();
174         if (dispVal == null)
175         {
176             try
177             {
178                 dispVal = getAttributeOption().getName();
179             }
180             catch (Exception JavaDoc e)
181             {
182                 getLog().error(e);
183                 dispVal = "!Error-Check Logs!";
184             }
185         }
186         return dispVal;
187     }
188     
189     /**
190      * Get the level in the option parent-child tree.
191      * @return value of level.
192      */

193     public int getLevel()
194     {
195         return level;
196     }
197     
198     /**
199      * Get the level in the option parent-child tree.
200      * @param v Value to assign to level.
201      */

202     public void setLevel(int v)
203     {
204         this.level = v;
205     }
206     
207     public RModuleAttribute getRModuleAttribute(IssueType issueType)
208         throws Exception JavaDoc
209     {
210         Module module = ModuleManager.getInstance(getModuleId());
211         Attribute attribute = getAttributeOption().getAttribute();
212         return module.getRModuleAttribute(attribute, issueType);
213     }
214     
215     /**
216      * Gets a list of this option's descendants
217      * That are associated with this module/Issue Type
218      * @return <code>List</code> of <code>RModuleOptions</code>
219      */

220     public List JavaDoc getDescendants(IssueType issueType)
221         throws Exception JavaDoc
222     {
223         List JavaDoc descendants = new ArrayList JavaDoc();
224         List JavaDoc attrDescendants = getAttributeOption().getDescendants();
225         for (int i =0;i < attrDescendants.size(); i++)
226         {
227             RModuleOption rmo = null;
228             AttributeOption option = (AttributeOption)attrDescendants.get(i);
229             rmo = getModule().getRModuleOption(option, issueType);
230             if (rmo != null && rmo.getOptionId().equals(option.getOptionId()))
231             {
232                 descendants.add(rmo);
233             }
234         }
235         return descendants;
236     }
237         
238     public void delete()
239          throws Exception JavaDoc
240     {
241         Module module = getModule();
242
243             IssueType issueType = IssueTypeManager
244                .getInstance(getIssueTypeId(), false);
245             if (issueType.getLocked())
246             {
247                 throw new ScarabException(L10NKeySet.ExceptionDeleteOptionFromLockedIssueType);
248             }
249             else
250             {
251                 Criteria c = new Criteria()
252                     .add(RModuleOptionPeer.MODULE_ID, getModuleId())
253                     .add(RModuleOptionPeer.ISSUE_TYPE_ID, getIssueTypeId())
254                     .add(RModuleOptionPeer.OPTION_ID, getOptionId());
255                 RModuleOptionPeer.doDelete(c);
256                 WorkflowFactory.getInstance().deleteWorkflowsForOption(getAttributeOption(),
257                                              module, issueType);
258                 // Correct the ordering of the remaining options
259
ArrayList JavaDoc optIds = new ArrayList JavaDoc();
260                 List JavaDoc rmos = module.getRModuleOptions(getAttributeOption().getAttribute(), issueType, false);
261                 for (int i=0; i<rmos.size();i++)
262                 {
263                     RModuleOption rmo = (RModuleOption)rmos.get(i);
264                     optIds.add(rmo.getOptionId());
265                 }
266                 Criteria c2 = new Criteria()
267                     .add(RModuleOptionPeer.MODULE_ID, getModuleId())
268                     .add(RModuleOptionPeer.ISSUE_TYPE_ID, getIssueTypeId())
269                     .addIn(RModuleOptionPeer.OPTION_ID, optIds)
270                     .add(RModuleOptionPeer.PREFERRED_ORDER, getOrder(), Criteria.GREATER_THAN);
271                 List JavaDoc adjustRmos = RModuleOptionPeer.doSelect(c2);
272                 for (int j=0; j<adjustRmos.size();j++)
273                 {
274                     RModuleOption rmo = (RModuleOption)adjustRmos.get(j);
275                     //rmos.remove(rmo);
276
rmo.setOrder(rmo.getOrder() -1);
277                     rmo.save();
278                     //rmos.add(rmo);
279
}
280             }
281             // notify module cache of this change
282
((ModuleManager)Torque.getManager(ModuleManager.MANAGED_CLASS))
283                 .refreshedObject(this);
284     }
285
286     public void save(Connection JavaDoc con) throws TorqueException
287     {
288         if (isModified())
289         {
290             if (isNew())
291             {
292                 super.save(con);
293             }
294             else
295             {
296                 Attribute attr = getAttributeOption().getAttribute();
297                 RIssueTypeAttribute ria = null;
298                 try
299                 {
300                     ria = getIssueType().getRIssueTypeAttribute(attr);
301                     if (ria != null && ria.getLocked())
302                     {
303                         throw new TorqueException(attr.getName() + "is locked"); //EXCEPTION
304
}
305                     else
306                     {
307                         super.save(con);
308                     }
309                 }
310                 catch (Exception JavaDoc e)
311                 {
312                     throw new TorqueException("An error has occurred."); //EXCEPTION
313
}
314             }
315         }
316     }
317 }
318
Popular Tags