1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package edu.sampleu.kew.krad.controller; |
17 | |
|
18 | |
import java.io.File; |
19 | |
import java.io.FileOutputStream; |
20 | |
import java.io.IOException; |
21 | |
import java.util.ArrayList; |
22 | |
import java.util.Collection; |
23 | |
import java.util.List; |
24 | |
|
25 | |
import javax.servlet.http.HttpServletRequest; |
26 | |
import javax.servlet.http.HttpServletResponse; |
27 | |
|
28 | |
import org.apache.commons.lang.StringUtils; |
29 | |
import org.apache.commons.lang.exception.ExceptionUtils; |
30 | |
import org.kuali.rice.core.api.CoreApiServiceLocator; |
31 | |
import org.kuali.rice.core.api.impex.xml.CompositeXmlDocCollection; |
32 | |
import org.kuali.rice.core.api.impex.xml.FileXmlDocCollection; |
33 | |
import org.kuali.rice.core.api.impex.xml.XmlDoc; |
34 | |
import org.kuali.rice.core.api.impex.xml.XmlDocCollection; |
35 | |
import org.kuali.rice.core.api.impex.xml.ZipXmlDocCollection; |
36 | |
import org.kuali.rice.krad.util.GlobalVariables; |
37 | |
import org.kuali.rice.krad.web.controller.UifControllerBase; |
38 | |
import org.kuali.rice.krad.web.form.UifFormBase; |
39 | |
import org.springframework.stereotype.Controller; |
40 | |
import org.springframework.validation.BindingResult; |
41 | |
import org.springframework.web.bind.annotation.ModelAttribute; |
42 | |
import org.springframework.web.bind.annotation.RequestMapping; |
43 | |
import org.springframework.web.bind.annotation.RequestMethod; |
44 | |
import org.springframework.web.multipart.MultipartFile; |
45 | |
import org.springframework.web.servlet.ModelAndView; |
46 | |
|
47 | |
import edu.sampleu.kew.krad.KEWConstants; |
48 | |
import edu.sampleu.kew.krad.form.IngesterForm; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | @Controller |
57 | |
@RequestMapping(value = "/ingester") |
58 | 0 | public class IngesterController extends UifControllerBase { |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
@Override |
64 | |
protected IngesterForm createInitialForm(HttpServletRequest request) { |
65 | 0 | return new IngesterForm(); |
66 | |
} |
67 | |
|
68 | |
@Override |
69 | |
@RequestMapping(params = "methodToCall=start") |
70 | |
public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, |
71 | |
HttpServletRequest request, HttpServletResponse response) { |
72 | |
|
73 | 0 | IngesterForm ingesterForm = (IngesterForm)form; |
74 | |
|
75 | |
|
76 | 0 | return super.start(ingesterForm, result, request, response); |
77 | |
} |
78 | |
|
79 | |
@RequestMapping(method = RequestMethod.POST, params = "methodToCall=upload") |
80 | |
public ModelAndView upload(@ModelAttribute("KualiForm") IngesterForm ingesterForm, BindingResult result, |
81 | |
HttpServletRequest request, HttpServletResponse response) { |
82 | |
|
83 | 0 | List<File> tempFiles = new ArrayList<File>(); |
84 | |
|
85 | |
try { |
86 | |
|
87 | 0 | List<XmlDocCollection> collections = new ArrayList<XmlDocCollection>(); |
88 | |
|
89 | 0 | for (MultipartFile file : ingesterForm.getFiles()) |
90 | |
{ |
91 | 0 | if (file == null || StringUtils.isBlank(file.getOriginalFilename())) { |
92 | 0 | continue; |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | 0 | FileOutputStream fos = null; |
99 | 0 | File temp = null; |
100 | |
try{ |
101 | 0 | temp = File.createTempFile("ingester", null); |
102 | 0 | tempFiles.add(temp); |
103 | 0 | fos = new FileOutputStream(temp); |
104 | 0 | fos.write(file.getBytes()); |
105 | 0 | } catch (IOException ioe) { |
106 | 0 | GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID,KEWConstants.ERROR_INGESTER_COPY_FILE , file.getOriginalFilename(), ExceptionUtils.getFullStackTrace(ioe)); |
107 | |
continue; |
108 | |
} finally{ |
109 | 0 | if (fos != null) { |
110 | |
try{ |
111 | 0 | fos.close(); |
112 | 0 | } catch (IOException ioe){ |
113 | |
|
114 | 0 | } |
115 | |
} |
116 | |
} |
117 | 0 | if (file.getOriginalFilename().toLowerCase().endsWith(".zip")) |
118 | |
{ |
119 | |
try { |
120 | 0 | collections.add(new ZipXmlDocCollection(temp)); |
121 | 0 | } catch (IOException ioe) { |
122 | 0 | GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_LOAD_FILE, file.getOriginalFilename()); |
123 | 0 | } |
124 | 0 | } else if (file.getOriginalFilename().endsWith(".xml")) { |
125 | 0 | collections.add(new FileXmlDocCollection(temp, file.getOriginalFilename())); |
126 | |
} else { |
127 | 0 | GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_EXTRANEOUS_FILE, file.getOriginalFilename()); |
128 | |
} |
129 | 0 | } |
130 | |
|
131 | 0 | if (collections.size() == 0) { |
132 | 0 | String message = "No valid files to ingest"; |
133 | 0 | GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_NO_VALID_FILES); |
134 | 0 | } else { |
135 | |
|
136 | 0 | CompositeXmlDocCollection compositeCollection = new CompositeXmlDocCollection(collections); |
137 | 0 | int totalProcessed = 0; |
138 | 0 | List<XmlDocCollection> c = new ArrayList<XmlDocCollection>(1); |
139 | 0 | c.add(compositeCollection); |
140 | |
try { |
141 | 0 | Collection<XmlDocCollection> failed = CoreApiServiceLocator.getXmlIngesterService().ingest(c, GlobalVariables.getUserSession().getPrincipalId()); |
142 | 0 | boolean txFailed = failed.size() > 0; |
143 | 0 | if (txFailed) { |
144 | 0 | GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_FAILED); |
145 | |
} |
146 | 0 | for (XmlDocCollection collection1 : collections) |
147 | |
{ |
148 | 0 | List<? extends XmlDoc> docs = collection1.getXmlDocs(); |
149 | 0 | for (XmlDoc doc1 : docs) |
150 | |
{ |
151 | 0 | if (doc1.isProcessed()) |
152 | |
{ |
153 | 0 | if (!txFailed) |
154 | |
{ |
155 | 0 | totalProcessed++; |
156 | 0 | GlobalVariables.getMessageMap().putInfoForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.INFO_INGESTER_SUCCESS, doc1.getName(),doc1.getProcessingMessage()); |
157 | |
|
158 | |
} else |
159 | 0 | {GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_ROLLEDBACK, doc1.getName(),doc1.getProcessingMessage()); |
160 | |
|
161 | |
} |
162 | |
} else |
163 | 0 | {GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_FAILED_XML, doc1.getName(),doc1.getProcessingMessage()); |
164 | |
|
165 | |
} |
166 | |
} |
167 | 0 | } |
168 | 0 | } catch (Exception e) { |
169 | |
|
170 | |
|
171 | |
|
172 | 0 | GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_DURING_INJECT, ExceptionUtils.getFullStackTrace(e)); |
173 | 0 | } |
174 | 0 | if (totalProcessed == 0) { |
175 | |
|
176 | 0 | GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_NO_XMLS); |
177 | |
} |
178 | |
} |
179 | |
} finally { |
180 | 0 | if (tempFiles.size() > 0) { |
181 | 0 | for (File tempFile : tempFiles) |
182 | |
{ |
183 | 0 | if (!tempFile.delete()) |
184 | |
{ |
185 | |
|
186 | |
} |
187 | |
} |
188 | |
} |
189 | |
} |
190 | |
|
191 | |
|
192 | 0 | return getUIFModelAndView(ingesterForm); |
193 | |
} |
194 | |
|
195 | |
@RequestMapping(method = RequestMethod.POST, params = "methodToCall=close") |
196 | |
public ModelAndView close(@ModelAttribute("KualiForm") IngesterForm ingesterForm, BindingResult result, |
197 | |
HttpServletRequest request, HttpServletResponse response) { |
198 | |
|
199 | 0 | return null; |
200 | |
} |
201 | |
|
202 | |
} |