View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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          // Get note text max length from DD.
85          int noteTextMaxLength = getDataDictionaryService().getAttributeMaxLength(Note.class, NOTE_TEXT_PROPERTY_NAME).intValue();
86          if (StringUtils.isBlank(asker.getReason()) || (noteTextLength > noteTextMaxLength)) {
87              // Figure out exact number of characters that the user can enter.
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             // Below used as a place holder to allow code to specify actionForward to return if not a 'success question'
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             // send FYI for to initiator and traveler
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      * @see org.kuali.kfs.module.tem.document.web.struts.QuestionHandler#askQuestion(org.kuali.kfs.module.tem.document.web.struts.Inquisitive)
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      * Sets the ConfigurationService attribute.
163      *
164      * @return Returns the ConfigurationService.
165      */
166     public void setConfigurationService(final ConfigurationService ConfigurationService) {
167         this.ConfigurationService = ConfigurationService;
168     }
169 
170     /**
171      * Gets the ConfigurationService attribute.
172      *
173      * @return Returns the ConfigurationService.
174      */
175     protected ConfigurationService getConfigurationService() {
176         return ConfigurationService;
177     }
178 
179     /**
180      * Gets the travelEncumbranceService attribute.
181      *
182      * @return Returns the travelEncumbranceService
183      */
184 
185     public TravelEncumbranceService getTravelEncumbranceService() {
186         return travelEncumbranceService;
187     }
188 
189     /**
190      * Sets the travelEncumbranceService attribute.
191      *
192      * @param travelEncumbranceService The travelEncumbranceService to set.
193      */
194     public void setTravelEncumbranceService(TravelEncumbranceService travelEncumbranceService) {
195         this.travelEncumbranceService = travelEncumbranceService;
196     }
197 
198     /**
199      * Gets the documentService attribute.
200      *
201      * @return Returns the documentService
202      */
203 
204     public DocumentService getDocumentService() {
205         return documentService;
206     }
207 
208     /**
209      * Sets the documentService attribute.
210      *
211      * @param documentService The documentService to set.
212      */
213     public void setDocumentService(DocumentService documentService) {
214         this.documentService = documentService;
215     }
216 
217     /**
218      * Gets the travelDocumentService attribute.
219      *
220      * @return Returns the travelDocumentService
221      */
222 
223     public TravelDocumentService getTravelDocumentService() {
224         return travelDocumentService;
225     }
226 
227     /**
228      * Sets the travelDocumentService attribute.
229      *
230      * @param travelDocumentService The travelDocumentService to set.
231      */
232     public void setTravelDocumentService(TravelDocumentService travelDocumentService) {
233         this.travelDocumentService = travelDocumentService;
234     }
235 
236     /**
237      * Sets the dataDictionaryService attribute.
238      *
239      * @return Returns the dataDictionaryService.
240      */
241     public void setDataDictionaryService(final DataDictionaryService dataDictionaryService) {
242         this.dataDictionaryService = dataDictionaryService;
243     }
244 
245     /**
246      * Gets the dataDictionaryService attribute.
247      *
248      * @return Returns the dataDictionaryService.
249      */
250     protected DataDictionaryService getDataDictionaryService() {
251         return dataDictionaryService;
252     }
253 
254 
255 }