View Javadoc

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