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: import java.util.ArrayList;
050: import java.util.Iterator;
051: import java.util.List;
052:
053: import org.apache.fulcrum.security.entity.Role;
054: import org.apache.fulcrum.security.impl.db.entity.TurbineRolePeer;
055: import org.apache.torque.NoRowsException;
056: import org.apache.torque.TooManyRowsException;
057: import org.apache.torque.TorqueException;
058: import org.apache.torque.om.Persistent;
059: import org.apache.torque.util.Criteria;
060: import org.tigris.scarab.tools.ScarabLocalizationTool;
061: import org.tigris.scarab.tools.localization.L10NKeySet;
062:
063: /**
064: * Every transition declare the ability of a role to change the value of an
065: * attribute from one of its options to another.
066: *
067: * @see org.tigris.scarab.workflow.CheapWorkflow
068: *
069: * @author Diego Martinez Velasco
070: * @author Jorge Uriarte Aretxaga
071: */
072: public class Transition extends BaseTransition implements Persistent,
073: Conditioned {
074: public Role getRole() {
075: Role role = null;
076: try {
077: role = TurbineRolePeer.retrieveByPK(this .getRoleId());
078: } catch (NoRowsException e) {
079: //Nothing to do, just ignore it
080: } catch (TooManyRowsException e) {
081: //Nothing to do, just ignore it
082: } catch (TorqueException e) {
083: e.printStackTrace();
084: }
085: return role;
086: }
087:
088: public AttributeOption getFrom() {
089: AttributeOption from = null;
090: if (null != this .getFromOptionId()) {
091: try {
092: from = AttributeOptionPeer.retrieveByPK(this
093: .getFromOptionId());
094: } catch (NoRowsException e) {
095: //Nothing to do, just ignore it
096: } catch (TooManyRowsException e) {
097: //Nothing to do, just ignore it
098: } catch (TorqueException e) {
099: e.printStackTrace();
100: }
101: }
102: return from;
103: }
104:
105: public AttributeOption getTo() {
106: AttributeOption to = null;
107: try {
108: to = AttributeOptionPeer.retrieveByPK(this .getToOptionId());
109: } catch (NoRowsException e) {
110: //Nothing to do, just ignore it
111: } catch (TooManyRowsException e) {
112: //Nothing to do, just ignore it
113: } catch (TorqueException e) {
114: e.printStackTrace();
115: }
116: return to;
117: }
118:
119: public boolean isRequiredIf(Integer optionID)
120: throws TorqueException {
121: Condition cond = new Condition();
122: cond.setAttributeId(null);
123: cond.setOptionId(optionID);
124: cond.setModuleId(null);
125: cond.setIssueTypeId(null);
126: cond.setTransitionId(this .getTransitionId());
127: return this .getConditions().contains(cond);
128: }
129:
130: public String getFromName() {
131: String fromName = null;
132: AttributeOption from = this .getFrom();
133: if (from == null) {
134: ScarabLocalizationTool l10n = new ScarabLocalizationTool();
135: fromName = l10n.get(L10NKeySet.TransitionsAnyOption);
136: } else {
137: fromName = from.getName();
138: }
139: return fromName;
140: }
141:
142: public String getToName() {
143: String toName = null;
144: AttributeOption to = this .getTo();
145: if (to == null) {
146: ScarabLocalizationTool l10n = new ScarabLocalizationTool();
147: toName = l10n.get(L10NKeySet.TransitionsAnyOption);
148: } else {
149: toName = to.getName();
150: }
151: return toName;
152: }
153:
154: public String getRoleName() {
155: String roleName = null;
156: Role role = this .getRole();
157: if (role == null) {
158: ScarabLocalizationTool l10n = new ScarabLocalizationTool();
159: roleName = l10n.get(L10NKeySet.TransitionsAnyRole);
160: } else {
161: roleName = role.getName();
162: }
163: return roleName;
164: }
165:
166: /**
167: * Returns the conditions associated to this Transition
168: */
169: public List getConditions() throws TorqueException {
170: List conds = (List) TransitionManager.getMethodResult().get(
171: this , "getConditions");
172: if (conds == null) {
173: conds = super .getConditions();
174: TransitionManager.getMethodResult().put(conds, this ,
175: "getConditions");
176: }
177: return conds;
178: }
179:
180: /**
181: * Returns the array of attributeOptionIds that will force the requiment of this
182: * attribute if set. Used by templates to load the combo.
183: * @return
184: */
185: public Integer[] getConditionsArray() {
186: List conditions = new ArrayList();
187: Integer[] aIDs = null;
188: try {
189:
190: conditions = this .getConditions();
191: aIDs = new Integer[conditions.size()];
192: int i = 0;
193: for (Iterator iter = conditions.iterator(); iter.hasNext(); i++) {
194: aIDs[i] = (Integer) iter.next();
195: }
196: } catch (TorqueException e) {
197: this .getLog().error("getConditionsArray: " + e);
198: }
199: return aIDs;
200: }
201:
202: /**
203: * Load the attribute options' IDs from the template combo.
204: * @param aOptionId
205: * @throws TorqueException
206: */
207: public void setConditionsArray(Integer aOptionId[])
208: throws TorqueException {
209: Criteria crit = new Criteria();
210: crit.add(ConditionPeer.ATTRIBUTE_ID, null);
211: crit.add(ConditionPeer.MODULE_ID, null);
212: crit.add(ConditionPeer.ISSUE_TYPE_ID, null);
213: crit.add(ConditionPeer.TRANSITION_ID, this .getTransitionId());
214: ConditionPeer.doDelete(crit);
215: this .getConditions().clear();
216: ConditionManager.clear();
217: if (aOptionId != null) {
218: for (int i = 0; i < aOptionId.length; i++) {
219: if (aOptionId[i].intValue() != 0) {
220: Condition cond = new Condition();
221: cond.setTransitionId(this .getTransitionId());
222: cond.setOptionId(aOptionId[i]);
223: cond.setAttributeId(null);
224: cond.setModuleId(null);
225: cond.setIssueTypeId(null);
226: this .addCondition(cond);
227: cond.save();
228: }
229: }
230: }
231: }
232:
233: public boolean isConditioned() {
234: boolean bRdo = false;
235: try {
236: bRdo = this .getConditions().size() > 0;
237: } catch (TorqueException te) {
238: // Nothing to do
239: }
240: return bRdo;
241: }
242:
243: public String toString() {
244: return this .getFromOptionId() + " -> " + this .getToOptionId()
245: + " (role: " + this .getRoleId() + ")";
246: }
247:
248: }
|