KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
50 import java.util.List JavaDoc;
51
52 import org.apache.fulcrum.TurbineServices;
53 import org.apache.fulcrum.cache.CachedObject;
54 import org.apache.fulcrum.cache.GlobalCacheService;
55 import org.apache.fulcrum.cache.ObjectExpiredException;
56 import org.apache.fulcrum.intake.Retrievable;
57 import org.apache.fulcrum.localization.Localization;
58 import org.apache.torque.TorqueException;
59 import org.apache.turbine.services.yaaficomponent.YaafiComponentService;
60 import org.tigris.scarab.tools.localization.L10NKeySet;
61 import org.tigris.scarab.util.ScarabException;
62 import org.tigris.scarab.util.ScarabRuntimeException;
63
64 /**
65   * This class is used by Intake on the GlobalAttributeEdit page
66   * to create combination of a ROptionOption and a AttributeOption
67   *
68   * @author <a HREF="mailto:jon@collab.net">Jon S. Stevens</a>
69   * @version $Id: ParentChildAttributeOption.java 9284 2004-12-02 21:13:20Z dabbous $
70   */

71 public class ParentChildAttributeOption
72     implements Retrievable, java.io.Serializable JavaDoc
73 {
74     /** the name of this class */
75     private static final String JavaDoc CLASS_NAME = "ParentChildAttributeOption";
76
77     private Integer JavaDoc attributeId = null;
78     private Integer JavaDoc optionId = null;
79     private Integer JavaDoc parentId = null;
80     private boolean deleted = false;
81     private String JavaDoc name = null;
82     private int preferredOrder = 0;
83     private int weight = 0;
84     private List JavaDoc ancestors = null;
85     private static final Integer JavaDoc ROOT_ID = new Integer JavaDoc(0);
86
87
88     /**
89      * Must call getInstance()
90      */

91     protected ParentChildAttributeOption()
92     {
93     }
94
95     /**
96      * Creates a key for use in caching AttributeOptions
97      */

98     static String JavaDoc getCacheKey(Integer JavaDoc option1, Integer JavaDoc option2)
99     {
100          String JavaDoc keyStringA = option1.toString();
101          String JavaDoc keyStringB = option2.toString();
102          String JavaDoc output = new StringBuffer JavaDoc(CLASS_NAME.length() +
103                                 keyStringA.length() + keyStringB.length())
104                                 .append(CLASS_NAME).append(keyStringA)
105                                 .append(keyStringB).toString();
106          return output;
107     }
108
109     /**
110      * Gets an instance of a new ParentChildAttributeOption
111      */

112     public static ParentChildAttributeOption getInstance()
113     {
114         return new ParentChildAttributeOption();
115     }
116
117     /**
118      * Gets an instance of a new ROptionOption
119      */

120     public static ParentChildAttributeOption getInstance(
121                                 Integer JavaDoc parent, Integer JavaDoc child)
122     {
123         GlobalCacheService tgcs =getGlobalCacheService();
124
125         String JavaDoc key = getCacheKey(parent, child);
126         ParentChildAttributeOption pcao = null;
127         try
128         {
129             pcao = (ParentChildAttributeOption)tgcs.getObject(key)
130                         .getContents();
131         }
132         catch (ObjectExpiredException oee)
133         {
134             pcao = getInstance();
135             pcao.setParentId(parent);
136             pcao.setOptionId(child);
137             tgcs.addObject(key, new CachedObject(pcao));
138         }
139         return pcao;
140     }
141
142     /**
143      * Implementation of the Retrievable interface because this object
144      * is used with Intake
145      */

146     public String JavaDoc getQueryKey()
147     {
148         if (parentId == null || optionId == null)
149         {
150             return "";
151         }
152         return getParentId().toString() + ":" + getOptionId().toString();
153     }
154
155     /**
156      * Implementation of the Retrievable interface because this object
157      * is used with Intake
158      */

159     public void setQueryKey(String JavaDoc key)
160         throws Exception JavaDoc
161     {
162         int index = key.indexOf(":");
163         String JavaDoc a = key.substring(0,index);
164         String JavaDoc b = key.substring(index,key.length());
165         setParentId(new Integer JavaDoc(a));
166         setOptionId(new Integer JavaDoc(b));
167     }
168
169     public Integer JavaDoc getAttributeId()
170     {
171         return attributeId;
172     }
173
174     public void setAttributeId(Integer JavaDoc attributeId)
175     {
176         this.attributeId = attributeId;
177     }
178
179     /**
180      * The 'child' optionid
181      */

182     public Integer JavaDoc getOptionId()
183     {
184         return this.optionId;
185     }
186
187     /**
188      * The 'child' optionid
189      */

190     public void setOptionId(Integer JavaDoc key)
191     {
192         this.optionId = key;
193     }
194
195     /**
196      * The 'child' AttributeOption
197      */

198     public AttributeOption getChildOption()
199         throws TorqueException
200     {
201         return AttributeOptionManager.getInstance(getOptionId());
202     }
203
204     public Integer JavaDoc getParentId()
205     {
206         if (this.parentId == null)
207         {
208             return new Integer JavaDoc(0);
209         }
210         return this.parentId;
211     }
212
213     public void setParentId(Integer JavaDoc id)
214     {
215         this.parentId = id;
216     }
217
218     public AttributeOption getParentOption()
219         throws TorqueException
220     {
221         return AttributeOptionManager.getInstance(getParentId());
222     }
223
224     public List JavaDoc getAncestors()
225         throws TorqueException, Exception JavaDoc
226     {
227         ancestors = new ArrayList JavaDoc();
228         AttributeOption parent = getParentOption();
229         if (!ROOT_ID.equals(parent.getOptionId()))
230         {
231             addAncestors(parent);
232         }
233         return ancestors;
234     }
235
236     /**
237      * recursive helper method for getAncestors()
238      */

239     private void addAncestors(AttributeOption option)
240         throws TorqueException, Exception JavaDoc
241     {
242         if (!ROOT_ID.equals(option.getParent().getOptionId()))
243         {
244             if (ancestors.contains(option.getParent()))
245             {
246                 throw new Exception JavaDoc("Tried to add a recursive parent-child " +
247                                     "attribute option relationship."); //EXCEPTION
248
}
249             else
250             {
251                 addAncestors(option.getParent());
252             }
253         }
254         ancestors.add(option.getOptionId());
255     }
256
257     public boolean getDeleted()
258     {
259         return this.deleted;
260     }
261
262     public void setDeleted(boolean deleted)
263     {
264         this.deleted = deleted;
265     }
266
267     public String JavaDoc getName()
268     {
269         if (this.name == null)
270         {
271             return "";
272         }
273         return this.name;
274     }
275
276     public void setName(String JavaDoc name)
277     {
278         this.name = name;
279     }
280
281     public int getPreferredOrder()
282     {
283         return this.preferredOrder;
284     }
285
286     public void setPreferredOrder(int preferredOrder)
287     {
288         this.preferredOrder = preferredOrder;
289     }
290
291     public int getWeight()
292     {
293         return this.weight;
294     }
295
296     public void setWeight(int weight)
297     {
298         this.weight = weight;
299     }
300
301     /**
302      * Removes the object from the cache
303      */

304     public static void doRemoveFromCache(Integer JavaDoc parent, Integer JavaDoc child)
305     {
306         GlobalCacheService tgcs =getGlobalCacheService();
307
308         String JavaDoc key = getCacheKey(parent, child);
309         tgcs.removeObject(key);
310     }
311
312     public String JavaDoc toString()
313     {
314         return getParentId() + ":" + getOptionId() + " -> " + getName();
315     }
316
317     public void save()
318         throws Exception JavaDoc
319     {
320         AttributeOption ao = null;
321         ROptionOption roo = null;
322
323         Attribute tmpAttr = AttributeManager.getInstance(getAttributeId());
324         
325         // if it is new, it won't already have an optionId
326
if (getOptionId() == null)
327         {
328             // if it is new, check for duplicates.
329
AttributeOption duplicate =
330                 AttributeOption.getInstance(tmpAttr, getName().trim());
331             AttributeOption parent =
332                 AttributeOptionManager.getInstance(getParentId());
333             if (duplicate != null)
334             {
335                 throw new Exception JavaDoc (Localization.getString("CannotCreateDuplicateOption")); //EXCEPTION
336
}
337             else if (parent.getDeleted())
338             {
339                 throw new Exception JavaDoc (Localization.getString("CannotCreateChild")); //EXCEPTION
340
}
341         }
342
343         // if getOptionId() is null, then it will just create a new instance
344
Integer JavaDoc optionId = getOptionId();
345         if (optionId == null)
346         {
347             ao = AttributeOptionManager.getInstance();
348         }
349         else
350         {
351             ao = AttributeOptionManager.getInstance(getOptionId());
352         }
353
354         
355         ao.setName(getName());
356         ao.setDeleted(getDeleted());
357         ao.setAttribute(tmpAttr);
358         ao.save();
359
360         // clean out the caches for the AO
361
tmpAttr.doRemoveCaches();
362
363         // now set our option id from the saved AO
364
this.setOptionId(ao.getOptionId());
365
366         // now create the ROO mapping
367
try
368         {
369             // look for a cached ROptionOption
370
roo = ROptionOption.getInstance(getParentId(), getOptionId());
371         }
372         catch (ScarabException se)
373         {
374             // could not find a cached instance create new one
375
roo = ROptionOption.getInstance();
376             roo.setOption1Id(getParentId());
377             roo.setOption2Id(getOptionId());
378         }
379         roo.setPreferredOrder(getPreferredOrder());
380         roo.setWeight(getWeight());
381         roo.setRelationshipId(OptionRelationship.PARENT_CHILD);
382         roo.save();
383     }
384     
385     /**
386      * Gets the <code>GlobalCacheService</code> implementation.
387      *
388      * @return the GlobalCacheService implementation.
389      */

390     protected static final GlobalCacheService getGlobalCacheService()
391     {
392         try{
393             YaafiComponentService yaafi = (YaafiComponentService) TurbineServices.getInstance().getService(
394                 YaafiComponentService.SERVICE_NAME);
395             return (GlobalCacheService) yaafi.lookup(GlobalCacheService.class.getName());
396         }
397         catch (Exception JavaDoc e) {
398             throw new ScarabRuntimeException(L10NKeySet.ExceptionLookupGlobalCache, e);
399         }
400     }
401 }
402
Popular Tags