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