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 org.kuali.workflow.workgroup;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Vector;
025:
026: import javax.xml.parsers.ParserConfigurationException;
027:
028: import org.jdom.Document;
029: import org.jdom.Element;
030: import org.jdom.JDOMException;
031: import org.xml.sax.SAXException;
032:
033: import edu.iu.uis.eden.KEWServiceLocator;
034: import edu.iu.uis.eden.exception.InvalidXmlException;
035: import edu.iu.uis.eden.routetemplate.RuleAttribute;
036: import edu.iu.uis.eden.util.Utilities;
037: import edu.iu.uis.eden.util.XmlHelper;
038: import edu.iu.uis.eden.xml.XmlConstants;
039:
040: /**
041: * Parses {@link WorkgroupType}s from XML.
042: *
043: * @see WorkgroupType
044: *
045: * @author ewestfal
046: */
047: public class WorkgroupTypeXmlParser implements XmlConstants {
048:
049: private WorkgroupTypeService workgroupTypeService;
050:
051: public List<WorkgroupType> parseWorkgroupTypes(InputStream input)
052: throws IOException, InvalidXmlException {
053:
054: try {
055: Document doc = XmlHelper.trimSAXXml(input);
056: Element root = doc.getRootElement();
057: return parseWorkgroupTypes(root);
058: } catch (JDOMException e) {
059: throw new InvalidXmlException("Parse error.", e);
060: } catch (SAXException e) {
061: throw new InvalidXmlException("Parse error.", e);
062: } catch (ParserConfigurationException e) {
063: throw new InvalidXmlException("Parse error.", e);
064: }
065: }
066:
067: public List<WorkgroupType> parseWorkgroupTypes(Element element)
068: throws InvalidXmlException {
069: List<WorkgroupType> workgroupTypes = new ArrayList<WorkgroupType>();
070:
071: Vector workgroupTypesElements = XmlHelper.findElements(element,
072: WORKGROUP_TYPES);
073: for (Iterator iterator = workgroupTypesElements.iterator(); iterator
074: .hasNext();) {
075: Element workgroupTypesElement = (Element) iterator.next();
076: Vector workgroupTypeElements = XmlHelper.findElements(
077: workgroupTypesElement, WORKGROUP_TYPE);
078: for (Iterator typeIt = workgroupTypeElements.iterator(); typeIt
079: .hasNext();) {
080: workgroupTypes.add(parseWorkgroupType((Element) typeIt
081: .next()));
082: }
083: }
084: return workgroupTypes;
085: }
086:
087: private WorkgroupType parseWorkgroupType(Element element)
088: throws InvalidXmlException {
089: String name = element.getChildText(NAME,
090: WORKGROUP_TYPE_NAMESPACE);
091: String label = element.getChildText(LABEL,
092: WORKGROUP_TYPE_NAMESPACE);
093: String description = element.getChildText(DESCRIPTION,
094: WORKGROUP_TYPE_NAMESPACE);
095: String documentTypeName = element.getChildText(DOCUMENT_TYPE,
096: WORKGROUP_TYPE_NAMESPACE);
097: if (Utilities.isEmpty(name)) {
098: throw new InvalidXmlException(
099: "WorkgroupType must have a name");
100: }
101: if (Utilities.isEmpty(label)) {
102: label = name;
103: }
104: WorkgroupType workgroupType = new WorkgroupType();
105: WorkgroupType existingType = workgroupTypeService
106: .findByName(name);
107: if (existingType != null) {
108: workgroupType = existingType;
109: }
110: workgroupType.setName(name);
111: workgroupType.setLabel(label);
112: workgroupType.setDescription(description);
113: workgroupType.setDocumentTypeName(documentTypeName);
114:
115: Element attributesElement = element.getChild(ATTRIBUTES,
116: WORKGROUP_TYPE_NAMESPACE);
117: if (attributesElement != null) {
118: List<WorkgroupTypeAttribute> attributes = parseAttributes(
119: attributesElement, workgroupType);
120: updateAttributes(workgroupType, attributes);
121: }
122:
123: return workgroupType;
124: }
125:
126: private List<WorkgroupTypeAttribute> parseAttributes(
127: Element element, WorkgroupType workgroupType)
128: throws InvalidXmlException {
129: List<WorkgroupTypeAttribute> attributes = new ArrayList<WorkgroupTypeAttribute>();
130: Vector attributeElements = XmlHelper.findElements(element,
131: ATTRIBUTE);
132: for (Iterator iterator = attributeElements.iterator(); iterator
133: .hasNext();) {
134: attributes.add(parseAttribute((Element) iterator.next(),
135: workgroupType));
136: }
137: return attributes;
138: }
139:
140: private WorkgroupTypeAttribute parseAttribute(Element element,
141: WorkgroupType workgroupType) throws InvalidXmlException {
142: String attributeName = element.getChildText(NAME,
143: WORKGROUP_TYPE_NAMESPACE);
144: if (Utilities.isEmpty(attributeName)) {
145: throw new InvalidXmlException(
146: "Attribute name must be non-empty on for Workgroup Type '"
147: + workgroupType.getName() + "'");
148: }
149: RuleAttribute ruleAttribute = KEWServiceLocator
150: .getRuleAttributeService().findByName(attributeName);
151: if (ruleAttribute == null) {
152: throw new InvalidXmlException(
153: "Could not locate rule attribute for name '"
154: + attributeName + "'");
155: }
156: WorkgroupTypeAttribute attribute = new WorkgroupTypeAttribute();
157: attribute.setAttribute(ruleAttribute);
158: attribute.setWorkgroupType(workgroupType);
159: return attribute;
160: }
161:
162: protected void updateAttributes(WorkgroupType workgroupType,
163: List<WorkgroupTypeAttribute> newAttributes) {
164: List<WorkgroupTypeAttribute> activeAttributes = new ArrayList<WorkgroupTypeAttribute>();
165: List<WorkgroupTypeAttribute> inactiveAttributes = new ArrayList<WorkgroupTypeAttribute>();
166: for (WorkgroupTypeAttribute newAttribute : newAttributes) {
167: // determine if this is an existing attribute
168: boolean isExistingAttribute = false;
169: for (WorkgroupTypeAttribute existingAttribute : workgroupType
170: .getAttributes()) {
171: if (existingAttribute.getAttribute().getName().equals(
172: newAttribute.getAttribute().getName())) {
173: existingAttribute.setActive(true);
174: activeAttributes.add(existingAttribute);
175: isExistingAttribute = true;
176: }
177: }
178: if (!isExistingAttribute) {
179: newAttribute.setActive(true);
180: activeAttributes.add(newAttribute);
181: }
182: }
183: // find attributes that are no longer on the workgroup type and mark them as inactive
184: outer: for (WorkgroupTypeAttribute existingAttribute : workgroupType
185: .getAttributes()) {
186: for (WorkgroupTypeAttribute activeAttribute : activeAttributes) {
187: if (existingAttribute.getAttribute().getName().equals(
188: activeAttribute.getAttribute().getName())) {
189: continue outer;
190: }
191: }
192: existingAttribute.setActive(false);
193: inactiveAttributes.add(existingAttribute);
194: }
195:
196: // clear out the current attributes and add the new ones
197: workgroupType.getAttributes().clear();
198: workgroupType.getAttributes().addAll(activeAttributes);
199: workgroupType.getAttributes().addAll(inactiveAttributes);
200: int orderIndex = 0;
201: for (WorkgroupTypeAttribute attribute : workgroupType
202: .getAttributes()) {
203: attribute.setOrderIndex(orderIndex++);
204: }
205: }
206:
207: public WorkgroupTypeService getWorkgroupTypeService() {
208: return workgroupTypeService;
209: }
210:
211: public void setWorkgroupTypeService(
212: WorkgroupTypeService workgroupTypeService) {
213: this.workgroupTypeService = workgroupTypeService;
214: }
215:
216: }
|