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 @Override
59 protected Class<IngesterForm> formType() {
60 return IngesterForm.class;
61 }
62
63 @Override
64 @RequestMapping(params = "methodToCall=start")
65 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
66 HttpServletRequest request, HttpServletResponse response) {
67
68 IngesterForm ingesterForm = (IngesterForm)form;
69
70
71 return super.start(ingesterForm, result, request, response);
72 }
73
74 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=upload")
75 public ModelAndView upload(@ModelAttribute("KualiForm") IngesterForm ingesterForm, BindingResult result,
76 HttpServletRequest request, HttpServletResponse response) {
77
78 List<File> tempFiles = new ArrayList<File>();
79
80 try {
81
82 List<XmlDocCollection> collections = new ArrayList<XmlDocCollection>();
83
84 for (MultipartFile file : ingesterForm.getFiles())
85 {
86 if (file == null || StringUtils.isBlank(file.getOriginalFilename())) {
87 continue;
88 }
89
90
91
92
93 FileOutputStream fos = null;
94 File temp = null;
95 try{
96 temp = File.createTempFile("ingester", null);
97 tempFiles.add(temp);
98 fos = new FileOutputStream(temp);
99 fos.write(file.getBytes());
100 } catch (IOException ioe) {
101 GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID,KEWConstants.ERROR_INGESTER_COPY_FILE , file.getOriginalFilename(), ExceptionUtils.getFullStackTrace(ioe));
102 continue;
103 } finally{
104 if (fos != null) {
105 try{
106 fos.close();
107 } catch (IOException ioe){
108
109 }
110 }
111 }
112 if (file.getOriginalFilename().toLowerCase().endsWith(".zip"))
113 {
114 try {
115 collections.add(new ZipXmlDocCollection(temp));
116 } catch (IOException ioe) {
117 GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_LOAD_FILE, file.getOriginalFilename());
118 }
119 } else if (file.getOriginalFilename().endsWith(".xml")) {
120 collections.add(new FileXmlDocCollection(temp, file.getOriginalFilename()));
121 } else {
122 GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_EXTRANEOUS_FILE, file.getOriginalFilename());
123 }
124 }
125
126 if (collections.size() == 0) {
127 String message = "No valid files to ingest";
128 GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_NO_VALID_FILES);
129 } else {
130
131 CompositeXmlDocCollection compositeCollection = new CompositeXmlDocCollection(collections);
132 int totalProcessed = 0;
133 List<XmlDocCollection> c = new ArrayList<XmlDocCollection>(1);
134 c.add(compositeCollection);
135 try {
136 Collection<XmlDocCollection> failed = CoreApiServiceLocator.getXmlIngesterService().ingest(c, GlobalVariables.getUserSession().getPrincipalId());
137 boolean txFailed = failed.size() > 0;
138 if (txFailed) {
139 GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_FAILED);
140 }
141 for (XmlDocCollection collection1 : collections)
142 {
143 List<? extends XmlDoc> docs = collection1.getXmlDocs();
144 for (XmlDoc doc1 : docs)
145 {
146 if (doc1.isProcessed())
147 {
148 if (!txFailed)
149 {
150 totalProcessed++;
151 GlobalVariables.getMessageMap().putInfoForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.INFO_INGESTER_SUCCESS, doc1.getName(),doc1.getProcessingMessage());
152
153 } else
154 {GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_ROLLEDBACK, doc1.getName(),doc1.getProcessingMessage());
155
156 }
157 } else
158 {GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_FAILED_XML, doc1.getName(),doc1.getProcessingMessage());
159
160 }
161 }
162 }
163 } catch (Exception e) {
164
165
166
167 GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_DURING_INJECT, ExceptionUtils.getFullStackTrace(e));
168 }
169 if (totalProcessed == 0) {
170
171 GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_NO_XMLS);
172 }
173 }
174 } finally {
175 if (tempFiles.size() > 0) {
176 for (File tempFile : tempFiles)
177 {
178 if (!tempFile.delete())
179 {
180
181 }
182 }
183 }
184 }
185
186
187 return getUIFModelAndView(ingesterForm);
188 }
189
190 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=close")
191 public ModelAndView close(@ModelAttribute("KualiForm") IngesterForm ingesterForm, BindingResult result,
192 HttpServletRequest request, HttpServletResponse response) {
193
194 return null;
195 }
196
197 }