View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 1.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/ecl1.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      @Override
59      protected Class<IngesterForm> formType() {
60          return IngesterForm.class;
61      }
62  
63  	@Override
64  	@RequestMapping(params = "methodToCall=start")
65  	public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
66  			HttpServletRequest request, HttpServletResponse response) {
67  
68  		IngesterForm ingesterForm = (IngesterForm)form;
69  		
70  //		checkAuthorization(form,"");
71  		return super.start(ingesterForm, result, request, response);
72  	}
73  
74  	@RequestMapping(method = RequestMethod.POST, params = "methodToCall=upload")
75  	public ModelAndView upload(@ModelAttribute("KualiForm") IngesterForm ingesterForm, BindingResult result,
76  			HttpServletRequest request, HttpServletResponse response) {
77  		
78  		List<File> tempFiles = new ArrayList<File>();
79  		
80  		try {
81  			
82  			List<XmlDocCollection> collections = new ArrayList<XmlDocCollection>();
83  			
84  			for (MultipartFile file : ingesterForm.getFiles())
85  	        {
86  	            if (file == null || StringUtils.isBlank(file.getOriginalFilename())) {
87  					continue;
88  				}
89  	            
90  	            // ok, we have to copy it to *another* file because Struts doesn't give us a File
91  	            // reference (which itself is not a bad abstraction) and XmlDocs based on ZipFile
92  	            // can't be constructed without a file reference.
93  	            FileOutputStream fos = null;
94  	            File temp = null;
95  	            try{
96  	                temp = File.createTempFile("ingester", null);
97  	                tempFiles.add(temp);
98  	                fos = new FileOutputStream(temp);
99  	                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 }