View Javadoc
1   /*
2    * Copyright 2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.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/ecl2.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.fp.document.validation.impl;
17  
18  import org.kuali.ole.fp.businessobject.CashDrawer;
19  import org.kuali.ole.fp.service.CashDrawerService;
20  import org.kuali.ole.sys.OLEKeyConstants;
21  import org.kuali.ole.sys.context.SpringContext;
22  import org.kuali.rice.core.api.util.type.KualiDecimal;
23  import org.kuali.rice.kns.document.MaintenanceDocument;
24  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
25  
26  /**
27   * Validations for the Cash Drawer Maintenance Document
28   */
29  public class CashDrawerMaintenanceDocumentRule extends MaintenanceDocumentRuleBase {
30  
31      /**
32       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
33       */
34      @Override
35      protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
36          boolean documentValid = super.processCustomRouteDocumentBusinessRules(document);
37          final CashDrawer cashDrawer = (CashDrawer)document.getNewMaintainableObject().getBusinessObject();
38          documentValid &= checkCashDrawerStillClosed(cashDrawer);
39          documentValid &= checkCurrencyAmountsPositive(cashDrawer);
40          documentValid &= checkCoinAmountsPositive(cashDrawer);
41          return documentValid;
42      }
43  
44      /**
45       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
46       */
47      @Override
48      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
49          boolean documentValid = super.processCustomSaveDocumentBusinessRules(document);
50          final CashDrawer cashDrawer = (CashDrawer)document.getNewMaintainableObject().getBusinessObject();
51          documentValid &= checkCashDrawerStillClosed(cashDrawer);
52          if (documentValid) {
53              checkCurrencyAmountsPositive(cashDrawer);
54              checkCoinAmountsPositive(cashDrawer);
55          }
56          return documentValid;
57      }
58  
59      /**
60       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
61       */
62      @Override
63      protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
64          boolean documentValid = super.processCustomApproveDocumentBusinessRules(document);
65          final CashDrawer cashDrawer = (CashDrawer)document.getNewMaintainableObject().getBusinessObject();
66          checkCurrencyAmountsPositive(cashDrawer);
67          checkCoinAmountsPositive(cashDrawer);
68          return documentValid;
69      }
70  
71      /**
72       * Validates that all the currency counts are positive
73       * @param cashDrawer the cash drawer to check
74       * @return true if the cash drawer has valid positive currency amounts, false otherwise
75       */
76      protected boolean checkCurrencyAmountsPositive(CashDrawer cashDrawer) {
77          boolean valid = true;
78          if (cashDrawer.getFinancialDocumentHundredDollarAmount() != null && cashDrawer.getFinancialDocumentHundredDollarAmount().compareTo(KualiDecimal.ZERO) < 0) {
79              putFieldError("hundredDollarCount", OLEKeyConstants.CashDrawerMaintenance.HUNDRED_DOLLAR_AMOUNT_NEGATIVE, new String[] { cashDrawer.getHundredDollarCount().toString() });
80              valid = false;
81          }
82          if (cashDrawer.getFinancialDocumentFiftyDollarAmount() != null && cashDrawer.getFinancialDocumentFiftyDollarAmount().compareTo(KualiDecimal.ZERO) < 0) {
83              putFieldError("fiftyDollarCount", OLEKeyConstants.CashDrawerMaintenance.FIFTY_DOLLAR_AMOUNT_NEGATIVE, new String[] { cashDrawer.getFiftyDollarCount().toString() });
84              valid = false;
85          }
86          if (cashDrawer.getFinancialDocumentTwentyDollarAmount() != null && cashDrawer.getFinancialDocumentTwentyDollarAmount().compareTo(KualiDecimal.ZERO) < 0) {
87              putFieldError("twentyDollarCount", OLEKeyConstants.CashDrawerMaintenance.TWENTY_DOLLAR_AMOUNT_NEGATIVE, new String[] { cashDrawer.getTwentyDollarCount().toString() });
88              valid = false;
89          }
90          if (cashDrawer.getFinancialDocumentTenDollarAmount() != null && cashDrawer.getFinancialDocumentTenDollarAmount().compareTo(KualiDecimal.ZERO) < 0) {
91              putFieldError("tenDollarCount", OLEKeyConstants.CashDrawerMaintenance.TEN_DOLLAR_AMOUNT_NEGATIVE, new String[] { cashDrawer.getTenDollarCount().toString() });
92              valid = false;
93          }
94          if (cashDrawer.getFinancialDocumentFiveDollarAmount() != null && cashDrawer.getFinancialDocumentFiveDollarAmount().compareTo(KualiDecimal.ZERO) < 0) {
95              putFieldError("fiveDollarCount", OLEKeyConstants.CashDrawerMaintenance.FIVE_DOLLAR_AMOUNT_NEGATIVE, new String[] { cashDrawer.getFiveDollarCount().toString() });
96              valid = false;
97          }
98          if (cashDrawer.getFinancialDocumentTwoDollarAmount() != null && cashDrawer.getFinancialDocumentTwoDollarAmount().compareTo(KualiDecimal.ZERO) < 0) {
99              putFieldError("twoDollarCount", OLEKeyConstants.CashDrawerMaintenance.TWO_DOLLAR_AMOUNT_NEGATIVE, new String[] { cashDrawer.getTwoDollarCount().toString() });
100             valid = false;
101         }
102         if (cashDrawer.getFinancialDocumentOneDollarAmount() != null && cashDrawer.getFinancialDocumentOneDollarAmount().compareTo(KualiDecimal.ZERO) < 0) {
103             putFieldError("oneDollarCount", OLEKeyConstants.CashDrawerMaintenance.ONE_DOLLAR_AMOUNT_NEGATIVE, new String[] { cashDrawer.getOneDollarCount().toString() });
104             valid = false;
105         }
106         if (cashDrawer.getFinancialDocumentOtherDollarAmount() != null && cashDrawer.getFinancialDocumentOtherDollarAmount().compareTo(KualiDecimal.ZERO) < 0) {
107             putFieldError("financialDocumentOtherDollarAmount", OLEKeyConstants.CashDrawerMaintenance.OTHER_DOLLAR_AMOUNT_NEGATIVE, new String[] { cashDrawer.getFinancialDocumentOtherDollarAmount().toString() });
108             valid = false;
109         }
110         return valid;
111     }
112     
113     /**
114      * Validates that all the coin counts are positive
115      * @param cashDrawer the cash drawer to check
116      * @return true if the cash drawer has valid positive coin amounts, false otherwise
117      */
118     protected boolean checkCoinAmountsPositive(CashDrawer cashDrawer) {
119         boolean valid = true;
120         if (cashDrawer.getFinancialDocumentHundredCentAmount() != null && cashDrawer.getFinancialDocumentHundredCentAmount().compareTo(KualiDecimal.ZERO) < 0) {
121             putFieldError("hundredCentCount", OLEKeyConstants.CashDrawerMaintenance.HUNDRED_CENT_AMOUNT_NEGATIVE, new String[] { cashDrawer.getHundredCentCount().toString() });
122             valid = false;
123         }
124         if (cashDrawer.getFinancialDocumentFiftyCentAmount() != null && cashDrawer.getFinancialDocumentFiftyCentAmount().compareTo(KualiDecimal.ZERO) < 0) {
125             putFieldError("fiftyCentCount", OLEKeyConstants.CashDrawerMaintenance.FIFTY_CENT_AMOUNT_NEGATIVE, new String[] { cashDrawer.getFiftyCentCount().toString() });
126             valid = false;
127         }
128         if (cashDrawer.getFinancialDocumentTwentyFiveCentAmount() != null && cashDrawer.getFinancialDocumentTwentyFiveCentAmount().compareTo(KualiDecimal.ZERO) < 0) {
129             putFieldError("twentyFiveCentCount", OLEKeyConstants.CashDrawerMaintenance.TWENTY_FIVE_CENT_AMOUNT_NEGATIVE, new String[] { cashDrawer.getTwentyFiveCentCount().toString() });
130             valid = false;
131         }
132         if (cashDrawer.getFinancialDocumentTenCentAmount() != null && cashDrawer.getFinancialDocumentTenCentAmount().compareTo(KualiDecimal.ZERO) < 0) {
133             putFieldError("tenCentCount", OLEKeyConstants.CashDrawerMaintenance.TEN_CENT_AMOUNT_NEGATIVE, new String[] { cashDrawer.getTenCentCount().toString() });
134             valid = false;
135         }
136         if (cashDrawer.getFinancialDocumentFiveCentAmount() != null && cashDrawer.getFinancialDocumentFiveCentAmount().compareTo(KualiDecimal.ZERO) < 0) {
137             putFieldError("fiveCentCount", OLEKeyConstants.CashDrawerMaintenance.FIVE_CENT_AMOUNT_NEGATIVE, new String[] { cashDrawer.getFiveCentCount().toString() });
138             valid = false;
139         }
140         if (cashDrawer.getFinancialDocumentOneCentAmount() != null && cashDrawer.getFinancialDocumentOneCentAmount().compareTo(KualiDecimal.ZERO) < 0) {
141             putFieldError("oneCentCount", OLEKeyConstants.CashDrawerMaintenance.ONE_CENT_AMOUNT_NEGATIVE, new String[] { cashDrawer.getOneCentCount().toString() });
142             valid = false;
143         }
144         if (cashDrawer.getFinancialDocumentOtherCentAmount() != null && cashDrawer.getFinancialDocumentOtherCentAmount().compareTo(KualiDecimal.ZERO) < 0) {
145             putFieldError("financialDocumentOtherCentAmount", OLEKeyConstants.CashDrawerMaintenance.OTHER_CENT_AMOUNT_NEGATIVE, new String[] { cashDrawer.getFinancialDocumentOtherCentAmount().toString() });
146             valid = false;
147         }
148         return valid;
149     }
150     
151     /**
152      * Checks that the cash drawer is still closed at the time of the rule invocation.
153      * @param cashDrawer the cash drawer to check
154      */
155     public boolean checkCashDrawerStillClosed(CashDrawer cashDrawer) {
156        boolean valid = true;
157        final CashDrawerService cashDrawerService = SpringContext.getBean(CashDrawerService.class);
158        final CashDrawer cashDrawerFromDB = cashDrawerService.getByCampusCode(cashDrawer.getCampusCode());
159        if (cashDrawerFromDB != null && !cashDrawerFromDB.isClosed()) {
160            putFieldError("campusCode", OLEKeyConstants.CashDrawerMaintenance.CASH_DRAWER_NOT_CLOSED, new String[] { cashDrawer.getCampusCode() });
161            valid = false;
162        }
163        return valid;
164     }
165 }