1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.ConfigurationService;
24 import org.kuali.rice.kew.api.KewApiConstants;
25 import org.kuali.rice.krad.bo.PersistableAttachment;
26 import org.kuali.rice.krad.bo.PersistableBusinessObject;
27 import org.kuali.rice.krad.datadictionary.DocumentEntry;
28 import org.kuali.rice.krad.exception.UnknownDocumentIdException;
29 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
30 import org.kuali.rice.krad.maintenance.Maintainable;
31 import org.kuali.rice.krad.maintenance.MaintenanceUtils;
32 import org.kuali.rice.krad.service.KRADServiceLocator;
33 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
34 import org.kuali.rice.krad.service.MaintenanceDocumentService;
35 import org.kuali.rice.krad.uif.UifConstants;
36 import org.kuali.rice.krad.uif.UifParameters;
37 import org.kuali.rice.krad.util.GlobalVariables;
38 import org.kuali.rice.krad.util.KRADConstants;
39 import org.kuali.rice.krad.web.form.DocumentFormBase;
40 import org.kuali.rice.krad.web.form.InitiatedDocumentInfoForm;
41 import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
42 import org.kuali.rice.krad.web.form.UifFormBase;
43 import org.springframework.stereotype.Controller;
44 import org.springframework.validation.BindingResult;
45 import org.springframework.web.bind.annotation.ModelAttribute;
46 import org.springframework.web.bind.annotation.RequestMapping;
47 import org.springframework.web.servlet.ModelAndView;
48
49 import java.util.Properties;
50
51
52
53
54
55
56
57 @Controller
58 @RequestMapping(value = "/maintenance")
59 public class MaintenanceDocumentController extends DocumentControllerBase {
60 protected static final Logger LOG = Logger.getLogger(MaintenanceDocumentController.class);
61
62
63
64
65 @Override
66 protected MaintenanceDocumentForm createInitialForm(HttpServletRequest request) {
67 return new MaintenanceDocumentForm();
68 }
69
70
71
72
73 @Override
74 @RequestMapping(params = "methodToCall=docHandler")
75 public ModelAndView docHandler(@ModelAttribute("KualiForm") DocumentFormBase formBase, BindingResult result,
76 HttpServletRequest request, HttpServletResponse response) throws Exception {
77
78
79
80
81
82 MaintenanceDocumentForm form = (MaintenanceDocumentForm) formBase;
83
84
85 if (ArrayUtils.contains(DOCUMENT_LOAD_COMMANDS, form.getCommand()) && form.getDocId() != null) {
86 try {
87 loadDocument(form);
88 } catch (UnknownDocumentIdException udie) {
89 ConfigurationService kualiConfigurationService = KRADServiceLocator.getKualiConfigurationService();
90 StringBuffer sb = new StringBuffer();
91 sb.append(kualiConfigurationService.getPropertyValueAsString(KRADConstants.KRAD_URL_KEY));
92 sb.append(kualiConfigurationService.getPropertyValueAsString(KRADConstants.KRAD_INITIATED_DOCUMENT_URL_KEY));
93 Properties props = new Properties();
94 props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.START);
95 GlobalVariables.getUifFormManager().removeSessionForm(form);
96 return performRedirect(new InitiatedDocumentInfoForm(), sb.toString(), props);
97 }
98 } else if (KewApiConstants.INITIATE_COMMAND.equals(form.getCommand())) {
99 createDocument(form);
100 } else {
101 LOG.error("docHandler called with invalid parameters");
102 throw new IllegalArgumentException("docHandler called with invalid parameters");
103 }
104
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
111
112 form.setMaintenanceAction((form.getDocument()).getNewMaintainableObject().getMaintenanceAction());
113
114
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
125 setupMaintenance(form, request, KRADConstants.MAINTENANCE_NEW_ACTION);
126 } else {
127 LOG.error("We should never have gotten to here");
128 throw new IllegalArgumentException("docHandler called with invalid parameters");
129 }
130
131 return getUIFModelAndView(form);
132 }
133
134
135
136
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
151
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
164
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
177
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203 protected void setupMaintenance(MaintenanceDocumentForm form, HttpServletRequest request, String maintenanceAction) {
204 MaintenanceDocument document = form.getDocument();
205
206
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
217 form.setMaintenanceAction(maintenanceAction);
218
219
220 getMaintenanceDocumentService().setupMaintenanceObject(document, maintenanceAction, request.getParameterMap());
221
222
223
224 if (KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction)) {
225 MaintenanceUtils.checkForLockingDocument(document, false);
226 }
227
228
229
230
231 DocumentEntry entry = KRADServiceLocatorWeb.getDocumentDictionaryService()
232 .getMaintenanceDocumentEntry(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
233 document.setDisplayTopicFieldInNotes(entry.getDisplayTopicFieldInNotes());
234 }
235
236
237
238
239
240
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 }