001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.routetemplate;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.jdom.Document;
025: import org.jdom.Element;
026:
027: import edu.iu.uis.eden.KEWServiceLocator;
028: import edu.iu.uis.eden.WorkflowServiceErrorImpl;
029: import edu.iu.uis.eden.doctype.DocumentType;
030: import edu.iu.uis.eden.doctype.DocumentTypeService;
031: import edu.iu.uis.eden.lookupable.Field;
032: import edu.iu.uis.eden.lookupable.Row;
033: import edu.iu.uis.eden.plugin.attributes.WorkflowAttribute;
034: import edu.iu.uis.eden.routeheader.DocumentContent;
035: import edu.iu.uis.eden.util.Utilities;
036: import edu.iu.uis.eden.util.XmlHelper;
037:
038: /**
039: * A {@link WorkflowAttribute} which is used to route a rule based on the
040: * {@link DocumentType} of the rule which is created.
041: *
042: * @author jhopf
043: */
044: public class RuleRoutingAttribute implements WorkflowAttribute {
045:
046: private static final long serialVersionUID = -8884711461398770563L;
047:
048: private static final String DOC_TYPE_NAME_PROPERTY = "doc_type_name";
049: private static final String DOC_TYPE_NAME_KEY = "docTypeFullName";
050:
051: private static final String LOOKUPABLE_CLASS = "DocumentTypeLookupableImplService";
052: private static final String DOC_TYPE_NAME_LABEL = "Document type name";
053:
054: private String doctypeName;
055: private List rows;
056: private boolean required;
057:
058: public RuleRoutingAttribute(String docTypeName) {
059: this ();
060: setDoctypeName(docTypeName);
061: }
062:
063: public RuleRoutingAttribute() {
064: buildRows();
065: }
066:
067: private void buildRows() {
068: rows = new ArrayList();
069:
070: List fields = new ArrayList();
071: fields.add(new Field(DOC_TYPE_NAME_LABEL, "", Field.TEXT, true,
072: DOC_TYPE_NAME_PROPERTY, "", null, LOOKUPABLE_CLASS,
073: DOC_TYPE_NAME_KEY));
074: fields.add(new Field("", "", Field.QUICKFINDER, false, "", "",
075: null, LOOKUPABLE_CLASS));
076: rows.add(new Row(fields));
077: }
078:
079: public boolean isMatch(DocumentContent docContent,
080: List ruleExtensions) {
081: for (Iterator extensionsIterator = ruleExtensions.iterator(); extensionsIterator
082: .hasNext();) {
083: RuleExtension extension = (RuleExtension) extensionsIterator
084: .next();
085: if (extension.getRuleTemplateAttribute().getRuleAttribute()
086: .getClassName().equals(getClass().getName())) {
087: for (Iterator valuesIterator = extension
088: .getExtensionValues().iterator(); valuesIterator
089: .hasNext();) {
090: RuleExtensionValue extensionValue = (RuleExtensionValue) valuesIterator
091: .next();
092: String key = extensionValue.getKey();
093: String value = extensionValue.getValue();
094: if (key.equals(DOC_TYPE_NAME_KEY)) {
095: setDoctypeName(value);
096: }
097: }
098: }
099: }
100: DocumentTypeService service = (DocumentTypeService) KEWServiceLocator
101: .getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE);
102: List documentTypeValues = parseDocContent(docContent);
103: for (Iterator iterator = documentTypeValues.iterator(); iterator
104: .hasNext();) {
105: RuleRoutingAttribute attribute = (RuleRoutingAttribute) iterator
106: .next();
107: if (attribute.getDoctypeName().equals(getDoctypeName())) {
108: return true;
109: }
110: DocumentType documentType = service.findByName(attribute
111: .getDoctypeName());
112: while (documentType != null
113: && documentType.getParentDocType() != null) {
114: documentType = documentType.getParentDocType();
115: if (documentType.getName().equals(getDoctypeName())) {
116: return true;
117: }
118: }
119: }
120:
121: if (ruleExtensions.isEmpty()) {
122: return true;
123: }
124:
125: return false;
126: }
127:
128: public List getRuleRows() {
129: return rows;
130: }
131:
132: public List getRoutingDataRows() {
133: return rows;
134: }
135:
136: public String getDocContent() {
137: if (!Utilities.isEmpty(getDoctypeName())) {
138: return "<ruleRouting><doctype>" + getDoctypeName()
139: + "</doctype></ruleRouting>";
140: } else {
141: return "";
142: }
143: }
144:
145: public List parseDocContent(DocumentContent docContent) {
146: try {
147: Document doc = XmlHelper.buildJDocument(docContent
148: .getDocument());
149:
150: List doctypeAttributes = new ArrayList();
151: List ruleRoutings = XmlHelper.findElements(doc
152: .getRootElement(), "ruleRouting");
153: for (Iterator iter = ruleRoutings.iterator(); iter
154: .hasNext();) {
155: Element ruleRoutingElement = (Element) iter.next();
156:
157: Element docTypeElement = ruleRoutingElement
158: .getChild("doctype");
159: if (docTypeElement != null) {
160: doctypeAttributes.add(new RuleRoutingAttribute(
161: docTypeElement.getText()));
162: }
163: }
164:
165: return doctypeAttributes;
166: } catch (Exception e) {
167: throw new RuntimeException(e);
168: }
169: }
170:
171: public List getRuleExtensionValues() {
172: List extensions = new ArrayList();
173:
174: if (!Utilities.isEmpty(getDoctypeName())) {
175: RuleExtensionValue extension = new RuleExtensionValue();
176: extension.setKey(DOC_TYPE_NAME_KEY);
177: extension.setValue(getDoctypeName());
178: extensions.add(extension);
179: }
180:
181: return extensions;
182: }
183:
184: public List validateRoutingData(Map paramMap) {
185: List errors = new ArrayList();
186: setDoctypeName((String) paramMap.get(DOC_TYPE_NAME_PROPERTY));
187: if (isRequired() && Utilities.isEmpty(getDoctypeName())) {
188: errors
189: .add(new WorkflowServiceErrorImpl(
190: "doc type is not valid.",
191: "routetemplate.ruleroutingattribute.doctype.invalid"));
192: }
193:
194: if (!Utilities.isEmpty(getDoctypeName())) {
195: DocumentTypeService service = (DocumentTypeService) KEWServiceLocator
196: .getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE);
197: DocumentType documentType = service
198: .findByName(getDoctypeName());
199: if (documentType == null) {
200: errors
201: .add(new WorkflowServiceErrorImpl(
202: "doc type is not valid",
203: "routetemplate.ruleroutingattribute.doctype.invalid"));
204: }
205: }
206: return errors;
207: }
208:
209: public List validateRuleData(Map paramMap) {
210: return validateRoutingData(paramMap);
211: }
212:
213: public String getDoctypeName() {
214: return this .doctypeName;
215: }
216:
217: public void setDoctypeName(String docTypeName) {
218: this .doctypeName = docTypeName;
219: }
220:
221: public void setRequired(boolean required) {
222: this .required = required;
223: }
224:
225: public boolean isRequired() {
226: return required;
227: }
228: }
|