001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kew.export;
017
018import org.kuali.rice.core.api.CoreApiServiceLocator;
019import org.kuali.rice.core.api.impex.ExportDataSet;
020import org.kuali.rice.kew.doctype.bo.DocumentType;
021import org.kuali.rice.kew.rule.RuleBaseValues;
022import org.kuali.rice.kew.rule.RuleDelegationBo;
023import org.kuali.rice.kew.rule.bo.RuleAttribute;
024import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
025import org.kuali.rice.kim.api.group.Group;
026import org.kuali.rice.kim.impl.group.GroupBo;
027import org.kuali.rice.krad.bo.Exporter;
028import org.kuali.rice.krad.exception.ExportNotSupportedException;
029import org.kuali.rice.krad.util.KRADConstants;
030
031import java.io.IOException;
032import java.io.OutputStream;
033import java.util.ArrayList;
034import java.util.List;
035
036/**
037 * The DataExporter allows for exporting of KEW BusinessObjects to various
038 * supported formats. The current implementation supports only XML export. This
039 * process is initiated from the KNS screens (lookups and inquiries) and this
040 * implementation leverages the existing XmlExporterService which is part of KEW
041 * and which was used to do exports before KEW was converted to use the KNS.
042 * 
043 * @author Kuali Rice Team (rice.collab@kuali.org)
044 */
045public class DataExporter implements Exporter {
046
047        private List<String> supportedFormats = new ArrayList<String>();
048
049        public DataExporter() {
050                supportedFormats.add(KRADConstants.XML_FORMAT);
051        }
052
053        /**
054         * @see org.kuali.rice.krad.bo.Exporter#getSupportedFormats(java.lang.Class)
055         */
056        @Override
057        public List<String> getSupportedFormats(Class<?> dataObjectClass) {
058                return supportedFormats;
059        }
060
061        /**
062         * Builds the ExportDataSet based on the BusinessObjects passed in.
063         */
064        protected ExportDataSet buildExportDataSet(Class<?> dataObjectClass, List<? extends Object> dataObjects) {
065                KewExportDataSet dataSet = new KewExportDataSet();
066                for (Object dataObject : dataObjects) {
067                        if (dataObjectClass.equals(RuleAttribute.class)) {
068                                dataSet.getRuleAttributes().add((RuleAttribute)dataObject);
069                        } else if (dataObjectClass.equals(RuleTemplateBo.class)) {
070                                dataSet.getRuleTemplates().add((RuleTemplateBo)dataObject);
071                        } else if (dataObjectClass.equals(DocumentType.class)) {
072                                dataSet.getDocumentTypes().add((DocumentType)dataObject);
073                        } else if (dataObjectClass.equals(RuleBaseValues.class)) {
074                                dataSet.getRules().add((RuleBaseValues)dataObject);
075                        } else if (dataObjectClass.equals(RuleDelegationBo.class)) {
076                                dataSet.getRuleDelegations().add((RuleDelegationBo)dataObject);
077                        } else if (dataObjectClass.equals(GroupBo.class)) {
078                                Group group = GroupBo.to((GroupBo)dataObject);
079                dataSet.getGroups().add(group);
080                        }
081                }
082
083                ExportDataSet exportDataSet = new ExportDataSet();
084                dataSet.populateExportDataSet(exportDataSet);
085                return exportDataSet;
086        }
087
088        /**
089         * Export the given List of Objects of the specified type to XML.
090         * 
091         * @see org.kuali.rice.krad.bo.Exporter#export(java.lang.Class, java.util.List, java.lang.String, java.io.OutputStream)
092         */
093        @Override
094        public void export(Class<?> dataObjectClass, List<? extends Object> dataObjects, String exportFormat, OutputStream outputStream) throws IOException,
095                        ExportNotSupportedException {
096                if (!KRADConstants.XML_FORMAT.equals(exportFormat)) {
097                        throw new ExportNotSupportedException("The given export format of "
098                                        + exportFormat
099                                        + " is not supported by the KEW XML Exporter!");
100                }
101                ExportDataSet dataSet = buildExportDataSet(dataObjectClass, dataObjects);
102                outputStream.write(CoreApiServiceLocator.getXmlExporterService()
103                                .export(dataSet));
104                outputStream.flush();
105                
106        }
107}