001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.resources.workflow.values;
020:
021: import java.util.*;
022:
023: import org.openharmonise.commons.cache.*;
024: import org.openharmonise.commons.dsi.AbstractDataStoreInterface;
025: import org.openharmonise.rm.*;
026: import org.openharmonise.rm.resources.*;
027: import org.openharmonise.rm.resources.lifecycle.*;
028: import org.openharmonise.rm.resources.metadata.values.ValueGroup;
029:
030: /**
031: * This class provides the functionality to manage groups of
032: * <code>WorkflowStage</code> objects.
033: *
034: * @author Michael Bell
035: * @version $Revision: 1.2 $
036: *
037: */
038: public class WorkflowStageValueGroup extends ValueGroup {
039:
040: private static List WORKFLOW_CHILDREN = null;
041:
042: static {
043: WORKFLOW_CHILDREN = new ArrayList();
044: WORKFLOW_CHILDREN.add(WorkflowStageValue.class.getName());
045: WORKFLOW_CHILDREN.add(WorkflowStageValueGroup.class.getName());
046: }
047:
048: /**
049: *
050: */
051: public WorkflowStageValueGroup() {
052: super ();
053:
054: }
055:
056: /**
057: * @param dbintrf
058: */
059: public WorkflowStageValueGroup(AbstractDataStoreInterface dbintrf) {
060: super (dbintrf);
061:
062: }
063:
064: /**
065: * @param dbintrf
066: * @param nId
067: */
068: public WorkflowStageValueGroup(AbstractDataStoreInterface dbintrf,
069: int nId) {
070: super (dbintrf, nId);
071:
072: }
073:
074: /**
075: * @param dbintrf
076: * @param nId
077: * @param bIsHist
078: */
079: public WorkflowStageValueGroup(AbstractDataStoreInterface dbintrf,
080: int nId, int nKey, boolean bIsHist) {
081: super (dbintrf, nId, nKey, bIsHist);
082:
083: }
084:
085: /**
086: * @param dbintrf
087: * @param bIsHist
088: */
089: public WorkflowStageValueGroup(AbstractDataStoreInterface dbintrf,
090: boolean bIsHist) {
091: super (dbintrf, bIsHist);
092:
093: }
094:
095: /* (non-Javadoc)
096: * @see org.openharmonise.rm.resources.AbstractParentObject#getChildClassNames()
097: */
098: public List getChildClassNames() {
099: return WORKFLOW_CHILDREN;
100: }
101:
102: /* (non-Javadoc)
103: * @see org.openharmonise.rm.resources.AbstractChildObject#getParentObjectClassName()
104: */
105: public String getParentObjectClassName() {
106:
107: return super .getParentObjectClassName();
108: }
109:
110: /* (non-Javadoc)
111: * @see org.openharmonise.rm.resources.AbstractParentObject#isValidChild(org.openharmonise.rm.resources.AbstractChildObject)
112: */
113: public boolean isValidChild(AbstractChildObject childObj) {
114: boolean bIsValid = false;
115:
116: try {
117: List children = getChildren();
118:
119: if (children.size() > 0) {
120: boolean bIsChildParent = (childObj instanceof AbstractParentObject);
121:
122: //if child is a parent object make sure that all children are parent objects
123: //just check against first child as they should all be the same
124: AbstractChildObject firstChild = (AbstractChildObject) children
125: .get(0);
126:
127: if (bIsChildParent == true
128: && firstChild instanceof AbstractParentObject) {
129: bIsValid = true;
130: } else if (bIsChildParent == false
131: && (firstChild instanceof AbstractParentObject) == false) {
132: bIsValid = true;
133: }
134: } else {
135: bIsValid = super .isValidChild(childObj);
136: }
137: } catch (DataAccessException e) {
138: //if something went wrong then fall back to super implementation
139: bIsValid = super .isValidChild(childObj);
140: }
141:
142: return bIsValid;
143: }
144:
145: /* (non-Javadoc)
146: * @see org.openharmonise.rm.resources.AbstractEditableObject#saveNonCoreData()
147: */
148: protected void saveNonCoreData() throws EditException {
149: //need to rejig dependancies for any child objects to be removed
150:
151: if (m_remove_children != null && m_remove_children.size() > 0) {
152:
153: List stagesToSave = new ArrayList();
154: try {
155: //find dependants and those depended upon
156: for (Iterator iter = m_remove_children.iterator(); iter
157: .hasNext();) {
158: CachePointer tmpChildPtr = (CachePointer) iter
159: .next();
160:
161: Object tmpObj = tmpChildPtr.getObject();
162:
163: if (tmpObj instanceof WorkflowStageValue) {
164: List dependantOnThis = new ArrayList();
165: List dependants = new ArrayList();
166:
167: WorkflowStageValue stageToRemove = (WorkflowStageValue) tmpObj;
168:
169: if (stageToRemove.getPendingVersions().size() == 0) {
170:
171: List depends = stageToRemove
172: .getDependencies();
173:
174: for (Iterator innerIter = m_children
175: .iterator(); innerIter.hasNext();) {
176: CachePointer innerPtr = (CachePointer) innerIter
177: .next();
178:
179: if (tmpChildPtr != innerPtr) {
180: Object innerObj = innerPtr
181: .getObject();
182:
183: if (innerObj instanceof WorkflowStageValue) {
184: WorkflowStageValue innerStage = (WorkflowStageValue) innerObj;
185: if (depends
186: .contains(innerStage)) {
187: //stage to be removed is dependant on innerObj
188: dependants.add(innerStage);
189:
190: } else {
191: List innerDepends = innerStage
192: .getDependencies();
193:
194: if (innerDepends
195: .contains(stageToRemove)) {
196: //innerObj is dependant on stage to be removed
197: dependantOnThis
198: .add(innerStage);
199: }
200: }
201: }
202: }
203: }
204:
205: // now rejig, if necessary
206: if (dependants.size() > 0
207: && dependantOnThis.size() > 0) {
208: for (Iterator depIter = dependantOnThis
209: .iterator(); depIter.hasNext();) {
210: WorkflowStageValue dependantStage = (WorkflowStageValue) depIter
211: .next();
212: if (dependantStage.getStatus() == Status.APPROVED) {
213: List otherDepends = dependantStage
214: .getDependencies();
215:
216: otherDepends
217: .remove(stageToRemove);
218: otherDepends.addAll(dependants);
219: dependantStage
220: .setDependencies(otherDepends);
221: stagesToSave
222: .add(dependantStage);
223:
224: } else {
225: throw new EditException(
226: "WorkflowStage should be approved by now!!!");
227: }
228:
229: }
230: }
231: }
232: }
233: }
234:
235: for (Iterator iter = stagesToSave.iterator(); iter
236: .hasNext();) {
237: WorkflowStageValue tmpStage = (WorkflowStageValue) iter
238: .next();
239: tmpStage.save().changeStatus(Status.APPROVED);
240: }
241: } catch (DataAccessException e) {
242: throw new EditException(e);
243: } catch (PopulateException e) {
244: throw new EditException(e);
245: } catch (CacheException e) {
246: throw new EditException(e);
247: }
248: }
249:
250: super.saveNonCoreData();
251: }
252: }
|