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