View Javadoc
1   /*
2    * Copyright 2013 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.select.document.validation.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.module.purap.document.RequisitionDocument;
20  import org.kuali.ole.module.purap.document.validation.impl.RequisitionDocumentPreRules;
21  import org.kuali.ole.select.businessobject.OleSufficientFundCheck;
22  import org.kuali.ole.select.document.OleRequisitionDocument;
23  import org.kuali.ole.select.document.service.OleRequisitionDocumentService;
24  import org.kuali.ole.sys.OLEConstants;
25  import org.kuali.ole.sys.OLEPropertyConstants;
26  import org.kuali.ole.sys.businessobject.SourceAccountingLine;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.rice.core.api.config.property.ConfigurationService;
29  import org.kuali.rice.krad.document.Document;
30  import org.kuali.rice.krad.service.BusinessObjectService;
31  
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  public class OleRequisitionDocumentPreRules extends RequisitionDocumentPreRules {
37      /**
38       * Default Constructor
39       */
40      public OleRequisitionDocumentPreRules() {
41          super();
42      }
43  
44      /**
45       * Main hook point to perform rules check.
46       *
47       * @see org.kuali.rice.kns.rules.PromptBeforeValidationBase#doRules(org.kuali.rice.krad.document.Document)
48       */
49      @Override
50      public boolean doPrompts(Document document) {
51          boolean preRulesOK = true;
52          OleRequisitionDocument reqDoc = null;
53          if (document instanceof RequisitionDocument) {
54              reqDoc = (OleRequisitionDocument) document;
55          }
56          OleRequisitionDocumentService oleRequisitionDocumentService = (OleRequisitionDocumentService) SpringContext
57                  .getBean("oleRequisitionDocumentService");
58          StringBuilder accountNumbers = new StringBuilder();
59          if (oleRequisitionDocumentService != null && reqDoc != null) {
60              List<SourceAccountingLine> sourceAccountingLineList = reqDoc.getSourceAccountingLines();
61              for (SourceAccountingLine accLine : sourceAccountingLineList) {
62                  String notificationOption = null;
63                  boolean sufficientFundCheck;
64                  Map<String, Object> key = new HashMap<String, Object>();
65                  String chartCode = accLine.getChartOfAccountsCode();
66                  String accNo = accLine.getAccountNumber();
67                  String objectCd = accLine.getFinancialObjectCode();
68                  key.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
69                  key.put(OLEPropertyConstants.ACCOUNT_NUMBER, accNo);
70                  OleSufficientFundCheck account = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(
71                          OleSufficientFundCheck.class, key);
72                  if (account != null) {
73                      notificationOption = account.getNotificationOption();
74                  }
75                  if (notificationOption != null && notificationOption.equals(OLEPropertyConstants.WARNING_MSG)
76                          && oleRequisitionDocumentService.hasSufficientFundsOnRequisition(accLine)) {
77                      accountNumbers.append(accLine.getAccountNumber());
78                  }
79              }
80          }
81          if (accountNumbers.length() > 0) {
82              preRulesOK = askForConfirmation(OLEConstants.SufficientFundCheck.REQUISITION_SFC_CHECKING_STRING,
83                      OLEConstants.SufficientFundCheck.REQUISITION_SFC_CHECKING, accountNumbers);
84          }
85          preRulesOK &= super.doPrompts(document);
86          return preRulesOK;
87      }
88  
89      /**
90       * Prompts user to confirm with a Yes or No to a question being asked.
91       *
92       * @param questionType    - type of question
93       * @param messageConstant - key to retrieve message
94       * @return - true if overriding, false otherwise
95       */
96      protected boolean askForConfirmation(String questionType, String messageConstant, StringBuilder accountNumbers) {
97  
98          String questionText = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(
99                  messageConstant);
100         if (questionText.contains("{")) {
101             questionText = prepareQuestionText(questionType, questionText, accountNumbers);
102         }
103 
104         boolean confirmOverride = super.askOrAnalyzeYesNoQuestion(questionType, questionText);
105 
106         if (!confirmOverride) {
107             event.setActionForwardName(OLEConstants.MAPPING_BASIC);
108             return false;
109         }
110         return true;
111     }
112 
113     /**
114      * Creates the actual text of the question, replacing place holders like pay date threshold with an actual constant value.
115      *
116      * @param questionType - type of question
117      * @param questionText - actual text of question pulled from resource file
118      * @return - question text with place holders replaced
119      */
120     protected String prepareQuestionText(String questionType, String questionText, StringBuilder accountNumbers) {
121         if (StringUtils.equals(questionType, OLEConstants.SufficientFundCheck.REQUISITION_SFC_CHECKING_STRING)) {
122             questionText = StringUtils.replace(questionText, "{0}", accountNumbers.toString());
123         }
124         return questionText;
125     }
126 
127 }