View Javadoc
1   /**
2    * Copyright 2005-2016 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 edu.sampleu.kew.krad.controller;
17  
18  import edu.sampleu.kew.krad.KEWConstants;
19  import edu.sampleu.kew.krad.form.IngesterForm;
20  import org.apache.commons.lang.StringUtils;
21  import org.apache.commons.lang.exception.ExceptionUtils;
22  import org.kuali.rice.core.api.CoreApiServiceLocator;
23  import org.kuali.rice.core.api.impex.xml.CompositeXmlDocCollection;
24  import org.kuali.rice.core.api.impex.xml.FileXmlDocCollection;
25  import org.kuali.rice.core.api.impex.xml.XmlDoc;
26  import org.kuali.rice.core.api.impex.xml.XmlDocCollection;
27  import org.kuali.rice.core.api.impex.xml.ZipXmlDocCollection;
28  import org.kuali.rice.krad.util.GlobalVariables;
29  import org.kuali.rice.krad.web.controller.UifControllerBase;
30  import org.kuali.rice.krad.web.form.UifFormBase;
31  import org.springframework.stereotype.Controller;
32  import org.springframework.validation.BindingResult;
33  import org.springframework.web.bind.annotation.ModelAttribute;
34  import org.springframework.web.bind.annotation.RequestMapping;
35  import org.springframework.web.bind.annotation.RequestMethod;
36  import org.springframework.web.multipart.MultipartFile;
37  import org.springframework.web.servlet.ModelAndView;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  import java.io.File;
42  import java.io.FileOutputStream;
43  import java.io.IOException;
44  import java.util.ArrayList;
45  import java.util.Collection;
46  import java.util.List;
47  
48  /**
49   * This is a description of what this class does - Venkat don't forget to fill this in. 
50   * 
51   * @author Kuali Rice Team (rice.collab@kuali.org)
52   *
53   */
54  @Controller
55  @RequestMapping(value = "/ingester")
56  public class IngesterController extends UifControllerBase {
57  
58      /**
59       * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
60       */
61      @Override
62      protected IngesterForm createInitialForm(HttpServletRequest request) {
63          return new IngesterForm();
64      }
65  
66  	@Override
67  	@RequestMapping(params = "methodToCall=start")
68  	public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
69  			HttpServletRequest request, HttpServletResponse response) {
70  
71  		IngesterForm ingesterForm = (IngesterForm)form;
72  		
73  //		checkAuthorization(form,"");
74  		return super.start(ingesterForm, result, request, response);
75  	}
76  
77  	@RequestMapping(method = RequestMethod.POST, params = "methodToCall=upload")
78  	public ModelAndView upload(@ModelAttribute("KualiForm") IngesterForm ingesterForm, BindingResult result,
79  			HttpServletRequest request, HttpServletResponse response) {
80  		
81  		List<File> tempFiles = new ArrayList<File>();
82  		
83  		try {
84  			
85  			List<XmlDocCollection> collections = new ArrayList<XmlDocCollection>();
86  			
87  			for (MultipartFile file : ingesterForm.getFiles())
88  	        {
89  	            if (file == null || StringUtils.isBlank(file.getOriginalFilename())) {
90  					continue;
91  				}
92  	            
93  	            // ok, we have to copy it to *another* file because Struts doesn't give us a File
94  	            // reference (which itself is not a bad abstraction) and XmlDocs based on ZipFile
95  	            // can't be constructed without a file reference.
96  	            FileOutputStream fos = null;
97  	            File temp = null;
98  	            try{
99  	                temp = File.createTempFile("ingester", null);
100 	                tempFiles.add(temp);
101 	                fos = new FileOutputStream(temp);
102 	                fos.write(file.getBytes());
103 	            } catch (IOException ioe) {
104 	            	GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID,KEWConstants.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(KEWConstants.INGESTER_SECTION_ID, KEWConstants.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(KEWConstants.INGESTER_SECTION_ID, KEWConstants.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(KEWConstants.INGESTER_SECTION_ID, KEWConstants.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 	                Collection<XmlDocCollection> failed = CoreApiServiceLocator.getXmlIngesterService().ingest(c, GlobalVariables.getUserSession().getPrincipalId());
140 	                boolean txFailed = failed.size() > 0;
141 	                if (txFailed) {
142 	                	GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_FAILED);
143 	                }
144 	                for (XmlDocCollection collection1 : collections)
145 	                {
146 	                    List<? extends XmlDoc> docs = collection1.getXmlDocs();
147 	                    for (XmlDoc doc1 : docs)
148 	                    {
149 	                        if (doc1.isProcessed())
150 	                        {
151 	                            if (!txFailed)
152 	                            {
153 	                                totalProcessed++;
154 	                                GlobalVariables.getMessageMap().putInfoForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.INFO_INGESTER_SUCCESS, doc1.getName(),doc1.getProcessingMessage());
155 //	                                messages.add("Ingested xml doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage()));
156 	                            } else
157 	                            {GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_ROLLEDBACK, doc1.getName(),doc1.getProcessingMessage());
158 //	                                messages.add("Rolled back doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage()));
159 	                            }
160 	                        } else
161 	                        {GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_FAILED_XML, doc1.getName(),doc1.getProcessingMessage());
162 //	                            messages.add("Failed to ingest xml doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage()));
163 	                        }
164 	                    }
165 	                }
166 	            } catch (Exception e) {
167 //	                String message = "Error during ingest";
168 	                //LOG.error(message, e);
169 //	                messages.add(message + ": " + e  + ":\n" + ExceptionUtils.getFullStackTrace(e));
170 	                GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_DURING_INJECT, ExceptionUtils.getFullStackTrace(e));
171 	            }
172 	            if (totalProcessed == 0) {
173 //	                String message = "No xml docs ingested";
174 	                GlobalVariables.getMessageMap().putErrorForSectionId(KEWConstants.INGESTER_SECTION_ID, KEWConstants.ERROR_INGESTER_NO_XMLS);
175 	            }
176 	        }
177 	    } finally {
178 	        if (tempFiles.size() > 0) {
179 	            for (File tempFile : tempFiles)
180 	            {
181 	                if (!tempFile.delete())
182 	                {
183 	                    //LOG.warn("Error deleting temp file: " + tempFile);
184 	                }
185 	            }
186 	        }
187 	    }
188 		
189 //	    request.setAttribute("messages", messages);
190 	    return getUIFModelAndView(ingesterForm);
191 	}
192 	
193 	@RequestMapping(method = RequestMethod.POST, params = "methodToCall=close")
194 	public ModelAndView close(@ModelAttribute("KualiForm") IngesterForm ingesterForm, BindingResult result,
195 			HttpServletRequest request, HttpServletResponse response) {
196 
197 		return null;
198 	}
199 
200 }