1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.core.web.impex; |
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.HashMap; |
24 | |
import java.util.List; |
25 | |
import java.util.Map; |
26 | |
|
27 | |
import javax.servlet.http.HttpServletRequest; |
28 | |
import javax.servlet.http.HttpServletResponse; |
29 | |
|
30 | |
import org.apache.commons.lang.exception.ExceptionUtils; |
31 | |
import org.apache.log4j.Logger; |
32 | |
import org.apache.struts.action.ActionForm; |
33 | |
import org.apache.struts.action.ActionForward; |
34 | |
import org.apache.struts.action.ActionMapping; |
35 | |
import org.apache.struts.upload.FormFile; |
36 | |
import org.kuali.rice.core.api.CoreApiServiceLocator; |
37 | |
import org.kuali.rice.core.api.impex.xml.CompositeXmlDocCollection; |
38 | |
import org.kuali.rice.core.api.impex.xml.FileXmlDocCollection; |
39 | |
import org.kuali.rice.core.api.impex.xml.XmlDoc; |
40 | |
import org.kuali.rice.core.api.impex.xml.XmlDocCollection; |
41 | |
import org.kuali.rice.core.api.impex.xml.ZipXmlDocCollection; |
42 | |
import org.kuali.rice.core.api.util.RiceConstants; |
43 | |
import org.kuali.rice.kim.api.KimConstants; |
44 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
45 | |
import org.kuali.rice.kns.web.struts.action.KualiAction; |
46 | |
import org.kuali.rice.krad.exception.AuthorizationException; |
47 | |
import org.kuali.rice.krad.util.GlobalVariables; |
48 | |
import org.kuali.rice.krad.util.KRADConstants; |
49 | |
import org.kuali.rice.krad.util.KRADUtils; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | 0 | public class IngesterAction extends KualiAction { |
58 | 0 | private static final Logger LOG = Logger.getLogger(IngesterAction.class); |
59 | |
|
60 | |
@Override |
61 | |
public ActionForward execute( |
62 | |
ActionMapping mapping, |
63 | |
ActionForm form, |
64 | |
HttpServletRequest request, |
65 | |
HttpServletResponse response) |
66 | |
throws Exception { |
67 | |
|
68 | 0 | checkAuthorization(form, ""); |
69 | |
|
70 | 0 | if(isModuleLocked(form, "", request)) { |
71 | 0 | return mapping.findForward(RiceConstants.MODULE_LOCKED_MAPPING); |
72 | |
} |
73 | |
|
74 | 0 | LOG.debug(request.getMethod()); |
75 | 0 | if (!"post".equals(request.getMethod().toLowerCase())) { |
76 | 0 | LOG.debug("returning to view"); |
77 | 0 | return mapping.findForward("view"); |
78 | |
} |
79 | |
|
80 | 0 | IngesterForm iform = (IngesterForm) form; |
81 | |
|
82 | 0 | List<String> messages = new ArrayList<String>(); |
83 | 0 | List<File> tempFiles = new ArrayList<File>(); |
84 | |
try { |
85 | 0 | Collection<FormFile> files = iform.getFiles(); |
86 | 0 | List<XmlDocCollection> collections = new ArrayList<XmlDocCollection>(files.size()); |
87 | 0 | LOG.debug(files); |
88 | 0 | LOG.debug("" + files.size()); |
89 | |
|
90 | 0 | for (FormFile file1 : files) |
91 | |
{ |
92 | 0 | if (file1.getFileName() == null || file1.getFileName().length() == 0) { |
93 | 0 | continue; |
94 | |
} |
95 | 0 | if (file1.getFileData() == null) |
96 | |
{ |
97 | 0 | messages.add("File '" + file1.getFileName() + "' contained no data"); |
98 | 0 | continue; |
99 | |
} |
100 | 0 | LOG.debug("Processing file: " + file1.getFileName()); |
101 | |
|
102 | |
|
103 | |
|
104 | 0 | FileOutputStream fos = null; |
105 | 0 | File temp = null; |
106 | |
try |
107 | |
{ |
108 | 0 | temp = File.createTempFile("ingester", null); |
109 | 0 | tempFiles.add(temp); |
110 | 0 | fos = new FileOutputStream(temp); |
111 | 0 | fos.write(file1.getFileData()); |
112 | 0 | } catch (IOException ioe) |
113 | |
{ |
114 | 0 | messages.add("Error copying file data for '" + file1.getFileName() + "': " + ioe); |
115 | |
continue; |
116 | |
} finally |
117 | |
{ |
118 | 0 | if (fos != null) { |
119 | |
try |
120 | |
{ |
121 | 0 | fos.close(); |
122 | 0 | } catch (IOException ioe) |
123 | |
{ |
124 | 0 | LOG.error("Error closing temp file output stream: " + temp, ioe); |
125 | 0 | } |
126 | |
} |
127 | |
} |
128 | 0 | if (file1.getFileName().toLowerCase().endsWith(".zip")) |
129 | |
{ |
130 | |
try |
131 | |
{ |
132 | 0 | collections.add(new ZipXmlDocCollection(temp)); |
133 | 0 | } catch (IOException ioe) |
134 | |
{ |
135 | 0 | String message = "Unable to load file: " + file1; |
136 | 0 | LOG.error(message); |
137 | 0 | messages.add(message); |
138 | 0 | } |
139 | 0 | } else if (file1.getFileName().endsWith(".xml")) |
140 | |
{ |
141 | 0 | collections.add(new FileXmlDocCollection(temp, file1.getFileName())); |
142 | |
} else |
143 | |
{ |
144 | 0 | messages.add("Ignoring extraneous file: " + file1.getFileName()); |
145 | |
} |
146 | 0 | } |
147 | |
|
148 | 0 | if (collections.size() == 0) { |
149 | 0 | String message = "No valid files to ingest"; |
150 | 0 | LOG.debug(message); |
151 | 0 | messages.add(message); |
152 | 0 | } else { |
153 | |
|
154 | 0 | CompositeXmlDocCollection compositeCollection = new CompositeXmlDocCollection(collections); |
155 | 0 | int totalProcessed = 0; |
156 | 0 | List<XmlDocCollection> c = new ArrayList<XmlDocCollection>(1); |
157 | 0 | c.add(compositeCollection); |
158 | |
try { |
159 | 0 | Collection<XmlDocCollection> failed = CoreApiServiceLocator.getXmlIngesterService().ingest(c, GlobalVariables.getUserSession().getPrincipalId()); |
160 | 0 | boolean txFailed = failed.size() > 0; |
161 | 0 | if (txFailed) { |
162 | 0 | messages.add("Ingestion failed"); |
163 | |
} |
164 | 0 | for (XmlDocCollection collection1 : collections) |
165 | |
{ |
166 | 0 | List<? extends XmlDoc> docs = collection1.getXmlDocs(); |
167 | 0 | for (XmlDoc doc1 : docs) |
168 | |
{ |
169 | 0 | if (doc1.isProcessed()) |
170 | |
{ |
171 | 0 | if (!txFailed) |
172 | |
{ |
173 | 0 | totalProcessed++; |
174 | 0 | messages.add("Ingested xml doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage())); |
175 | |
} else |
176 | |
{ |
177 | 0 | messages.add("Rolled back doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage())); |
178 | |
} |
179 | |
} else |
180 | |
{ |
181 | 0 | messages.add("Failed to ingest xml doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage())); |
182 | |
} |
183 | |
} |
184 | 0 | } |
185 | 0 | } catch (Exception e) { |
186 | 0 | String message = "Error during ingest"; |
187 | 0 | LOG.error(message, e); |
188 | 0 | messages.add(message + ": " + e + ":\n" + ExceptionUtils.getFullStackTrace(e)); |
189 | 0 | } |
190 | 0 | if (totalProcessed == 0) { |
191 | 0 | String message = "No xml docs ingested"; |
192 | 0 | LOG.debug(message); |
193 | 0 | messages.add(message); |
194 | |
} |
195 | |
} |
196 | |
} finally { |
197 | 0 | if (tempFiles.size() > 0) { |
198 | 0 | for (File tempFile : tempFiles) |
199 | |
{ |
200 | 0 | if (!tempFile.delete()) |
201 | |
{ |
202 | 0 | LOG.warn("Error deleting temp file: " + tempFile); |
203 | |
} |
204 | |
} |
205 | |
} |
206 | |
} |
207 | |
|
208 | 0 | request.setAttribute("messages", messages); |
209 | 0 | return mapping.findForward("view"); |
210 | |
} |
211 | |
|
212 | |
@Override |
213 | |
protected void checkAuthorization( ActionForm form, String methodToCall) throws AuthorizationException |
214 | |
{ |
215 | 0 | String principalId = GlobalVariables.getUserSession().getPrincipalId(); |
216 | 0 | Map<String, String> roleQualifier = new HashMap<String, String>(); |
217 | 0 | Map<String, String> permissionDetails = KRADUtils.getNamespaceAndActionClass(this.getClass()); |
218 | |
|
219 | 0 | if (!KimApiServiceLocator.getPermissionService().isAuthorizedByTemplateName(principalId, KRADConstants.KRAD_NAMESPACE, |
220 | |
KimConstants.PermissionTemplateNames.USE_SCREEN, permissionDetails, roleQualifier)) |
221 | |
{ |
222 | 0 | throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalName(), |
223 | |
methodToCall, |
224 | |
this.getClass().getSimpleName()); |
225 | |
} |
226 | 0 | } |
227 | |
|
228 | |
|
229 | |
} |