View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.maintenance;
17  
18  import org.apache.commons.lang3.ArrayUtils;
19  import org.kuali.rice.core.api.util.RiceKeyConstants;
20  import org.kuali.rice.kew.api.KewApiConstants;
21  import org.kuali.rice.kew.api.exception.WorkflowException;
22  import org.kuali.rice.krad.bo.PersistableAttachment;
23  import org.kuali.rice.krad.bo.PersistableAttachmentList;
24  import org.kuali.rice.krad.datadictionary.DocumentEntry;
25  import org.kuali.rice.krad.document.DocumentControllerServiceImpl;
26  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
27  import org.kuali.rice.krad.service.MaintenanceDocumentService;
28  import org.kuali.rice.krad.uif.UifParameters;
29  import org.kuali.rice.krad.util.GlobalVariables;
30  import org.kuali.rice.krad.util.KRADConstants;
31  import org.kuali.rice.krad.util.KRADUtils;
32  import org.kuali.rice.krad.web.form.DocumentFormBase;
33  import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
34  import org.kuali.rice.krad.web.form.UifFormBase;
35  import org.springframework.web.servlet.ModelAndView;
36  
37  import javax.servlet.http.HttpServletResponse;
38  import java.io.ByteArrayInputStream;
39  import java.io.IOException;
40  
41  /**
42   * Default implementation of the maintenance document controller service.
43   *
44   * @author Kuali Rice Team (rice.collab@kuali.org)
45   */
46  public class MaintenanceDocumentControllerServiceImpl extends DocumentControllerServiceImpl implements MaintenanceDocumentControllerService {
47  
48      private MaintenanceDocumentService maintenanceDocumentService;
49  
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      public ModelAndView start(UifFormBase form) {
55          setupMaintenanceDocument((MaintenanceDocumentForm) form, KRADConstants.MAINTENANCE_NEW_ACTION);
56  
57          return super.start(form);
58      }
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      protected void loadDocument(DocumentFormBase form) throws WorkflowException {
65          super.loadDocument(form);
66  
67          MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
68          maintenanceForm.setMaintenanceAction(
69                  (maintenanceForm.getDocument()).getNewMaintainableObject().getMaintenanceAction());
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      protected void createDocument(DocumentFormBase form) throws WorkflowException {
77          setupMaintenanceDocument((MaintenanceDocumentForm) form, KRADConstants.MAINTENANCE_NEW_ACTION);
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public ModelAndView setupMaintenanceEdit(MaintenanceDocumentForm form) {
85          setupMaintenanceDocument(form, KRADConstants.MAINTENANCE_EDIT_ACTION);
86  
87          return getModelAndViewService().getModelAndView(form);
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public ModelAndView setupMaintenanceCopy(MaintenanceDocumentForm form) {
95          setupMaintenanceDocument(form, KRADConstants.MAINTENANCE_COPY_ACTION);
96  
97          return getModelAndViewService().getModelAndView(form);
98      }
99  
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 }