1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.kuali.kfs.module.tem.document.web.struts;
20
21 import static org.kuali.kfs.module.tem.TemConstants.CANCEL_NOTE_PREFIX;
22 import static org.kuali.kfs.module.tem.TemConstants.CANCEL_TA_QUESTION;
23 import static org.kuali.kfs.module.tem.TemConstants.CANCEL_TA_TEXT;
24 import static org.kuali.kfs.module.tem.TemConstants.CONFIRM_CANCEL_QUESTION;
25 import static org.kuali.kfs.module.tem.TemKeyConstants.ERROR_TA_REASON_PASTLIMIT;
26 import static org.kuali.kfs.module.tem.TemKeyConstants.ERROR_TA_REASON_REQUIRED;
27 import static org.kuali.kfs.module.tem.TemKeyConstants.TA_QUESTION_DOCUMENT;
28 import static org.kuali.kfs.sys.KFSConstants.BLANK_SPACE;
29 import static org.kuali.kfs.sys.KFSConstants.MAPPING_BASIC;
30 import static org.kuali.kfs.sys.KFSConstants.NOTE_TEXT_PROPERTY_NAME;
31 import static org.kuali.kfs.sys.KFSConstants.QUESTION_REASON_ATTRIBUTE_NAME;
32
33 import java.util.ArrayList;
34
35 import org.apache.commons.lang.StringUtils;
36 import org.kuali.kfs.module.tem.TemConstants;
37 import org.kuali.kfs.module.tem.TemConstants.TravelAuthorizationStatusCodeKeys;
38 import org.kuali.kfs.module.tem.document.TravelAuthorizationDocument;
39 import org.kuali.kfs.module.tem.document.TravelDocument;
40 import org.kuali.kfs.module.tem.document.service.TravelDocumentService;
41 import org.kuali.kfs.module.tem.service.TravelEncumbranceService;
42 import org.kuali.kfs.sys.KFSConstants;
43 import org.kuali.kfs.sys.KFSPropertyConstants;
44 import org.kuali.kfs.sys.context.SpringContext;
45 import org.kuali.kfs.sys.document.validation.event.AccountingDocumentSaveWithNoLedgerEntryGenerationEvent;
46 import org.kuali.rice.core.api.config.property.ConfigurationService;
47 import org.kuali.rice.kim.api.identity.principal.Principal;
48 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
49 import org.kuali.rice.krad.bo.AdHocRouteRecipient;
50 import org.kuali.rice.krad.bo.Note;
51 import org.kuali.rice.krad.exception.ValidationException;
52 import org.kuali.rice.krad.service.DataDictionaryService;
53 import org.kuali.rice.krad.service.DocumentService;
54 import org.kuali.rice.krad.util.ObjectUtils;
55 import org.kuali.rice.krad.workflow.service.WorkflowDocumentService;
56
57
58
59
60 public class CancelQuestionHandler implements QuestionHandler<TravelDocument> {
61
62 private ConfigurationService ConfigurationService;
63 private TravelEncumbranceService travelEncumbranceService;
64 private DocumentService documentService;
65 private TravelDocumentService travelDocumentService;
66 private DataDictionaryService dataDictionaryService;
67
68
69 @Override
70 public <T> T handleResponse(final Inquisitive<TravelDocument,?> asker) throws Exception {
71 if (asker.denied(CANCEL_TA_QUESTION)) {
72 return (T) asker.back();
73 }
74 else if (asker.confirmed(CONFIRM_CANCEL_QUESTION)) {
75 return (T) asker.end();
76 }
77 TravelAuthorizationDocument taDocument = (TravelAuthorizationDocument)asker.getDocument();
78
79 String note = createNote(asker.getReason(), taDocument.getDocumentNumber());
80 final StringBuilder noteText = new StringBuilder(note);
81
82 int noteTextLength = noteText.length();
83
84
85 int noteTextMaxLength = getDataDictionaryService().getAttributeMaxLength(Note.class, NOTE_TEXT_PROPERTY_NAME).intValue();
86 if (StringUtils.isBlank(asker.getReason()) || (noteTextLength > noteTextMaxLength)) {
87
88 int reasonLimit = noteTextMaxLength - noteTextLength;
89 reasonLimit = reasonLimit<0?reasonLimit*-1:reasonLimit;
90 String message = getMessageFrom(TA_QUESTION_DOCUMENT);
91 String question = StringUtils.replace(message, "{0}", CANCEL_TA_TEXT);
92 if (StringUtils.isBlank(asker.getReason())){
93 return (T) asker.confirm(CANCEL_TA_QUESTION, question, true, ERROR_TA_REASON_REQUIRED,QUESTION_REASON_ATTRIBUTE_NAME,CANCEL_TA_TEXT);
94 }
95 else {
96 return (T) asker.confirm(CANCEL_TA_QUESTION, question, true, ERROR_TA_REASON_PASTLIMIT, QUESTION_REASON_ATTRIBUTE_NAME, new Integer(reasonLimit).toString());
97 }
98 }
99
100 try {
101
102 T returnActionForward = (T) ((StrutsInquisitor) asker).getMapping().findForward(MAPPING_BASIC);
103
104 taDocument.refreshReferenceObject(KFSPropertyConstants.GENERAL_LEDGER_PENDING_ENTRIES);
105
106 final Note reasonNote = getDocumentService().createNoteFromDocument(taDocument, noteText.toString());
107 reasonNote.setNoteText(noteText.toString());
108 taDocument.addNote(reasonNote);
109
110 final Note cancelNote = getDocumentService().createNoteFromDocument(taDocument, TemConstants.TA_CANCELLED_MESSAGE);
111 Principal systemUser = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(KFSConstants.SYSTEM_USER);
112 cancelNote.setAuthorUniversalIdentifier(systemUser.getPrincipalId());
113 taDocument.addNote(cancelNote);
114
115 taDocument.updateAndSaveAppDocStatus(TravelAuthorizationStatusCodeKeys.CANCELLED);
116 getTravelEncumbranceService().liquidateEncumbranceForCancelTA(taDocument);
117 SpringContext.getBean(DocumentService.class).saveDocument(taDocument, AccountingDocumentSaveWithNoLedgerEntryGenerationEvent.class);
118 taDocument.refreshReferenceObject(KFSPropertyConstants.GENERAL_LEDGER_PENDING_ENTRIES);
119
120
121 getTravelDocumentService().addAdHocFYIRecipient(taDocument, taDocument.getDocumentHeader().getWorkflowDocument().getInitiatorPrincipalId());
122 getTravelDocumentService().addAdHocFYIRecipient(taDocument, taDocument.getTraveler().getPrincipalId());
123
124 SpringContext.getBean(WorkflowDocumentService.class).acknowledge(taDocument.getDocumentHeader().getWorkflowDocument(), null, new ArrayList<AdHocRouteRecipient>(taDocument.getAdHocRoutePersons()));
125
126 if (ObjectUtils.isNotNull(returnActionForward)) {
127 return returnActionForward;
128 }
129 else {
130
131 String message = getMessageFrom(TA_QUESTION_DOCUMENT);
132 String question = StringUtils.replace(message, "{0}", CANCEL_TA_TEXT);
133 return (T) asker.confirm(CANCEL_TA_QUESTION, question, true, "temSingleConfirmationQuestion", CANCEL_TA_QUESTION, "");
134 }
135 }
136 catch (ValidationException ve) {
137 throw ve;
138 }
139 }
140
141
142
143
144 @Override
145 public <T> T askQuestion(final Inquisitive<TravelDocument,?> asker) throws Exception {
146 final String key = getMessageFrom(TA_QUESTION_DOCUMENT);
147 final String question = StringUtils.replace(key, "{0}", CANCEL_TA_TEXT);
148 T retval = (T) asker.confirm(CANCEL_TA_QUESTION, question, true);
149 return retval;
150 }
151
152 public String getMessageFrom(final String messageType) {
153 return getConfigurationService().getPropertyValueAsString(messageType);
154 }
155
156 private String createNote(String reason, String documentNumber) {
157 String introNoteMessage = CANCEL_NOTE_PREFIX + BLANK_SPACE;
158 return introNoteMessage + reason;
159 }
160
161
162
163
164
165
166 public void setConfigurationService(final ConfigurationService ConfigurationService) {
167 this.ConfigurationService = ConfigurationService;
168 }
169
170
171
172
173
174
175 protected ConfigurationService getConfigurationService() {
176 return ConfigurationService;
177 }
178
179
180
181
182
183
184
185 public TravelEncumbranceService getTravelEncumbranceService() {
186 return travelEncumbranceService;
187 }
188
189
190
191
192
193
194 public void setTravelEncumbranceService(TravelEncumbranceService travelEncumbranceService) {
195 this.travelEncumbranceService = travelEncumbranceService;
196 }
197
198
199
200
201
202
203
204 public DocumentService getDocumentService() {
205 return documentService;
206 }
207
208
209
210
211
212
213 public void setDocumentService(DocumentService documentService) {
214 this.documentService = documentService;
215 }
216
217
218
219
220
221
222
223 public TravelDocumentService getTravelDocumentService() {
224 return travelDocumentService;
225 }
226
227
228
229
230
231
232 public void setTravelDocumentService(TravelDocumentService travelDocumentService) {
233 this.travelDocumentService = travelDocumentService;
234 }
235
236
237
238
239
240
241 public void setDataDictionaryService(final DataDictionaryService dataDictionaryService) {
242 this.dataDictionaryService = dataDictionaryService;
243 }
244
245
246
247
248
249
250 protected DataDictionaryService getDataDictionaryService() {
251 return dataDictionaryService;
252 }
253
254
255 }