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