001 /**
002 * Copyright 2005-2012 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.ConfigurationService;
024 import org.kuali.rice.kew.api.KewApiConstants;
025 import org.kuali.rice.krad.bo.PersistableAttachment;
026 import org.kuali.rice.krad.bo.PersistableBusinessObject;
027 import org.kuali.rice.krad.datadictionary.DocumentEntry;
028 import org.kuali.rice.krad.exception.UnknownDocumentIdException;
029 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
030 import org.kuali.rice.krad.maintenance.Maintainable;
031 import org.kuali.rice.krad.maintenance.MaintenanceUtils;
032 import org.kuali.rice.krad.service.KRADServiceLocator;
033 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
034 import org.kuali.rice.krad.service.MaintenanceDocumentService;
035 import org.kuali.rice.krad.uif.UifConstants;
036 import org.kuali.rice.krad.uif.UifParameters;
037 import org.kuali.rice.krad.util.GlobalVariables;
038 import org.kuali.rice.krad.util.KRADConstants;
039 import org.kuali.rice.krad.web.form.DocumentFormBase;
040 import org.kuali.rice.krad.web.form.InitiatedDocumentInfoForm;
041 import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
042 import org.kuali.rice.krad.web.form.UifFormBase;
043 import org.springframework.stereotype.Controller;
044 import org.springframework.validation.BindingResult;
045 import org.springframework.web.bind.annotation.ModelAttribute;
046 import org.springframework.web.bind.annotation.RequestMapping;
047 import org.springframework.web.servlet.ModelAndView;
048
049 import java.util.Properties;
050
051 /**
052 * Controller for <code>MaintenanceDocumentView</code> screens which operate on
053 * <code>MaintenanceDocument</code> instances
054 *
055 * @author Kuali Rice Team (rice.collab@kuali.org)
056 */
057 @Controller
058 @RequestMapping(value = "/maintenance")
059 public class MaintenanceDocumentController extends DocumentControllerBase {
060 protected static final Logger LOG = Logger.getLogger(MaintenanceDocumentController.class);
061
062 /**
063 * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
064 */
065 @Override
066 protected MaintenanceDocumentForm createInitialForm(HttpServletRequest request) {
067 return new MaintenanceDocumentForm();
068 }
069
070 /**
071 * After the document is loaded calls method to setup the maintenance object
072 */
073 @Override
074 @RequestMapping(params = "methodToCall=docHandler")
075 public ModelAndView docHandler(@ModelAttribute("KualiForm") DocumentFormBase formBase, BindingResult result,
076 HttpServletRequest request, HttpServletResponse response) throws Exception {
077
078 // TODO getting double view if we call base, not sure how to handle
079 // so pasting in superclass code
080 // super.docHandler(formBase, request, response);
081 // * begin copy/paste from the base
082 MaintenanceDocumentForm form = (MaintenanceDocumentForm) formBase;
083
084 // in all of the following cases we want to load the document
085 if (ArrayUtils.contains(DOCUMENT_LOAD_COMMANDS, form.getCommand()) && form.getDocId() != null) {
086 try {
087 loadDocument(form);
088 } catch (UnknownDocumentIdException udie) {
089 ConfigurationService kualiConfigurationService = KRADServiceLocator.getKualiConfigurationService();
090 StringBuffer sb = new StringBuffer();
091 sb.append(kualiConfigurationService.getPropertyValueAsString(KRADConstants.KRAD_URL_KEY));
092 sb.append(kualiConfigurationService.getPropertyValueAsString(KRADConstants.KRAD_INITIATED_DOCUMENT_URL_KEY));
093 Properties props = new Properties();
094 props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.START);
095 GlobalVariables.getUifFormManager().removeSessionForm(form); // removeForm(form);
096 return performRedirect(new InitiatedDocumentInfoForm(), sb.toString(), props);
097 }
098 } else if (KewApiConstants.INITIATE_COMMAND.equals(form.getCommand())) {
099 createDocument(form);
100 } else {
101 LOG.error("docHandler called with invalid parameters");
102 throw new IllegalStateException("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 IllegalStateException("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 }