1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys.batch.service.impl;
17
18 import java.text.ParseException;
19 import java.util.Calendar;
20 import java.util.Collections;
21 import java.util.Date;
22
23 import org.joda.time.DateTime;
24 import org.kuali.ole.sys.OLEConstants;
25 import org.kuali.ole.sys.OLEParameterKeyConstants;
26 import org.kuali.ole.sys.batch.AutoDisapproveDocumentsStep;
27 import org.kuali.ole.sys.batch.service.AutoDisapproveDocumentsService;
28 import org.kuali.ole.sys.context.SpringContext;
29 import org.kuali.ole.sys.service.ReportWriterService;
30 import org.kuali.rice.core.api.datetime.DateTimeService;
31 import org.kuali.rice.core.api.parameter.ParameterEvaluator;
32 import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
33 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
34 import org.kuali.rice.kew.api.KewApiServiceLocator;
35 import org.kuali.rice.kew.api.WorkflowRuntimeException;
36 import org.kuali.rice.kew.api.doctype.DocumentType;
37 import org.kuali.rice.kew.api.doctype.DocumentTypeService;
38 import org.kuali.rice.kew.api.document.DocumentStatus;
39 import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
40 import org.kuali.rice.kew.api.document.search.DocumentSearchResult;
41 import org.kuali.rice.kew.api.document.search.DocumentSearchResults;
42 import org.kuali.rice.kew.api.exception.WorkflowException;
43 import org.kuali.rice.kim.api.identity.Person;
44 import org.kuali.rice.kim.api.identity.PersonService;
45 import org.kuali.rice.krad.bo.Note;
46 import org.kuali.rice.krad.datadictionary.exception.UnknownDocumentTypeException;
47 import org.kuali.rice.krad.document.Document;
48 import org.kuali.rice.krad.service.DocumentService;
49 import org.kuali.rice.krad.service.NoteService;
50 import org.kuali.rice.krad.util.ObjectUtils;
51 import org.springframework.transaction.annotation.Transactional;
52
53
54
55
56 @Transactional
57 public class AutoDisapproveDocumentsServiceImpl implements AutoDisapproveDocumentsService {
58 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AutoDisapproveDocumentsServiceImpl.class);
59 public static final String WORKFLOW_DOCUMENT_HEADER_ID_SEARCH_RESULT_KEY = "routeHeaderId";
60
61 private DocumentService documentService;
62 private DocumentTypeService documentTypeService;
63
64 private DateTimeService dateTimeService;
65 private ParameterService parameterService;
66
67 private NoteService noteService;
68 private PersonService personService;
69
70 private ReportWriterService autoDisapproveErrorReportWriterService;
71
72
73
74
75 public AutoDisapproveDocumentsServiceImpl() {
76
77 }
78
79
80
81
82
83 public boolean autoDisapproveDocumentsInEnrouteStatus() {
84 boolean success = true ;
85
86 if (systemParametersForAutoDisapproveDocumentsJobExist()) {
87 if (canAutoDisapproveJobRun()) {
88 LOG.debug("autoDisapproveDocumentsInEnrouteStatus() started");
89
90 Person systemUser = getPersonService().getPersonByPrincipalName(OLEConstants.SYSTEM_USER);
91
92 String principalId = systemUser.getPrincipalId();
93 String annotationForAutoDisapprovalDocument = getParameterService().getParameterValueAsString(AutoDisapproveDocumentsStep.class, OLEParameterKeyConstants.YearEndAutoDisapprovalConstants.YEAR_END_AUTO_DISAPPROVE_ANNOTATION);
94
95 Date documentCompareDate = getDocumentCompareDateParameter();
96 success = processAutoDisapproveDocuments(principalId, annotationForAutoDisapprovalDocument, documentCompareDate);
97 }
98 }
99
100 return success;
101 }
102
103
104
105
106
107 protected boolean systemParametersForAutoDisapproveDocumentsJobExist() {
108 LOG.debug("systemParametersForAutoDisapproveDocumentsJobExist() started.");
109
110 boolean systemParametersExists = true;
111
112 systemParametersExists &= checkIfRunDateParameterExists();
113 systemParametersExists &= checkIfParentDocumentTypeParameterExists();
114 systemParametersExists &= checkIfDocumentCompareCreateDateParameterExists();
115 systemParametersExists &= checkIfDocumentTypesExceptionParameterExists();
116 systemParametersExists &= checkIfAnnotationForDisapprovalParameterExists();
117
118 return systemParametersExists;
119 }
120
121
122
123
124
125
126 protected boolean checkIfRunDateParameterExists() {
127 boolean parameterExists = true;
128
129
130 if (!getParameterService().parameterExists(AutoDisapproveDocumentsStep.class, OLEParameterKeyConstants.YearEndAutoDisapprovalConstants.YEAR_END_AUTO_DISAPPROVE_DOCUMENT_STEP_RUN_DATE)) {
131 LOG.warn("YEAR_END_AUTO_DISAPPROVE_DOCUMENT_RUN_DATE System parameter does not exist in the parameters list. The job can not continue without this parameter");
132 autoDisapproveErrorReportWriterService.writeFormattedMessageLine("YEAR_END_AUTO_DISAPPROVE_DOCUMENTS_RUN_DATE System parameter does not exist in the parameters list. The job can not continue without this parameter");
133 return false;
134 }
135
136 return parameterExists;
137 }
138
139
140
141
142
143
144 protected boolean checkIfParentDocumentTypeParameterExists() {
145 boolean parameterExists = true;
146
147
148 if (!getParameterService().parameterExists(AutoDisapproveDocumentsStep.class, OLEParameterKeyConstants.YearEndAutoDisapprovalConstants.YEAR_END_AUTO_DISAPPROVE_PARENT_DOCUMENT_TYPE)) {
149 LOG.warn("YEAR_END_AUTO_DISAPPROVE_PARENT_DOCUMENT_TYPE System parameter does not exist in the parameters list. The job can not continue without this parameter");
150 autoDisapproveErrorReportWriterService.writeFormattedMessageLine("YEAR_END_AUTO_DISAPPROVE_PARENT_DOCUMENT_TYPE System parameter does not exist in the parameters list. The job can not continue without this parameter");
151 return false;
152 }
153
154 return parameterExists;
155 }
156
157
158
159
160
161
162 protected boolean checkIfDocumentCompareCreateDateParameterExists() {
163 boolean parameterExists = true;
164
165
166 if (!getParameterService().parameterExists(AutoDisapproveDocumentsStep.class, OLEParameterKeyConstants.YearEndAutoDisapprovalConstants.YEAR_END_AUTO_DISAPPROVE_DOCUMENT_CREATE_DATE)) {
167 LOG.warn("YEAR_END_AUTO_DISAPPROVE_DOCUMENT_CREATE_DATE System parameter does not exist in the parameters list. The job can not continue without this parameter");
168 autoDisapproveErrorReportWriterService.writeFormattedMessageLine("YEAR_END_AUTO_DISAPPROVE_DOCUMENT_CREATE_DATE System parameter does not exist in the parameters list. The job can not continue without this parameter");
169 return false;
170 }
171
172 return parameterExists;
173 }
174
175
176
177
178
179
180 protected boolean checkIfDocumentTypesExceptionParameterExists() {
181 boolean parameterExists = true;
182
183
184 if (!getParameterService().parameterExists(AutoDisapproveDocumentsStep.class, OLEParameterKeyConstants.YearEndAutoDisapprovalConstants.YEAR_END_AUTO_DISAPPROVE_DOCUMENT_TYPES)) {
185 LOG.warn("YEAR_END_AUTO_DISAPPROVE_DOCUMENT_TYPES System parameter does not exist in the parameters list. The job can not continue without this parameter");
186 autoDisapproveErrorReportWriterService.writeFormattedMessageLine("YEAR_END_AUTO_DISAPPROVE_DOCUMENT_TYPES System parameter does not exist in the parameters list. The job can not continue without this parameter");
187 return false;
188 }
189
190 return parameterExists;
191 }
192
193
194
195
196
197
198 protected boolean checkIfAnnotationForDisapprovalParameterExists() {
199 boolean parameterExists = true;
200
201
202 if (!getParameterService().parameterExists(AutoDisapproveDocumentsStep.class, OLEParameterKeyConstants.YearEndAutoDisapprovalConstants.YEAR_END_AUTO_DISAPPROVE_ANNOTATION)) {
203 LOG.warn("YEAR_END_AUTO_DISAPPROVE_ANNOTATION System parameter does not exist in the parameters list. The job can not continue without this parameter");
204 autoDisapproveErrorReportWriterService.writeFormattedMessageLine("YEAR_END_AUTO_DISAPPROVE_ANNOTATION System parameter does not exist in the parameters list. The job can not continue without this parameter");
205 return false;
206 }
207
208 return parameterExists;
209 }
210
211
212
213
214
215 protected boolean canAutoDisapproveJobRun() {
216 boolean autoDisapproveCanRun = true;
217
218
219 String yearEndAutoDisapproveRunDate = getParameterService().getParameterValueAsString(AutoDisapproveDocumentsStep.class, OLEParameterKeyConstants.YearEndAutoDisapprovalConstants.YEAR_END_AUTO_DISAPPROVE_DOCUMENT_STEP_RUN_DATE);
220
221 String today = getDateTimeService().toDateString(getDateTimeService().getCurrentDate());
222
223 if (!yearEndAutoDisapproveRunDate.equals(today)) {
224 LOG.warn("YEAR_END_AUTO_DISAPPROVE_DOCUMENTS_RUN_DATE: Automatic disapproval bypassed. The date on which the auto disapproval step should run: " + yearEndAutoDisapproveRunDate + " does not equal to today's date: " + today);
225 String message = ("YEAR_END_AUTO_DISAPPROVE_DOCUMENTS_RUN_DATE: Automatic disapproval bypassed. The date on which the auto disapproval step should run: ").concat(yearEndAutoDisapproveRunDate).concat(" does not equal to today's date: ").concat(today);
226 autoDisapproveErrorReportWriterService.writeFormattedMessageLine(message);
227 autoDisapproveCanRun = false;
228 }
229
230 return autoDisapproveCanRun;
231 }
232
233
234
235
236
237
238
239 protected boolean processAutoDisapproveDocuments(String principalId, String annotation, Date documentCompareDate) {
240 boolean success = true;
241
242 DocumentSearchCriteria.Builder criteria = DocumentSearchCriteria.Builder.create();
243 criteria.setDocumentStatuses(Collections.singletonList(DocumentStatus.ENROUTE));
244 criteria.setSaveName(null);
245
246 try {
247 DocumentSearchResults results = KewApiServiceLocator.getWorkflowDocumentService().documentSearch(principalId, criteria.build());
248
249 String documentHeaderId = null;
250
251 for (DocumentSearchResult result : results.getSearchResults()) {
252 documentHeaderId = result.getDocument().getDocumentId();
253 Document document = findDocumentForAutoDisapproval(documentHeaderId);
254 if (document != null) {
255 if (checkIfDocumentEligibleForAutoDispproval(document)) {
256 if (!exceptionsToAutoDisapproveProcess(document, documentCompareDate)) {
257 try {
258 autoDisapprovalYearEndDocument(document, annotation);
259 LOG.info("The document with header id: " + documentHeaderId + " is automatically disapproved by this job.");
260 }catch (Exception e) {
261 LOG.error("Exception encountered trying to auto disapprove the document " + e.getMessage());
262 String message = ("Exception encountered trying to auto disapprove the document: ").concat(documentHeaderId);
263 autoDisapproveErrorReportWriterService.writeFormattedMessageLine(message);
264 }
265 }else {
266 LOG.info("Year End Auto Disapproval Exceptions: The document: " + documentHeaderId + " is NOT AUTO DISAPPROVED.");
267 }
268 }
269 }else{
270 LOG.error("Document is NULL. It should never have been null");
271 String message = ("Error: Document with id: ").concat(documentHeaderId).concat(" - Document is NULL. It should never have been null");
272 autoDisapproveErrorReportWriterService.writeFormattedMessageLine(message);
273 }
274 }
275 } catch (WorkflowRuntimeException wfre) {
276 success = false;
277 LOG.warn("Error with workflow search for documents for auto disapproval");
278 String message = ("Error with workflow search for documents for auto disapproval. The auto disapproval job is stopped.");
279 autoDisapproveErrorReportWriterService.writeFormattedMessageLine(message);
280 }
281
282 return success;
283 }
284
285
286
287
288
289
290 protected boolean checkIfDocumentEligibleForAutoDispproval(Document document) {
291 boolean documentEligible = false;
292
293 String yearEndAutoDisapproveParentDocumentType = getParameterService().getParameterValueAsString(AutoDisapproveDocumentsStep.class, OLEParameterKeyConstants.YearEndAutoDisapprovalConstants.YEAR_END_AUTO_DISAPPROVE_PARENT_DOCUMENT_TYPE);
294
295 DocumentType parentDocumentType = (DocumentType) getDocumentTypeService().getDocumentTypeByName(yearEndAutoDisapproveParentDocumentType);
296
297 String documentTypeName = document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName();
298 DocumentType childDocumentType = (DocumentType) getDocumentTypeService().getDocumentTypeByName(documentTypeName);
299 documentEligible = childDocumentType.getParentId().equals(parentDocumentType.getId());
300 return documentEligible;
301 }
302
303
304
305
306
307
308 protected Date getDocumentCompareDateParameter() {
309 Date documentCompareDate = null;
310
311 String yearEndAutoDisapproveDocumentDate = getParameterService().getParameterValueAsString(AutoDisapproveDocumentsStep.class, OLEParameterKeyConstants.YearEndAutoDisapprovalConstants.YEAR_END_AUTO_DISAPPROVE_DOCUMENT_CREATE_DATE);
312
313 if (ObjectUtils.isNull(yearEndAutoDisapproveDocumentDate)) {
314 LOG.warn("Exception: System Parameter YEAR_END_AUTO_DISAPPROVE_DOCUMENT_CREATE_DATE can not be determined.");
315 String message = ("Exception: The value for System Parameter YEAR_END_AUTO_DISAPPROVE_DOCUMENT_CREATE_DATE can not be determined. The auto disapproval job is stopped.");
316 autoDisapproveErrorReportWriterService.writeFormattedMessageLine(message);
317 throw new RuntimeException("Exception: AutoDisapprovalStep job stopped because System Parameter YEAR_END_AUTO_DISAPPROVE_DOCUMENT_CREATE_DATE is null");
318 }
319
320 try {
321 Date compareDate = getDateTimeService().convertToDate(yearEndAutoDisapproveDocumentDate);
322 Calendar calendar = Calendar.getInstance();
323 calendar.setTime(compareDate);
324 calendar.set(Calendar.HOUR, 23);
325 calendar.set(Calendar.MINUTE, 59);
326 calendar.set(Calendar.SECOND, 59);
327 documentCompareDate = calendar.getTime();
328 }
329 catch (ParseException pe) {
330 LOG.warn("ParseException: System Parameter YEAR_END_AUTO_DISAPPROVE_DOCUMENT_CREATE_DATE can not be determined.");
331 String message = ("ParseException: The value for System Parameter YEAR_END_AUTO_DISAPPROVE_DOCUMENT_CREATE_DATE is invalid. The auto disapproval job is stopped.");
332 autoDisapproveErrorReportWriterService.writeFormattedMessageLine(message);
333 throw new RuntimeException("ParseException: AutoDisapprovalStep job stopped because System Parameter YEAR_END_AUTO_DISAPPROVE_DOCUMENT_CREATE_DATE is invalid");
334 }
335
336 return documentCompareDate;
337 }
338
339
340
341
342
343
344 protected Document findDocumentForAutoDisapproval(String documentHeaderId) {
345 Document document = null;
346
347 try {
348 document = documentService.getByDocumentHeaderId(documentHeaderId);
349 }
350 catch (WorkflowException ex) {
351 LOG.error("Exception encountered on finding the document: " + documentHeaderId, ex );
352 } catch ( UnknownDocumentTypeException ex ) {
353
354 LOG.error("Exception encountered on finding the document: " + documentHeaderId, ex );
355 }
356
357 return document;
358 }
359
360
361
362
363
364
365
366
367
368 protected boolean exceptionsToAutoDisapproveProcess(Document document, Date documentCompareDate) {
369 boolean exceptionToDisapprove = true;
370 Date createDate = null;
371
372 String documentNumber = document.getDocumentHeader().getDocumentNumber();
373
374 DateTime documentCreateDate = document.getDocumentHeader().getWorkflowDocument().getDateCreated();
375 createDate = documentCreateDate.toDate();
376
377 Calendar calendar = Calendar.getInstance();
378 calendar.setTime(documentCompareDate);
379 String strCompareDate = calendar.getTime().toString();
380
381 calendar.setTime(createDate);
382 String strCreateDate = calendar.getTime().toString();
383
384 if (createDate.before(documentCompareDate) || createDate.equals(documentCompareDate)) {
385 String documentTypeName = document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName();
386
387 ParameterEvaluator evaluatorDocumentType = SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(AutoDisapproveDocumentsStep.class, OLEParameterKeyConstants.YearEndAutoDisapprovalConstants.YEAR_END_AUTO_DISAPPROVE_DOCUMENT_TYPES, documentTypeName);
388 exceptionToDisapprove = !evaluatorDocumentType.evaluationSucceeds();
389 if (exceptionToDisapprove) {
390 LOG.info("Document Id: " + documentNumber + " - Exception to Auto Disapprove: Document's type: " + documentTypeName + " is in the System Parameter For Document Types Exception List.");
391 }
392 }
393 else {
394 LOG.info("Document Id: " + documentNumber + " - Exception to Auto Disapprove: Document's create date: " + strCreateDate + " is NOT less than or equal to System Parameter Compare Date: " + strCompareDate);
395 exceptionToDisapprove = true;
396 }
397
398 return exceptionToDisapprove;
399 }
400
401
402
403
404
405
406
407
408 protected void autoDisapprovalYearEndDocument(Document document, String annotationForAutoDisapprovalDocument) throws Exception {
409 Person systemUser = getPersonService().getPersonByPrincipalName(OLEConstants.SYSTEM_USER);
410
411 Note approveNote = noteService.createNote(new Note(), document.getDocumentHeader(), systemUser.getPrincipalId());
412 approveNote.setNoteText(annotationForAutoDisapprovalDocument);
413
414 approveNote.setAuthorUniversalIdentifier(systemUser.getPrincipalId());
415
416 approveNote.setNotePostedTimestampToCurrent();
417
418 noteService.save(approveNote);
419
420 document.addNote(approveNote);
421
422 documentService.superUserDisapproveDocumentWithoutSaving(document, "Disapproval of Outstanding Documents - Year End Cancellation Process");
423 }
424
425
426
427
428
429
430 public void setDocumentService(DocumentService documentService) {
431 this.documentService = documentService;
432 }
433
434
435
436
437
438
439 public DocumentService getDocumentService() {
440 return documentService;
441 }
442
443
444
445
446
447
448 protected ParameterService getParameterService() {
449 return parameterService;
450 }
451
452
453
454
455
456
457 public void setParameterService(ParameterService parameterService) {
458 this.parameterService = parameterService;
459 }
460
461
462
463
464
465
466 protected DateTimeService getDateTimeService() {
467 return dateTimeService;
468 }
469
470
471
472
473
474
475 public void setDateTimeService(DateTimeService dateTimeService) {
476 this.dateTimeService = dateTimeService;
477 }
478
479
480
481
482
483 protected synchronized NoteService getNoteService() {
484 if (this.noteService == null) {
485 this.noteService = SpringContext.getBean(NoteService.class);
486 }
487 return this.noteService;
488 }
489
490
491
492
493
494
495 public void setNoteService(NoteService noteService) {
496 this.noteService = noteService;
497 }
498
499
500
501
502 protected PersonService getPersonService() {
503 if(personService==null)
504 personService = SpringContext.getBean(PersonService.class);
505 return personService;
506 }
507
508
509
510
511
512
513 protected DocumentTypeService getDocumentTypeService() {
514 if(documentTypeService==null)
515 documentTypeService = SpringContext.getBean(DocumentTypeService.class);
516 return documentTypeService;
517 }
518
519
520
521
522
523 protected ReportWriterService getAutoDisapproveErrorReportWriterService() {
524 return autoDisapproveErrorReportWriterService;
525 }
526
527
528
529
530
531 public void setAutoDisapproveErrorReportWriterService(ReportWriterService autoDisapproveErrorReportWriterService) {
532 this.autoDisapproveErrorReportWriterService = autoDisapproveErrorReportWriterService;
533 }
534
535 }