001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.test.lifecycles;
017:
018: import java.io.File;
019: import java.io.FileOutputStream;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.kuali.rice.config.ConfigurationException;
027: import org.kuali.rice.core.Core;
028: import org.kuali.rice.lifecycle.Lifecycle;
029: import org.springframework.core.io.DefaultResourceLoader;
030: import org.springframework.core.io.Resource;
031:
032: import edu.iu.uis.eden.KEWServiceLocator;
033: import edu.iu.uis.eden.batch.FileXmlDocCollection;
034: import edu.iu.uis.eden.batch.XmlDoc;
035: import edu.iu.uis.eden.batch.XmlDocCollection;
036:
037: /**
038: * A lifecycle for loading KEW XML datasets.
039: *
040: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
041: */
042: public class KEWXmlDataLoaderLifecycle implements Lifecycle {
043:
044: private boolean started;
045:
046: private String filename;
047:
048: public KEWXmlDataLoaderLifecycle() {
049: this ("classpath:DefaultTestData.xml");
050: }
051:
052: public KEWXmlDataLoaderLifecycle(String filename) {
053: this .filename = filename;
054: }
055:
056: public boolean isStarted() {
057: return started;
058: }
059:
060: public void start() throws Exception {
061: if (new Boolean(Core.getCurrentContextConfig().getProperty(
062: "use.kewXmlmlDataLoaderLifecycle"))) {
063: loadDefaultTestData();
064: started = true;
065: }
066: }
067:
068: public void stop() throws Exception {
069: started = false;
070: }
071:
072: /**
073: * By default this loads the "default" data set from the DefaultTestData.sql
074: * and DefaultTestData.xml files. Subclasses can override this to change
075: * this behaviour or pass in a filename to the constructor
076: */
077: protected void loadDefaultTestData() throws Exception {
078: this .loadXmlFile(filename);
079: }
080:
081: protected void loadXmlFile(String fileName) throws Exception {
082: Resource resource = new DefaultResourceLoader()
083: .getResource(fileName);
084: InputStream xmlFile = resource.getInputStream();
085: if (xmlFile == null) {
086: throw new ConfigurationException("Didn't find file "
087: + fileName);
088: }
089: List<XmlDocCollection> xmlFiles = new ArrayList<XmlDocCollection>();
090: XmlDocCollection docCollection = getFileXmlDocCollection(
091: xmlFile, "UnitTestTemp");
092: xmlFiles.add(docCollection);
093: KEWServiceLocator.getXmlIngesterService().ingest(xmlFiles);
094: for (Iterator iterator = docCollection.getXmlDocs().iterator(); iterator
095: .hasNext();) {
096: XmlDoc doc = (XmlDoc) iterator.next();
097: if (!doc.isProcessed()) {
098: throw new RuntimeException("Failed to ingest xml doc: "
099: + doc.getName());
100: }
101: }
102: }
103:
104: protected FileXmlDocCollection getFileXmlDocCollection(
105: InputStream xmlFile, String tempFileName)
106: throws IOException {
107: if (xmlFile == null) {
108: throw new RuntimeException("Didn't find the xml file "
109: + tempFileName);
110: }
111: File temp = File.createTempFile(tempFileName, ".xml");
112: FileOutputStream fos = new FileOutputStream(temp);
113: int data = -1;
114: while ((data = xmlFile.read()) != -1) {
115: fos.write(data);
116: }
117: fos.close();
118: return new FileXmlDocCollection(temp);
119: }
120:
121: }
|