1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.web.spring.controller;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.apache.commons.lang.ArrayUtils;
26 import org.kuali.rice.core.framework.parameter.ParameterConstants;
27 import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator;
28 import org.kuali.rice.core.util.RiceKeyConstants;
29 import org.kuali.rice.kew.exception.WorkflowException;
30 import org.kuali.rice.kew.util.KEWConstants;
31 import org.kuali.rice.kns.bo.AdHocRouteRecipient;
32 import org.kuali.rice.kns.document.Document;
33 import org.kuali.rice.kns.exception.DocumentAuthorizationException;
34 import org.kuali.rice.kns.exception.UnknownDocumentIdException;
35 import org.kuali.rice.kns.exception.ValidationException;
36 import org.kuali.rice.kns.question.ConfirmationQuestion;
37 import org.kuali.rice.kns.service.BusinessObjectAuthorizationService;
38 import org.kuali.rice.kns.service.BusinessObjectMetaDataService;
39 import org.kuali.rice.kns.service.BusinessObjectService;
40 import org.kuali.rice.kns.service.DataDictionaryService;
41 import org.kuali.rice.kns.service.DocumentHelperService;
42 import org.kuali.rice.kns.service.DocumentService;
43 import org.kuali.rice.kns.service.KNSServiceLocator;
44 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
45 import org.kuali.rice.kns.util.GlobalVariables;
46 import org.kuali.rice.kns.util.KNSConstants;
47 import org.kuali.rice.kns.util.KNSPropertyConstants;
48 import org.kuali.rice.kns.util.SessionTicket;
49 import org.kuali.rice.kns.util.WebUtils;
50 import org.kuali.rice.kns.web.spring.form.DocumentFormBase;
51 import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
52 import org.springframework.validation.BindingResult;
53 import org.springframework.web.bind.annotation.ModelAttribute;
54 import org.springframework.web.bind.annotation.RequestMapping;
55 import org.springframework.web.servlet.ModelAndView;
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public abstract class DocumentControllerBase extends UifControllerBase {
70 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocumentControllerBase.class);
71
72
73
74 protected static final String[] DOCUMENT_LOAD_COMMANDS = { KEWConstants.ACTIONLIST_COMMAND,
75 KEWConstants.DOCSEARCH_COMMAND, KEWConstants.SUPERUSER_COMMAND, KEWConstants.HELPDESK_ACTIONLIST_COMMAND };
76
77 private BusinessObjectService businessObjectService;
78 private BusinessObjectAuthorizationService businessObjectAuthorizationService;
79 private BusinessObjectMetaDataService businessObjectMetaDataService;
80 private DataDictionaryService dataDictionaryService;
81 private DocumentService documentService;
82 private DocumentHelperService documentHelperService;
83
84 @Override
85 public abstract DocumentFormBase createInitialForm(HttpServletRequest request);
86
87
88
89
90
91
92
93
94
95
96 @RequestMapping(params = "methodToCall=docHandler")
97 public ModelAndView docHandler(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result, HttpServletRequest request,
98 HttpServletResponse response) throws Exception {
99 String command = form.getCommand();
100
101
102 if (ArrayUtils.contains(DOCUMENT_LOAD_COMMANDS, command) && form.getDocId() != null) {
103 loadDocument(form);
104 }
105 else if (KEWConstants.INITIATE_COMMAND.equals(command)) {
106 createDocument(form);
107 }
108 else {
109 LOG.error("docHandler called with invalid parameters");
110 throw new IllegalStateException("docHandler called with invalid parameters");
111 }
112
113
114
115
116
117
118 return getUIFModelAndView(form);
119 }
120
121
122
123
124
125
126
127
128
129
130 protected void loadDocument(DocumentFormBase form) throws WorkflowException {
131 String docId = form.getDocId();
132
133 Document doc = null;
134 doc = getDocumentService().getByDocumentHeaderId(docId);
135 if (doc == null) {
136 throw new UnknownDocumentIdException(
137 "Document no longer exists. It may have been cancelled before being saved.");
138 }
139
140 KualiWorkflowDocument workflowDocument = doc.getDocumentHeader().getWorkflowDocument();
141 if (!getDocumentHelperService().getDocumentAuthorizer(doc).canOpen(doc,
142 GlobalVariables.getUserSession().getPerson())) {
143 throw buildAuthorizationException("open", doc);
144 }
145
146
147
148 if (workflowDocument != doc.getDocumentHeader().getWorkflowDocument()) {
149 LOG.warn("Workflow document changed via canOpen check");
150 doc.getDocumentHeader().setWorkflowDocument(workflowDocument);
151 }
152
153 form.setDocument(doc);
154 KualiWorkflowDocument workflowDoc = doc.getDocumentHeader().getWorkflowDocument();
155 form.setDocTypeName(workflowDoc.getDocumentType());
156
157 KNSServiceLocatorWeb.getSessionDocumentService().addDocumentToUserSession(GlobalVariables.getUserSession(), workflowDoc);
158 }
159
160
161
162
163
164
165
166
167
168
169 protected void createDocument(DocumentFormBase form) throws WorkflowException {
170 Document doc = getDocumentService().getNewDocument(form.getDocTypeName());
171
172 form.setDocument(doc);
173 form.setDocTypeName(doc.getDocumentHeader().getWorkflowDocument().getDocumentType());
174 }
175
176
177
178
179 @RequestMapping(params = "methodToCall=save")
180 public ModelAndView save(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result, HttpServletRequest request,
181 HttpServletResponse response) throws Exception {
182
183 doProcessingAfterPost(form, request);
184
185
186
187
188
189 Document document = form.getDocument();
190
191 String viewName = checkAndWarnAboutSensitiveData(form, request, response,
192 KNSPropertyConstants.DOCUMENT_EXPLANATION, document.getDocumentHeader().getExplanation(), "save", "");
193
194
195 if (viewName != null) {
196 return new ModelAndView(viewName);
197 }
198
199 try {
200
201 getDocumentService().saveDocument(document);
202
203 GlobalVariables.getMessageList().add(RiceKeyConstants.MESSAGE_SAVED);
204 form.setAnnotation("");
205 }
206 catch(ValidationException vex) {
207
208 }
209
210 return getUIFModelAndView(form);
211 }
212
213
214
215
216 @RequestMapping(params = "methodToCall=route")
217 public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result, HttpServletRequest request,
218 HttpServletResponse response) throws Exception {
219 doProcessingAfterPost(form, request);
220
221
222
223
224
225
226
227
228
229 Document document = form.getDocument();
230
231 String viewName = checkAndWarnAboutSensitiveData(form, request, response,
232 KNSPropertyConstants.DOCUMENT_EXPLANATION, document.getDocumentHeader().getExplanation(), "route", "");
233 if (viewName != null) {
234 return new ModelAndView(viewName);
235 }
236
237
238
239
240 getDocumentService().routeDocument(document, form.getAnnotation(), new ArrayList<AdHocRouteRecipient>());
241
242 GlobalVariables.getMessageList().add(RiceKeyConstants.MESSAGE_ROUTE_SUCCESSFUL);
243 form.setAnnotation("");
244
245
246 return getUIFModelAndView(form);
247 }
248
249
250
251
252
253
254
255
256 protected void doProcessingAfterPost(DocumentFormBase form, HttpServletRequest request) {
257 getBusinessObjectService().linkUserFields(form.getDocument());
258 }
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331 protected String checkAndWarnAboutSensitiveData(DocumentFormBase form, HttpServletRequest request,
332 HttpServletResponse response, String fieldName, String fieldValue, String caller, String context)
333 throws Exception {
334
335 String viewName = null;
336 Document document = form.getDocument();
337
338 boolean containsSensitiveData = WebUtils.containsSensitiveDataPatternMatch(fieldValue);
339
340
341
342 boolean warnForSensitiveData = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsBoolean(
343 KNSConstants.KNS_NAMESPACE, ParameterConstants.ALL_COMPONENT,
344 KNSConstants.SystemGroupParameterNames.SENSITIVE_DATA_PATTERNS_WARNING_IND);
345
346
347 Map<String, String> ticketContext = new HashMap<String, String>();
348 ticketContext.put(KNSPropertyConstants.DOCUMENT_NUMBER, document.getDocumentNumber());
349 ticketContext.put(KNSConstants.CALLING_METHOD, caller);
350 ticketContext.put(KNSPropertyConstants.NAME, fieldName);
351
352 boolean questionAsked = GlobalVariables.getUserSession().hasMatchingSessionTicket(
353 KNSConstants.SENSITIVE_DATA_QUESTION_SESSION_TICKET, ticketContext);
354
355
356 if (containsSensitiveData && warnForSensitiveData && !questionAsked) {
357 Object question = request.getParameter(KNSConstants.QUESTION_INST_ATTRIBUTE_NAME);
358 if (question == null || !KNSConstants.DOCUMENT_SENSITIVE_DATA_QUESTION.equals(question)) {
359
360
361
362
363
364
365
366
367
368
369
370 viewName = "ask_user_questions";
371 }
372 else {
373 Object buttonClicked = request.getParameter(KNSConstants.QUESTION_CLICKED_BUTTON);
374
375
376 if (ConfirmationQuestion.NO.equals(buttonClicked)) {
377
378 viewName = "user_says_no";
379 }
380
381
382
383 SessionTicket ticket = new SessionTicket(KNSConstants.SENSITIVE_DATA_QUESTION_SESSION_TICKET);
384 ticket.setTicketContext(ticketContext);
385 GlobalVariables.getUserSession().putSessionTicket(ticket);
386 }
387 }
388
389
390 return viewName;
391 }
392
393
394
395
396
397
398
399
400
401 protected DocumentAuthorizationException buildAuthorizationException(String action, Document document) {
402 return new DocumentAuthorizationException(GlobalVariables.getUserSession().getPerson().getPrincipalName(),
403 action, document.getDocumentNumber());
404 }
405
406 public BusinessObjectService getBusinessObjectService() {
407 if (this.businessObjectService == null) {
408 this.businessObjectService = KNSServiceLocator.getBusinessObjectService();
409 }
410 return this.businessObjectService;
411 }
412
413 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
414 this.businessObjectService = businessObjectService;
415 }
416
417 public BusinessObjectAuthorizationService getBusinessObjectAuthorizationService() {
418 if (this.businessObjectAuthorizationService == null) {
419 this.businessObjectAuthorizationService = KNSServiceLocatorWeb.getBusinessObjectAuthorizationService();
420 }
421 return this.businessObjectAuthorizationService;
422 }
423
424 public void setBusinessObjectAuthorizationService(
425 BusinessObjectAuthorizationService businessObjectAuthorizationService) {
426 this.businessObjectAuthorizationService = businessObjectAuthorizationService;
427 }
428
429 public BusinessObjectMetaDataService getBusinessObjectMetaDataService() {
430 if (this.businessObjectMetaDataService == null) {
431 this.businessObjectMetaDataService = KNSServiceLocatorWeb.getBusinessObjectMetaDataService();
432 }
433 return this.businessObjectMetaDataService;
434 }
435
436 public void setBusinessObjectMetaDataService(BusinessObjectMetaDataService businessObjectMetaDataService) {
437 this.businessObjectMetaDataService = businessObjectMetaDataService;
438 }
439
440 public DataDictionaryService getDataDictionaryService() {
441 if (this.dataDictionaryService == null) {
442 this.dataDictionaryService = KNSServiceLocatorWeb.getDataDictionaryService();
443 }
444 return this.dataDictionaryService;
445 }
446
447 public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
448 this.dataDictionaryService = dataDictionaryService;
449 }
450
451 public DocumentService getDocumentService() {
452 if (this.documentService == null) {
453 this.documentService = KNSServiceLocatorWeb.getDocumentService();
454 }
455 return this.documentService;
456 }
457
458 public void setDocumentService(DocumentService documentService) {
459 this.documentService = documentService;
460 }
461
462 public DocumentHelperService getDocumentHelperService() {
463 if (this.documentHelperService == null) {
464 this.documentHelperService = KNSServiceLocatorWeb.getDocumentHelperService();
465 }
466 return this.documentHelperService;
467 }
468
469 public void setDocumentHelperService(DocumentHelperService documentHelperService) {
470 this.documentHelperService = documentHelperService;
471 }
472 }