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 java.util.Properties;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.apache.struts.action.ActionForm;
28  import org.apache.struts.action.ActionForward;
29  import org.apache.struts.action.ActionMapping;
30  import org.apache.struts.action.ActionMessage;
31  import org.apache.struts.action.ActionMessages;
32  import org.kuali.kfs.module.tem.TemConstants;
33  import org.kuali.kfs.module.tem.TemKeyConstants;
34  import org.kuali.kfs.module.tem.TemWorkflowConstants;
35  import org.kuali.kfs.module.tem.businessobject.TemProfile;
36  import org.kuali.kfs.module.tem.businessobject.TemProfileAccount;
37  import org.kuali.kfs.module.tem.document.CardApplicationDocument;
38  import org.kuali.kfs.module.tem.document.TemCorporateCardApplicationDocument;
39  import org.kuali.kfs.module.tem.document.service.TravelDocumentService;
40  import org.kuali.kfs.module.tem.service.TemProfileService;
41  import org.kuali.kfs.sys.KFSConstants;
42  import org.kuali.kfs.sys.context.SpringContext;
43  import org.kuali.kfs.sys.document.web.struts.FinancialSystemTransactionalDocumentActionBase;
44  import org.kuali.rice.core.api.config.property.ConfigurationService;
45  import org.kuali.rice.core.api.util.RiceKeyConstants;
46  import org.kuali.rice.kew.api.KewApiConstants;
47  import org.kuali.rice.kew.api.KewApiServiceLocator;
48  import org.kuali.rice.kew.api.document.attribute.DocumentAttributeIndexingQueue;
49  import org.kuali.rice.kew.api.exception.WorkflowException;
50  import org.kuali.rice.kim.api.identity.Person;
51  import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
52  import org.kuali.rice.krad.exception.AuthorizationException;
53  import org.kuali.rice.krad.util.GlobalVariables;
54  import org.kuali.rice.krad.util.UrlFactory;
55  
56  public class TemCardApplicationAction extends FinancialSystemTransactionalDocumentActionBase {
57  
58      private final static String CTS_ACTION = "temCTSCardApplication.do";
59      private final static String CORP_ACTION = "temCorporateCardApplication.do";
60      private final static String QUESTION_FORWARD = "cardQuestion";
61      private final static String ERROR_FORWARD = "cardApplicationError";
62  
63      @Override
64      public ActionForward docHandler(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
65          ActionForward forward = super.docHandler(mapping, form, request, response);
66  
67          Person currentUser = GlobalVariables.getUserSession().getPerson();
68          TemCardApplicationForm applicationForm = (TemCardApplicationForm)form;
69  
70          String command = applicationForm.getCommand();
71          if (StringUtils.equals(KewApiConstants.INITIATE_COMMAND,command)) {
72              final TemProfile profile = SpringContext.getBean(TemProfileService.class).findTemProfileByPrincipalId(currentUser.getPrincipalId());
73              if (profile == null){
74                  applicationForm.setEmptyProfile(true);
75                  forward =  mapping.findForward(ERROR_FORWARD);
76              } else {
77                  if (StringUtils.isEmpty(profile.getDefaultAccount())){
78                      applicationForm.setEmptyAccount(true);
79                      forward =  mapping.findForward(ERROR_FORWARD);
80                  }
81              }
82          }
83  
84          return forward;
85      }
86  
87  
88      /**
89       * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#approve(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
90       */
91      @Override
92      public ActionForward approve(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
93          TemCardApplicationForm applicationForm = (TemCardApplicationForm)form;
94          CardApplicationDocument document = (CardApplicationDocument) applicationForm.getDocument();
95          if (document.getDocumentHeader().getWorkflowDocument().getApplicationDocumentStatus().equals(TemWorkflowConstants.RouteNodeNames.FISCAL_OFFICER_REVIEW)){
96              if (document instanceof TemCorporateCardApplicationDocument){
97                  if (!((TemCorporateCardApplicationDocument)document).isDepartmentHeadAgreement()){
98                      GlobalVariables.getMessageMap().putError("departmentHeadAgreement", TemKeyConstants.ERROR_TEM_CARD_APP_NO_AGREEMENT, "Department Head");
99                  }
100             }
101 
102         }
103         Person currentUser = GlobalVariables.getUserSession().getPerson();
104         if (SpringContext.getBean(TravelDocumentService.class).isTravelManager(currentUser)
105                 && document.getDocumentHeader().getWorkflowDocument().getApplicationDocumentStatus().equals(TemWorkflowConstants.RouteNodeNames.APPLIED_TO_BANK)){
106             document.sendAcknowledgement();
107             document.approvedByBank();
108             document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(TemWorkflowConstants.RouteNodeNames.APPROVED_BY_BANK);
109             document.saveAppDocStatus();
110             final DocumentAttributeIndexingQueue documentAttributeIndexingQueue = KewApiServiceLocator.getDocumentAttributeIndexingQueue();
111             documentAttributeIndexingQueue.indexDocument(document.getDocumentHeader().getDocumentNumber());
112         }
113         return super.approve(mapping, form, request, response);
114     }
115 
116     /**
117      * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#disapprove(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
118      */
119     @Override
120     public ActionForward disapprove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
121         TemCardApplicationForm applicationForm = (TemCardApplicationForm)form;
122         CardApplicationDocument document = (CardApplicationDocument) applicationForm.getDocument();
123         Person currentUser = GlobalVariables.getUserSession().getPerson();
124         if (SpringContext.getBean(TravelDocumentService.class).isTravelManager(currentUser)
125                 && document.getDocumentHeader().getWorkflowDocument().getApplicationDocumentStatus().equals(TemWorkflowConstants.RouteNodeNames.APPLIED_TO_BANK)){
126             document.sendAcknowledgement();
127         }
128         return super.disapprove(mapping, form, request, response);
129     }
130 
131     /**
132      * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#route(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
133      */
134     @Override
135     public ActionForward route(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
136         TemCardApplicationForm applicationForm = (TemCardApplicationForm)form;
137         CardApplicationDocument document = (CardApplicationDocument) applicationForm.getDocument();
138 
139         if (applicationForm.getDocument().getDocumentHeader().getWorkflowDocument().isInitiated()){
140             if (!document.isUserAgreement()){
141                 GlobalVariables.getMessageMap().putError("userAgreement", TemKeyConstants.ERROR_TEM_CARD_APP_NO_AGREEMENT, "User");
142             }
143             if (StringUtils.isEmpty(document.getDocumentHeader().getDocumentDescription())){
144                 GlobalVariables.getMessageMap().putError("document.documentHeader.documentDescription", RiceKeyConstants.ERROR_REQUIRED, "Description");
145             }
146         }
147 
148         return super.route(mapping, form, request, response);
149     }
150 
151     public ActionForward applyToBank(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
152         TemCardApplicationForm applicationForm = (TemCardApplicationForm)form;
153         CardApplicationDocument document = (CardApplicationDocument) applicationForm.getDocument();
154         Person currentUser = GlobalVariables.getUserSession().getPerson();
155         if (!SpringContext.getBean(TravelDocumentService.class).isTravelManager(currentUser)){
156             throw new AuthorizationException(GlobalVariables.getUserSession().getPerson().getPrincipalName(), "Apply To Bank",this.getClass().getSimpleName());
157         }
158         document.applyToBank();
159         document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(TemWorkflowConstants.RouteNodeNames.APPLIED_TO_BANK);
160         document.saveAppDocStatus();
161         return mapping.findForward(KFSConstants.MAPPING_BASIC);
162     }
163 
164 
165     public ActionForward submit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
166         TemCardApplicationForm applicationForm = (TemCardApplicationForm)form;
167         CardApplicationDocument document = (CardApplicationDocument) applicationForm.getDocument();
168         Person currentUser = GlobalVariables.getUserSession().getPerson();
169         if (!SpringContext.getBean(TravelDocumentService.class).isTravelManager(currentUser)){
170             throw new AuthorizationException(GlobalVariables.getUserSession().getPerson().getPrincipalName(), "Submit",this.getClass().getSimpleName());
171         }
172         document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(TemWorkflowConstants.RouteNodeNames.PENDING_BANK_APPLICATION);
173         document.saveAppDocStatus();
174         return mapping.findForward(KFSConstants.MAPPING_BASIC);
175     }
176 
177 
178 
179     @Override
180     protected void createDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
181         super.createDocument(kualiDocumentFormBase);
182 
183         TemCardApplicationForm applicationForm = (TemCardApplicationForm) kualiDocumentFormBase;
184         final Person currentUser = GlobalVariables.getUserSession().getPerson();
185         TemProfile profile = SpringContext.getBean(TemProfileService.class).findTemProfileByPrincipalId(currentUser.getPrincipalId());
186 
187         CardApplicationDocument document = (CardApplicationDocument)applicationForm.getDocument();
188         document.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(TemWorkflowConstants.RouteNodeNames.APPLICATION);
189         document.setTemProfile(profile);
190         document.setTemProfileId(profile.getProfileId());
191         profile.getTravelerTypeCode();
192         applicationForm.setInitiator(true);
193     }
194 
195     public ActionForward checkExistingCard(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
196         ActionForward forward = null;
197 
198         TemCardApplicationForm applicationForm = (TemCardApplicationForm) form;
199         Person currentUser = GlobalVariables.getUserSession().getPerson();
200         TemProfile profile = SpringContext.getBean(TemProfileService.class).findTemProfileByPrincipalId(currentUser.getPrincipalId());
201 
202         if (profile == null) {
203             applicationForm.setEmptyProfile(true);
204             return mapping.findForward(ERROR_FORWARD);
205         }
206 
207         if (profile.getAccounts() != null && profile.getAccounts().size() > 0){
208             boolean hasCardType = false;
209             for (TemProfileAccount account : profile.getAccounts()){
210                 if (account.getCreditCardAgency().getTravelCardTypeCode().equals(TemConstants.TRAVEL_TYPE_CTS) && applicationForm.getDocTypeName().equals(TemConstants.TravelDocTypes.TRAVEL_CTS_CARD_DOCUMENT) ){
211                     hasCardType = true;
212                     break;
213                 }
214                 else if (account.getCreditCardAgency().getTravelCardTypeCode().equals(TemConstants.TRAVEL_TYPE_CORP) && applicationForm.getDocTypeName().equals(TemConstants.TravelDocTypes.TRAVEL_CORP_CARD_DOCUMENT)){
215                     hasCardType = true;
216                     break;
217                 }
218             }
219             if (hasCardType){
220                 ActionMessage message = new ActionMessage(TemKeyConstants.MESSAGE_CARD_EXISTS_PROMPT);
221                 ActionMessages messages = new ActionMessages();
222                 messages.add(ActionMessages.GLOBAL_MESSAGE, message);
223                 saveMessages(request, messages);
224                 forward = mapping.findForward("QUESTION_FORWARD");
225             }
226 
227         }
228         if (forward == null) {
229             String basePath = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(KFSConstants.APPLICATION_URL_KEY);
230 
231             Properties parameters = new Properties();
232             parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.DOC_HANDLER_METHOD);
233             parameters.put(KFSConstants.PARAMETER_COMMAND, KewApiConstants.INITIATE_COMMAND);
234             parameters.put(KFSConstants.DOCUMENT_TYPE_NAME, applicationForm.getDocTypeName());
235 
236             String action = applicationForm.getDocTypeName().equals(TemConstants.TravelDocTypes.TRAVEL_CTS_CARD_DOCUMENT) ? CTS_ACTION : CORP_ACTION;
237 
238             String lookupUrl = UrlFactory.parameterizeUrl(basePath +"/"+ action, parameters);
239             forward = new ActionForward(lookupUrl, true);
240 
241         }
242         return forward;
243 
244     }
245 
246     /**
247      * Returns the user to the index page.
248      *
249      * @param mapping
250      * @param form
251      * @param request
252      * @param response
253      * @throws Exception
254      */
255     public ActionForward returnToIndex(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
256         return mapping.findForward(KFSConstants.MAPPING_PORTAL);
257     }
258 
259     public ActionForward openNew(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
260         TemCardApplicationForm applicationForm = (TemCardApplicationForm) form;
261         String basePath = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(KFSConstants.APPLICATION_URL_KEY);
262 
263         Properties parameters = new Properties();
264         parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.DOC_HANDLER_METHOD);
265         parameters.put(KFSConstants.PARAMETER_COMMAND, KewApiConstants.INITIATE_COMMAND);
266         parameters.put(KFSConstants.DOCUMENT_TYPE_NAME, applicationForm.getDocTypeName());
267 
268         String action = applicationForm.getDocTypeName().equals(TemConstants.TravelDocTypes.TRAVEL_CTS_CARD_DOCUMENT) ? CTS_ACTION : CORP_ACTION;
269 
270         String lookupUrl = UrlFactory.parameterizeUrl(basePath +"/"+ action, parameters);
271 
272         return new ActionForward(lookupUrl, true);
273     }
274 
275 }