001package org.kuali.ole.select.controller; 002 003import org.kuali.ole.OLEConstants; 004import org.kuali.ole.select.OleSelectConstant; 005import org.kuali.ole.select.document.OLEPurchaseOrderBatchDocument; 006import org.kuali.ole.select.document.service.OLEPurchaseOrderBatchService; 007import org.kuali.ole.select.form.OLEPurchaseOrderBatchForm; 008import org.kuali.ole.sys.OLEKeyConstants; 009import org.kuali.ole.sys.context.SpringContext; 010import org.kuali.rice.core.api.config.property.ConfigContext; 011import org.kuali.rice.krad.UserSession; 012 013import org.kuali.rice.krad.bo.Note; 014import org.kuali.rice.krad.bo.PersistableBusinessObject; 015import org.kuali.rice.krad.uif.UifConstants; 016import org.kuali.rice.krad.uif.UifParameters; 017import org.kuali.rice.krad.util.GlobalVariables; 018import org.kuali.rice.krad.web.controller.TransactionalDocumentControllerBase; 019import org.kuali.rice.krad.web.form.DocumentFormBase; 020import org.kuali.rice.krad.web.form.UifFormBase; 021import org.kuali.rice.ksb.messaging.threadpool.KSBThreadPool; 022import org.kuali.rice.ksb.service.KSBServiceLocator; 023import org.springframework.stereotype.Controller; 024import org.springframework.validation.BindingResult; 025import org.springframework.web.bind.annotation.ModelAttribute; 026import org.springframework.web.bind.annotation.RequestMapping; 027import org.springframework.web.multipart.MultipartFile; 028import org.springframework.web.servlet.ModelAndView; 029 030import javax.servlet.http.HttpServletRequest; 031import javax.servlet.http.HttpServletResponse; 032import java.io.*; 033import java.text.SimpleDateFormat; 034import java.util.*; 035 036/** 037 * Created with IntelliJ IDEA. 038 * User: gopalp 039 * Date: 5/15/15 040 * Time: 1:21 PM 041 * To change this template use File | Settings | File Templates. 042 */ 043 044@Controller 045@RequestMapping(value = "/olePurchaseOrderBatchController") 046public class OLEPurchaseOrderBatchController extends TransactionalDocumentControllerBase { 047 048 OLEPurchaseOrderBatchService olePurchaseOrderBatchService = (OLEPurchaseOrderBatchService)SpringContext.getService("olePurchaseOrderBatchService"); 049 private OLEPurchaseOrderBatchDocument purchaseOrderBatchDocument; 050 private String principalName; 051 052 @Override 053 protected DocumentFormBase createInitialForm(HttpServletRequest request) { 054 OLEPurchaseOrderBatchForm olePurchaseOrderBatchForm = new OLEPurchaseOrderBatchForm(); 055 return olePurchaseOrderBatchForm; 056 } 057 058 @RequestMapping(params = "methodToCall=docHandler") 059 public ModelAndView docHandler(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result, 060 HttpServletRequest request, HttpServletResponse response) throws Exception { 061 OLEPurchaseOrderBatchForm olePurchaseOrderBatchForm = (OLEPurchaseOrderBatchForm)form; 062 ModelAndView modelAndView = super.docHandler(olePurchaseOrderBatchForm,result,request,response); 063 return modelAndView; 064 } 065 066 @RequestMapping(params = "methodToCall=upload") 067 public ModelAndView createFileForPOBA(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result, 068 HttpServletRequest request, HttpServletResponse response) { 069 OLEPurchaseOrderBatchForm olePurchaseOrderBatchForm = (OLEPurchaseOrderBatchForm)form; 070 OLEPurchaseOrderBatchDocument olePurchaseOrderBatchDocument = (OLEPurchaseOrderBatchDocument)olePurchaseOrderBatchForm.getDocument(); 071 if(olePurchaseOrderBatchDocument.getDocIdIngestFile() == null) { 072 GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.INPUT_FILE_REQ); 073 return getUIFModelAndView(olePurchaseOrderBatchForm); 074 } 075 try { 076 olePurchaseOrderBatchService.downloadCSV(olePurchaseOrderBatchService.createFileForPOBA(olePurchaseOrderBatchDocument)); 077 olePurchaseOrderBatchDocument.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.SUCCESSFULLEY_UPLOADED)); 078 079 080 } catch(Exception e) { 081 GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.INPUT_FILE_REQ); 082 return getUIFModelAndView(olePurchaseOrderBatchForm); 083 } 084 return getUIFModelAndView(olePurchaseOrderBatchForm); 085 086 } 087 088 089 @RequestMapping(params = "methodToCall=route") 090 public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result, 091 HttpServletRequest request, HttpServletResponse response) { 092 OLEPurchaseOrderBatchForm olePurchaseOrderBatchForm = (OLEPurchaseOrderBatchForm) form; 093 OLEPurchaseOrderBatchDocument olePurchaseOrderBatchDocument = (OLEPurchaseOrderBatchDocument) olePurchaseOrderBatchForm.getDocument(); 094 olePurchaseOrderBatchDocument.setMessage(""); 095 principalName = GlobalVariables.getUserSession().getPrincipalName(); 096 final UserSession userSession = new UserSession(principalName); 097 098 olePurchaseOrderBatchDocument.getDocumentHeader().setDocumentDescription(OLEConstants.OLEPurchaseOrderBulkAmendment.DOC_DESC); 099 if(GlobalVariables.getUserSession().getPrincipalName() != null) { 100 olePurchaseOrderBatchDocument.setPrincipalName(GlobalVariables.getUserSession().getPrincipalName()); 101 } 102 if (olePurchaseOrderBatchDocument.getIngestedFile() == null) { 103 GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.INPUT_FILE_REQ); 104 return getUIFModelAndView(olePurchaseOrderBatchForm); 105 } 106 if (olePurchaseOrderBatchDocument.getType() == null) { 107 GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.SELECT_PROCESS); 108 return getUIFModelAndView(olePurchaseOrderBatchForm); 109 } 110 if (olePurchaseOrderBatchDocument.getType().equals(OLEConstants.OLEPurchaseOrderBulkAmendment.SPEC_TIME) && (olePurchaseOrderBatchDocument.getBatchStartDate() == null || olePurchaseOrderBatchDocument.getTime() == null)) { 111 GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.ERROR_DATE); 112 return getUIFModelAndView(olePurchaseOrderBatchForm); 113 } 114 if(olePurchaseOrderBatchDocument.getType().equals(OLEConstants.OLEPurchaseOrderBulkAmendment.SPEC_TIME) && !(olePurchaseOrderBatchDocument.getTime().length() == 8)) { 115 GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.ERROR_TIME_LENGTH); 116 return getUIFModelAndView(olePurchaseOrderBatchForm); 117 } 118 olePurchaseOrderBatchDocument.setUploadFileName(olePurchaseOrderBatchDocument.getDocumentNumber()+"_"+olePurchaseOrderBatchDocument.getIngestedFile().getOriginalFilename()); 119 if (olePurchaseOrderBatchDocument.getType().equals(OLEConstants.OLEPurchaseOrderBulkAmendment.IMMEDIATE)) { 120 KSBThreadPool threadPool = KSBServiceLocator.getThreadPool(); 121 try { 122 File file = new File(System.getProperty("java.io.tmpdir") + "/" + olePurchaseOrderBatchDocument.getIngestedFile().getOriginalFilename()); 123 olePurchaseOrderBatchDocument.getIngestedFile().transferTo(file); 124 threadPool.submit(new POBACallable(olePurchaseOrderBatchDocument, userSession, file)); 125 } catch (IOException e) { 126 e.printStackTrace(); 127 } 128 } else { 129 try { 130 String jobId = olePurchaseOrderBatchDocument.getDocumentNumber(); 131 String fileDirectory = ConfigContext.getCurrentContextConfig().getProperty(org.kuali.ole.OLEConstants.STAGING_DIRECTORY)+"/"+olePurchaseOrderBatchDocument.getUploadFileName(); 132 MultipartFile multipartFile = olePurchaseOrderBatchDocument.getIngestedFile(); 133 multipartFile.transferTo(new File(fileDirectory)); 134 String date = new SimpleDateFormat(OLEConstants.OLEPurchaseOrderBulkAmendment.FORMAT).format(olePurchaseOrderBatchDocument.getBatchStartDate()); 135 date += " " + olePurchaseOrderBatchDocument.getTime(); 136 purchaseOrderBatchDocument = olePurchaseOrderBatchDocument; 137 Timer t = new Timer(); 138 t.schedule(new TimerTask() { 139 public void run() { 140 GlobalVariables.setUserSession(new UserSession(principalName)); 141 getOlePurchaseOrderBatchService().readFile(purchaseOrderBatchDocument, userSession, null); 142 } 143 }, new SimpleDateFormat(OLEConstants.OLEPurchaseOrderBulkAmendment.TIMESTAMP_FORMAT).parse(date)); 144 } catch (Exception e) { 145 e.printStackTrace(); 146 } 147 } 148 return super.route(olePurchaseOrderBatchForm,result,request,response); 149 } 150 151 @RequestMapping(params = "methodToCall=blanketApprove") 152 public ModelAndView blanketApprove(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result, 153 HttpServletRequest request, HttpServletResponse response) throws Exception { 154 155 OLEPurchaseOrderBatchForm olePurchaseOrderBatchForm = (OLEPurchaseOrderBatchForm) form; 156 OLEPurchaseOrderBatchDocument olePurchaseOrderBatchDocument = (OLEPurchaseOrderBatchDocument) olePurchaseOrderBatchForm.getDocument(); 157 olePurchaseOrderBatchDocument.setMessage(""); 158 principalName = GlobalVariables.getUserSession().getPrincipalName(); 159 final UserSession userSession = new UserSession(principalName); 160 161 olePurchaseOrderBatchDocument.getDocumentHeader().setDocumentDescription(OLEConstants.OLEPurchaseOrderBulkAmendment.DOC_DESC); 162 if(GlobalVariables.getUserSession().getPrincipalName() != null) { 163 olePurchaseOrderBatchDocument.setPrincipalName(GlobalVariables.getUserSession().getPrincipalName()); 164 } 165 if (olePurchaseOrderBatchDocument.getIngestedFile() == null) { 166 GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.INPUT_FILE_REQ); 167 return getUIFModelAndView(olePurchaseOrderBatchForm); 168 } 169 if (olePurchaseOrderBatchDocument.getType() == null) { 170 GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.SELECT_PROCESS); 171 return getUIFModelAndView(olePurchaseOrderBatchForm); 172 } 173 if (olePurchaseOrderBatchDocument.getType().equals(OLEConstants.OLEPurchaseOrderBulkAmendment.SPEC_TIME) && (olePurchaseOrderBatchDocument.getBatchStartDate() == null || olePurchaseOrderBatchDocument.getTime() == null)) { 174 GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.ERROR_DATE); 175 return getUIFModelAndView(olePurchaseOrderBatchForm); 176 } 177 if(olePurchaseOrderBatchDocument.getType().equals(OLEConstants.OLEPurchaseOrderBulkAmendment.SPEC_TIME) && !(olePurchaseOrderBatchDocument.getTime().length() == 8)) { 178 GlobalVariables.getMessageMap().putError(OleSelectConstant.POBA_VIEW, OLEKeyConstants.ERROR_TIME_LENGTH); 179 return getUIFModelAndView(olePurchaseOrderBatchForm); 180 } 181 olePurchaseOrderBatchDocument.setUploadFileName(olePurchaseOrderBatchDocument.getDocumentNumber()+"_"+olePurchaseOrderBatchDocument.getIngestedFile().getOriginalFilename()); 182 if (olePurchaseOrderBatchDocument.getType().equals(OLEConstants.OLEPurchaseOrderBulkAmendment.IMMEDIATE)) { 183 getOlePurchaseOrderBatchService().readFile(olePurchaseOrderBatchDocument, userSession, null); 184 } else { 185 try { 186 String jobId = olePurchaseOrderBatchDocument.getDocumentNumber(); 187 String fileDirectory = ConfigContext.getCurrentContextConfig().getProperty(org.kuali.ole.OLEConstants.STAGING_DIRECTORY)+"/"+olePurchaseOrderBatchDocument.getUploadFileName(); 188 MultipartFile multipartFile = olePurchaseOrderBatchDocument.getIngestedFile(); 189 multipartFile.transferTo(new File(fileDirectory)); 190 String date = new SimpleDateFormat(OLEConstants.OLEPurchaseOrderBulkAmendment.FORMAT).format(olePurchaseOrderBatchDocument.getBatchStartDate()); 191 date += " " + olePurchaseOrderBatchDocument.getTime(); 192 purchaseOrderBatchDocument = olePurchaseOrderBatchDocument; 193 Timer t = new Timer(); 194 t.schedule(new TimerTask() { 195 public void run() { 196 GlobalVariables.setUserSession(new UserSession(principalName)); 197 getOlePurchaseOrderBatchService().readFile(purchaseOrderBatchDocument, userSession, null); 198 } 199 }, new SimpleDateFormat(OLEConstants.OLEPurchaseOrderBulkAmendment.TIMESTAMP_FORMAT).parse(date)); 200 } catch (Exception e) { 201 e.printStackTrace(); 202 } 203 } 204 return super.blanketApprove(olePurchaseOrderBatchForm, result, request, response); 205 } 206 207 208 @RequestMapping(params = "methodToCall=approveDocument") 209 public ModelAndView approveDocument(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result, 210 HttpServletRequest request, HttpServletResponse response) throws Exception { 211 212 principalName = GlobalVariables.getUserSession().getPrincipalName(); 213 final UserSession userSession = new UserSession(principalName); 214 215 OLEPurchaseOrderBatchForm olePurchaseOrderBatchForm = (OLEPurchaseOrderBatchForm)form; 216 OLEPurchaseOrderBatchDocument olePurchaseOrderBatchDocument = (OLEPurchaseOrderBatchDocument)olePurchaseOrderBatchForm.getDocument(); 217 olePurchaseOrderBatchDocument.getDocumentHeader().setDocumentDescription(OLEConstants.OLEPurchaseOrderBulkAmendment.DOC_DESC); 218 GlobalVariables.setUserSession(new UserSession(olePurchaseOrderBatchDocument.getPrincipalName())); 219 if(olePurchaseOrderBatchDocument.getType().equals(OLEConstants.OLEPurchaseOrderBulkAmendment.IMMEDIATE)) { 220 getOlePurchaseOrderBatchService().readFile(olePurchaseOrderBatchDocument, userSession, null); 221 } else { 222 try { 223 String date = new SimpleDateFormat(OLEConstants.OLEPurchaseOrderBulkAmendment.FORMAT).format(olePurchaseOrderBatchDocument.getBatchStartDate()); 224 date += " "+olePurchaseOrderBatchDocument.getTime(); 225 purchaseOrderBatchDocument = olePurchaseOrderBatchDocument; 226 Timer t=new Timer(); 227 t.schedule(new TimerTask() { 228 public void run() { 229 GlobalVariables.setUserSession(new UserSession(purchaseOrderBatchDocument.getPrincipalName())); 230 getOlePurchaseOrderBatchService().readFile(purchaseOrderBatchDocument, userSession, null); 231 } 232 }, new SimpleDateFormat(OLEConstants.OLEPurchaseOrderBulkAmendment.TIMESTAMP_FORMAT).parse(date)); 233 } catch (Exception e) { 234 e.printStackTrace(); 235 } 236 } 237 238 super.approve(olePurchaseOrderBatchForm, result, request, response); 239 return closeDocument(olePurchaseOrderBatchForm, result, request, response); 240 241 } 242 243 @RequestMapping(params = "methodToCall=" + "closeDocument") 244 public ModelAndView closeDocument(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 245 HttpServletRequest request, HttpServletResponse response) throws Exception { 246 247 String baseUrl = ConfigContext.getCurrentContextConfig().getProperty(org.kuali.ole.OLEPropertyConstants.OLE_URL_BASE); 248 String url = baseUrl + "/portal.do"; 249 Properties props = new Properties(); 250 props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH); 251 if (org.apache.commons.lang.StringUtils.isNotBlank(form.getReturnFormKey())) { 252 props.put(UifParameters.FORM_KEY, form.getReturnFormKey()); 253 } 254 GlobalVariables.getUifFormManager().removeSessionForm(form); 255 return performRedirect(form, url, props); 256 } 257 258 public OLEPurchaseOrderBatchService getOlePurchaseOrderBatchService() { 259 if(olePurchaseOrderBatchService == null) { 260 olePurchaseOrderBatchService = (OLEPurchaseOrderBatchService)SpringContext.getService("olePurchaseOrderBatchService"); 261 } 262 return olePurchaseOrderBatchService; 263 } 264 265 @RequestMapping(params = "methodToCall=disapprove") 266 public ModelAndView disapprove(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result, 267 HttpServletRequest request, HttpServletResponse response) throws Exception { 268 OLEPurchaseOrderBatchForm olePurchaseOrderBatchForm = (OLEPurchaseOrderBatchForm) form; 269 OLEPurchaseOrderBatchDocument olePurchaseOrderBatchDocument = (OLEPurchaseOrderBatchDocument) olePurchaseOrderBatchForm.getDocument(); 270 271 Note noteObj = getDocumentService().createNoteFromDocument(olePurchaseOrderBatchDocument, "Dispproved at Exceptione By : " + GlobalVariables.getUserSession().getPerson().getName()); 272 PersistableBusinessObject noteParent = olePurchaseOrderBatchDocument.getNoteTarget(); 273 List<Note> noteList = getNoteService().getByRemoteObjectId(noteParent.getObjectId()); 274 noteList.add(noteObj); 275 getNoteService().saveNoteList(noteList); 276 getNoteService().save(noteObj); 277 olePurchaseOrderBatchDocument.setNotes(noteList); 278 getDocumentService().saveDocument(olePurchaseOrderBatchDocument); 279 280 performWorkflowAction(olePurchaseOrderBatchForm, UifConstants.WorkflowAction.DISAPPROVE, true); 281 return returnToPrevious(olePurchaseOrderBatchForm); 282 283 } 284 285}