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: // JDK classes
050: import java.util.Comparator;
051: import java.util.List;
052: import java.util.ArrayList;
053: import java.sql.Connection;
054:
055: // Turbine classes
056: import org.apache.torque.Torque;
057: import org.apache.torque.TorqueException;
058: import org.apache.torque.om.Persistent;
059: import org.apache.torque.util.Criteria;
060:
061: import org.tigris.scarab.om.ModuleManager;
062: import org.tigris.scarab.om.Module;
063: import org.tigris.scarab.tools.localization.L10NKeySet;
064: import org.tigris.scarab.util.ScarabException;
065: import org.tigris.scarab.workflow.WorkflowFactory;
066:
067: /**
068: * This class represents a RModuleOption
069: *
070: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
071: * @version $Id: RModuleOption.java 9977 2005-12-09 00:40:59Z hair $
072: */
073: public class RModuleOption extends BaseRModuleOption implements
074: Persistent {
075:
076: private int level;
077:
078: private static final Comparator COMPARATOR = new Comparator() {
079: public int compare(Object obj1, Object obj2) {
080: int result = 1;
081: RModuleOption opt1 = (RModuleOption) obj1;
082: RModuleOption opt2 = (RModuleOption) obj2;
083: if (opt1.getOrder() < opt2.getOrder()) {
084: result = -1;
085: } else if (opt1.getOrder() == opt2.getOrder()) {
086: result = opt1.getDisplayValue().compareTo(
087: opt2.getDisplayValue());
088: }
089: return result;
090: }
091: };
092:
093: /**
094: * Throws UnsupportedOperationException. Use
095: * <code>getModule()</code> instead.
096: *
097: * @return a <code>ScarabModule</code> value
098: */
099: public ScarabModule getScarabModule() {
100: throw new UnsupportedOperationException("Should use getModule"); //EXCEPTION
101: }
102:
103: /**
104: * Throws UnsupportedOperationException. Use
105: * <code>setModule(Module)</code> instead.
106: *
107: */
108: public void setScarabModule(ScarabModule module) {
109: throw new UnsupportedOperationException(
110: "Should use setModule(Module). Note module cannot be new."); //EXCEPTION
111: }
112:
113: /**
114: * Use this instead of setScarabModule. Note: module cannot be new.
115: */
116: public void setModule(Module me) throws TorqueException {
117: Integer id = me.getModuleId();
118: if (id == null) {
119: throw new TorqueException("Modules must be saved prior to "
120: + "being associated with other objects."); //EXCEPTION
121: }
122: setModuleId(id);
123: }
124:
125: /**
126: * Module getter. Use this method instead of getScarabModule().
127: *
128: * @return a <code>Module</code> value
129: */
130: public Module getModule() throws TorqueException {
131: Module module = null;
132: Integer id = getModuleId();
133: if (id != null) {
134: module = ModuleManager.getInstance(id);
135: }
136:
137: return module;
138: }
139:
140: /**
141: * Compares numeric value and in cases where the numeric value
142: * is the same it compares the display values.
143: */
144: public static Comparator getComparator() {
145: return COMPARATOR;
146: }
147:
148: /**
149: * A convenience method for getting the option name. It is
150: * preferred over using getAttributeOption().getName() as it
151: * leaves open the possibility of per module display values.
152: */
153: public String getDisplayValue() {
154: String dispVal = super .getDisplayValue();
155: if (dispVal == null) {
156: try {
157: dispVal = getAttributeOption().getName();
158: } catch (Exception e) {
159: getLog().error(e);
160: dispVal = "!Error-Check Logs!";
161: }
162: }
163: return dispVal;
164: }
165:
166: /**
167: * Get the level in the option parent-child tree.
168: * @return value of level.
169: */
170: public int getLevel() {
171: return level;
172: }
173:
174: /**
175: * Get the level in the option parent-child tree.
176: * @param v Value to assign to level.
177: */
178: public void setLevel(int v) {
179: this .level = v;
180: }
181:
182: public RModuleAttribute getRModuleAttribute(IssueType issueType)
183: throws TorqueException {
184: Module module = ModuleManager.getInstance(getModuleId());
185: Attribute attribute = getAttributeOption().getAttribute();
186: return module.getRModuleAttribute(attribute, issueType);
187: }
188:
189: /**
190: * Gets a list of this option's descendants
191: * That are associated with this module/Issue Type
192: * @return <code>List</code> of <code>RModuleOptions</code>
193: */
194: public List getDescendants(IssueType issueType)
195: throws TorqueException {
196: List descendants = new ArrayList();
197: List attrDescendants = getAttributeOption().getDescendants();
198: for (int i = 0; i < attrDescendants.size(); i++) {
199: RModuleOption rmo = null;
200: AttributeOption option = (AttributeOption) attrDescendants
201: .get(i);
202: rmo = getModule().getRModuleOption(option, issueType);
203: if (rmo != null
204: && rmo.getOptionId().equals(option.getOptionId())) {
205: descendants.add(rmo);
206: }
207: }
208: return descendants;
209: }
210:
211: public void delete() throws TorqueException, ScarabException {
212: final Module module = getModule();
213:
214: final IssueType issueType = IssueTypeManager.getInstance(
215: getIssueTypeId(), false);
216: if (issueType.getLocked()) {
217: throw new ScarabException(
218: L10NKeySet.ExceptionDeleteOptionFromLockedIssueType);
219: } else {
220: final Criteria c = new Criteria().add(
221: RModuleOptionPeer.MODULE_ID, getModuleId()).add(
222: RModuleOptionPeer.ISSUE_TYPE_ID, getIssueTypeId())
223: .add(RModuleOptionPeer.OPTION_ID, getOptionId());
224: RModuleOptionPeer.doDelete(c);
225: WorkflowFactory.getInstance().deleteWorkflowsForOption(
226: getAttributeOption(), module, issueType);
227: // Correct the ordering of the remaining options
228: final List optIds = new ArrayList();
229: final List rmos = module.getRModuleOptions(
230: getAttributeOption().getAttribute(), issueType,
231: false);
232: for (int i = 0; i < rmos.size(); i++) {
233: final RModuleOption rmo = (RModuleOption) rmos.get(i);
234: optIds.add(rmo.getOptionId());
235: }
236: final Criteria c2 = new Criteria().add(
237: RModuleOptionPeer.MODULE_ID, getModuleId()).add(
238: RModuleOptionPeer.ISSUE_TYPE_ID, getIssueTypeId())
239: .addIn(RModuleOptionPeer.OPTION_ID, optIds).add(
240: RModuleOptionPeer.PREFERRED_ORDER,
241: getOrder(), Criteria.GREATER_THAN);
242: final List adjustRmos = RModuleOptionPeer.doSelect(c2);
243: for (int j = 0; j < adjustRmos.size(); j++) {
244: final RModuleOption rmo = (RModuleOption) adjustRmos
245: .get(j);
246: //rmos.remove(rmo);
247: rmo.setOrder(rmo.getOrder() - 1);
248: rmo.save();
249: //rmos.add(rmo);
250: }
251: }
252: // notify module cache of this change
253: ((ModuleManager) Torque.getManager(ModuleManager.MANAGED_CLASS))
254: .refreshedObject(this );
255: }
256:
257: public void save(Connection con) throws TorqueException {
258: if (isModified()) {
259: if (isNew()) {
260: super .save(con);
261: } else {
262: Attribute attr = getAttributeOption().getAttribute();
263: RIssueTypeAttribute ria = null;
264: try {
265: ria = getIssueType().getRIssueTypeAttribute(attr);
266: if (ria != null && ria.getLocked()) {
267: throw new TorqueException(attr.getName()
268: + "is locked"); //EXCEPTION
269: } else {
270: super .save(con);
271: }
272: } catch (Exception e) {
273: throw new TorqueException("An error has occurred."); //EXCEPTION
274: }
275: }
276: }
277: }
278: }
|