KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > resources > workflow > values > WorkflowStageValueGroup


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.resources.workflow.values;
20
21 import java.util.*;
22
23 import org.openharmonise.commons.cache.*;
24 import org.openharmonise.commons.dsi.AbstractDataStoreInterface;
25 import org.openharmonise.rm.*;
26 import org.openharmonise.rm.resources.*;
27 import org.openharmonise.rm.resources.lifecycle.*;
28 import org.openharmonise.rm.resources.metadata.values.ValueGroup;
29
30
31 /**
32  * This class provides the functionality to manage groups of
33  * <code>WorkflowStage</code> objects.
34  *
35  * @author Michael Bell
36  * @version $Revision: 1.2 $
37  *
38  */

39 public class WorkflowStageValueGroup extends ValueGroup {
40
41     private static List WORKFLOW_CHILDREN = null;
42
43     static {
44         WORKFLOW_CHILDREN = new ArrayList();
45         WORKFLOW_CHILDREN.add(WorkflowStageValue.class.getName());
46         WORKFLOW_CHILDREN.add(WorkflowStageValueGroup.class.getName());
47     }
48
49     /**
50      *
51      */

52     public WorkflowStageValueGroup() {
53         super();
54         
55     }
56
57     /**
58      * @param dbintrf
59      */

60     public WorkflowStageValueGroup(AbstractDataStoreInterface dbintrf) {
61         super(dbintrf);
62         
63     }
64
65     /**
66      * @param dbintrf
67      * @param nId
68      */

69     public WorkflowStageValueGroup(AbstractDataStoreInterface dbintrf, int nId) {
70         super(dbintrf, nId);
71         
72     }
73
74     /**
75      * @param dbintrf
76      * @param nId
77      * @param bIsHist
78      */

79     public WorkflowStageValueGroup(
80         AbstractDataStoreInterface dbintrf,
81         int nId,
82         int nKey,
83         boolean bIsHist) {
84         super(dbintrf, nId, nKey, bIsHist);
85         
86     }
87
88     /**
89      * @param dbintrf
90      * @param bIsHist
91      */

92     public WorkflowStageValueGroup(
93         AbstractDataStoreInterface dbintrf,
94         boolean bIsHist) {
95         super(dbintrf, bIsHist);
96         
97     }
98
99     /* (non-Javadoc)
100      * @see org.openharmonise.rm.resources.AbstractParentObject#getChildClassNames()
101      */

102     public List getChildClassNames() {
103         return WORKFLOW_CHILDREN;
104     }
105
106     /* (non-Javadoc)
107      * @see org.openharmonise.rm.resources.AbstractChildObject#getParentObjectClassName()
108      */

109     public String JavaDoc getParentObjectClassName() {
110         
111         return super.getParentObjectClassName();
112     }
113
114     /* (non-Javadoc)
115      * @see org.openharmonise.rm.resources.AbstractParentObject#isValidChild(org.openharmonise.rm.resources.AbstractChildObject)
116      */

117     public boolean isValidChild(AbstractChildObject childObj) {
118         boolean bIsValid = false;
119         
120         try {
121             List children = getChildren();
122             
123             if(children.size() > 0) {
124                 boolean bIsChildParent = (childObj instanceof AbstractParentObject);
125                 
126                 //if child is a parent object make sure that all children are parent objects
127
//just check against first child as they should all be the same
128
AbstractChildObject firstChild = (AbstractChildObject) children.get(0);
129                 
130                 if(bIsChildParent == true && firstChild instanceof AbstractParentObject) {
131                     bIsValid = true;
132                 } else if(bIsChildParent == false && (firstChild instanceof AbstractParentObject) == false) {
133                     bIsValid = true;
134                 }
135             } else {
136                 bIsValid = super.isValidChild(childObj);
137             }
138         } catch (DataAccessException e) {
139             //if something went wrong then fall back to super implementation
140
bIsValid = super.isValidChild(childObj);
141         }
142         
143         return bIsValid;
144     }
145
146     /* (non-Javadoc)
147      * @see org.openharmonise.rm.resources.AbstractEditableObject#saveNonCoreData()
148      */

149     protected void saveNonCoreData() throws EditException {
150         //need to rejig dependancies for any child objects to be removed
151

152         if(m_remove_children != null && m_remove_children.size() > 0) {
153             
154             List stagesToSave = new ArrayList();
155             try {
156                 //find dependants and those depended upon
157
for (Iterator iter = m_remove_children.iterator(); iter.hasNext();) {
158                     CachePointer tmpChildPtr = (CachePointer) iter.next();
159                     
160                     Object JavaDoc tmpObj = tmpChildPtr.getObject();
161                     
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.getDependencies();
172                             
173                             for (Iterator innerIter = m_children.iterator(); innerIter
174                                     .hasNext();) {
175                                 CachePointer innerPtr = (CachePointer) innerIter.next();
176                                 
177                                 if(tmpChildPtr != innerPtr) {
178                                     Object JavaDoc innerObj = innerPtr.getObject();
179                                     
180                                     if(innerObj instanceof WorkflowStageValue) {
181                                         WorkflowStageValue innerStage = (WorkflowStageValue) innerObj;
182                                         if(depends.contains(innerStage)) {
183                                             //stage to be removed is dependant on innerObj
184
dependants.add(innerStage);
185                                             
186                                         } else {
187                                             List innerDepends = innerStage.getDependencies();
188                                             
189                                             if(innerDepends.contains(stageToRemove)) {
190                                                 //innerObj is dependant on stage to be removed
191
dependantOnThis.add(innerStage);
192                                             }
193                                         }
194                                     }
195                                 }
196                             }
197                             
198                             // now rejig, if necessary
199
if(dependants.size() > 0 && dependantOnThis.size() > 0) {
200                                 for (Iterator depIter = dependantOnThis.iterator(); depIter.hasNext();) {
201                                     WorkflowStageValue dependantStage = (WorkflowStageValue) depIter.next();
202                                     if(dependantStage.getStatus() == Status.APPROVED) {
203                                         List otherDepends = dependantStage.getDependencies();
204                                         
205                                         otherDepends.remove(stageToRemove);
206                                         otherDepends.addAll(dependants);
207                                         dependantStage.setDependencies(otherDepends);
208                                         stagesToSave.add(dependantStage);
209                                         
210                                     } else {
211                                         throw new EditException("WorkflowStage should be approved by now!!!");
212                                     }
213                                     
214                                 }
215                             }
216                         }
217                     }
218                 }
219                 
220                 for (Iterator iter = stagesToSave.iterator(); iter.hasNext();) {
221                     WorkflowStageValue tmpStage = (WorkflowStageValue) iter.next();
222                     tmpStage.save().changeStatus(Status.APPROVED);
223                 }
224             } catch (DataAccessException e) {
225                 throw new EditException(e);
226             } catch (PopulateException e) {
227                 throw new EditException(e);
228             } catch (CacheException e) {
229                 throw new EditException(e);
230             }
231         }
232         
233         super.saveNonCoreData();
234     }
235 }
236
Popular Tags