View Javadoc

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