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.xml.export;
018:
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.apache.commons.lang.StringUtils;
023: import org.jdom.Element;
024: import org.kuali.workflow.attribute.Extension;
025: import org.kuali.workflow.attribute.ExtensionData;
026:
027: import edu.iu.uis.eden.export.ExportDataSet;
028: import edu.iu.uis.eden.user.Recipient;
029: import edu.iu.uis.eden.user.WorkflowUser;
030: import edu.iu.uis.eden.util.Utilities;
031: import edu.iu.uis.eden.workgroup.Workgroup;
032: import edu.iu.uis.eden.xml.WorkgroupXmlConstants;
033: import edu.iu.uis.eden.xml.XmlConstants;
034:
035: /**
036: * Exports {@link Workgroup}s to XML.
037: *
038: * @see Workgroup
039: *
040: * @author ewestfal
041: */
042: public class WorkgroupXmlExporter implements XmlExporter, XmlConstants,
043: WorkgroupXmlConstants {
044:
045: protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
046: .getLogger(getClass());
047:
048: private ExportRenderer renderer = new ExportRenderer(
049: WORKGROUP_NAMESPACE);
050:
051: public Element export(ExportDataSet dataSet) {
052: if (!dataSet.getWorkgroups().isEmpty()) {
053: Element rootElement = renderer.renderElement(null,
054: WORKGROUPS);
055: rootElement.setAttribute(SCHEMA_LOCATION_ATTR,
056: WORKGROUP_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
057: for (Iterator iterator = dataSet.getWorkgroups().iterator(); iterator
058: .hasNext();) {
059: Workgroup workgroup = (Workgroup) iterator.next();
060: exportWorkgroup(rootElement, workgroup);
061: }
062: return rootElement;
063: }
064: return null;
065: }
066:
067: private void exportWorkgroup(Element parent, Workgroup workgroup) {
068: Element workgroupElement = renderer.renderElement(parent,
069: WORKGROUP);
070: renderer.renderTextElement(workgroupElement, WORKGROUP_NAME,
071: workgroup.getGroupNameId().getNameId());
072: renderer.renderTextElement(workgroupElement, DESCRIPTION,
073: workgroup.getDescription());
074: if (!StringUtils.isBlank(workgroup.getWorkgroupType())) {
075: renderer.renderTextElement(workgroupElement,
076: WORKGROUP_TYPE, workgroup.getWorkgroupType());
077: }
078: if (!workgroup.getActiveInd().booleanValue()) {
079: renderer.renderBooleanElement(workgroupElement, ACTIVE_IND,
080: workgroup.getActiveInd(), true);
081: }
082: exportExtensions(workgroupElement, workgroup.getExtensions());
083: exportMembers(workgroupElement, workgroup.getMembers());
084: }
085:
086: private void exportExtensions(Element parent,
087: List<Extension> extensions) {
088: if (extensions != null && !extensions.isEmpty()) {
089: Element extensionsElement = renderer.renderElement(parent,
090: EXTENSIONS);
091: for (Extension extension : extensions) {
092: Element extensionElement = renderer.renderElement(
093: extensionsElement, EXTENSION);
094: extensionElement.setAttribute(ATTRIBUTE, extension
095: .getAttributeName());
096: if (extension.getData() != null
097: && !extension.getData().isEmpty()) {
098: for (ExtensionData extensionData : extension
099: .getData()) {
100: Element dataElement = renderer.renderElement(
101: extensionElement, DATA);
102: if (!Utilities
103: .isEmpty(extensionData.getValue())) {
104: dataElement.setText(extensionData
105: .getValue());
106: }
107: // Element dataElement = renderer.renderTextElement(extensionElement, DATA, extensionData.getValue());
108: dataElement.setAttribute(KEY, extensionData
109: .getKey());
110: }
111: }
112: }
113: }
114: }
115:
116: private void exportMembers(Element parent, List<Recipient> members) {
117: if (!members.isEmpty()) {
118: Element membersElement = renderer.renderElement(parent,
119: MEMBERS);
120: for (Recipient member : members) {
121: if (member instanceof WorkflowUser) {
122: WorkflowUser user = (WorkflowUser) member;
123: renderer.renderTextElement(membersElement,
124: AUTHENTICATION_ID, user
125: .getAuthenticationUserId().getId());
126: } else if (member instanceof Workgroup) {
127: Workgroup workgroup = (Workgroup) member;
128: renderer.renderTextElement(membersElement,
129: WORKGROUP_NAME, workgroup.getGroupNameId()
130: .getNameId());
131: }
132: }
133: }
134: }
135:
136: }
|