001/** 002 * Copyright 2005-2015 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 org.kuali.rice.krad.maintenance; 017 018import org.apache.commons.lang3.ArrayUtils; 019import org.kuali.rice.core.api.util.RiceKeyConstants; 020import org.kuali.rice.kew.api.KewApiConstants; 021import org.kuali.rice.kew.api.exception.WorkflowException; 022import org.kuali.rice.krad.bo.PersistableAttachment; 023import org.kuali.rice.krad.bo.PersistableAttachmentList; 024import org.kuali.rice.krad.datadictionary.DocumentEntry; 025import org.kuali.rice.krad.document.DocumentControllerServiceImpl; 026import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 027import org.kuali.rice.krad.service.MaintenanceDocumentService; 028import org.kuali.rice.krad.uif.UifParameters; 029import org.kuali.rice.krad.util.GlobalVariables; 030import org.kuali.rice.krad.util.KRADConstants; 031import org.kuali.rice.krad.util.KRADUtils; 032import org.kuali.rice.krad.web.form.DocumentFormBase; 033import org.kuali.rice.krad.web.form.MaintenanceDocumentForm; 034import org.kuali.rice.krad.web.form.UifFormBase; 035import org.springframework.web.servlet.ModelAndView; 036 037import javax.servlet.http.HttpServletResponse; 038import java.io.ByteArrayInputStream; 039import java.io.IOException; 040 041/** 042 * Default implementation of the maintenance document controller service. 043 * 044 * @author Kuali Rice Team (rice.collab@kuali.org) 045 */ 046public class MaintenanceDocumentControllerServiceImpl extends DocumentControllerServiceImpl implements MaintenanceDocumentControllerService { 047 048 private MaintenanceDocumentService maintenanceDocumentService; 049 050 /** 051 * {@inheritDoc} 052 */ 053 @Override 054 public ModelAndView start(UifFormBase form) { 055 setupMaintenanceDocument((MaintenanceDocumentForm) form, KRADConstants.MAINTENANCE_NEW_ACTION); 056 057 return super.start(form); 058 } 059 060 /** 061 * {@inheritDoc} 062 */ 063 @Override 064 protected void loadDocument(DocumentFormBase form) throws WorkflowException { 065 super.loadDocument(form); 066 067 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form; 068 maintenanceForm.setMaintenanceAction( 069 (maintenanceForm.getDocument()).getNewMaintainableObject().getMaintenanceAction()); 070 } 071 072 /** 073 * {@inheritDoc} 074 */ 075 @Override 076 protected void createDocument(DocumentFormBase form) throws WorkflowException { 077 setupMaintenanceDocument((MaintenanceDocumentForm) form, KRADConstants.MAINTENANCE_NEW_ACTION); 078 } 079 080 /** 081 * {@inheritDoc} 082 */ 083 @Override 084 public ModelAndView setupMaintenanceEdit(MaintenanceDocumentForm form) { 085 setupMaintenanceDocument(form, KRADConstants.MAINTENANCE_EDIT_ACTION); 086 087 return getModelAndViewService().getModelAndView(form); 088 } 089 090 /** 091 * {@inheritDoc} 092 */ 093 @Override 094 public ModelAndView setupMaintenanceCopy(MaintenanceDocumentForm form) { 095 setupMaintenanceDocument(form, KRADConstants.MAINTENANCE_COPY_ACTION); 096 097 return getModelAndViewService().getModelAndView(form); 098 } 099 100 /** 101 * {@inheritDoc} 102 */ 103 @Override 104 public ModelAndView setupMaintenanceNewWithExisting(MaintenanceDocumentForm form) { 105 setupMaintenanceDocument(form, KRADConstants.MAINTENANCE_NEWWITHEXISTING_ACTION); 106 107 return getModelAndViewService().getModelAndView(form); 108 } 109 110 /** 111 * {@inheritDoc} 112 */ 113 @Override 114 public ModelAndView setupMaintenanceDelete(MaintenanceDocumentForm form) { 115 GlobalVariables.getMessageMap().putWarning(KRADConstants.GLOBAL_MESSAGES, RiceKeyConstants.MESSAGE_DELETE); 116 117 setupMaintenanceDocument(form, KRADConstants.MAINTENANCE_DELETE_ACTION); 118 119 return getModelAndViewService().getModelAndView(form); 120 } 121 122 /** 123 * {@inheritDoc} 124 */ 125 @Override 126 public void setupMaintenanceDocument(MaintenanceDocumentForm form, String maintenanceAction) { 127 MaintenanceDocument document = form.getDocument(); 128 129 if (document == null) { 130 createMaintenanceDocument(form, maintenanceAction); 131 132 document = form.getDocument(); 133 } 134 135 form.setMaintenanceAction(maintenanceAction); 136 137 // invoke maintenance document service to setup the object for maintenance 138 getMaintenanceDocumentService().setupMaintenanceObject(document, maintenanceAction, 139 form.getRequest().getParameterMap()); 140 141 // for new maintainable check if a maintenance lock exists and if so warn the user 142 if (KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction)) { 143 MaintenanceUtils.checkForLockingDocument(document, false); 144 } 145 146 // retrieve notes topic display flag from data dictionary and add to document 147 // TODO: should be in the view as permission 148 DocumentEntry entry = KRADServiceLocatorWeb.getDocumentDictionaryService().getMaintenanceDocumentEntry( 149 document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName()); 150 document.setDisplayTopicFieldInNotes(entry.getDisplayTopicFieldInNotes()); 151 } 152 153 /** 154 * Helper method to create a new maintenance document instance and set the instance on the given form. 155 * 156 * @param form form instance to pull data object from and set new document instance on 157 * @param maintenanceAction type of maintenance action being requested 158 */ 159 protected void createMaintenanceDocument(MaintenanceDocumentForm form, String maintenanceAction) { 160 MaintenanceDocument document = getMaintenanceDocumentService().setupNewMaintenanceDocument( 161 form.getDataObjectClassName(), form.getDocTypeName(), maintenanceAction); 162 163 form.setDocument(document); 164 form.setDocTypeName(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName()); 165 } 166 167 /** 168 * {@inheritDoc} 169 */ 170 @Override 171 public void downloadDataObjectAttachment(MaintenanceDocumentForm form, HttpServletResponse response) { 172 MaintenanceDocument document = form.getDocument(); 173 Object dataObject = document.getDocumentDataObject(); 174 175 if (dataObject instanceof PersistableAttachment) { 176 PersistableAttachment attachment = (PersistableAttachment) dataObject; 177 178 byte[] attachmentContent = attachment.getAttachmentContent(); 179 if (attachmentContent != null) { 180 ByteArrayInputStream inputStream = new ByteArrayInputStream(attachmentContent); 181 try { 182 KRADUtils.addAttachmentToResponse(response, inputStream, attachment.getContentType(), 183 attachment.getFileName(), attachmentContent.length); 184 } catch (IOException e) { 185 throw new RuntimeException("Unable to retrieve attachment contents", e); 186 } 187 } 188 } else if (dataObject instanceof PersistableAttachmentList) { 189 PersistableAttachmentList<PersistableAttachment> attachmentList = 190 (PersistableAttachmentList<PersistableAttachment>) dataObject; 191 PersistableAttachmentList<PersistableAttachment> attachmentListBo = 192 (PersistableAttachmentList<PersistableAttachment>) document.getNewMaintainableObject() 193 .getDataObject(); 194 195 PersistableAttachment attachment = (PersistableAttachment) attachmentListBo.getAttachments().get( 196 Integer.parseInt(form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX))); 197 198 byte[] attachmentContent = attachment.getAttachmentContent(); 199 if (attachmentContent != null) { 200 ByteArrayInputStream inputStream = new ByteArrayInputStream(attachmentContent); 201 try { 202 KRADUtils.addAttachmentToResponse(response, inputStream, attachment.getContentType(), 203 attachment.getFileName(), attachmentContent.length); 204 } catch (IOException e) { 205 throw new RuntimeException("Unable to retrieve attachment contents", e); 206 } 207 } 208 } 209 } 210 211 protected MaintenanceDocumentService getMaintenanceDocumentService() { 212 return maintenanceDocumentService; 213 } 214 215 public void setMaintenanceDocumentService(MaintenanceDocumentService maintenanceDocumentService) { 216 this.maintenanceDocumentService = maintenanceDocumentService; 217 } 218}