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