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.kew.api.KewApiConstants;
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.maintenance.MaintenanceUtils;
31 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
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.web.form.DocumentFormBase;
36 import org.kuali.rice.krad.web.form.MaintenanceForm;
37 import org.kuali.rice.krad.web.form.UifFormBase;
38 import org.springframework.stereotype.Controller;
39 import org.springframework.validation.BindingResult;
40 import org.springframework.web.bind.annotation.ModelAttribute;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.servlet.ModelAndView;
43
44
45
46
47
48
49
50 @Controller
51 @RequestMapping(value = "/maintenance")
52 public class MaintenanceDocumentController extends DocumentControllerBase {
53 protected static final Logger LOG = Logger.getLogger(MaintenanceDocumentController.class);
54
55
56
57
58 @Override
59 protected MaintenanceForm createInitialForm(HttpServletRequest request) {
60 return new MaintenanceForm();
61 }
62
63
64
65
66 @Override
67 @RequestMapping(params = "methodToCall=docHandler")
68 public ModelAndView docHandler(@ModelAttribute("KualiForm") DocumentFormBase formBase, BindingResult result,
69 HttpServletRequest request, HttpServletResponse response) throws Exception {
70
71
72
73
74
75 MaintenanceForm form = (MaintenanceForm) formBase;
76
77
78 if (ArrayUtils.contains(DOCUMENT_LOAD_COMMANDS, form.getCommand()) && form.getDocId() != null) {
79 loadDocument(form);
80 } else if (KewApiConstants.INITIATE_COMMAND.equals(form.getCommand())) {
81 createDocument(form);
82 } else {
83 LOG.error("docHandler called with invalid parameters");
84 throw new IllegalStateException("docHandler called with invalid parameters");
85 }
86
87
88 if (KewApiConstants.ACTIONLIST_COMMAND.equals(form.getCommand()) ||
89 KewApiConstants.DOCSEARCH_COMMAND.equals(form.getCommand()) ||
90 KewApiConstants.SUPERUSER_COMMAND.equals(form.getCommand()) ||
91 KewApiConstants.HELPDESK_ACTIONLIST_COMMAND.equals(form.getCommand()) && form.getDocId() != null) {
92
93
94 form.setMaintenanceAction((form.getDocument()).getNewMaintainableObject().getMaintenanceAction());
95
96
97 Maintainable tmpMaintainable = form.getDocument().getNewMaintainableObject();
98 if (tmpMaintainable.getDataObject() instanceof PersistableAttachment) {
99 PersistableAttachment bo = (PersistableAttachment) getBusinessObjectService()
100 .retrieve((PersistableBusinessObject) tmpMaintainable.getDataObject());
101 if (bo != null) {
102 request.setAttribute("fileName", bo.getFileName());
103 }
104 }
105 } else if (KewApiConstants.INITIATE_COMMAND.equals(form.getCommand())) {
106
107 setupMaintenance(form, request, KRADConstants.MAINTENANCE_NEW_ACTION);
108 } else {
109 LOG.error("We should never have gotten to here");
110 throw new IllegalStateException("docHandler called with invalid parameters");
111 }
112
113 return getUIFModelAndView(form);
114 }
115
116
117
118
119
120 @RequestMapping(params = "methodToCall=" + KRADConstants.Maintenance.METHOD_TO_CALL_NEW)
121 @Override
122 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
123 HttpServletRequest request, HttpServletResponse response) {
124 MaintenanceForm maintenanceForm = (MaintenanceForm) form;
125
126 setupMaintenance(maintenanceForm, request, KRADConstants.MAINTENANCE_NEW_ACTION);
127
128 return getUIFModelAndView(maintenanceForm);
129 }
130
131
132
133
134
135 @RequestMapping(params = "methodToCall=" + KRADConstants.Maintenance.METHOD_TO_CALL_EDIT)
136 public ModelAndView maintenanceEdit(@ModelAttribute("KualiForm") MaintenanceForm form, BindingResult result,
137 HttpServletRequest request, HttpServletResponse response) throws Exception {
138
139 setupMaintenance(form, request, KRADConstants.MAINTENANCE_EDIT_ACTION);
140
141 return getUIFModelAndView(form);
142 }
143
144
145
146
147
148 @RequestMapping(params = "methodToCall=" + KRADConstants.Maintenance.METHOD_TO_CALL_COPY)
149 public ModelAndView maintenanceCopy(@ModelAttribute("KualiForm") MaintenanceForm form, BindingResult result,
150 HttpServletRequest request, HttpServletResponse response) throws Exception {
151
152 setupMaintenance(form, request, KRADConstants.MAINTENANCE_COPY_ACTION);
153
154 return getUIFModelAndView(form);
155 }
156
157
158
159
160
161 @RequestMapping(params = "methodToCall=" + KRADConstants.Maintenance.METHOD_TO_CALL_NEW_WITH_EXISTING)
162 public ModelAndView maintenanceNewWithExisting(@ModelAttribute("KualiForm") MaintenanceForm form,
163 BindingResult result, HttpServletRequest request, HttpServletResponse response) throws Exception {
164
165 setupMaintenance(form, request, KRADConstants.MAINTENANCE_NEWWITHEXISTING_ACTION);
166
167 return getUIFModelAndView(form);
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 protected void setupMaintenance(MaintenanceForm form, HttpServletRequest request, String maintenanceAction) {
186 MaintenanceDocument document = form.getDocument();
187
188
189 if (document == null) {
190 document = getMaintenanceDocumentService()
191 .setupNewMaintenanceDocument(form.getDataObjectClassName(), form.getDocTypeName(),
192 maintenanceAction);
193
194 form.setDocument(document);
195 form.setDocTypeName(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
196 }
197
198
199 form.setMaintenanceAction(maintenanceAction);
200
201
202 getMaintenanceDocumentService().setupMaintenanceObject(document, maintenanceAction, request.getParameterMap());
203
204
205
206 if (KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction)) {
207 MaintenanceUtils.checkForLockingDocument(document, false);
208 }
209
210
211
212
213 DocumentEntry entry = KRADServiceLocatorWeb.getDocumentDictionaryService()
214 .getMaintenanceDocumentEntry(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
215 document.setDisplayTopicFieldInNotes(entry.getDisplayTopicFieldInNotes());
216 }
217
218
219
220
221
222
223
224 @Override
225 @RequestMapping(params = "methodToCall=route")
226 public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
227 HttpServletRequest request, HttpServletResponse response) throws Exception {
228
229 ModelAndView modelAndView;
230
231 try {
232 modelAndView = super.route(form, result, request, response);
233
234 MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
235 if (document.getNewMaintainableObject().getDataObject() instanceof PersistableAttachment) {
236 PersistableAttachment bo = (PersistableAttachment) getBusinessObjectService()
237 .retrieve((PersistableBusinessObject) document.getNewMaintainableObject().getDataObject());
238 request.setAttribute("fileName", bo.getFileName());
239 }
240 } catch (ValidationException vex) {
241 if (GlobalVariables.getMessageMap().hasNoErrors()) {
242 throw new RuntimeException("Validation Exception with no error message.", vex);
243 }
244
245 modelAndView = getUIFModelAndView(form);
246 }
247
248 return modelAndView;
249 }
250
251 protected MaintenanceDocumentService getMaintenanceDocumentService() {
252 return KRADServiceLocatorWeb.getMaintenanceDocumentService();
253 }
254
255 }