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.him.actions.rules;
020:
021: import java.util.*;
022:
023: import org.openharmonise.him.*;
024: import org.openharmonise.him.harmonise.*;
025: import org.openharmonise.him.metadata.range.swing.relatedevents.*;
026: import org.openharmonise.vfs.*;
027: import org.openharmonise.vfs.authentication.*;
028: import org.openharmonise.vfs.metadata.*;
029: import org.openharmonise.vfs.metadata.value.*;
030: import org.openharmonise.vfs.servers.ServerList;
031: import org.openharmonise.workfloweditor.vfs.*;
032:
033: /**
034: * Rule that will return true if all the metadata, for the currently
035: * selected virtual file, is valid.
036: *
037: * @author Michael Bell
038: * @version $Revision: 1.1 $
039: *
040: */
041: public class isMetadataValidRule implements EnableRule {
042:
043: boolean m_bComparator = true;
044:
045: /**
046: *
047: */
048: public isMetadataValidRule() {
049: super ();
050: // TODO Auto-generated constructor stub
051: }
052:
053: /* (non-Javadoc)
054: * @see com.simulacramedia.contentmanager.actions.rules.EnableRule#isEnabled(com.simulacramedia.vfs.VirtualFile)
055: */
056: public boolean isEnabled(VirtualFile vfFile) {
057: List props = vfFile.getProperties();
058:
059: Iterator iter = props.iterator();
060:
061: boolean bIsValid = true;
062:
063: while (iter.hasNext() && bIsValid == true) {
064: PropertyInstance propInst = (PropertyInstance) iter.next();
065:
066: List vals = propInst.getValues();
067: int nVals = 0;
068:
069: for (int i = 0; i < vals.size(); i++) {
070: ValueInstance tmpVal = (ValueInstance) vals.get(i);
071: if (tmpVal != null) {
072: if (tmpVal instanceof DateValue) {
073: String dtVal = ((DateValue) tmpVal).getValue();
074:
075: if (dtVal != null && dtVal.length() > 0) {
076: nVals++;
077: }
078: } else {
079: nVals++;
080: }
081:
082: }
083: }
084:
085: List domains = propInst.getDefinition().getDomains();
086: Iterator domIter = domains.iterator();
087:
088: while (domIter.hasNext() && bIsValid == true) {
089: Domain domain = (Domain) domIter.next();
090: boolean bApplicableDomain = false;
091: String sFilePath = ((VersionedVirtualFile) vfFile)
092: .getLogicalPath();
093: Iterator itor2 = domain.getPaths().iterator();
094: while (itor2.hasNext()) {
095: String sDomainPath = (String) itor2.next();
096: if (sFilePath.startsWith(sDomainPath)) {
097: bApplicableDomain = true;
098: }
099: }
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
109: && nMaxOccurs > domain.getMaxOccurs()) {
110: nMaxOccurs = domain.getMaxOccurs();
111: }
112:
113: if (nMinOccurs > -1) {
114: bIsValid = nVals >= nMinOccurs;
115: }
116:
117: if (bIsValid == true && nMaxOccurs > -1) {
118: bIsValid = nVals <= nMaxOccurs;
119: }
120: }
121:
122: }
123:
124: if (bIsValid == true) {
125: VFSUser user = ServerList.getInstance()
126: .getHarmoniseServer().getVFS()
127: .getAuthentication().getUser();
128:
129: ValidationResult result = propInst.getDefinition()
130: .getRange().validate(vals);
131:
132: bIsValid = result.isValid();
133:
134: if (propInst.getDefinition().getHREF().startsWith(
135: HarmonisePaths.PATH_WORKFLOW_PROPS)) {
136: ArrayList stages = new ArrayList();
137:
138: VFSWorkflowModel model = new VFSWorkflowModel("",
139: propInst.getDefinition());
140: Iterator itor = model.getWorkflowStages()
141: .iterator();
142: while (itor.hasNext()) {
143: VFSWorkflowStage stage = (VFSWorkflowStage) itor
144: .next();
145:
146: Iterator itor2 = propInst.getValues()
147: .iterator();
148: boolean bFoundVal = false;
149: while (itor2.hasNext()) {
150: ResourceValue tempValue = (ResourceValue) itor2
151: .next();
152: if (tempValue.getValue().equals(
153: stage.getPath())) {
154: bFoundVal = true;
155: break;
156: }
157: }
158: if (stage.isMandatory() && !bFoundVal) {
159: bIsValid = false;
160: break;
161: }
162: }
163: } else if (propInst.getName().equals("related_events")
164: || propInst.getName().equals("Related Events")) {
165: RelatedEventRangeDisplay display = new RelatedEventRangeDisplay(
166: propInst);
167: display.getPanel();
168: if (!display.isMetadataValid()) {
169: bIsValid = false;
170: }
171: }
172: }
173: }
174:
175: return bIsValid == m_bComparator;
176: }
177:
178: /* (non-Javadoc)
179: * @see com.simulacramedia.contentmanager.actions.rules.EnableRule#setResultComparator(boolean)
180: */
181: public void setResultComparator(boolean bComparator) {
182: m_bComparator = bComparator;
183: }
184:
185: }
|