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