KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > actions > rules > isMetadataValidRule


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.him.actions.rules;
20
21 import java.util.*;
22
23 import org.openharmonise.him.*;
24 import org.openharmonise.him.harmonise.*;
25 import org.openharmonise.him.metadata.range.swing.relatedevents.*;
26 import org.openharmonise.vfs.*;
27 import org.openharmonise.vfs.authentication.*;
28 import org.openharmonise.vfs.metadata.*;
29 import org.openharmonise.vfs.metadata.value.*;
30 import org.openharmonise.vfs.servers.ServerList;
31 import org.openharmonise.workfloweditor.vfs.*;
32
33
34 /**
35  * Rule that will return true if all the metadata, for the currently
36  * selected virtual file, is valid.
37  *
38  * @author Michael Bell
39  * @version $Revision: 1.1 $
40  *
41  */

42 public class isMetadataValidRule implements EnableRule {
43
44     boolean m_bComparator = true;
45
46     /**
47      *
48      */

49     public isMetadataValidRule() {
50         super();
51         // TODO Auto-generated constructor stub
52
}
53
54     /* (non-Javadoc)
55      * @see com.simulacramedia.contentmanager.actions.rules.EnableRule#isEnabled(com.simulacramedia.vfs.VirtualFile)
56      */

57     public boolean isEnabled(VirtualFile vfFile) {
58         List props = vfFile.getProperties();
59         
60         Iterator iter = props.iterator();
61         
62         boolean bIsValid = true;
63         
64         while (iter.hasNext() && bIsValid == true) {
65             PropertyInstance propInst = (PropertyInstance) iter.next();
66             
67             List vals = propInst.getValues();
68             int nVals = 0;
69             
70             for(int i=0;i<vals.size();i++) {
71                 ValueInstance tmpVal = (ValueInstance) vals.get(i);
72                 if(tmpVal != null) {
73                     if(tmpVal instanceof DateValue) {
74                         String JavaDoc dtVal = ((DateValue)tmpVal).getValue();
75                         
76                         if(dtVal != null && dtVal.length() > 0) {
77                             nVals++;
78                         }
79                     } else {
80                         nVals++;
81                     }
82                     
83                 }
84             }
85             
86             List domains = propInst.getDefinition().getDomains();
87             Iterator domIter = domains.iterator();
88
89             while (domIter.hasNext() && bIsValid == true) {
90                 Domain domain = (Domain)domIter.next();
91                 boolean bApplicableDomain = false;
92                 String JavaDoc sFilePath = ((VersionedVirtualFile)vfFile).getLogicalPath();
93                 Iterator itor2 = domain.getPaths().iterator();
94                 while (itor2.hasNext()) {
95                     String JavaDoc sDomainPath = (String JavaDoc) itor2.next();
96                     if(sFilePath.startsWith(sDomainPath)) {
97                         bApplicableDomain=true;
98                     }
99                 }
100
101                 if(bApplicableDomain) {
102                     int nMaxOccurs = -1;
103                     int nMinOccurs = -1;
104                     
105                     if (nMinOccurs < domain.getMinOccurs()) {
106                         nMinOccurs = domain.getMinOccurs();
107                     }
108                     if (domain.getMaxOccurs()!=-1 && nMaxOccurs > domain.getMaxOccurs()) {
109                         nMaxOccurs = domain.getMaxOccurs();
110                     }
111                     
112                     if(nMinOccurs > -1) {
113                         bIsValid = nVals >= nMinOccurs;
114                     }
115                     
116                     if(bIsValid == true && nMaxOccurs > -1) {
117                         bIsValid = nVals <= nMaxOccurs;
118                     }
119                 }
120
121             }
122             
123             if(bIsValid == true) {
124                 VFSUser user = ServerList.getInstance().getHarmoniseServer().getVFS().getAuthentication().getUser();
125                 
126                 ValidationResult result = propInst.getDefinition()
127                                             .getRange().validate(vals);
128             
129                 bIsValid = result.isValid();
130                 
131                 if(propInst.getDefinition().getHREF().startsWith(HarmonisePaths.PATH_WORKFLOW_PROPS)) {
132                     ArrayList stages = new ArrayList();
133                     
134                     VFSWorkflowModel model = new VFSWorkflowModel("", propInst.getDefinition());
135                     Iterator itor = model.getWorkflowStages().iterator();
136                     while (itor.hasNext()) {
137                         VFSWorkflowStage stage = (VFSWorkflowStage) itor.next();
138                         
139                         Iterator itor2 = propInst.getValues().iterator();
140                         boolean bFoundVal = false;
141                         while (itor2.hasNext()) {
142                             ResourceValue tempValue = (ResourceValue) itor2.next();
143                             if(tempValue.getValue().equals(stage.getPath())) {
144                                 bFoundVal=true;
145                                 break;
146                             }
147                         }
148                         if(stage.isMandatory() && !bFoundVal) {
149                             bIsValid=false;
150                             break;
151                         }
152                     }
153                 } else if(propInst.getName().equals("related_events") ||
154                         propInst.getName().equals("Related Events")) {
155                     RelatedEventRangeDisplay display = new RelatedEventRangeDisplay(propInst);
156                     display.getPanel();
157                     if(!display.isMetadataValid()) {
158                         bIsValid=false;
159                     }
160                 }
161             }
162         }
163         
164         return bIsValid == m_bComparator;
165     }
166
167     /* (non-Javadoc)
168      * @see com.simulacramedia.contentmanager.actions.rules.EnableRule#setResultComparator(boolean)
169      */

170     public void setResultComparator(boolean bComparator) {
171         m_bComparator = bComparator;
172     }
173
174 }
175
Popular Tags