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.Config;
24 import org.kuali.rice.core.api.config.property.ConfigContext;
25 import org.kuali.rice.core.api.config.property.ConfigurationService;
26 import org.kuali.rice.kew.api.KewApiConstants;
27 import org.kuali.rice.krad.bo.PersistableAttachment;
28 import org.kuali.rice.krad.bo.PersistableBusinessObject;
29 import org.kuali.rice.krad.datadictionary.DocumentEntry;
30 import org.kuali.rice.krad.exception.UnknownDocumentIdException;
31 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
32 import org.kuali.rice.krad.maintenance.Maintainable;
33 import org.kuali.rice.krad.maintenance.MaintenanceUtils;
34 import org.kuali.rice.krad.service.KRADServiceLocator;
35 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
36 import org.kuali.rice.krad.service.MaintenanceDocumentService;
37 import org.kuali.rice.krad.uif.UifConstants;
38 import org.kuali.rice.krad.uif.UifParameters;
39 import org.kuali.rice.krad.util.GlobalVariables;
40 import org.kuali.rice.krad.util.KRADConstants;
41 import org.kuali.rice.krad.web.form.DocumentFormBase;
42 import org.kuali.rice.krad.web.form.InitiatedDocumentInfoForm;
43 import org.kuali.rice.krad.web.form.MaintenanceForm;
44 import org.kuali.rice.krad.web.form.UifFormBase;
45 import org.springframework.stereotype.Controller;
46 import org.springframework.validation.BindingResult;
47 import org.springframework.web.bind.annotation.ModelAttribute;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.servlet.ModelAndView;
50
51 import java.net.URL;
52 import java.util.Enumeration;
53 import java.util.Iterator;
54 import java.util.Map;
55 import java.util.Properties;
56
57
58
59
60
61
62
63 @Controller
64 @RequestMapping(value = "/maintenance")
65 public class MaintenanceDocumentController extends DocumentControllerBase {
66 protected static final Logger LOG = Logger.getLogger(MaintenanceDocumentController.class);
67
68
69
70
71 @Override
72 protected MaintenanceForm createInitialForm(HttpServletRequest request) {
73 return new MaintenanceForm();
74 }
75
76
77
78
79 @Override
80 @RequestMapping(params = "methodToCall=docHandler")
81 public ModelAndView docHandler(@ModelAttribute("KualiForm") DocumentFormBase formBase, BindingResult result,
82 HttpServletRequest request, HttpServletResponse response) throws Exception {
83
84
85
86
87
88 MaintenanceForm form = (MaintenanceForm) formBase;
89
90
91 if (ArrayUtils.contains(DOCUMENT_LOAD_COMMANDS, form.getCommand()) && form.getDocId() != null) {
92 try {
93 loadDocument(form);
94 } catch (UnknownDocumentIdException udie) {
95 ConfigurationService kualiConfigurationService = KRADServiceLocator.getKualiConfigurationService();
96 StringBuffer sb = new StringBuffer();
97 sb.append(kualiConfigurationService.getPropertyValueAsString(KRADConstants.KRAD_URL_KEY));
98 sb.append(kualiConfigurationService.getPropertyValueAsString(KRADConstants.KRAD_INITIATED_DOCUMENT_URL_KEY));
99 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
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
117
118 form.setMaintenanceAction((form.getDocument()).getNewMaintainableObject().getMaintenanceAction());
119
120
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
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
142
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
157
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
170
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
183
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209 protected void setupMaintenance(MaintenanceForm form, HttpServletRequest request, String maintenanceAction) {
210 MaintenanceDocument document = form.getDocument();
211
212
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
223 form.setMaintenanceAction(maintenanceAction);
224
225
226 getMaintenanceDocumentService().setupMaintenanceObject(document, maintenanceAction, request.getParameterMap());
227
228
229
230 if (KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction)) {
231 MaintenanceUtils.checkForLockingDocument(document, false);
232 }
233
234
235
236
237 DocumentEntry entry = KRADServiceLocatorWeb.getDocumentDictionaryService()
238 .getMaintenanceDocumentEntry(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
239 document.setDisplayTopicFieldInNotes(entry.getDisplayTopicFieldInNotes());
240 }
241
242
243
244
245
246
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 }