001package org.kuali.ole.select.controller; 002 003import org.apache.log4j.Logger; 004import org.kuali.ole.OLEConstants; 005import org.kuali.ole.select.bo.OleCheckListBo; 006import org.kuali.ole.service.OleCheckListMaintenanceDocumentService; 007import org.kuali.rice.core.api.CoreApiServiceLocator; 008import org.kuali.rice.core.api.config.property.ConfigurationService; 009import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; 010import org.kuali.rice.core.api.util.RiceKeyConstants; 011import org.kuali.rice.kim.api.identity.Person; 012import org.kuali.rice.krad.bo.PersistableAttachment; 013import org.kuali.rice.krad.bo.PersistableBusinessObject; 014import org.kuali.rice.krad.maintenance.MaintenanceDocument; 015import org.kuali.rice.krad.maintenance.MaintenanceUtils; 016import org.kuali.rice.krad.service.KRADServiceLocator; 017import org.kuali.rice.krad.util.GlobalVariables; 018import org.kuali.rice.krad.util.KRADConstants; 019import org.kuali.rice.krad.web.controller.MaintenanceDocumentController; 020import org.kuali.rice.krad.web.form.DocumentFormBase; 021import org.kuali.rice.krad.web.form.MaintenanceDocumentForm; 022import org.kuali.rice.krad.web.form.UifFormBase; 023import org.springframework.stereotype.Controller; 024import org.springframework.util.FileCopyUtils; 025import org.springframework.validation.BindingResult; 026import org.springframework.web.bind.annotation.ModelAttribute; 027import org.springframework.web.bind.annotation.RequestMapping; 028import org.springframework.web.multipart.MultipartFile; 029import org.springframework.web.servlet.ModelAndView; 030 031import javax.servlet.http.HttpServletRequest; 032import javax.servlet.http.HttpServletResponse; 033import java.io.*; 034import java.sql.Timestamp; 035import java.util.HashMap; 036import java.util.Map; 037import java.util.UUID; 038 039/** 040 * OleCheckListController is the controller class for CheckList Maintenance Document. 041 */ 042@Controller 043@RequestMapping(value = "/oleCheckListMaintenance") 044public class OleCheckListController extends MaintenanceDocumentController { 045 protected static final Logger LOG = Logger.getLogger(OleCheckListController.class); 046 047 /** 048 * This method is used to download the attachment. 049 * 050 * @param form 051 * @param result 052 * @param request 053 * @param response 054 * @return ModelAndView 055 */ 056 @RequestMapping(params = "methodToCall=downloadAttachment") 057 public ModelAndView downloadAttachment(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 058 HttpServletRequest request, HttpServletResponse response) { 059 060 String checkListId = request.getParameter("oleCheckListId"); 061 Map criteriaMap = new HashMap(); 062 criteriaMap.put("oleCheckListId", checkListId); 063 OleCheckListBo checkListBo = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleCheckListBo.class, criteriaMap); 064 String fileName = ""; 065 String objectId = ""; 066 if (checkListBo != null) { 067 fileName = checkListBo.getFileName(); 068 objectId = checkListBo.getObjectId(); 069 } 070 String directory = getKualiConfigurationService().getPropertyValueAsString( 071 KRADConstants.ATTACHMENTS_PENDING_DIRECTORY_KEY) + File.separator + "checkList" + File.separator + checkListBo.getRemoteObjectIdentifier(); 072 if (LOG.isInfoEnabled()) { 073 LOG.info("file location : " + directory); 074 } 075 076 try { 077 File file = new File(directory); 078 response.setContentType(checkListBo.getMimeType()); 079 response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); 080 response.setHeader("Expires", "0"); 081 response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); 082 response.setHeader("Pragma", "public"); 083 response.setContentLength((int) file.length()); 084 InputStream fis = new BufferedInputStream(new FileInputStream(file)); 085 FileCopyUtils.copy(fis, response.getOutputStream()); 086 } catch (IOException e) { 087 088 } 089 090 return null; 091 } 092 093 /** 094 * This method invokes setupMaintenanceForDelete method to populate the document for deleting. 095 * 096 * @param form 097 * @param result 098 * @param request 099 * @param response 100 * @return ModelAndView 101 * @throws Exception 102 */ 103 @RequestMapping(params = "methodToCall=" + "maintenanceDelete") 104 public ModelAndView maintenanceDelete(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result, 105 HttpServletRequest request, HttpServletResponse response) throws Exception { 106 setupMaintenanceForDelete(form, request, "Delete"); 107 return getUIFModelAndView(form); 108 } 109 110 /** 111 * This method invokes deleteAttachment method to delete attachment and set the error accordingly .. 112 * 113 * @param form 114 * @param result 115 * @param request 116 * @param response 117 * @return ModelAndView 118 * @throws Exception 119 */ 120 @RequestMapping(params = "methodToCall=" + "deleteDocument") 121 public ModelAndView deleteDocument(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result, 122 HttpServletRequest request, HttpServletResponse response) throws Exception { 123 MaintenanceDocument document = form.getDocument(); 124 OleCheckListBo oleCheckListBo = new OleCheckListBo(); 125 if (document.getDocumentDataObject() != null) { 126 oleCheckListBo = (OleCheckListBo) document.getDocumentDataObject(); 127 if (oleCheckListBo != null && oleCheckListBo.getRemoteObjectIdentifier() != null) { 128 boolean isDeleted = deleteAttachment(oleCheckListBo); 129 if (isDeleted) { 130 KRADServiceLocator.getBusinessObjectService().delete(oleCheckListBo); 131 GlobalVariables.getMessageMap().putInfoWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, 132 OLEConstants.OleLicenseRequest.MSG_DELETE_DOC); 133 } else { 134 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_MESSAGES, OLEConstants.OleLicenseRequest.ERROR_FILE_NOT_FOUND); 135 } 136 } else { 137 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_MESSAGES, OLEConstants.OleLicenseRequest.ERROR_CHECKLIST_NOT_FOUND); 138 } 139 } 140 return close(form, result, request, response); 141 } 142 143 /** 144 * This method populates confirmation to delete the document. 145 * 146 * @param form 147 * @param request 148 * @param maintenanceAction 149 */ 150 protected void setupMaintenanceForDelete(MaintenanceDocumentForm form, HttpServletRequest request, String maintenanceAction) { 151 MaintenanceDocument document = form.getDocument(); 152 if (document == null) { 153 document = getMaintenanceDocumentService() 154 .setupNewMaintenanceDocument(form.getDataObjectClassName(), form.getDocTypeName(), 155 maintenanceAction); 156 157 form.setDocument(document); 158 form.setDocTypeName(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName()); 159 } 160 161 form.setMaintenanceAction(maintenanceAction); 162 getMaintenanceDocumentService().setupMaintenanceObjectForDelete(document, maintenanceAction, request.getParameterMap()); 163 MaintenanceUtils.checkForLockingDocument(document, false); 164 } 165 166 /** 167 * This method is used to delete the attachment(checklist template) file. 168 * 169 * @param oleCheckListBo 170 * @return boolean 171 */ 172 protected boolean deleteAttachment(OleCheckListBo oleCheckListBo) { 173 if (oleCheckListBo != null) { 174 String fullPathUniqueFileName = getKualiConfigurationService().getPropertyValueAsString( 175 KRADConstants.ATTACHMENTS_PENDING_DIRECTORY_KEY) + File.separator + "checkList" + File.separator + oleCheckListBo.getRemoteObjectIdentifier(); 176 File attachmentFile = new File(fullPathUniqueFileName); 177 return attachmentFile.delete(); 178 } 179 return false; 180 } 181 182 @Override 183 protected OleCheckListMaintenanceDocumentService getMaintenanceDocumentService() { 184 return GlobalResourceLoader.getService("oleCheckListMaintenanceDocumentService"); 185 } 186 187 public ConfigurationService getKualiConfigurationService() { 188 return KRADServiceLocator.getKualiConfigurationService(); 189 } 190 191 /** 192 * This method store the uploaded checklist template in the specified path and also populates the document 193 * for routing 194 * 195 * @param docForm 196 * @param result 197 * @param request 198 * @param response 199 * @return ModelAndView 200 */ 201 @Override 202 @RequestMapping(params = "methodToCall=route") 203 public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase docForm, BindingResult result, 204 HttpServletRequest request, HttpServletResponse response) { 205 206 ModelAndView modelAndView; 207 MaintenanceDocumentForm form = (MaintenanceDocumentForm) docForm; 208 MultipartFile attachmentFile = form.getAttachmentFile(); 209 MaintenanceDocument document = form.getDocument(); 210 OleCheckListBo oleCheckListBo = (OleCheckListBo) document.getNewMaintainableObject().getDataObject(); 211 OleCheckListBo oleCheckListBoOld = (OleCheckListBo) document.getOldMaintainableObject().getDataObject(); 212 Person kualiUser = GlobalVariables.getUserSession().getPerson(); 213 oleCheckListBo.setAuthor(kualiUser.getPrincipalId()); 214 oleCheckListBo.setLastModified((Timestamp) CoreApiServiceLocator.getDateTimeService().getCurrentTimestamp()); 215 216 if (kualiUser == null) { 217 LOG.error("Current UserSession has a null Person"); 218 } 219 if (form.getMaintenanceAction().equals("New")) { 220 try { 221 if (oleCheckListBo.getFileName() == null) { 222 if ((attachmentFile.getSize() == 0 || (attachmentFile.getOriginalFilename().isEmpty() || 223 attachmentFile.getOriginalFilename().equalsIgnoreCase("")))) { 224 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, 225 RiceKeyConstants.ERROR_UPLOADFILE_EMPTY, ""); 226 modelAndView = getUIFModelAndView(form); 227 return modelAndView; 228 } else { 229 oleCheckListBo.setFileName(attachmentFile.getOriginalFilename()); 230 oleCheckListBo.setMimeType(attachmentFile.getContentType()); 231 String remoteObjectIdentifier = UUID.randomUUID().toString(); 232 if (storeAttachment(attachmentFile, remoteObjectIdentifier)) 233 oleCheckListBo.setRemoteObjectIdentifier(remoteObjectIdentifier); 234 } 235 } 236 } catch (Exception e) { 237 LOG.error("Error while storing the attachment"); 238 } 239 } 240 if (form.getMaintenanceAction().equals("Edit")) { 241 try { 242 if (!(attachmentFile.getOriginalFilename().isEmpty() || 243 attachmentFile.getOriginalFilename().equalsIgnoreCase(""))) { 244 if (attachmentFile.getSize() == 0) { 245 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, 246 RiceKeyConstants.ERROR_UPLOADFILE_EMPTY, attachmentFile.getOriginalFilename()); 247 } else { 248 oleCheckListBo.setFileName(attachmentFile.getOriginalFilename()); 249 oleCheckListBo.setMimeType(attachmentFile.getContentType()); 250 String remoteObjectIdentifier = UUID.randomUUID().toString(); 251 if (storeAttachment(attachmentFile, remoteObjectIdentifier)) 252 deleteAttachment(oleCheckListBoOld); 253 oleCheckListBo.setRemoteObjectIdentifier(remoteObjectIdentifier); 254 } 255 } 256 } catch (Exception e) { 257 LOG.error("Error while storing the attachment"); 258 } 259 } 260 261 262 modelAndView = super.route(form, result, request, response); 263 264 if (document.getNewMaintainableObject().getDataObject() instanceof PersistableAttachment) { 265 PersistableAttachment bo = (PersistableAttachment) getBusinessObjectService() 266 .retrieve((PersistableBusinessObject) document.getNewMaintainableObject().getDataObject()); 267 request.setAttribute("fileName", bo.getFileName()); 268 } 269 270 modelAndView = getUIFModelAndView(form); 271 272 return modelAndView; 273 } 274 275 276 /** 277 * Performs the blanket approve workflow action on the form document instance 278 * 279 * @param docForm - document form base containing the document instance that will be blanket approved 280 * @return ModelAndView 281 */ 282 @RequestMapping(params = "methodToCall=blanketApprove") 283 public ModelAndView blanketApprove(@ModelAttribute("KualiForm") DocumentFormBase docForm, BindingResult result, 284 HttpServletRequest request, HttpServletResponse response) throws Exception { 285 ModelAndView modelAndView; 286 MaintenanceDocumentForm form = (MaintenanceDocumentForm) docForm; 287 MultipartFile attachmentFile = form.getAttachmentFile(); 288 MaintenanceDocument document = form.getDocument(); 289 OleCheckListBo oleCheckListBo = (OleCheckListBo) document.getNewMaintainableObject().getDataObject(); 290 OleCheckListBo oleCheckListBoOld = (OleCheckListBo) document.getOldMaintainableObject().getDataObject(); 291 Person kualiUser = GlobalVariables.getUserSession().getPerson(); 292 oleCheckListBo.setAuthor(kualiUser.getPrincipalId()); 293 oleCheckListBo.setLastModified((Timestamp) CoreApiServiceLocator.getDateTimeService().getCurrentTimestamp()); 294 295 if (kualiUser == null) { 296 LOG.error("Current UserSession has a null Person"); 297 } 298 if (form.getMaintenanceAction().equals("New")) { 299 try { 300 if (oleCheckListBo.getFileName() == null) { 301 if ((attachmentFile.getSize() == 0 || (attachmentFile.getOriginalFilename().isEmpty() || 302 attachmentFile.getOriginalFilename().equalsIgnoreCase("")))) { 303 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, 304 RiceKeyConstants.ERROR_UPLOADFILE_EMPTY, ""); 305 modelAndView = getUIFModelAndView(form); 306 return modelAndView; 307 } else { 308 oleCheckListBo.setFileName(attachmentFile.getOriginalFilename()); 309 oleCheckListBo.setMimeType(attachmentFile.getContentType()); 310 String remoteObjectIdentifier = UUID.randomUUID().toString(); 311 if (storeAttachment(attachmentFile, remoteObjectIdentifier)) 312 oleCheckListBo.setRemoteObjectIdentifier(remoteObjectIdentifier); 313 } 314 } 315 316 } catch (Exception e) { 317 LOG.error("Error while storing the attachment"); 318 } 319 } 320 if (form.getMaintenanceAction().equals("Edit")) { 321 try { 322 if (!(attachmentFile.getOriginalFilename().isEmpty() || 323 attachmentFile.getOriginalFilename().equalsIgnoreCase(""))) { 324 if (attachmentFile.getSize() == 0) { 325 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, 326 RiceKeyConstants.ERROR_UPLOADFILE_EMPTY, attachmentFile.getOriginalFilename()); 327 } else { 328 oleCheckListBo.setFileName(attachmentFile.getOriginalFilename()); 329 oleCheckListBo.setMimeType(attachmentFile.getContentType()); 330 String remoteObjectIdentifier = UUID.randomUUID().toString(); 331 if (storeAttachment(attachmentFile, remoteObjectIdentifier)) 332 deleteAttachment(oleCheckListBoOld); 333 oleCheckListBo.setRemoteObjectIdentifier(remoteObjectIdentifier); 334 } 335 } 336 } catch (Exception e) { 337 LOG.error("Error while storing the attachment"); 338 } 339 } 340 341 342 modelAndView = super.blanketApprove(form, result, request, response); 343 344 if (document.getNewMaintainableObject().getDataObject() instanceof PersistableAttachment) { 345 PersistableAttachment bo = (PersistableAttachment) getBusinessObjectService() 346 .retrieve((PersistableBusinessObject) document.getNewMaintainableObject().getDataObject()); 347 request.setAttribute("fileName", bo.getFileName()); 348 } 349 350 modelAndView = getUIFModelAndView(form); 351 352 return modelAndView; 353 } 354 355 /** 356 * This method stores uploaded checklist template in the specified path. 357 * 358 * @param attachedFile 359 * @param fileName 360 * @return boolean 361 * @throws java.io.IOException 362 */ 363 public boolean storeAttachment(MultipartFile attachedFile, String fileName) throws IOException { 364 String location = null; 365 /*if(objectId != null) { 366 location = getKualiConfigurationService().getPropertyValueAsString( 367 KRADConstants.ATTACHMENTS_PENDING_DIRECTORY_KEY)+ File.separator+"checkList"+File.separator+objectId; 368 } 369 else {*/ 370 location = getKualiConfigurationService().getPropertyValueAsString( 371 KRADConstants.ATTACHMENTS_PENDING_DIRECTORY_KEY) + File.separator + "checkList"; 372 //} 373 if (LOG.isInfoEnabled()) { 374 LOG.info("file location : " + location); 375 } 376 File dirLocation = new File(location); 377 if (!dirLocation.exists()) { 378 boolean success = dirLocation.mkdirs(); 379 if (!success) { 380 LOG.error("Could not generate directory for File at: " + dirLocation.getAbsolutePath()); 381 return false; 382 } 383 } 384 location = location + File.separator + fileName; 385 File fileOut = new File(location); 386 FileOutputStream streamOut = null; 387 BufferedOutputStream bufferedStreamOut = null; 388 try { 389 InputStream fileContent = attachedFile.getInputStream(); 390 streamOut = new FileOutputStream(fileOut); 391 bufferedStreamOut = new BufferedOutputStream(streamOut); 392 int c; 393 while ((c = fileContent.read()) != -1) { 394 bufferedStreamOut.write(c); 395 } 396 } finally { 397 bufferedStreamOut.close(); 398 streamOut.close(); 399 } 400 return true; 401 } 402 403 /** 404 * Saves the document instance contained on the form 405 * 406 * @param docForm - document form base containing the document instance that will be saved 407 * @return ModelAndView 408 */ 409 @RequestMapping(params = "methodToCall=save") 410 public ModelAndView save(@ModelAttribute("KualiForm") DocumentFormBase docForm, BindingResult result, 411 HttpServletRequest request, HttpServletResponse response) throws Exception { 412 ModelAndView modelAndView; 413 MaintenanceDocumentForm form = (MaintenanceDocumentForm) docForm; 414 MultipartFile attachmentFile = form.getAttachmentFile(); 415 MaintenanceDocument document = form.getDocument(); 416 OleCheckListBo oleCheckListBo = (OleCheckListBo) document.getNewMaintainableObject().getDataObject(); 417 OleCheckListBo oleCheckListBoOld = (OleCheckListBo) document.getOldMaintainableObject().getDataObject(); 418 Person kualiUser = GlobalVariables.getUserSession().getPerson(); 419 oleCheckListBo.setAuthor(kualiUser.getPrincipalId()); 420 oleCheckListBo.setLastModified((Timestamp) CoreApiServiceLocator.getDateTimeService().getCurrentTimestamp()); 421 422 if (kualiUser == null) { 423 LOG.error("Current UserSession has a null Person"); 424 } 425 if (form.getMaintenanceAction().equals("New")) { 426 try { 427 if ((attachmentFile.getSize() == 0 || (attachmentFile.getOriginalFilename().isEmpty() || 428 attachmentFile.getOriginalFilename().equalsIgnoreCase("")))) { 429 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, 430 RiceKeyConstants.ERROR_UPLOADFILE_EMPTY, ""); 431 modelAndView = getUIFModelAndView(form); 432 return modelAndView; 433 } else { 434 oleCheckListBo.setFileName(attachmentFile.getOriginalFilename()); 435 oleCheckListBo.setMimeType(attachmentFile.getContentType()); 436 String remoteObjectIdentifier = UUID.randomUUID().toString(); 437 if (storeAttachment(attachmentFile, remoteObjectIdentifier)) 438 oleCheckListBo.setRemoteObjectIdentifier(remoteObjectIdentifier); 439 } 440 } catch (Exception e) { 441 LOG.error("Error while storing the attachment"); 442 } 443 } 444 if (form.getMaintenanceAction().equals("Edit")) { 445 try { 446 if (!(attachmentFile.getOriginalFilename().isEmpty() || 447 attachmentFile.getOriginalFilename().equalsIgnoreCase(""))) { 448 if (attachmentFile.getSize() == 0) { 449 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, 450 RiceKeyConstants.ERROR_UPLOADFILE_EMPTY, attachmentFile.getOriginalFilename()); 451 } else { 452 oleCheckListBo.setFileName(attachmentFile.getOriginalFilename()); 453 oleCheckListBo.setMimeType(attachmentFile.getContentType()); 454 String remoteObjectIdentifier = UUID.randomUUID().toString(); 455 if (storeAttachment(attachmentFile, remoteObjectIdentifier)) 456 deleteAttachment(oleCheckListBoOld); 457 oleCheckListBo.setRemoteObjectIdentifier(remoteObjectIdentifier); 458 } 459 } 460 } catch (Exception e) { 461 LOG.error("Error while storing the attachment"); 462 } 463 } 464 465 466 return super.save(form, result, request, response); 467 } 468 469}