001 /** 002 * Copyright 2005-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krad.web.controller; 017 018 import javax.servlet.http.HttpServletRequest; 019 import javax.servlet.http.HttpServletResponse; 020 021 import org.apache.commons.lang.ArrayUtils; 022 import org.apache.log4j.Logger; 023 import org.kuali.rice.core.api.config.property.Config; 024 import org.kuali.rice.core.api.config.property.ConfigContext; 025 import org.kuali.rice.core.api.config.property.ConfigurationService; 026 import org.kuali.rice.kew.api.KewApiConstants; 027 import org.kuali.rice.krad.bo.PersistableAttachment; 028 import org.kuali.rice.krad.bo.PersistableBusinessObject; 029 import org.kuali.rice.krad.datadictionary.DocumentEntry; 030 import org.kuali.rice.krad.exception.UnknownDocumentIdException; 031 import org.kuali.rice.krad.maintenance.MaintenanceDocument; 032 import org.kuali.rice.krad.maintenance.Maintainable; 033 import org.kuali.rice.krad.maintenance.MaintenanceUtils; 034 import org.kuali.rice.krad.service.KRADServiceLocator; 035 import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 036 import org.kuali.rice.krad.service.MaintenanceDocumentService; 037 import org.kuali.rice.krad.uif.UifConstants; 038 import org.kuali.rice.krad.uif.UifParameters; 039 import org.kuali.rice.krad.util.GlobalVariables; 040 import org.kuali.rice.krad.util.KRADConstants; 041 import org.kuali.rice.krad.web.form.DocumentFormBase; 042 import org.kuali.rice.krad.web.form.InitiatedDocumentInfoForm; 043 import org.kuali.rice.krad.web.form.MaintenanceForm; 044 import org.kuali.rice.krad.web.form.UifFormBase; 045 import org.springframework.stereotype.Controller; 046 import org.springframework.validation.BindingResult; 047 import org.springframework.web.bind.annotation.ModelAttribute; 048 import org.springframework.web.bind.annotation.RequestMapping; 049 import org.springframework.web.servlet.ModelAndView; 050 051 import java.net.URL; 052 import java.util.Enumeration; 053 import java.util.Iterator; 054 import java.util.Map; 055 import java.util.Properties; 056 057 /** 058 * Controller for <code>MaintenanceView</code> screens which operate on 059 * <code>MaintenanceDocument</code> instances 060 * 061 * @author Kuali Rice Team (rice.collab@kuali.org) 062 */ 063 @Controller 064 @RequestMapping(value = "/maintenance") 065 public class MaintenanceDocumentController extends DocumentControllerBase { 066 protected static final Logger LOG = Logger.getLogger(MaintenanceDocumentController.class); 067 068 /** 069 * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest) 070 */ 071 @Override 072 protected MaintenanceForm createInitialForm(HttpServletRequest request) { 073 return new MaintenanceForm(); 074 } 075 076 /** 077 * After the document is loaded calls method to setup the maintenance object 078 */ 079 @Override 080 @RequestMapping(params = "methodToCall=docHandler") 081 public ModelAndView docHandler(@ModelAttribute("KualiForm") DocumentFormBase formBase, BindingResult result, 082 HttpServletRequest request, HttpServletResponse response) throws Exception { 083 084 // TODO getting double view if we call base, not sure how to handle 085 // so pasting in superclass code 086 // super.docHandler(formBase, request, response); 087 // * begin copy/paste from the base 088 MaintenanceForm form = (MaintenanceForm) formBase; 089 090 // in all of the following cases we want to load the document 091 if (ArrayUtils.contains(DOCUMENT_LOAD_COMMANDS, form.getCommand()) && form.getDocId() != null) { 092 try { 093 loadDocument(form); 094 } catch (UnknownDocumentIdException udie) { 095 ConfigurationService kualiConfigurationService = KRADServiceLocator.getKualiConfigurationService(); 096 StringBuffer sb = new StringBuffer(); 097 sb.append(kualiConfigurationService.getPropertyValueAsString(KRADConstants.KRAD_URL_KEY)); 098 sb.append(kualiConfigurationService.getPropertyValueAsString(KRADConstants.KRAD_INITIATED_DOCUMENT_URL_KEY)); 099 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 }