Coverage Report - org.kuali.rice.kns.web.spring.controller.MaintenanceDocumentController
 
Classes in this File Line Coverage Branch Coverage Complexity
MaintenanceDocumentController
0%
0/57
0%
0/28
2.545
 
 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.kns.web.spring.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.util.KEWConstants;
 24  
 import org.kuali.rice.kns.bo.PersistableAttachment;
 25  
 import org.kuali.rice.kns.bo.PersistableBusinessObject;
 26  
 import org.kuali.rice.kns.datadictionary.DocumentEntry;
 27  
 import org.kuali.rice.kns.document.MaintenanceDocument;
 28  
 import org.kuali.rice.kns.exception.ValidationException;
 29  
 import org.kuali.rice.kns.maintenance.Maintainable;
 30  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 31  
 import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService;
 32  
 import org.kuali.rice.kns.service.MaintenanceDocumentService;
 33  
 import org.kuali.rice.kns.util.KNSConstants;
 34  
 import org.kuali.rice.kns.util.MaintenanceUtils;
 35  
 import org.kuali.rice.kns.web.spring.form.DocumentFormBase;
 36  
 import org.kuali.rice.kns.web.spring.form.MaintenanceForm;
 37  
 import org.kuali.rice.kns.web.spring.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  0
 @Controller
 51  
 @RequestMapping(value = "/maintenance")
 52  0
 public class MaintenanceDocumentController extends DocumentControllerBase {
 53  0
         protected static final Logger LOG = Logger.getLogger(MaintenanceDocumentController.class);
 54  
 
 55  
         public static final String REQUEST_MAPPING_MAINTENANCE = "maintenance";
 56  
         public static final String METHOD_TO_CALL_NEW = "start";
 57  
         public static final String METHOD_TO_CALL_NEW_WITH_EXISTING = "maintenanceNewWithExisting";
 58  
         public static final String METHOD_TO_CALL_EDIT = "maintenanceEdit";
 59  
         public static final String METHOD_TO_CALL_COPY = "maintenanceCopy";
 60  
         public static final String METHOD_TO_CALL_DELETE = "maintenanceDelete";
 61  
 
 62  
         @Override
 63  
         public MaintenanceForm createInitialForm(HttpServletRequest request) {
 64  0
                 return new MaintenanceForm();
 65  
         }
 66  
 
 67  
         /**
 68  
          * After the document is loaded calls method to setup the maintenance object
 69  
          */
 70  
         @Override
 71  
         @RequestMapping(params = "methodToCall=docHandler")
 72  
         public ModelAndView docHandler(@ModelAttribute("KualiForm") DocumentFormBase formBase, BindingResult result, HttpServletRequest request,
 73  
                         HttpServletResponse response) throws Exception {
 74  
 
 75  
                 // TODO getting double view if we call base, not sure how to handle
 76  
             // so pasting in superclass code
 77  
                 // super.docHandler(formBase, request, response);
 78  
                 // * begin copy/paste from the base
 79  0
             MaintenanceForm form = (MaintenanceForm)formBase;
 80  
             
 81  
                 // in all of the following cases we want to load the document
 82  0
         if (ArrayUtils.contains(DOCUMENT_LOAD_COMMANDS, form.getCommand()) && form.getDocId() != null) {
 83  0
             loadDocument(form);
 84  
         }
 85  0
         else if (KEWConstants.INITIATE_COMMAND.equals(form.getCommand())) {
 86  0
             createDocument(form);
 87  
         }
 88  
         else {
 89  0
             LOG.error("docHandler called with invalid parameters");
 90  0
             throw new IllegalStateException("docHandler called with invalid parameters");
 91  
         }
 92  
                 // * end copy/paste from the base
 93  
         
 94  0
                 if (KEWConstants.ACTIONLIST_COMMAND.equals(form.getCommand())
 95  
                                 || KEWConstants.DOCSEARCH_COMMAND.equals(form.getCommand())
 96  
                                 || KEWConstants.SUPERUSER_COMMAND.equals(form.getCommand())
 97  
                                 || KEWConstants.HELPDESK_ACTIONLIST_COMMAND.equals(form.getCommand()) && form.getDocId() != null) {
 98  
                         // TODO: set state in view
 99  
                         // form.setReadOnly(true);
 100  0
                         form.setMaintenanceAction((form.getDocument()).getNewMaintainableObject().getMaintenanceAction());
 101  
 
 102  
                         // Retrieving the FileName from BO table
 103  0
                         Maintainable tmpMaintainable = form.getDocument().getNewMaintainableObject();
 104  0
                         if (tmpMaintainable.getDataObject() instanceof PersistableAttachment) {
 105  0
                                 PersistableAttachment bo = (PersistableAttachment) getBusinessObjectService().retrieve(
 106  
                                                 (PersistableBusinessObject) tmpMaintainable.getDataObject());
 107  0
                                 if (bo != null) {
 108  0
                                         request.setAttribute("fileName", bo.getFileName());
 109  
                                 }
 110  
                         }
 111  0
                 }
 112  0
                 else if (KEWConstants.INITIATE_COMMAND.equals(form.getCommand())) {
 113  
                         // form.setReadOnly(false);
 114  0
                         setupMaintenance(form, request, KNSConstants.MAINTENANCE_NEW_ACTION);
 115  
                 }
 116  
                 else {
 117  0
                         LOG.error("We should never have gotten to here");
 118  0
                         throw new IllegalStateException("docHandler called with invalid parameters");
 119  
                 }
 120  
 
 121  0
                 return getUIFModelAndView(form);
 122  
         }
 123  
 
 124  
         /**
 125  
          * Default method for controller that setups a new
 126  
          * <code>MaintenanceView</code> with the default new action
 127  
          */
 128  
         @RequestMapping(params = "methodToCall=" + METHOD_TO_CALL_NEW)
 129  
         @Override
 130  
         public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
 131  
                         HttpServletRequest request, HttpServletResponse response) {
 132  0
             MaintenanceForm maintenanceForm = (MaintenanceForm) form;
 133  
 
 134  0
                 setupMaintenance(maintenanceForm, request, KNSConstants.MAINTENANCE_NEW_ACTION);
 135  
 
 136  0
                 return getUIFModelAndView(maintenanceForm);
 137  
         }
 138  
 
 139  
         /**
 140  
          * Setups a new <code>MaintenanceView</code> with the edit maintenance
 141  
          * action
 142  
          */
 143  
         @RequestMapping(params = "methodToCall=" + METHOD_TO_CALL_EDIT)
 144  
         public ModelAndView maintenanceEdit(@ModelAttribute("KualiForm") MaintenanceForm form, BindingResult result,
 145  
                         HttpServletRequest request, HttpServletResponse response) throws Exception {
 146  
 
 147  0
                 setupMaintenance(form, request, KNSConstants.MAINTENANCE_EDIT_ACTION);
 148  
 
 149  0
                 return getUIFModelAndView(form);
 150  
         }
 151  
 
 152  
         /**
 153  
          * Setups a new <code>MaintenanceView</code> with the copy maintenance
 154  
          * action
 155  
          */
 156  
         @RequestMapping(params = "methodToCall=" + METHOD_TO_CALL_COPY)
 157  
         public ModelAndView maintenanceCopy(@ModelAttribute("KualiForm") MaintenanceForm form, BindingResult result,
 158  
                         HttpServletRequest request, HttpServletResponse response) throws Exception {
 159  
 
 160  0
                 setupMaintenance(form, request, KNSConstants.MAINTENANCE_COPY_ACTION);
 161  
 
 162  0
                 return getUIFModelAndView(form);
 163  
         }
 164  
 
 165  
         /**
 166  
          * Setups a new <code>MaintenanceView</code> with the new with existing
 167  
          * maintenance action
 168  
          */
 169  
         @RequestMapping(params = "methodToCall=" + METHOD_TO_CALL_NEW_WITH_EXISTING)
 170  
         public ModelAndView maintenanceNewWithExisting(@ModelAttribute("KualiForm") MaintenanceForm form,
 171  
                         BindingResult result, HttpServletRequest request, HttpServletResponse response) throws Exception {
 172  
 
 173  0
                 setupMaintenance(form, request, KNSConstants.MAINTENANCE_NEWWITHEXISTING_ACTION);
 174  
 
 175  0
                 return getUIFModelAndView(form);
 176  
         }
 177  
 
 178  
         /**
 179  
          * Sets up the <code>MaintenanceDocument</code> on initial get request
 180  
          * 
 181  
          * <p>
 182  
          * First step is to create a new document instance based on the query
 183  
          * parameters (document type name or object class name). Then call the
 184  
          * <code>Maintainable</code> to do setup on the object being maintained.
 185  
          * </p>
 186  
          * 
 187  
          * @param form
 188  
          *            - <code>MaintenanceForm</code> instance
 189  
          * @param request
 190  
          *            - HTTP request object
 191  
          * @param maintenanceAction
 192  
          *            - the maintenance action (new, new from existing, edit, copy)
 193  
          *            being request
 194  
          * @throws Exception
 195  
          */
 196  
         protected void setupMaintenance(MaintenanceForm form, HttpServletRequest request, String maintenanceAction) {
 197  0
                 MaintenanceDocument document = form.getDocument();
 198  
 
 199  
                 // create a new document object, if required
 200  0
                 if (document == null) {
 201  0
                         document = getMaintenanceDocumentService().setupNewMaintenanceDocument(form.getDataObjectClassName(),
 202  
                                         form.getDocTypeName(), maintenanceAction);
 203  
 
 204  0
                         form.setDocument(document);
 205  0
                         form.setDocTypeName(document.getDocumentHeader().getWorkflowDocument().getDocumentType());
 206  
                 }
 207  
 
 208  
                 // set action on form
 209  0
                 form.setMaintenanceAction(maintenanceAction);
 210  
 
 211  
                 // invoke maintainable to setup the object for maintenance
 212  0
                 document.getNewMaintainableObject().setupMaintenanceObject(document, maintenanceAction,
 213  
                                 request.getParameterMap());
 214  
 
 215  
                 // for new maintainable check if a maintenance lock exists and if so
 216  
                 // warn the user
 217  0
                 if (KNSConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction)) {
 218  0
                         MaintenanceUtils.checkForLockingDocument(document.getNewMaintainableObject(), false);
 219  
                 }
 220  
 
 221  
                 // Retrieve notes topic display flag from data dictionary and add to
 222  
                 // document
 223  
                 // TODO: should be in the view as permission
 224  0
                 DocumentEntry entry = getMaintenanceDocumentDictionaryService().getMaintenanceDocumentEntry(
 225  
                                 document.getDocumentHeader().getWorkflowDocument().getDocumentType());
 226  0
                 document.setDisplayTopicFieldInNotes(entry.getDisplayTopicFieldInNotes());
 227  0
         }
 228  
 
 229  
         /**
 230  
          * Override route to retrieve the maintenance object if it is an attachment
 231  
          * 
 232  
          * @see org.kuali.rice.kns.web.spring.controller.DocumentControllerBase.route
 233  
          *      (DocumentFormBase, HttpServletRequest, HttpServletResponse)
 234  
          */
 235  
         @Override
 236  
         @RequestMapping(params = "methodToCall=route")
 237  
     public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result, HttpServletRequest request,
 238  
             HttpServletResponse response) throws Exception {
 239  
 
 240  
         ModelAndView modelAndView;
 241  
 
 242  
         try {
 243  0
             modelAndView = super.route(form, result, request, response);
 244  
 
 245  0
             MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
 246  0
             if (document.getNewMaintainableObject().getDataObject() instanceof PersistableAttachment) {
 247  0
                 PersistableAttachment bo = (PersistableAttachment) getBusinessObjectService().retrieve((PersistableBusinessObject) document.getNewMaintainableObject().getDataObject());
 248  0
                 request.setAttribute("fileName", bo.getFileName());
 249  
             }
 250  0
         } catch (ValidationException vex) {
 251  0
             modelAndView = getUIFModelAndView(form);
 252  0
         }
 253  
 
 254  0
         return modelAndView;
 255  
     }
 256  
         
 257  
         @Override
 258  
     protected ModelAndView getUIFModelAndView(UifFormBase form, String viewId, String pageId) {
 259  
             // TODO: remove once error path is fixed in business rules
 260  
 //            // update error messages
 261  
 //            Map<String, AutoPopulatingList> adjustedPathErrors = new HashMap<String, AutoPopulatingList>();
 262  
 //            MessageMap messageMap = GlobalVariables.getMessageMap();
 263  
 //            for (Entry<String, AutoPopulatingList> pathErrors : messageMap.getErrorMessages().entrySet()) {
 264  
 //                String path = pathErrors.getKey();
 265  
 //                if (path.startsWith("document.newMaintainableObject") && !path.startsWith("document.newMaintainableObject.dataObject")) {
 266  
 //                    String adjustedPath = StringUtils.replace(path, "document.newMaintainableObject", "document.newMaintainableObject.dataObject");
 267  
 //                    adjustedPathErrors.put(adjustedPath, pathErrors.getValue());
 268  
 //                }
 269  
 //            }
 270  
             
 271  0
         return super.getUIFModelAndView(form, viewId, pageId);
 272  
     }
 273  
 
 274  
     protected MaintenanceDocumentService getMaintenanceDocumentService() {
 275  0
                 return KNSServiceLocatorWeb.getMaintenanceDocumentService();
 276  
         }
 277  
 
 278  
         protected MaintenanceDocumentDictionaryService getMaintenanceDocumentDictionaryService() {
 279  0
                 return KNSServiceLocatorWeb.getMaintenanceDocumentDictionaryService();
 280  
         }
 281  
 
 282  
 }