001: package org.tigris.scarab.om;
002:
003: /* ================================================================
004: * Copyright (c) 2000-2005 CollabNet. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by Collab.Net <http://www.Collab.Net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of Collab.Net.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: *
043: * ====================================================================
044: *
045: * This software consists of voluntary contributions made by many
046: * individuals on behalf of Collab.Net.
047: */
048:
049: // Turbine classes
050: import org.apache.fulcrum.cache.CachedObject;
051: import org.apache.fulcrum.cache.GlobalCacheService;
052: import org.apache.fulcrum.cache.ObjectExpiredException;
053: import org.apache.torque.TorqueException;
054: import org.apache.torque.om.Persistent;
055: import org.apache.torque.util.Criteria;
056: import org.tigris.scarab.services.ServiceManager;
057: import org.tigris.scarab.tools.localization.L10NKeySet;
058: import org.tigris.scarab.util.ScarabException;
059: import org.tigris.scarab.util.ScarabRuntimeException;
060:
061: /**
062: * This class represents the SCARAB_R_OPTION_OPTION table.
063: *
064: * @author <a href="mailto:jon@collab.net">Jon S. Stevens</a>
065: * @version $Id: ROptionOption.java 10193 2006-06-30 12:49:42Z dabbous $
066: */
067: public class ROptionOption extends BaseROptionOption implements
068: Persistent {
069: private int level;
070:
071: /** the name of this class */
072: private static final String CLASS_NAME = "ROptionOption";
073:
074: /**
075: * Must call getInstance()
076: */
077: protected ROptionOption() {
078: }
079:
080: /**
081: * Creates a key for use in caching AttributeOptions
082: */
083: static String getCacheKey(Integer option1, Integer option2) {
084: String keyStringA = option1.toString();
085: String keyStringB = option2.toString();
086: return new StringBuffer(CLASS_NAME.length()
087: + keyStringA.length() + keyStringB.length()).append(
088: CLASS_NAME).append(keyStringA).append(keyStringB)
089: .toString();
090: }
091:
092: /**
093: * Gets an instance of a new ROptionOption
094: */
095: public static ROptionOption getInstance() {
096: return new ROptionOption();
097: }
098:
099: /**
100: * Gets an instance of a new ROptionOption
101: */
102: public static ROptionOption getInstance(final Integer parent,
103: final Integer child) throws TorqueException,
104: ScarabException {
105: final GlobalCacheService tgcs = getGlobalCacheService();
106:
107: final String key = getCacheKey(parent, child);
108: ROptionOption option = null;
109: try {
110: option = (ROptionOption) tgcs.getObject(key).getContents();
111: } catch (ObjectExpiredException oee) {
112: try {
113: final Criteria crit = new Criteria();
114: crit.add(ROptionOptionPeer.OPTION1_ID, parent);
115: crit.add(ROptionOptionPeer.OPTION2_ID, child);
116: option = (ROptionOption) (ROptionOptionPeer
117: .doSelect(crit)).get(0);
118: } catch (Exception e) {
119: throw new ScarabException(
120: L10NKeySet.ExceptionOptionNotFound, parent
121: .toString(), child.toString());
122: }
123: tgcs.addObject(key, new CachedObject(option));
124: }
125: return option;
126: }
127:
128: /**
129: * This will also remove the ROptionOption from the internal cache
130: * as well as from the database.
131: */
132: public static void doRemove(ROptionOption roo)
133: throws TorqueException {
134: // using Criteria because there is a bug in Torque
135: // where doDelete(roo) doesn't work because it has
136: // multple primary keys
137: Criteria crit = new Criteria();
138: crit.add(ROptionOptionPeer.OPTION1_ID, roo.getOption1Id());
139: crit.add(ROptionOptionPeer.OPTION2_ID, roo.getOption2Id());
140:
141: ROptionOptionPeer.doDelete(crit);
142:
143: GlobalCacheService tgcs = getGlobalCacheService();
144:
145: String key = getCacheKey(roo.getOption1Id(), roo.getOption2Id());
146: tgcs.removeObject(key);
147: }
148:
149: /**
150: * This will also remove the ROptionOption from the internal cache
151: * as well as from the database.
152: */
153: public static void doRemove(Integer parent, Integer child)
154: throws TorqueException {
155: ROptionOption roo = getInstance();
156: roo.setOption1Id(parent);
157: roo.setOption2Id(child);
158: ROptionOption.doRemove(roo);
159: }
160:
161: /**
162: * Gets the AttributeOption assigned to the Option1Id
163: */
164: public AttributeOption getOption1Option() throws TorqueException {
165: return AttributeOptionManager.getInstance(getOption1Id());
166: }
167:
168: /**
169: * Gets the AttributeOption assigned to the Option2Id
170: */
171: public AttributeOption getOption2Option() throws TorqueException {
172: return AttributeOptionManager.getInstance(getOption2Id());
173: }
174:
175: /**
176: * Get the level in the option parent-child tree.
177: * Note: Not currently used.
178: * @return value of level.
179: */
180: public int getLevel() {
181: return level;
182: }
183:
184: /**
185: * Get the level in the option parent-child tree.
186: * Note: Not currently used.
187: * @param v Value to assign to level.
188: */
189: public void setLevel(int v) {
190: this .level = v;
191: }
192:
193: /**
194: * Will return true if this ROptionOption is the parent
195: * of the given AttributeOption. In other words, the value of
196: * this.OPTION2_ID == option.getOptionId()
197: public boolean isParentOf(AttributeOption option)
198: {
199: return option.getOptionId().equals(this.getOption2Id());
200: }
201: */
202:
203: /**
204: * Will return true if this ROptionOption is the child
205: * of the given AttributeOption. In other words, the value of
206: * this.OPTION1_ID == option.getOptionId()
207: public boolean isChildOf(AttributeOption option)
208: {
209: return option.getOptionId().equals(this.getOption1Id());
210: }
211: */
212:
213: /**
214: * A String representation of this object.
215: */
216: public String toString() {
217: return "Parent: " + getOption1Id() + " Child: "
218: + getOption2Id() + " : Order: " + getPreferredOrder();
219: }
220:
221: /**
222: * Gets the <code>GlobalCacheService</code> implementation.
223: *
224: * @return the GlobalCacheService implementation.
225: */
226: protected static final GlobalCacheService getGlobalCacheService() {
227: try {
228: ServiceManager sm = ServiceManager.getInstance();
229: return (GlobalCacheService) sm
230: .lookup(GlobalCacheService.class);
231: } catch (Exception e) {
232: throw new ScarabRuntimeException(
233: L10NKeySet.ExceptionLookupGlobalCache, e);
234: }
235: }
236: }
|