KickJava   Java API By Example, From Geeks To Geeks.

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


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 // Turbine classes
50
import org.apache.fulcrum.TurbineServices;
51 import org.apache.fulcrum.cache.CachedObject;
52 import org.apache.fulcrum.cache.GlobalCacheService;
53 import org.apache.fulcrum.cache.ObjectExpiredException;
54 import org.apache.torque.TorqueException;
55 import org.apache.torque.om.Persistent;
56 import org.apache.torque.util.Criteria;
57 import org.apache.turbine.services.yaaficomponent.YaafiComponentService;
58 import org.tigris.scarab.tools.localization.L10NKeySet;
59 import org.tigris.scarab.util.ScarabException;
60 import org.tigris.scarab.util.ScarabRuntimeException;
61
62 /**
63   * This class represents the SCARAB_R_OPTION_OPTION table.
64   *
65   * @author <a HREF="mailto:jon@collab.net">Jon S. Stevens</a>
66   * @version $Id: ROptionOption.java 9284 2004-12-02 21:13:20Z dabbous $
67   */

68 public class ROptionOption
69     extends org.tigris.scarab.om.BaseROptionOption
70     implements Persistent
71 {
72     private int level;
73
74     /** the name of this class */
75     private static final String JavaDoc CLASS_NAME = "ROptionOption";
76
77     /**
78      * Must call getInstance()
79      */

80     protected ROptionOption()
81     {
82     }
83
84     /**
85      * Creates a key for use in caching AttributeOptions
86      */

87     static String JavaDoc getCacheKey(Integer JavaDoc option1, Integer JavaDoc option2)
88     {
89          String JavaDoc keyStringA = option1.toString();
90          String JavaDoc keyStringB = option2.toString();
91          return new StringBuffer JavaDoc(CLASS_NAME.length() + keyStringA.length() + keyStringB.length())
92              .append(CLASS_NAME).append(keyStringA).append(keyStringB).toString();
93     }
94
95     /**
96      * Gets an instance of a new ROptionOption
97      */

98     public static ROptionOption getInstance()
99     {
100         return new ROptionOption();
101     }
102
103
104     /**
105      * Gets an instance of a new ROptionOption
106      */

107     public static ROptionOption getInstance(Integer JavaDoc parent, Integer JavaDoc child)
108         throws Exception JavaDoc
109     {
110         GlobalCacheService tgcs = getGlobalCacheService();
111
112         String JavaDoc key = getCacheKey(parent, child);
113         ROptionOption option = null;
114         try
115         {
116             option = (ROptionOption)tgcs.getObject(key).getContents();
117         }
118         catch (ObjectExpiredException oee)
119         {
120             try
121             {
122                 Criteria crit = new Criteria();
123                 crit.add (ROptionOptionPeer.OPTION1_ID, parent);
124                 crit.add (ROptionOptionPeer.OPTION2_ID, child);
125                 option = (ROptionOption) (ROptionOptionPeer.doSelect(crit)).get(0);
126             }
127             catch (Exception JavaDoc e)
128             {
129                 throw new ScarabException(L10NKeySet.ExceptionOptionNotFound,
130                                           parent.toString(),
131                                           child.toString());
132             }
133             tgcs.addObject(key, new CachedObject(option));
134         }
135         return option;
136     }
137
138     /**
139      * This will also remove the ROptionOption from the internal cache
140      * as well as from the database.
141      */

142     public static void doRemove(ROptionOption roo)
143         throws Exception JavaDoc
144     {
145         // using Criteria because there is a bug in Torque
146
// where doDelete(roo) doesn't work because it has
147
// multple primary keys
148
Criteria crit = new Criteria();
149         crit.add (ROptionOptionPeer.OPTION1_ID, roo.getOption1Id());
150         crit.add (ROptionOptionPeer.OPTION2_ID, roo.getOption2Id());
151
152         ROptionOptionPeer.doDelete(crit);
153
154         GlobalCacheService tgcs = getGlobalCacheService();
155
156         String JavaDoc key = getCacheKey(roo.getOption1Id(), roo.getOption2Id());
157         tgcs.removeObject(key);
158     }
159
160     /**
161      * This will also remove the ROptionOption from the internal cache
162      * as well as from the database.
163      */

164     public static void doRemove(Integer JavaDoc parent, Integer JavaDoc child)
165         throws Exception JavaDoc
166     {
167         ROptionOption roo = getInstance();
168         roo.setOption1Id(parent);
169         roo.setOption2Id(child);
170         ROptionOption.doRemove(roo);
171     }
172
173     /**
174      * Gets the AttributeOption assigned to the Option1Id
175      */

176     public AttributeOption getOption1Option()
177         throws TorqueException
178     {
179         return AttributeOptionManager.getInstance(getOption1Id());
180     }
181
182     /**
183      * Gets the AttributeOption assigned to the Option2Id
184      */

185     public AttributeOption getOption2Option()
186         throws TorqueException
187     {
188         return AttributeOptionManager.getInstance(getOption2Id());
189     }
190
191     /**
192      * Get the level in the option parent-child tree.
193      * Note: Not currently used.
194      * @return value of level.
195      */

196     public int getLevel()
197     {
198         return level;
199     }
200     
201     /**
202      * Get the level in the option parent-child tree.
203      * Note: Not currently used.
204      * @param v Value to assign to level.
205      */

206     public void setLevel(int v)
207     {
208         this.level = v;
209     }
210
211     /**
212      * Will return true if this ROptionOption is the parent
213      * of the given AttributeOption. In other words, the value of
214      * this.OPTION2_ID == option.getOptionId()
215     public boolean isParentOf(AttributeOption option)
216     {
217         return option.getOptionId().equals(this.getOption2Id());
218     }
219      */

220
221     /**
222      * Will return true if this ROptionOption is the child
223      * of the given AttributeOption. In other words, the value of
224      * this.OPTION1_ID == option.getOptionId()
225     public boolean isChildOf(AttributeOption option)
226     {
227         return option.getOptionId().equals(this.getOption1Id());
228     }
229      */

230
231     /**
232      * A String representation of this object.
233      */

234     public String JavaDoc toString()
235     {
236         return "Parent: " + getOption1Id() + " Child: " +
237                 getOption2Id() + " : Order: " + getPreferredOrder();
238     }
239     
240     /**
241      * Gets the <code>GlobalCacheService</code> implementation.
242      *
243      * @return the GlobalCacheService implementation.
244      */

245     protected static final GlobalCacheService getGlobalCacheService()
246     {
247         try{
248             YaafiComponentService yaafi = (YaafiComponentService) TurbineServices.getInstance().getService(
249                 YaafiComponentService.SERVICE_NAME);
250             return (GlobalCacheService) yaafi.lookup(GlobalCacheService.class.getName());
251         }
252         catch (Exception JavaDoc e) {
253             throw new ScarabRuntimeException(L10NKeySet.ExceptionLookupGlobalCache, e);
254         }
255     }
256 }
257
258
259
260
Popular Tags