View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.labs.fileUploads;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.commons.lang.exception.ExceptionUtils;
20  import org.kuali.rice.core.api.CoreApiServiceLocator;
21  import org.kuali.rice.core.api.impex.xml.CompositeXmlDocCollection;
22  import org.kuali.rice.core.api.impex.xml.FileXmlDocCollection;
23  import org.kuali.rice.core.api.impex.xml.XmlDoc;
24  import org.kuali.rice.core.api.impex.xml.XmlDocCollection;
25  import org.kuali.rice.core.api.impex.xml.ZipXmlDocCollection;
26  import org.kuali.rice.krad.util.GlobalVariables;
27  import org.kuali.rice.krad.web.controller.UifControllerBase;
28  import org.kuali.rice.krad.web.form.UifFormBase;
29  import org.springframework.stereotype.Controller;
30  import org.springframework.validation.BindingResult;
31  import org.springframework.web.bind.annotation.ModelAttribute;
32  import org.springframework.web.bind.annotation.RequestMapping;
33  import org.springframework.web.bind.annotation.RequestMethod;
34  import org.springframework.web.multipart.MultipartFile;
35  import org.springframework.web.servlet.ModelAndView;
36  
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  import java.io.File;
40  import java.io.FileOutputStream;
41  import java.io.IOException;
42  import java.util.ArrayList;
43  import java.util.Collection;
44  import java.util.List;
45  
46  /**
47   * Controller for the XML Ingester View
48   *
49   * <p>
50   *     Displays the initial Ingester view page and processes file upload requests.
51   * </p>
52   * 
53   * @author Kuali Rice Team (rice.collab@kuali.org)
54   *
55   */
56  @Controller
57  @RequestMapping(value = "/ingester")
58  public class XmlIngesterController extends UifControllerBase {
59  
60      /**
61       * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
62       */
63      @Override
64      protected XmlIngesterForm createInitialForm(HttpServletRequest request) {
65          return new XmlIngesterForm();
66      }
67  
68  	@Override
69  	@RequestMapping(params = "methodToCall=start")
70  	public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
71  			HttpServletRequest request, HttpServletResponse response) {
72  
73  		XmlIngesterForm ingesterForm = (XmlIngesterForm)form;
74  		
75  //		checkAuthorization(form,"");
76  		return super.start(ingesterForm, result, request, response);
77  	}
78  
79  	@RequestMapping(method = RequestMethod.POST, params = "methodToCall=upload")
80  	public ModelAndView upload(@ModelAttribute("KualiForm") XmlIngesterForm ingesterForm, BindingResult result,
81  			HttpServletRequest request, HttpServletResponse response) {
82  		
83  		List<File> tempFiles = new ArrayList<File>();
84  		try {
85  			List<XmlDocCollection> collections = new ArrayList<XmlDocCollection>();
86  			for (MultipartFile file : ingesterForm.getFiles())
87  	        {
88  	            if (file == null || StringUtils.isBlank(file.getOriginalFilename())) {
89  					continue;
90  				}
91  	            
92  	            // ok, we have to copy it to *another* file because Struts doesn't give us a File
93  	            // reference (which itself is not a bad abstraction) and XmlDocs based on ZipFile
94  	            // can't be constructed without a file reference.
95  	            FileOutputStream fos = null;
96  	            File temp = null;
97  	            try{
98  	                temp = File.createTempFile("ingester", null);
99  	                tempFiles.add(temp);
100 	                fos = new FileOutputStream(temp);
101 	                fos.write(file.getBytes());
102 	            } catch (IOException ioe) {
103 	            	GlobalVariables.getMessageMap().putErrorForSectionId(XmlIngesterConstants.INGESTER_SECTION_ID,
104                             XmlIngesterConstants.ERROR_INGESTER_COPY_FILE , file.getOriginalFilename(), ExceptionUtils.getFullStackTrace(ioe));
105 	                continue;
106 	            } finally{
107 	                if (fos != null) {
108 						try{
109 						    fos.close();
110 						} catch (IOException ioe){
111 	//					    //LOG.error("Error closing temp file output stream: " + temp, ioe);
112 						}
113 					}
114 	            }
115 	            if (file.getOriginalFilename().toLowerCase().endsWith(".zip"))
116 	            {
117 	                try {
118 	                    collections.add(new ZipXmlDocCollection(temp));
119 	                } catch (IOException ioe) {
120 	                    GlobalVariables.getMessageMap().putErrorForSectionId(XmlIngesterConstants.INGESTER_SECTION_ID, XmlIngesterConstants.ERROR_INGESTER_LOAD_FILE, file.getOriginalFilename());
121 	                }
122 	            } else if (file.getOriginalFilename().endsWith(".xml")) {
123 	                collections.add(new FileXmlDocCollection(temp, file.getOriginalFilename()));
124 	            } else {
125 	            	GlobalVariables.getMessageMap().putErrorForSectionId(XmlIngesterConstants.INGESTER_SECTION_ID, XmlIngesterConstants.ERROR_INGESTER_EXTRANEOUS_FILE, file.getOriginalFilename());
126 	            }
127 	        }
128 	
129 	        if (collections.size() == 0) {
130 	            String message = "No valid files to ingest";
131 	            GlobalVariables.getMessageMap().putErrorForSectionId(XmlIngesterConstants.INGESTER_SECTION_ID, XmlIngesterConstants.ERROR_INGESTER_NO_VALID_FILES);
132 	        } else {
133 	            // wrap in composite collection to make transactional
134 	            CompositeXmlDocCollection compositeCollection = new CompositeXmlDocCollection(collections);
135 	            int totalProcessed = 0;
136 	            List<XmlDocCollection> c = new ArrayList<XmlDocCollection>(1);
137 	            c.add(compositeCollection);
138 	            try {
139                     // ingest the collection of files
140 	                Collection<XmlDocCollection> failed = CoreApiServiceLocator.getXmlIngesterService().ingest(c, GlobalVariables.getUserSession().getPrincipalId());
141 	                boolean txFailed = failed.size() > 0;
142 	                if (txFailed) {
143 	                	GlobalVariables.getMessageMap().putErrorForSectionId(XmlIngesterConstants.INGESTER_SECTION_ID, XmlIngesterConstants.ERROR_INGESTER_FAILED);
144 	                }
145 
146                     // loop through the results, collecting the error messages for each doc
147 	                for (XmlDocCollection collection1 : collections)
148 	                {
149 	                    List<? extends XmlDoc> docs = collection1.getXmlDocs();
150 	                    for (XmlDoc doc1 : docs)
151 	                    {
152 	                        if (doc1.isProcessed())
153 	                        {
154 	                            if (!txFailed)
155 	                            {
156 	                                totalProcessed++;
157 	                                GlobalVariables.getMessageMap().putInfoForSectionId(XmlIngesterConstants.INGESTER_SECTION_ID, XmlIngesterConstants.INFO_INGESTER_SUCCESS, doc1.getName(),doc1.getProcessingMessage());
158 //	                                messages.add("Ingested xml doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage()));
159 	                            } else
160 	                            {GlobalVariables.getMessageMap().putErrorForSectionId(XmlIngesterConstants.INGESTER_SECTION_ID, XmlIngesterConstants.ERROR_INGESTER_ROLLEDBACK, doc1.getName(),doc1.getProcessingMessage());
161 //	                                messages.add("Rolled back doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage()));
162 	                            }
163 	                        } else
164 	                        {GlobalVariables.getMessageMap().putErrorForSectionId(XmlIngesterConstants.INGESTER_SECTION_ID, XmlIngesterConstants.ERROR_INGESTER_FAILED_XML, doc1.getName(),doc1.getProcessingMessage());
165 //	                            messages.add("Failed to ingest xml doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage()));
166 	                        }
167 	                    }
168 	                }
169 	            } catch (Exception e) {
170 //	                String message = "Error during ingest";
171 	                //LOG.error(message, e);
172 //	                messages.add(message + ": " + e  + ":\n" + ExceptionUtils.getFullStackTrace(e));
173 	                GlobalVariables.getMessageMap().putErrorForSectionId(XmlIngesterConstants.INGESTER_SECTION_ID, XmlIngesterConstants.ERROR_INGESTER_DURING_INJECT, ExceptionUtils.getFullStackTrace(e));
174 	            }
175 
176 	            if (totalProcessed == 0) {
177 //	                String message = "No xml docs ingested";
178 	                GlobalVariables.getMessageMap().putErrorForSectionId(XmlIngesterConstants.INGESTER_SECTION_ID, XmlIngesterConstants.ERROR_INGESTER_NO_XMLS);
179 	            }
180 	        }
181 	    } finally {
182 	        if (tempFiles.size() > 0) {
183 	            for (File tempFile : tempFiles)
184 	            {
185 	                if (!tempFile.delete())
186 	                {
187 	                    //LOG.warn("Error deleting temp file: " + tempFile);
188 	                }
189 	            }
190 	        }
191 	    }
192 		
193 //	    request.setAttribute("messages", messages);
194 	    return getUIFModelAndView(ingesterForm);
195 	}
196 	
197 	@RequestMapping(method = RequestMethod.POST, params = "methodToCall=close")
198 	public ModelAndView close(@ModelAttribute("KualiForm") XmlIngesterForm ingesterForm, BindingResult result,
199 			HttpServletRequest request, HttpServletResponse response) {
200 
201 		return null;
202 	}
203 
204 }