View Javadoc

1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.export;
17  
18  import java.io.IOException;
19  import java.io.OutputStream;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.kuali.rice.kew.doctype.bo.DocumentType;
24  import org.kuali.rice.kew.edl.bo.EDocLiteAssociation;
25  import org.kuali.rice.kew.edl.bo.EDocLiteStyle;
26  import org.kuali.rice.kew.help.HelpEntry;
27  import org.kuali.rice.kew.rule.RuleBaseValues;
28  import org.kuali.rice.kew.rule.RuleDelegation;
29  import org.kuali.rice.kew.rule.bo.RuleAttribute;
30  import org.kuali.rice.kew.rule.bo.RuleTemplate;
31  import org.kuali.rice.kew.service.KEWServiceLocator;
32  import org.kuali.rice.kim.bo.impl.GroupImpl;
33  import org.kuali.rice.kns.bo.BusinessObject;
34  import org.kuali.rice.kns.bo.Exporter;
35  import org.kuali.rice.kns.exception.ExportNotSupportedException;
36  import org.kuali.rice.kns.util.KNSConstants;
37  
38  /**
39   * The DataExporter allows for exporting of KEW BusinessObjects to various supported
40   * formats.  The current implementation supports only XML export.  This process is initiated
41   * from the KNS screens (lookups and inquiries) and this implementation leverages the
42   * existing XmlExporterService which is part of KEW and which was used to do exports before
43   * KEW was converted to use the KNS.
44   *
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   */
47  public class DataExporter implements Exporter {
48  
49  	private List<String> supportedFormats = new ArrayList<String>();
50  
51  	public DataExporter() {
52  		supportedFormats.add(KNSConstants.XML_FORMAT);
53  	}
54  
55  	/**
56  	 * Export the given List of BusinessObjects of the specified type to XML.
57  	 */
58  	public void export(Class<? extends BusinessObject> businessObjectClass, List<BusinessObject> businessObjects, String exportFormat, OutputStream outputStream) throws IOException {
59  		if (!KNSConstants.XML_FORMAT.equals(exportFormat)) {
60  			throw new ExportNotSupportedException("The given export format of " + exportFormat + " is not supported by the KEW XML Exporter!");
61  		}
62  		ExportDataSet dataSet = buildExportDataSet(businessObjectClass, businessObjects);
63  		outputStream.write(KEWServiceLocator.getXmlExporterService().export(dataSet));
64  		outputStream.flush();
65  	}
66  
67  	public List<String> getSupportedFormats(Class<? extends BusinessObject> businessObjectClass) {
68  		return supportedFormats;
69  	}
70  
71  	/**
72  	 * Builds the ExportDataSet based on the BusinessObjects passed in.
73  	 */
74  	protected ExportDataSet buildExportDataSet(Class<? extends BusinessObject> businessObjectClass, List<BusinessObject> businessObjects) {
75  		ExportDataSet dataSet = new ExportDataSet();
76  		for (BusinessObject businessObject : businessObjects) {
77  			if (businessObjectClass.equals(RuleAttribute.class)) {
78  				dataSet.getRuleAttributes().add((RuleAttribute)businessObject);
79  			} else if (businessObjectClass.equals(RuleTemplate.class)) {
80  				dataSet.getRuleTemplates().add((RuleTemplate)businessObject);
81  			} else if (businessObjectClass.equals(DocumentType.class)) {
82  				dataSet.getDocumentTypes().add((DocumentType)businessObject);
83  			} else if (businessObjectClass.equals(EDocLiteAssociation.class)) {
84  				dataSet.getEdocLites().add((EDocLiteAssociation)businessObject);
85  			} else if (businessObjectClass.equals(HelpEntry.class)) {
86  				dataSet.getHelp().add((HelpEntry)businessObject);
87  			} else if (businessObjectClass.equals(RuleBaseValues.class)) {
88  				dataSet.getRules().add((RuleBaseValues)businessObject);
89  			} else if (businessObjectClass.equals(RuleDelegation.class)) {
90  				dataSet.getRuleDelegations().add((RuleDelegation)businessObject);
91  			} else if (businessObjectClass.equals(EDocLiteStyle.class)) {
92  				dataSet.getStyles().add((EDocLiteStyle)businessObject);
93  			} else if (businessObjectClass.equals(GroupImpl.class)) {
94  				dataSet.getGroups().add((GroupImpl)businessObject);
95  			}    
96  		}
97  		return dataSet;
98  	}
99  
100 }