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.validation.impl.PurchaseOrderDocumentPreRules;
20  import org.kuali.ole.select.businessobject.OleSufficientFundCheck;
21  import org.kuali.ole.select.document.OlePurchaseOrderAmendmentDocument;
22  import org.kuali.ole.select.document.service.OleRequisitionDocumentService;
23  import org.kuali.ole.sys.OLEConstants;
24  import org.kuali.ole.sys.OLEPropertyConstants;
25  import org.kuali.ole.sys.businessobject.SourceAccountingLine;
26  import org.kuali.ole.sys.context.SpringContext;
27  import org.kuali.rice.core.api.config.property.ConfigurationService;
28  import org.kuali.rice.krad.document.Document;
29  import org.kuali.rice.krad.service.BusinessObjectService;
30  
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  
35  public class OlePurchaseOrderDocumentPreRules extends PurchaseOrderDocumentPreRules {
36      /**
37       * Default Constructor
38       */
39      public OlePurchaseOrderDocumentPreRules() {
40          super();
41      }
42  
43      /**
44       * Main hook point to perform rules check.
45       *
46       * @see org.kuali.rice.kns.rules.PromptBeforeValidationBase#doRules(org.kuali.rice.krad.document.Document)
47       */
48      @Override
49      public boolean doPrompts(Document document) {
50          boolean preRulesOK = true;
51          OlePurchaseOrderAmendmentDocument purDoc = null;
52          if (document instanceof OlePurchaseOrderAmendmentDocument) {
53              purDoc = (OlePurchaseOrderAmendmentDocument) document;
54          }
55          OleRequisitionDocumentService oleRequisitionDocumentService = (OleRequisitionDocumentService) SpringContext
56                  .getBean("oleRequisitionDocumentService");
57          StringBuilder accountNumbers = new StringBuilder();
58          if (oleRequisitionDocumentService != null && purDoc != null) {
59              List<SourceAccountingLine> sourceAccountingLineList = purDoc.getSourceAccountingLines();
60              for (SourceAccountingLine accLine : sourceAccountingLineList) {
61                  String notificationOption = null;
62                  boolean sufficientFundCheck;
63                  Map<String, Object> key = new HashMap<String, Object>();
64                  String chartCode = accLine.getChartOfAccountsCode();
65                  String accNo = accLine.getAccountNumber();
66                  String objectCd = accLine.getFinancialObjectCode();
67                  key.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
68                  key.put(OLEPropertyConstants.ACCOUNT_NUMBER, accNo);
69                  OleSufficientFundCheck account = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(
70                          OleSufficientFundCheck.class, key);
71                  if (account != null) {
72                      notificationOption = account.getNotificationOption();
73                  }
74                  if (notificationOption != null && notificationOption.equals(OLEPropertyConstants.WARNING_MSG)
75                          && oleRequisitionDocumentService.hasSufficientFundsOnRequisition(accLine)) {
76                      accountNumbers.append(accLine.getAccountNumber());
77                  }
78              }
79          }
80          if (accountNumbers.length() > 0) {
81              preRulesOK = askForConfirmation(OLEConstants.SufficientFundCheck.REQUISITION_SFC_CHECKING_STRING,
82                      OLEConstants.SufficientFundCheck.REQUISITION_SFC_CHECKING, accountNumbers);
83          }
84          preRulesOK &= super.doPrompts(document);
85          return preRulesOK;
86      }
87  
88      /**
89       * Prompts user to confirm with a Yes or No to a question being asked.
90       *
91       * @param questionType    - type of question
92       * @param messageConstant - key to retrieve message
93       * @return - true if overriding, false otherwise
94       */
95      protected boolean askForConfirmation(String questionType, String messageConstant, StringBuilder accountNumbers) {
96  
97          String questionText = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(
98                  messageConstant);
99          if (questionText.contains("{")) {
100             questionText = prepareQuestionText(questionType, questionText, accountNumbers);
101         }
102 
103         boolean confirmOverride = super.askOrAnalyzeYesNoQuestion(questionType, questionText);
104 
105         if (!confirmOverride) {
106             event.setActionForwardName(OLEConstants.MAPPING_BASIC);
107             return false;
108         }
109         return true;
110     }
111 
112     /**
113      * Creates the actual text of the question, replacing place holders like pay date threshold with an actual constant value.
114      *
115      * @param questionType - type of question
116      * @param questionText - actual text of question pulled from resource file
117      * @return - question text with place holders replaced
118      */
119     protected String prepareQuestionText(String questionType, String questionText, StringBuilder accountNumbers) {
120         if (StringUtils.equals(questionType, OLEConstants.SufficientFundCheck.REQUISITION_SFC_CHECKING_STRING)) {
121             questionText = StringUtils.replace(questionText, "{0}", accountNumbers.toString());
122         }
123         return questionText;
124     }
125 
126 }