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.batch;
017
018import org.apache.commons.io.IOUtils;
019import org.kuali.rice.core.api.CoreApiServiceLocator;
020import org.kuali.rice.core.api.config.ConfigurationException;
021import org.kuali.rice.core.api.impex.xml.FileXmlDocCollection;
022import org.kuali.rice.core.api.impex.xml.XmlDoc;
023import org.kuali.rice.core.api.impex.xml.XmlDocCollection;
024import org.kuali.rice.kew.api.WorkflowRuntimeException;
025import org.springframework.core.io.DefaultResourceLoader;
026import org.springframework.core.io.Resource;
027
028import java.io.File;
029import java.io.FileInputStream;
030import java.io.FileOutputStream;
031import java.io.IOException;
032import java.io.InputStream;
033import java.util.ArrayList;
034import java.util.List;
035
036
037/**
038 * This is a description of what this class does - arh14 don't forget to fill this in. 
039 * 
040 * @author Kuali Rice Team (rice.collab@kuali.org)
041 *
042 */
043public class KEWXmlDataLoader {
044    /*protected void loadXmlFile(String fileName) {
045        if (fileName.indexOf('/') < 0) {
046            this.loadXmlFile(getClass(), fileName);
047        } else {
048            loadXmlStream(getClass().getClassLoader().getResourceAsStream(fileName));
049        }
050    }*/
051
052    /**
053     * Loads the XML specified by the resource string, which should be in Spring resource notation
054     * @param resource resource string in Spring resource notation
055     * @throws Exception 
056     */
057    public static void loadXmlResource(String resource) throws Exception {
058        Resource res = new DefaultResourceLoader().getResource(resource);
059        InputStream xmlFile = res.getInputStream();
060        if (xmlFile == null) {
061            throw new ConfigurationException("Didn't find resource " + resource);
062        }
063        try {
064            loadXmlStream(xmlFile);
065        } finally {
066            xmlFile.close();
067        }
068
069    }
070
071    /**
072     * Loads the XML resource from the classloader, from the package of the specified class
073     * if the class appears relative, or from the root of the classloader if it contains a slash
074     * @param clazz the class whose package should be used to qualify the path
075     * @param path the package-relative path of the XML resource
076     * @throws Exception
077     */
078    public static void loadXmlClassLoaderResource(Class clazz, String path) throws Exception {
079        if (path.indexOf('/') < 0) {
080            loadXmlPackageResource(clazz, path);
081        } else {
082            loadXmlClassLoaderResource(clazz.getClassLoader(), path);
083        }
084    }
085
086    /**
087     * Loads the XML resource from the classloader, from the package of the specified class.
088     * @param clazz the class whose package should be used to qualify the path
089     * @param path the package-relative path of the XML resource
090     * @throws Exception
091     */
092    public static void loadXmlPackageResource(Class clazz, String path) throws Exception {
093        InputStream xmlFile = clazz.getResourceAsStream(path);
094        if (xmlFile == null) {
095            throw new WorkflowRuntimeException("Didn't find resource " + path);
096        }
097        try {
098            loadXmlStream(xmlFile);
099        } finally {
100            xmlFile.close();
101        }
102    }
103
104    /**
105     * Loads the XML resource from the specified classloader
106     * @param classloader the classloader from which to load the resource
107     * @param path the classloader path of the XML resource
108     * @throws Exception
109     */
110    public static void loadXmlClassLoaderResource(ClassLoader classloader, String path) throws Exception {
111        InputStream xmlFile = classloader.getResourceAsStream(path);
112        if (xmlFile == null) {
113            throw new WorkflowRuntimeException("Didn't find resource " + path);
114        }
115        try {
116            loadXmlStream(xmlFile);
117        } finally {
118            xmlFile.close();
119        }
120    }
121
122    /**
123     * Load the XML file from the file system.
124     * 
125     * @param fileName the path to the XML file
126     * @throws Exception
127     */
128    public static void loadXmlFile(String fileName) throws Exception {
129        FileInputStream fis = new FileInputStream(fileName);
130        try {
131            loadXmlStream(fis);
132        } finally {
133            fis.close();
134        }
135    }
136
137    /**
138     * Loads XML from a stream
139     * @param xmlStream the XML byte stream
140     * @throws Exception
141     */
142    public static void loadXmlStream(InputStream xmlStream) throws Exception {
143       List<XmlDocCollection> xmlFiles = new ArrayList<XmlDocCollection>();
144        XmlDocCollection docCollection = getFileXmlDocCollection(xmlStream, "UnitTestTemp");
145        //XmlDocCollection docCollection = new StreamXmlDocCollection(xmlStream);
146        xmlFiles.add(docCollection);
147        CoreApiServiceLocator.getXmlIngesterService().ingest(xmlFiles);
148        for (XmlDoc doc: docCollection.getXmlDocs()) {
149            if (!doc.isProcessed()) {
150                throw new RuntimeException("Failed to ingest xml doc: " + doc.getName());
151            }
152        }
153    }
154
155    /**
156     * Helper method that turns a stream into a FileXmlDocCollection by first making a copy on the file system.
157     * @param xmlFile
158     * @param tempFileName
159     * @return
160     * @throws IOException
161     */
162    public static FileXmlDocCollection getFileXmlDocCollection(InputStream stream, String tempFileName) throws IOException {
163        if (stream == null) {
164            throw new RuntimeException("Stream is null!");
165        }
166
167        File temp = File.createTempFile(tempFileName, ".xml");
168        temp.deleteOnExit();
169
170        FileOutputStream fos = new FileOutputStream(temp);
171        try {
172            IOUtils.copy(stream, fos);
173        } finally {
174            fos.close();
175        }
176
177        return new FileXmlDocCollection(temp);
178    }
179}