001 /**
002 * Copyright 2005-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package edu.sampleu.kew.krad.controller;
017
018 import java.io.File;
019 import java.io.FileOutputStream;
020 import java.io.IOException;
021 import java.util.ArrayList;
022 import java.util.Collection;
023 import java.util.List;
024
025 import javax.servlet.http.HttpServletRequest;
026 import javax.servlet.http.HttpServletResponse;
027
028 import org.apache.commons.lang.StringUtils;
029 import org.apache.commons.lang.exception.ExceptionUtils;
030 import org.kuali.rice.core.api.CoreApiServiceLocator;
031 import org.kuali.rice.core.api.impex.xml.CompositeXmlDocCollection;
032 import org.kuali.rice.core.api.impex.xml.FileXmlDocCollection;
033 import org.kuali.rice.core.api.impex.xml.XmlDoc;
034 import org.kuali.rice.core.api.impex.xml.XmlDocCollection;
035 import org.kuali.rice.core.api.impex.xml.ZipXmlDocCollection;
036 import org.kuali.rice.krad.util.GlobalVariables;
037 import org.kuali.rice.krad.web.controller.UifControllerBase;
038 import org.kuali.rice.krad.web.form.UifFormBase;
039 import org.springframework.stereotype.Controller;
040 import org.springframework.validation.BindingResult;
041 import org.springframework.web.bind.annotation.ModelAttribute;
042 import org.springframework.web.bind.annotation.RequestMapping;
043 import org.springframework.web.bind.annotation.RequestMethod;
044 import org.springframework.web.multipart.MultipartFile;
045 import org.springframework.web.servlet.ModelAndView;
046
047 import edu.sampleu.kew.krad.KEWConstants;
048 import edu.sampleu.kew.krad.form.IngesterForm;
049
050 /**
051 * This is a description of what this class does - Venkat don't forget to fill this in.
052 *
053 * @author Kuali Rice Team (rice.collab@kuali.org)
054 *
055 */
056 @Controller
057 @RequestMapping(value = "/ingester")
058 public class IngesterController extends UifControllerBase {
059
060 /**
061 * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
062 */
063 @Override
064 protected IngesterForm createInitialForm(HttpServletRequest request) {
065 return new IngesterForm();
066 }
067
068 @Override
069 @RequestMapping(params = "methodToCall=start")
070 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
071 HttpServletRequest request, HttpServletResponse response) {
072
073 IngesterForm ingesterForm = (IngesterForm)form;
074
075 // checkAuthorization(form,"");
076 return super.start(ingesterForm, result, request, response);
077 }
078
079 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=upload")
080 public ModelAndView upload(@ModelAttribute("KualiForm") IngesterForm ingesterForm, BindingResult result,
081 HttpServletRequest request, HttpServletResponse response) {
082
083 List<File> tempFiles = new ArrayList<File>();
084
085 try {
086
087 List<XmlDocCollection> collections = new ArrayList<XmlDocCollection>();
088
089 for (MultipartFile file : ingesterForm.getFiles())
090 {
091 if (file == null || StringUtils.isBlank(file.getOriginalFilename())) {
092 continue;
093 }
094
095 // ok, we have to copy it to *another* file because Struts doesn't give us a File
096 // reference (which itself is not a bad abstraction) and XmlDocs based on ZipFile
097 // can't be constructed without a file reference.
098 FileOutputStream fos = null;
099 File temp = null;
100 try{
101 temp = File.createTempFile("ingester", null);
102 tempFiles.add(temp);
103 fos = new FileOutputStream(temp);
104 fos.write(file.getBytes());
105 } catch (IOException ioe) {
106 GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID,KEWConstants.ERROR_INGESTER_COPY_FILE , file.getOriginalFilename(), ExceptionUtils.getFullStackTrace(ioe));
107 continue;
108 } finally{
109 if (fos != null) {
110 try{
111 fos.close();
112 } catch (IOException ioe){
113 // //LOG.error("Error closing temp file output stream: " + temp, ioe);
114 }
115 }
116 }
117 if (file.getOriginalFilename().toLowerCase().endsWith(".zip"))
118 {
119 try {
120 collections.add(new ZipXmlDocCollection(temp));
121 } catch (IOException ioe) {
122 GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_LOAD_FILE, file.getOriginalFilename());
123 }
124 } else if (file.getOriginalFilename().endsWith(".xml")) {
125 collections.add(new FileXmlDocCollection(temp, file.getOriginalFilename()));
126 } else {
127 GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_EXTRANEOUS_FILE, file.getOriginalFilename());
128 }
129 }
130
131 if (collections.size() == 0) {
132 String message = "No valid files to ingest";
133 GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_NO_VALID_FILES);
134 } else {
135 // wrap in composite collection to make transactional
136 CompositeXmlDocCollection compositeCollection = new CompositeXmlDocCollection(collections);
137 int totalProcessed = 0;
138 List<XmlDocCollection> c = new ArrayList<XmlDocCollection>(1);
139 c.add(compositeCollection);
140 try {
141 Collection<XmlDocCollection> failed = CoreApiServiceLocator.getXmlIngesterService().ingest(c, GlobalVariables.getUserSession().getPrincipalId());
142 boolean txFailed = failed.size() > 0;
143 if (txFailed) {
144 GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_FAILED);
145 }
146 for (XmlDocCollection collection1 : collections)
147 {
148 List<? extends XmlDoc> docs = collection1.getXmlDocs();
149 for (XmlDoc doc1 : docs)
150 {
151 if (doc1.isProcessed())
152 {
153 if (!txFailed)
154 {
155 totalProcessed++;
156 GlobalVariables.getMessageMap().putInfoForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.INFO_INGESTER_SUCCESS, doc1.getName(),doc1.getProcessingMessage());
157 // messages.add("Ingested xml doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage()));
158 } else
159 {GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_ROLLEDBACK, doc1.getName(),doc1.getProcessingMessage());
160 // messages.add("Rolled back doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage()));
161 }
162 } else
163 {GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_FAILED_XML, doc1.getName(),doc1.getProcessingMessage());
164 // messages.add("Failed to ingest xml doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage()));
165 }
166 }
167 }
168 } catch (Exception e) {
169 // String message = "Error during ingest";
170 //LOG.error(message, e);
171 // messages.add(message + ": " + e + ":\n" + ExceptionUtils.getFullStackTrace(e));
172 GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_DURING_INJECT, ExceptionUtils.getFullStackTrace(e));
173 }
174 if (totalProcessed == 0) {
175 // String message = "No xml docs ingested";
176 GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_NO_XMLS);
177 }
178 }
179 } finally {
180 if (tempFiles.size() > 0) {
181 for (File tempFile : tempFiles)
182 {
183 if (!tempFile.delete())
184 {
185 //LOG.warn("Error deleting temp file: " + tempFile);
186 }
187 }
188 }
189 }
190
191 // request.setAttribute("messages", messages);
192 return getUIFModelAndView(ingesterForm);
193 }
194
195 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=close")
196 public ModelAndView close(@ModelAttribute("KualiForm") IngesterForm ingesterForm, BindingResult result,
197 HttpServletRequest request, HttpServletResponse response) {
198
199 return null;
200 }
201
202 }