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