KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.*;
22 import java.util.*;
23 import java.util.logging.*;
24
25 import org.openharmonise.commons.dsi.*;
26 import org.openharmonise.commons.dsi.dml.SelectStatement;
27 import org.openharmonise.rm.*;
28 import org.openharmonise.rm.factory.*;
29 import org.openharmonise.rm.metadata.*;
30 import org.openharmonise.rm.resources.lifecycle.*;
31 import org.openharmonise.rm.resources.metadata.properties.*;
32 import org.openharmonise.rm.resources.metadata.values.Value;
33
34
35 /**
36  * A definition of the workflow stage.
37  *
38  * This class extends <code>Property</code> with utility methods for workflow
39  * specific operations.
40  *
41  * @author Michael Bell
42  * @version $Revision: 1.2 $
43  *
44  */

45 public class WorkflowStageValue extends Value {
46
47     public static final String JavaDoc TAG_WORKFLOW_STAGE = "WorkflowStage";
48     
49     public static final String JavaDoc PROPNAME_ALLOWED_ROLES = "WORKFLOW_ROLES";
50     public static final String JavaDoc PROPNAME_IS_MANDATORY = "WORKFLOW_MAND";
51     public static final String JavaDoc PROPNAME_IS_INHERITABLE = "WORKFLOW_INHERIT";
52     public static final String JavaDoc PROPNAME_DEPENDENCY = "WORKFLOW_DEPEND";
53     public static final String JavaDoc PROPNAME_DEFINITION = "WORKFLOW_RELN";
54     
55     /**
56      * Logger for this class.
57      */

58     private static final Logger m_logger = Logger.getLogger(WorkflowStageValue.class.getName());
59
60     /**
61      * Basic constructor.
62      */

63     public WorkflowStageValue() {
64         super();
65     }
66
67     /**
68      * Constructs object with an interface to the datastore.
69      *
70      * @param dbintrf
71      */

72     public WorkflowStageValue(AbstractDataStoreInterface dbintrf) {
73         super(dbintrf);
74     }
75
76     /**
77      * Constructs particular object with interface to datastore.
78      *
79      * @param dbintrf
80      * @param nId
81      */

82     public WorkflowStageValue(
83         AbstractDataStoreInterface dbintrf,
84         int nId) {
85         super(dbintrf, nId);
86     }
87
88     /**
89      * Constructs object with an interface to the datastore and a flag to specify
90      * whether object is to be historical.
91      *
92      * @param dbintrf
93      * @param bIsHist
94      * @throws Exception
95      */

96     public WorkflowStageValue(
97         AbstractDataStoreInterface dbintrf,
98         boolean bIsHist)
99         throws Exception JavaDoc {
100         super(dbintrf, bIsHist);
101     }
102
103     /**
104      * Constructs a particular object which may be historical.
105      *
106      * @param dbintrf
107      * @param nId
108      * @param bIsHist
109      */

110     public WorkflowStageValue(
111         AbstractDataStoreInterface dbintrf,
112         int nId,
113         int nKey,
114         boolean bIsHist) {
115         super(dbintrf, nId, nKey, bIsHist);
116     }
117     
118     /**
119      * Returns <code>true</code> if instances of this workflow stage are to be
120      * inherited by successive versions.
121      *
122      * @return
123      * @throws DataAccessException
124      */

125     public boolean isInheritable() throws DataAccessException {
126         boolean bIsInherit = false;
127         
128         try {
129             GeneralPropertyInstance propInst = (GeneralPropertyInstance) getProfile().getPropertyInstance(PROPNAME_IS_INHERITABLE);
130         
131             if(propInst != null) {
132                 bIsInherit = propInst.getBooleanValue();
133             }
134             
135         } catch (InvalidPropertyInstanceException e) {
136             //property was not valid on profile - it really should be
137
//must be a seeding problem - log and ignore it
138
m_logger.log(Level.INFO, "Inheritable property not valid on workflow stage!", e);
139         }
140         
141         return bIsInherit;
142     }
143     
144     /**
145      * Returns the list of workflow stages this stage is dependent on.
146      *
147      * @return
148      * @throws DataAccessException
149      */

150     public List getDependencies() throws DataAccessException {
151         List depends = new ArrayList();
152         
153         try {
154             ChildObjectPropertyInstance propInst =
155                 (ChildObjectPropertyInstance) getProfile().getPropertyInstance(
156                     PROPNAME_DEPENDENCY);
157             if(propInst != null) {
158                 depends = propInst.getValues();
159             }
160         } catch (InvalidPropertyInstanceException e) {
161             //property was not valid on profile - it really should be
162
//must be a seeding problem - log and ignore it
163
m_logger.log(Level.INFO, "Inheritable property not valid on workflow stage!", e);
164         }
165         
166         return depends;
167     }
168     
169     /**
170      * Sets the list of workflow stages this work flow stage is dependent on
171      * being complete before this stage can be complete .
172      *
173      * @param depends
174      * @throws PopulateException
175      */

176     public void setDependencies(List depends) throws PopulateException {
177         try {
178             ChildObjectPropertyInstance propInst =
179                 (ChildObjectPropertyInstance) getProfile().getPropertyInstance(
180                     PROPNAME_DEPENDENCY);
181             
182             if(propInst == null) {
183                 Property prop = PropertyFactory.getPropertyFromName(m_dsi, PROPNAME_DEPENDENCY);
184                 propInst = (ChildObjectPropertyInstance) PropertyInstanceFactory.getPropertyInstance(m_dsi, prop);
185         getProfile().addPropertyInstance(propInst);
186       }
187       
188       propInst.setValues(depends);
189         } catch (InvalidPropertyInstanceException e) {
190             //property was not valid on profile - it really should be
191
//must be a seeding problem - log and ignore it
192
m_logger.log(Level.INFO, "Inheritable property not valid on workflow stage!", e);
193         } catch (DataAccessException e) {
194             throw new PopulateException(e.getLocalizedMessage(),e);
195         } catch (HarmoniseFactoryException e) {
196             throw new PopulateException(e.getLocalizedMessage(),e);
197         } catch (InvalidPropertyValueException e) {
198             throw new PopulateException(e.getLocalizedMessage(),e);
199         } catch (ProfileException e) {
200       throw new PopulateException(e.getLocalizedMessage(),e);
201         }
202     }
203     
204     /**
205      * Returns the list of role <code>Value</code>s that can assign this work flow
206      * stage as complete.
207      *
208      * @return
209      * @throws DataAccessException
210      */

211     public List getRoles() throws DataAccessException {
212         List roles = new ArrayList();
213         
214         try {
215             ChildObjectPropertyInstance propInst =
216                 (ChildObjectPropertyInstance) getProfile().getPropertyInstance(
217                     PROPNAME_ALLOWED_ROLES);
218             if(propInst != null) {
219                 roles = propInst.getValues();
220             }
221         } catch (InvalidPropertyInstanceException e) {
222             //property was not valid on profile - it really should be
223
//must be a seeding problem - log and ignore it
224
m_logger.log(Level.INFO, "Inheritable property not valid on workflow stage!", e);
225         }
226         
227         return roles;
228     }
229
230     /**
231      * Sets the list of role <code>Value</code>s that can assign this work flow
232      * stage as complete.
233      *
234      * @param roles
235      * @throws PopulateException
236      */

237     public void setRoles(List roles) throws PopulateException {
238         try {
239             ChildObjectPropertyInstance propInst =
240                 (ChildObjectPropertyInstance) getProfile().getPropertyInstance(
241                     PROPNAME_ALLOWED_ROLES);
242     
243             if(propInst == null) {
244                 Property prop = PropertyFactory.getPropertyFromName(m_dsi, PROPNAME_ALLOWED_ROLES);
245                 propInst = (ChildObjectPropertyInstance) PropertyInstanceFactory.getPropertyInstance(m_dsi, prop);
246                 getProfile().addPropertyInstance(propInst);
247             }
248       
249       propInst.setValues(roles);
250         } catch (InvalidPropertyInstanceException e) {
251             //property was not valid on profile - it really should be
252
//must be a seeding problem - log and ignore it
253
m_logger.log(Level.INFO, "Inheritable property not valid on workflow stage!", e);
254         } catch (DataAccessException e) {
255             throw new PopulateException(e.getLocalizedMessage(),e);
256         } catch (HarmoniseFactoryException e) {
257             throw new PopulateException(e.getLocalizedMessage(),e);
258         } catch (InvalidPropertyValueException e) {
259             throw new PopulateException(e.getLocalizedMessage(),e);
260         } catch (ProfileException e) {
261       throw new PopulateException(e.getLocalizedMessage(),e);
262         }
263     }
264
265     /* (non-Javadoc)
266      * @see org.openharmonise.rm.resources.AbstractChildObject#getParentObjectClassName()
267      */

268     public String JavaDoc getParentObjectClassName() {
269         return WorkflowStageValueGroup.class.getName();
270     }
271
272     /**
273      * Returns <code>true</code> if this stage is mandatory.
274      *
275      * @return
276      */

277     public boolean isMandatory() throws DataAccessException {
278         boolean bIsMandatory = false;
279         
280         try {
281             GeneralPropertyInstance propInst = (GeneralPropertyInstance) getProfile().getPropertyInstance(PROPNAME_IS_MANDATORY);
282         
283             if(propInst != null) {
284                 bIsMandatory = propInst.getBooleanValue();
285             }
286             
287         } catch (InvalidPropertyInstanceException e) {
288             //property was not valid on profile - it really should be
289
//must be a seeding problem - log and ignore it
290
m_logger.log(Level.INFO, "Inheritable property not valid on workflow stage!", e);
291         }
292         
293         return bIsMandatory;
294     }
295
296     public Editable changeStatus(Status status) throws EditException {
297         
298         //ensure that if this is to be an inheritable stage it's mandatory
299
//dependancies are inheritable too
300
try {
301             Profile prof = this.getProfile();
302             Property inheritProp = PropertyFactory.getPropertyFromName(m_dsi, PROPNAME_IS_INHERITABLE);
303             
304             if(prof != null && prof.isValidProperty(inheritProp)) {
305                 GeneralPropertyInstance isInherit = (GeneralPropertyInstance) prof.getPropertyInstance(inheritProp);
306                 
307                 if(isInherit != null && isInherit.getBooleanValue() == true) {
308                     List depends = this.getDependencies();
309                     
310                     Iterator iter = depends.iterator();
311                     
312                     while (iter.hasNext()) {
313                         WorkflowStageValue stage = (WorkflowStageValue) iter.next();
314                         if(stage.isMandatory() == true && stage.isInheritable() == false) {
315                             throw new StatusChangeNotAllowedException("Mandatory dependancies must be inheritable");
316                         }
317                     }
318                     
319                 }
320             }
321         } catch (InvalidPropertyInstanceException e) {
322             throw new EditException(e.getLocalizedMessage(),e);
323         } catch (DataAccessException e) {
324             throw new EditException(e.getLocalizedMessage(),e);
325         } catch (HarmoniseFactoryException e) {
326             throw new EditException(e.getLocalizedMessage(),e);
327         }
328         
329         return super.changeStatus(status);
330     }
331
332     /**
333      * Remove all instances of <code>WorkflowStage</code> which refer to
334      * this stage as their definition.
335      */

336     public void removeAllInstances() throws EditException {
337         ResultSet rs = null;
338         
339         try {
340             SelectStatement select = new SelectStatement();
341             
342             Property prop = PropertyFactory.getPropertyFromName(m_dsi, PROPNAME_DEFINITION);
343             
344             AbstractPropertyInstance propInst = PropertyInstanceFactory.getPropertyInstance(m_dsi, prop);
345             Profile prof = getProfile();
346             propInst.setProfile(prof);
347             select.addSelectColumn(this.getInstanceColumnRef(ATTRIB_ID, false));
348             
349             select.addJoinCondition(prof.getInstanceColumnRef(Profile.ATTRIB_OBJECT_KEY, false), this.getInstanceColumnRef(ATTRIB_KEY, false));
350             select.addJoinCondition(propInst.getInstanceColumnRef(Profile.TAG_PROFILE, false), prof.getInstanceColumnRef(ATTRIB_ID, false));
351             
352             select.addWhereCondition(propInst.getInstanceColumnRef(Property.TAG_PROPERTY, false), "=", prop.getId());
353             select.addWhereCondition(propInst.getInstanceColumnRef(AbstractPropertyInstance.TAG_VALUE, false), "=", getId());
354             
355             rs = m_dsi.execute(select);
356             
357             while(rs.next()) {
358                 int nId = rs.getInt(1);
359                 
360                 WorkflowStageValue stage = (WorkflowStageValue) HarmoniseObjectFactory.instantiateHarmoniseObject(m_dsi, WorkflowStageValue.class.getName(), nId);
361                 
362                 if(stage != null) {
363                     stage.archive();
364                 }
365             }
366         } catch (HarmoniseFactoryException e) {
367             throw new EditException(e);
368         } catch (DataAccessException e) {
369             throw new EditException(e);
370         } catch (DataStoreException e) {
371             throw new EditException(e);
372         } catch (SQLException e) {
373             throw new EditException(e);
374         } finally {
375             if(rs != null) {
376                 try {
377                     rs.close();
378                 } catch (SQLException e) {
379                     throw new EditException(e);
380                 }
381             }
382         }
383     }
384
385     /* (non-Javadoc)
386      * @see org.openharmonise.rm.publishing.Publishable#getTagName()
387      */

388     public String JavaDoc getTagName() {
389         return TAG_WORKFLOW_STAGE;
390     }
391 }
392
Popular Tags