1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.batch;
17
18 import org.apache.commons.io.IOUtils;
19 import org.kuali.rice.core.config.ConfigurationException;
20 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
21 import org.kuali.rice.kew.service.KEWServiceLocator;
22 import org.springframework.core.io.DefaultResourceLoader;
23 import org.springframework.core.io.Resource;
24
25 import java.io.*;
26 import java.util.ArrayList;
27 import java.util.List;
28
29
30
31
32
33
34
35
36 public class KEWXmlDataLoader {
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public static void loadXmlResource(String resource) throws Exception {
51 Resource res = new DefaultResourceLoader().getResource(resource);
52 InputStream xmlFile = res.getInputStream();
53 if (xmlFile == null) {
54 throw new ConfigurationException("Didn't find resource " + resource);
55 }
56 try {
57 loadXmlStream(xmlFile);
58 } finally {
59 xmlFile.close();
60 }
61
62 }
63
64
65
66
67
68
69
70
71 public static void loadXmlClassLoaderResource(Class clazz, String path) throws Exception {
72 if (path.indexOf('/') < 0) {
73 loadXmlPackageResource(clazz, path);
74 } else {
75 loadXmlClassLoaderResource(clazz.getClassLoader(), path);
76 }
77 }
78
79
80
81
82
83
84
85 public static void loadXmlPackageResource(Class clazz, String path) throws Exception {
86 InputStream xmlFile = clazz.getResourceAsStream(path);
87 if (xmlFile == null) {
88 throw new WorkflowRuntimeException("Didn't find resource " + path);
89 }
90 try {
91 loadXmlStream(xmlFile);
92 } finally {
93 xmlFile.close();
94 }
95 }
96
97
98
99
100
101
102
103 public static void loadXmlClassLoaderResource(ClassLoader classloader, String path) throws Exception {
104 InputStream xmlFile = classloader.getResourceAsStream(path);
105 if (xmlFile == null) {
106 throw new WorkflowRuntimeException("Didn't find resource " + path);
107 }
108 try {
109 loadXmlStream(xmlFile);
110 } finally {
111 xmlFile.close();
112 }
113 }
114
115
116
117
118
119
120
121 public static void loadXmlFile(String fileName) throws Exception {
122 FileInputStream fis = new FileInputStream(fileName);
123 try {
124 loadXmlStream(fis);
125 } finally {
126 fis.close();
127 }
128 }
129
130
131
132
133
134
135 public static void loadXmlStream(InputStream xmlStream) throws Exception {
136 List<XmlDocCollection> xmlFiles = new ArrayList<XmlDocCollection>();
137 XmlDocCollection docCollection = getFileXmlDocCollection(xmlStream, "UnitTestTemp");
138
139 xmlFiles.add(docCollection);
140 KEWServiceLocator.getXmlIngesterService().ingest(xmlFiles);
141 for (XmlDoc doc: docCollection.getXmlDocs()) {
142 if (!doc.isProcessed()) {
143 throw new RuntimeException("Failed to ingest xml doc: " + doc.getName());
144 }
145 }
146 }
147
148
149
150
151
152
153
154
155 public static FileXmlDocCollection getFileXmlDocCollection(InputStream stream, String tempFileName) throws IOException {
156 if (stream == null) {
157 throw new RuntimeException("Stream is null!");
158 }
159
160 File temp = File.createTempFile(tempFileName, ".xml");
161 temp.deleteOnExit();
162
163 FileOutputStream fos = new FileOutputStream(temp);
164 try {
165 IOUtils.copy(stream, fos);
166 } finally {
167 fos.close();
168 }
169
170 return new FileXmlDocCollection(temp);
171 }
172 }