View Javadoc

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