001/**
002 * Copyright 2005-2016 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 */
016package edu.sampleu.kew.krad.controller;
017
018import edu.sampleu.kew.krad.KEWConstants;
019import edu.sampleu.kew.krad.form.IngesterForm;
020import org.apache.commons.lang.StringUtils;
021import org.apache.commons.lang.exception.ExceptionUtils;
022import org.kuali.rice.core.api.CoreApiServiceLocator;
023import org.kuali.rice.core.api.impex.xml.CompositeXmlDocCollection;
024import org.kuali.rice.core.api.impex.xml.FileXmlDocCollection;
025import org.kuali.rice.core.api.impex.xml.XmlDoc;
026import org.kuali.rice.core.api.impex.xml.XmlDocCollection;
027import org.kuali.rice.core.api.impex.xml.ZipXmlDocCollection;
028import org.kuali.rice.krad.util.GlobalVariables;
029import org.kuali.rice.krad.web.controller.UifControllerBase;
030import org.kuali.rice.krad.web.form.UifFormBase;
031import org.springframework.stereotype.Controller;
032import org.springframework.validation.BindingResult;
033import org.springframework.web.bind.annotation.ModelAttribute;
034import org.springframework.web.bind.annotation.RequestMapping;
035import org.springframework.web.bind.annotation.RequestMethod;
036import org.springframework.web.multipart.MultipartFile;
037import org.springframework.web.servlet.ModelAndView;
038
039import javax.servlet.http.HttpServletRequest;
040import javax.servlet.http.HttpServletResponse;
041import java.io.File;
042import java.io.FileOutputStream;
043import java.io.IOException;
044import java.util.ArrayList;
045import java.util.Collection;
046import 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")
056public class IngesterController extends UifControllerBase {
057
058    /**
059     * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
060     */
061    @Override
062    protected IngesterForm createInitialForm(HttpServletRequest request) {
063        return new IngesterForm();
064    }
065
066        @Override
067        @RequestMapping(params = "methodToCall=start")
068        public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
069                        HttpServletRequest request, HttpServletResponse response) {
070
071                IngesterForm ingesterForm = (IngesterForm)form;
072                
073//              checkAuthorization(form,"");
074                return super.start(ingesterForm, result, request, response);
075        }
076
077        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=upload")
078        public ModelAndView upload(@ModelAttribute("KualiForm") IngesterForm ingesterForm, BindingResult result,
079                        HttpServletRequest request, HttpServletResponse response) {
080                
081                List<File> tempFiles = new ArrayList<File>();
082                
083                try {
084                        
085                        List<XmlDocCollection> collections = new ArrayList<XmlDocCollection>();
086                        
087                        for (MultipartFile file : ingesterForm.getFiles())
088                {
089                    if (file == null || StringUtils.isBlank(file.getOriginalFilename())) {
090                                        continue;
091                                }
092                    
093                    // ok, we have to copy it to *another* file because Struts doesn't give us a File
094                    // reference (which itself is not a bad abstraction) and XmlDocs based on ZipFile
095                    // can't be constructed without a file reference.
096                    FileOutputStream fos = null;
097                    File temp = null;
098                    try{
099                        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}