001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.xml.export;
017    
018    import org.jdom.Element;
019    import org.kuali.rice.core.api.impex.ExportDataSet;
020    import org.kuali.rice.core.api.util.xml.XmlRenderer;
021    import org.kuali.rice.core.framework.impex.xml.XmlExporter;
022    import org.kuali.rice.kew.export.KewExportDataSet;
023    import org.kuali.rice.kim.api.group.Group;
024    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
025    import org.kuali.rice.kim.api.type.KimType;
026    
027    import java.util.Iterator;
028    
029    import static org.kuali.rice.core.api.impex.xml.XmlConstants.*;
030    
031    /**
032     * This is a description of what this class does - jjhanso don't forget to fill this in.
033     *
034     * @author Kuali Rice Team (rice.collab@kuali.org)
035     *
036     */
037    public class GroupXmlExporter implements XmlExporter {
038        protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
039    
040        private XmlRenderer renderer = new XmlRenderer(GROUP_NAMESPACE);
041    
042            @Override
043            public boolean supportPrettyPrint() {
044                    return true;
045            }
046        
047        /**
048         * This overridden method ...
049         *
050         * @see org.kuali.rice.core.framework.impex.xml.XmlExporter#export(org.kuali.rice.core.api.impex.ExportDataSet)
051         */
052        public Element export(ExportDataSet exportDataSet) {
053            KewExportDataSet dataSet = KewExportDataSet.fromExportDataSet(exportDataSet);
054            if (!dataSet.getGroups().isEmpty()) {
055                Element rootElement = renderer.renderElement(null, GROUPS);
056                rootElement.setAttribute(SCHEMA_LOCATION_ATTR, GROUP_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
057                for (Iterator iterator = dataSet.getGroups().iterator(); iterator.hasNext();) {
058                    Group group = (Group) iterator.next();
059                    exportGroup(rootElement, group);
060                }
061                return rootElement;
062            }
063            // TODO jjhanso - THIS METHOD NEEDS JAVADOCS
064            return null;
065        }
066    
067        /**
068         * This method ...
069         *
070         * @param rootElement
071         * @param group
072         * @param object
073         */
074        private void exportGroup(Element parent, Group group) {
075            Element groupElement = renderer.renderElement(parent, GROUP);
076            if (group.getName() != null) {
077                renderer.renderTextElement(groupElement, NAME, group.getName());
078            }
079            if (group.getNamespaceCode() != null) {
080                renderer.renderTextElement(groupElement, NAMESPACE, group.getNamespaceCode());
081            }
082    
083            if (group.getDescription() != null && !group.getDescription().trim().equals("")) {
084                renderer.renderTextElement(groupElement, DESCRIPTION, group.getDescription());
085            }
086    
087            renderer.renderTextElement(groupElement, ACTIVE, new Boolean(group.isActive()).toString());
088    
089            if (group.getKimTypeId() != null) {
090                Element typeElement = renderer.renderElement(groupElement, TYPE);
091                KimType kimType = KimApiServiceLocator.getKimTypeInfoService().getKimType(group.getKimTypeId());
092                renderer.renderTextElement(typeElement, NAMESPACE, kimType.getNamespaceCode());
093                renderer.renderTextElement(typeElement, NAME, kimType.getName());
094            }
095    
096            if (group.getAttributes().size() > 0) {
097                Element attributesElement = renderer.renderElement(groupElement, ATTRIBUTES);
098                for (String key : group.getAttributes().keySet()) {
099                    Element attributeElement = renderer.renderElement(attributesElement, ATTRIBUTE);
100                    attributeElement.setAttribute(KEY, key);
101                    attributeElement.setAttribute(VALUE, group.getAttributes().get(key));
102                }
103            }
104    
105            java.util.List<String> memberGroupIds = KimApiServiceLocator.getGroupService().getDirectMemberGroupIds(group.getId());
106    
107            java.util.List<String> memberPrincipalIds = KimApiServiceLocator.getGroupService().getDirectMemberPrincipalIds(
108                    group.getId());
109    
110            if (memberGroupIds.size() > 0 || memberPrincipalIds.size() > 0) {
111                Element membersElement = renderer.renderElement(groupElement, MEMBERS);
112                for (String memberGroupId : memberGroupIds) {
113                    Group memberGroup = KimApiServiceLocator.getGroupService().getGroup(memberGroupId);
114                    Element groupNameElement = renderer.renderElement(membersElement, GROUP_NAME);
115                    renderer.renderTextElement(groupNameElement, NAME, memberGroup.getName());
116                    renderer.renderTextElement(groupNameElement, NAMESPACE, memberGroup.getNamespaceCode());
117                }
118                for (String memberPrincipalId : memberPrincipalIds) {
119                    renderer.renderTextElement(membersElement, PRINCIPAL_NAME, KimApiServiceLocator.getIdentityService().getPrincipal(memberPrincipalId).getPrincipalName());
120                }
121            }
122        }
123    
124    }