View Javadoc
1   /*
2    * MoneyWise: Finance Application
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.moneywise.data.validate;
18  
19  import io.github.tonywasher.joceanus.metis.field.MetisFieldRequired;
20  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDepositInfoSet;
21  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseLoan;
22  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseLoanCategory;
23  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseLoanInfo;
24  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseLoanInfoSet;
25  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAccountInfoClass;
26  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseCurrency;
27  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
28  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusMoney;
29  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataInfoClass;
30  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
31  import io.github.tonywasher.joceanus.prometheus.validate.PrometheusValidateInfoSet;
32  
33  import java.util.Currency;
34  
35  /**
36   * Validate LoanInfoSet.
37   */
38  public class MoneyWiseValidateLoanInfoSet
39          extends PrometheusValidateInfoSet<MoneyWiseLoanInfo> {
40      @Override
41      public MoneyWiseLoan getOwner() {
42          return (MoneyWiseLoan) super.getOwner();
43      }
44  
45      @Override
46      public MoneyWiseLoanInfoSet getInfoSet() {
47          return (MoneyWiseLoanInfoSet) super.getInfoSet();
48      }
49  
50      @Override
51      public MetisFieldRequired isClassRequired(final PrometheusDataInfoClass pClass) {
52          /* Access details about the Loan */
53          final MoneyWiseLoan myLoan = getOwner();
54          final MoneyWiseLoanCategory myCategory = myLoan.getCategory();
55  
56          /* If we have no Category, no class is allowed */
57          if (myCategory == null) {
58              return MetisFieldRequired.NOTALLOWED;
59          }
60  
61          /* Switch on class */
62          return switch ((MoneyWiseAccountInfoClass) pClass) {
63              /* Allowed set */
64              case NOTES, SORTCODE, ACCOUNT, REFERENCE, OPENINGBALANCE -> MetisFieldRequired.CANEXIST;
65  
66              /* Not allowed */
67              default -> MetisFieldRequired.NOTALLOWED;
68          };
69      }
70  
71      @Override
72      public void validateClass(final MoneyWiseLoanInfo pInfo,
73                                final PrometheusDataInfoClass pClass) {
74          /* Switch on class */
75          switch ((MoneyWiseAccountInfoClass) pClass) {
76              case OPENINGBALANCE:
77                  validateOpeningBalance(pInfo);
78                  break;
79              case SORTCODE, ACCOUNT, NOTES, REFERENCE:
80                  validateInfoLength(pInfo);
81                  break;
82              default:
83                  break;
84          }
85      }
86  
87      /**
88       * Validate the opening balance.
89       *
90       * @param pInfo the info
91       */
92      private void validateOpeningBalance(final MoneyWiseLoanInfo pInfo) {
93          final OceanusMoney myBalance = pInfo.getValue(OceanusMoney.class);
94          if (!myBalance.getCurrency().equals(getOwner().getCurrency())) {
95              getOwner().addError(MoneyWiseDepositInfoSet.ERROR_CURRENCY, MoneyWiseLoanInfoSet.getFieldForClass(MoneyWiseAccountInfoClass.OPENINGBALANCE));
96          }
97      }
98  
99      /**
100      * Validate the info length.
101      *
102      * @param pInfo the info
103      */
104     private void validateInfoLength(final MoneyWiseLoanInfo pInfo) {
105         final char[] myArray = pInfo.getValue(char[].class);
106         final MoneyWiseAccountInfoClass myClass = pInfo.getInfoClass();
107         if (myArray.length > myClass.getMaximumLength()) {
108             getOwner().addError(PrometheusDataItem.ERROR_LENGTH, MoneyWiseLoanInfoSet.getFieldForClass(myClass));
109         }
110     }
111 
112     @Override
113     protected void autoCorrect(final PrometheusDataInfoClass pClass) throws OceanusException {
114         /* If the info is Opening balance */
115         if (MoneyWiseAccountInfoClass.OPENINGBALANCE.equals(pClass)) {
116             /* Access the value */
117             final MoneyWiseLoan myOwner = getOwner();
118             OceanusMoney myOpening = myOwner.getOpeningBalance();
119             final MoneyWiseCurrency myAssetCurrency = myOwner.getAssetCurrency();
120             final Currency myCurrency = myAssetCurrency.getCurrency();
121 
122             /* If we need to change currency */
123             if (!myCurrency.equals(myOpening.getCurrency())) {
124                 myOpening = myOpening.changeCurrency(myCurrency);
125                 getInfoSet().setValue(pClass, myOpening);
126             }
127         }
128     }
129 }