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