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