001 /* 002 * Copyright 2011 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 1.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/ecl1.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 edu.sampleu.kew.krad.KEWConstants; 019 import edu.sampleu.kew.krad.form.IngesterForm; 020 import org.apache.commons.lang.StringUtils; 021 import org.apache.commons.lang.exception.ExceptionUtils; 022 import org.kuali.rice.core.api.CoreApiServiceLocator; 023 import org.kuali.rice.core.api.impex.xml.CompositeXmlDocCollection; 024 import org.kuali.rice.core.api.impex.xml.FileXmlDocCollection; 025 import org.kuali.rice.core.api.impex.xml.XmlDoc; 026 import org.kuali.rice.core.api.impex.xml.XmlDocCollection; 027 import org.kuali.rice.core.api.impex.xml.ZipXmlDocCollection; 028 import org.kuali.rice.krad.util.GlobalVariables; 029 import org.kuali.rice.krad.web.controller.UifControllerBase; 030 import org.kuali.rice.krad.web.form.UifFormBase; 031 import org.springframework.stereotype.Controller; 032 import org.springframework.validation.BindingResult; 033 import org.springframework.web.bind.annotation.ModelAttribute; 034 import org.springframework.web.bind.annotation.RequestMapping; 035 import org.springframework.web.bind.annotation.RequestMethod; 036 import org.springframework.web.multipart.MultipartFile; 037 import org.springframework.web.servlet.ModelAndView; 038 039 import javax.servlet.http.HttpServletRequest; 040 import javax.servlet.http.HttpServletResponse; 041 import java.io.File; 042 import java.io.FileOutputStream; 043 import java.io.IOException; 044 import java.util.ArrayList; 045 import java.util.Collection; 046 import java.util.List; 047 048 /** 049 * This is a description of what this class does - Venkat don't forget to fill this in. 050 * 051 * @author Kuali Rice Team (rice.collab@kuali.org) 052 * 053 */ 054 @Controller 055 @RequestMapping(value = "/ingester") 056 public class IngesterController extends UifControllerBase { 057 058 @Override 059 protected Class<IngesterForm> formType() { 060 return IngesterForm.class; 061 } 062 063 @Override 064 @RequestMapping(params = "methodToCall=start") 065 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 066 HttpServletRequest request, HttpServletResponse response) { 067 068 IngesterForm ingesterForm = (IngesterForm)form; 069 070 // checkAuthorization(form,""); 071 return super.start(ingesterForm, result, request, response); 072 } 073 074 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=upload") 075 public ModelAndView upload(@ModelAttribute("KualiForm") IngesterForm ingesterForm, BindingResult result, 076 HttpServletRequest request, HttpServletResponse response) { 077 078 List<File> tempFiles = new ArrayList<File>(); 079 080 try { 081 082 List<XmlDocCollection> collections = new ArrayList<XmlDocCollection>(); 083 084 for (MultipartFile file : ingesterForm.getFiles()) 085 { 086 if (file == null || StringUtils.isBlank(file.getOriginalFilename())) { 087 continue; 088 } 089 090 // ok, we have to copy it to *another* file because Struts doesn't give us a File 091 // reference (which itself is not a bad abstraction) and XmlDocs based on ZipFile 092 // can't be constructed without a file reference. 093 FileOutputStream fos = null; 094 File temp = null; 095 try{ 096 temp = File.createTempFile("ingester", null); 097 tempFiles.add(temp); 098 fos = new FileOutputStream(temp); 099 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 // //LOG.error("Error closing temp file output stream: " + temp, ioe); 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 // wrap in composite collection to make transactional 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 // messages.add("Ingested xml doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage())); 153 } else 154 {GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_ROLLEDBACK, doc1.getName(),doc1.getProcessingMessage()); 155 // messages.add("Rolled back doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage())); 156 } 157 } else 158 {GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_FAILED_XML, doc1.getName(),doc1.getProcessingMessage()); 159 // messages.add("Failed to ingest xml doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage())); 160 } 161 } 162 } 163 } catch (Exception e) { 164 // String message = "Error during ingest"; 165 //LOG.error(message, e); 166 // messages.add(message + ": " + e + ":\n" + ExceptionUtils.getFullStackTrace(e)); 167 GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_DURING_INJECT, ExceptionUtils.getFullStackTrace(e)); 168 } 169 if (totalProcessed == 0) { 170 // String message = "No xml docs ingested"; 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 //LOG.warn("Error deleting temp file: " + tempFile); 181 } 182 } 183 } 184 } 185 186 // request.setAttribute("messages", messages); 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 }