001 /* 002 * Copyright 2011 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 */ 016 package org.kuali.kfs.module.bc.document.web.struts; 017 018 import java.io.ByteArrayOutputStream; 019 import java.text.SimpleDateFormat; 020 import java.util.ArrayList; 021 import java.util.Date; 022 import java.util.List; 023 import java.util.Locale; 024 025 import javax.servlet.http.HttpServletRequest; 026 import javax.servlet.http.HttpServletResponse; 027 028 import org.apache.commons.lang.StringUtils; 029 import org.apache.struts.action.ActionForm; 030 import org.apache.struts.action.ActionForward; 031 import org.apache.struts.action.ActionMapping; 032 import org.kuali.kfs.module.bc.BCConstants; 033 import org.kuali.kfs.module.bc.BCKeyConstants; 034 import org.kuali.kfs.module.bc.document.service.BudgetRequestImportService; 035 import org.kuali.kfs.sys.KFSConstants; 036 import org.kuali.kfs.sys.KFSConstants.ReportGeneration; 037 import org.kuali.kfs.sys.context.SpringContext; 038 import org.kuali.rice.kim.bo.Person; 039 import org.kuali.rice.kns.util.GlobalVariables; 040 import org.kuali.rice.kns.util.MessageMap; 041 import org.kuali.rice.kns.util.WebUtils; 042 043 044 /** 045 * Handles Budget Construction Import Requests 046 */ 047 public class BudgetConstructionRequestImportAction extends BudgetConstructionImportExportAction { 048 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BudgetConstructionRequestImportAction.class); 049 050 public ActionForward start(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception { 051 return arg0.findForward("import_export"); 052 } 053 054 /** 055 * Imports file 056 * 057 * @param mapping 058 * @param form 059 * @param request 060 * @param response 061 * @return 062 * @throws Exception 063 */ 064 public ActionForward submit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 065 BudgetConstructionRequestImportForm budgetConstructionImportForm = (BudgetConstructionRequestImportForm) form; 066 BudgetRequestImportService budgetRequestImportService = SpringContext.getBean(BudgetRequestImportService.class); 067 Integer budgetYear = budgetConstructionImportForm.getUniversityFiscalYear(); 068 List<String> messageList = new ArrayList<String>(); 069 SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy ' ' HH:mm:ss", Locale.US); 070 071 boolean isValid = validateFormData(budgetConstructionImportForm); 072 073 if (!isValid) { 074 return mapping.findForward(BCConstants.MAPPING_IMPORT_EXPORT); 075 } 076 077 Person user = GlobalVariables.getUserSession().getPerson(); 078 String principalId = user.getPrincipalId(); 079 Date startTime = new Date(); 080 messageList.add("Import run started " + dateFormatter.format(startTime)); 081 messageList.add(" "); 082 messageList.add("Text file load phase - parsing"); 083 084 List<String> parsingErrors = budgetRequestImportService.processImportFile(budgetConstructionImportForm.getFile().getInputStream(), principalId, getFieldSeparator(budgetConstructionImportForm), getTextFieldDelimiter(budgetConstructionImportForm), budgetConstructionImportForm.getFileType(), budgetYear); 085 086 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 087 if (!parsingErrors.isEmpty()) { 088 messageList.addAll(parsingErrors); 089 messageList.add("Import run finished at " + dateFormatter.getCalendar().getTime().toString()); 090 budgetRequestImportService.generatePdf(messageList, baos); 091 WebUtils.saveMimeOutputStreamAsFile(response, ReportGeneration.PDF_MIME_TYPE, baos, BCConstants.REQUEST_IMPORT_OUTPUT_FILE); 092 return null; 093 } 094 095 messageList.add("Text file load complete"); 096 messageList.add(" "); 097 messageList.add("Validate data phase"); 098 099 List<String> dataValidationErrorList = budgetRequestImportService.validateData(budgetYear, principalId); 100 101 if (!dataValidationErrorList.isEmpty()) { 102 messageList.add("Errors found during data validation"); 103 messageList.addAll(dataValidationErrorList); 104 } 105 106 messageList.add("Validate data compete"); 107 messageList.add(" "); 108 messageList.add("Update budget phase"); 109 110 List<String> updateErrorMessages = budgetRequestImportService.loadBudget(user, budgetConstructionImportForm.getFileType(), budgetYear); 111 112 messageList.addAll(updateErrorMessages); 113 messageList.add("Update budget complete"); 114 messageList.add(" "); 115 Date endTime = new Date(); 116 messageList.add("Import run finished at " + dateFormatter.format(endTime)); 117 118 budgetRequestImportService.generatePdf(messageList, baos); 119 WebUtils.saveMimeOutputStreamAsFile(response, ReportGeneration.PDF_MIME_TYPE, baos, BCConstants.REQUEST_IMPORT_OUTPUT_FILE); 120 return null; 121 } 122 123 /** 124 * 125 * @see org.kuali.kfs.module.bc.document.web.struts.BudgetConstructionImportExportAction#validateFormData(org.kuali.kfs.module.bc.document.web.struts.BudgetConstructionImportExportForm) 126 */ 127 public boolean validateFormData(BudgetConstructionImportExportForm form) { 128 boolean isValid = super.validateFormData(form); 129 BudgetConstructionRequestImportForm requestImportForm = (BudgetConstructionRequestImportForm) form; 130 MessageMap errorMap = GlobalVariables.getMessageMap(); 131 132 if ( requestImportForm.getFile() == null || requestImportForm.getFile().getFileSize() == 0 ) { 133 errorMap.putError(KFSConstants.GLOBAL_ERRORS, BCKeyConstants.ERROR_FILE_IS_REQUIRED); 134 isValid = false; 135 } 136 if ( requestImportForm.getFile() != null && requestImportForm.getFile().getFileSize() == 0 ) { 137 errorMap.putError(KFSConstants.GLOBAL_ERRORS, BCKeyConstants.ERROR_FILE_EMPTY); 138 isValid = false; 139 } 140 if (requestImportForm.getFile() != null && (StringUtils.isBlank(requestImportForm.getFile().getFileName())) ) { 141 errorMap.putError(KFSConstants.GLOBAL_ERRORS, BCKeyConstants.ERROR_FILENAME_REQUIRED); 142 isValid = false; 143 } 144 145 //file type validation 146 if ( StringUtils.isBlank(requestImportForm.getFileType()) ) { 147 errorMap.putError(KFSConstants.GLOBAL_ERRORS, BCKeyConstants.ERROR_FILE_TYPE_IS_REQUIRED); 148 isValid = false; 149 } 150 /*if (!StringUtils.isBlank(requestImportForm.getFileType()) && 151 !requestImportForm.getFileType().equalsIgnoreCase(BCConstants.RequestImportFileType.ANNUAL.toString()) && 152 !requestImportForm.getFileType().equalsIgnoreCase(BCConstants.RequestImportFileType.MONTHLY.toString())) { 153 errorMap.putError(KFSConstants.GLOBAL_ERRORS, BCKeyConstants.ERROR_FILE_TYPE_IS_REQUIRED); 154 isValid = false; 155 }*/ 156 157 return isValid; 158 } 159 160 } 161