View Javadoc

1   /**
2    * Copyright 2005-2011 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.web.controller;
17  
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  import org.apache.commons.lang.ArrayUtils;
22  import org.apache.log4j.Logger;
23  import org.kuali.rice.kew.api.KewApiConstants;
24  import org.kuali.rice.krad.bo.PersistableAttachment;
25  import org.kuali.rice.krad.bo.PersistableBusinessObject;
26  import org.kuali.rice.krad.datadictionary.DocumentEntry;
27  import org.kuali.rice.krad.document.MaintenanceDocument;
28  import org.kuali.rice.krad.exception.ValidationException;
29  import org.kuali.rice.krad.maintenance.Maintainable;
30  import org.kuali.rice.krad.maintenance.MaintenanceUtils;
31  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
32  import org.kuali.rice.krad.service.MaintenanceDocumentService;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  import org.kuali.rice.krad.util.KRADConstants;
35  import org.kuali.rice.krad.web.form.DocumentFormBase;
36  import org.kuali.rice.krad.web.form.MaintenanceForm;
37  import org.kuali.rice.krad.web.form.UifFormBase;
38  import org.springframework.stereotype.Controller;
39  import org.springframework.validation.BindingResult;
40  import org.springframework.web.bind.annotation.ModelAttribute;
41  import org.springframework.web.bind.annotation.RequestMapping;
42  import org.springframework.web.servlet.ModelAndView;
43  
44  /**
45   * Controller for <code>MaintenanceView</code> screens which operate on
46   * <code>MaintenanceDocument</code> instances
47   *
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   */
50  @Controller
51  @RequestMapping(value = "/maintenance")
52  public class MaintenanceDocumentController extends DocumentControllerBase {
53      protected static final Logger LOG = Logger.getLogger(MaintenanceDocumentController.class);
54  
55      /**
56       * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
57       */
58      @Override
59      protected MaintenanceForm createInitialForm(HttpServletRequest request) {
60          return new MaintenanceForm();
61      }
62  
63      /**
64       * After the document is loaded calls method to setup the maintenance object
65       */
66      @Override
67      @RequestMapping(params = "methodToCall=docHandler")
68      public ModelAndView docHandler(@ModelAttribute("KualiForm") DocumentFormBase formBase, BindingResult result,
69              HttpServletRequest request, HttpServletResponse response) throws Exception {
70  
71          // TODO getting double view if we call base, not sure how to handle
72          // so pasting in superclass code
73          // super.docHandler(formBase, request, response);
74          // * begin copy/paste from the base
75          MaintenanceForm form = (MaintenanceForm) formBase;
76  
77          // in all of the following cases we want to load the document
78          if (ArrayUtils.contains(DOCUMENT_LOAD_COMMANDS, form.getCommand()) && form.getDocId() != null) {
79              loadDocument(form);
80          } else if (KewApiConstants.INITIATE_COMMAND.equals(form.getCommand())) {
81              createDocument(form);
82          } else {
83              LOG.error("docHandler called with invalid parameters");
84              throw new IllegalStateException("docHandler called with invalid parameters");
85          }
86          // * end copy/paste from the base
87  
88          if (KewApiConstants.ACTIONLIST_COMMAND.equals(form.getCommand()) ||
89                  KewApiConstants.DOCSEARCH_COMMAND.equals(form.getCommand()) ||
90                  KewApiConstants.SUPERUSER_COMMAND.equals(form.getCommand()) ||
91                  KewApiConstants.HELPDESK_ACTIONLIST_COMMAND.equals(form.getCommand()) && form.getDocId() != null) {
92              // TODO: set state in view
93              // form.setReadOnly(true);
94              form.setMaintenanceAction((form.getDocument()).getNewMaintainableObject().getMaintenanceAction());
95  
96              // Retrieving the FileName from BO table
97              Maintainable tmpMaintainable = form.getDocument().getNewMaintainableObject();
98              if (tmpMaintainable.getDataObject() instanceof PersistableAttachment) {
99                  PersistableAttachment bo = (PersistableAttachment) getBusinessObjectService()
100                         .retrieve((PersistableBusinessObject) tmpMaintainable.getDataObject());
101                 if (bo != null) {
102                     request.setAttribute("fileName", bo.getFileName());
103                 }
104             }
105         } else if (KewApiConstants.INITIATE_COMMAND.equals(form.getCommand())) {
106             // form.setReadOnly(false);
107             setupMaintenance(form, request, KRADConstants.MAINTENANCE_NEW_ACTION);
108         } else {
109             LOG.error("We should never have gotten to here");
110             throw new IllegalStateException("docHandler called with invalid parameters");
111         }
112 
113         return getUIFModelAndView(form);
114     }
115 
116     /**
117      * Default method for controller that setups a new
118      * <code>MaintenanceView</code> with the default new action
119      */
120     @RequestMapping(params = "methodToCall=" + KRADConstants.Maintenance.METHOD_TO_CALL_NEW)
121     @Override
122     public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
123             HttpServletRequest request, HttpServletResponse response) {
124         MaintenanceForm maintenanceForm = (MaintenanceForm) form;
125 
126         setupMaintenance(maintenanceForm, request, KRADConstants.MAINTENANCE_NEW_ACTION);
127 
128         return getUIFModelAndView(maintenanceForm);
129     }
130 
131     /**
132      * Setups a new <code>MaintenanceView</code> with the edit maintenance
133      * action
134      */
135     @RequestMapping(params = "methodToCall=" + KRADConstants.Maintenance.METHOD_TO_CALL_EDIT)
136     public ModelAndView maintenanceEdit(@ModelAttribute("KualiForm") MaintenanceForm form, BindingResult result,
137             HttpServletRequest request, HttpServletResponse response) throws Exception {
138 
139         setupMaintenance(form, request, KRADConstants.MAINTENANCE_EDIT_ACTION);
140 
141         return getUIFModelAndView(form);
142     }
143 
144     /**
145      * Setups a new <code>MaintenanceView</code> with the copy maintenance
146      * action
147      */
148     @RequestMapping(params = "methodToCall=" + KRADConstants.Maintenance.METHOD_TO_CALL_COPY)
149     public ModelAndView maintenanceCopy(@ModelAttribute("KualiForm") MaintenanceForm form, BindingResult result,
150             HttpServletRequest request, HttpServletResponse response) throws Exception {
151 
152         setupMaintenance(form, request, KRADConstants.MAINTENANCE_COPY_ACTION);
153 
154         return getUIFModelAndView(form);
155     }
156 
157     /**
158      * Setups a new <code>MaintenanceView</code> with the new with existing
159      * maintenance action
160      */
161     @RequestMapping(params = "methodToCall=" + KRADConstants.Maintenance.METHOD_TO_CALL_NEW_WITH_EXISTING)
162     public ModelAndView maintenanceNewWithExisting(@ModelAttribute("KualiForm") MaintenanceForm form,
163             BindingResult result, HttpServletRequest request, HttpServletResponse response) throws Exception {
164 
165         setupMaintenance(form, request, KRADConstants.MAINTENANCE_NEWWITHEXISTING_ACTION);
166 
167         return getUIFModelAndView(form);
168     }
169 
170     /**
171      * Sets up the <code>MaintenanceDocument</code> on initial get request
172      *
173      * <p>
174      * First step is to create a new document instance based on the query
175      * parameters (document type name or object class name). Then call the
176      * <code>Maintainable</code> to do setup on the object being maintained.
177      * </p>
178      *
179      * @param form - <code>MaintenanceForm</code> instance
180      * @param request - HTTP request object
181      * @param maintenanceAction - the maintenance action (new, new from existing, edit, copy)
182      * being request
183      * @throws Exception
184      */
185     protected void setupMaintenance(MaintenanceForm form, HttpServletRequest request, String maintenanceAction) {
186         MaintenanceDocument document = form.getDocument();
187 
188         // create a new document object, if required
189         if (document == null) {
190             document = getMaintenanceDocumentService()
191                     .setupNewMaintenanceDocument(form.getDataObjectClassName(), form.getDocTypeName(),
192                             maintenanceAction);
193 
194             form.setDocument(document);
195             form.setDocTypeName(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
196         }
197 
198         // set action on form
199         form.setMaintenanceAction(maintenanceAction);
200 
201         // invoke maintenance document service to setup the object for maintenance
202         getMaintenanceDocumentService().setupMaintenanceObject(document, maintenanceAction, request.getParameterMap());
203 
204         // for new maintainable check if a maintenance lock exists and if so
205         // warn the user
206         if (KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction)) {
207             MaintenanceUtils.checkForLockingDocument(document, false);
208         }
209 
210         // Retrieve notes topic display flag from data dictionary and add to
211         // document
212         // TODO: should be in the view as permission
213         DocumentEntry entry = KRADServiceLocatorWeb.getDocumentDictionaryService()
214                 .getMaintenanceDocumentEntry(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
215         document.setDisplayTopicFieldInNotes(entry.getDisplayTopicFieldInNotes());
216     }
217 
218     /**
219      * Override route to retrieve the maintenance object if it is an attachment
220      *
221      * @see DocumentControllerBase.route
222      *      (DocumentFormBase, HttpServletRequest, HttpServletResponse)
223      */
224     @Override
225     @RequestMapping(params = "methodToCall=route")
226     public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
227             HttpServletRequest request, HttpServletResponse response) throws Exception {
228 
229         ModelAndView modelAndView;
230 
231         try {
232             modelAndView = super.route(form, result, request, response);
233 
234             MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
235             if (document.getNewMaintainableObject().getDataObject() instanceof PersistableAttachment) {
236                 PersistableAttachment bo = (PersistableAttachment) getBusinessObjectService()
237                         .retrieve((PersistableBusinessObject) document.getNewMaintainableObject().getDataObject());
238                 request.setAttribute("fileName", bo.getFileName());
239             }
240         } catch (ValidationException vex) {
241             if (GlobalVariables.getMessageMap().hasNoErrors()) {
242                 throw new RuntimeException("Validation Exception with no error message.", vex);
243             }
244 
245             modelAndView = getUIFModelAndView(form);
246         }
247 
248         return modelAndView;
249     }
250 
251     protected MaintenanceDocumentService getMaintenanceDocumentService() {
252         return KRADServiceLocatorWeb.getMaintenanceDocumentService();
253     }
254 
255 }