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.oceanus.base.OceanusException;
20  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusMoney;
21  import io.github.tonywasher.joceanus.metis.field.MetisFieldRequired;
22  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDepositInfoSet;
23  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseLoan;
24  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseLoanCategory;
25  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseLoanInfo;
26  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseLoanInfoSet;
27  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAccountInfoClass;
28  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseCurrency;
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          switch ((MoneyWiseAccountInfoClass) pClass) {
63              /* Allowed set */
64              case NOTES:
65              case SORTCODE:
66              case ACCOUNT:
67              case REFERENCE:
68              case OPENINGBALANCE:
69                  return MetisFieldRequired.CANEXIST;
70  
71              /* Not allowed */
72              case WEBSITE:
73              case CUSTOMERNO:
74              case USERID:
75              case PASSWORD:
76              case MATURITY:
77              case AUTOEXPENSE:
78              case AUTOPAYEE:
79              case SYMBOL:
80              case REGION:
81              case UNDERLYINGSTOCK:
82              case OPTIONPRICE:
83              default:
84                  return MetisFieldRequired.NOTALLOWED;
85          }
86      }
87  
88      @Override
89      public void validateClass(final MoneyWiseLoanInfo pInfo,
90                                final PrometheusDataInfoClass pClass) {
91          /* Switch on class */
92          switch ((MoneyWiseAccountInfoClass) pClass) {
93              case OPENINGBALANCE:
94                  validateOpeningBalance(pInfo);
95                  break;
96              case SORTCODE:
97              case ACCOUNT:
98              case NOTES:
99              case REFERENCE:
100                 validateInfoLength(pInfo);
101                 break;
102             default:
103                 break;
104         }
105     }
106 
107     /**
108      * Validate the opening balance.
109      *
110      * @param pInfo the info
111      */
112     private void validateOpeningBalance(final MoneyWiseLoanInfo pInfo) {
113         final OceanusMoney myBalance = pInfo.getValue(OceanusMoney.class);
114         if (!myBalance.getCurrency().equals(getOwner().getCurrency())) {
115             getOwner().addError(MoneyWiseDepositInfoSet.ERROR_CURRENCY, MoneyWiseLoanInfoSet.getFieldForClass(MoneyWiseAccountInfoClass.OPENINGBALANCE));
116         }
117     }
118 
119     /**
120      * Validate the info length.
121      *
122      * @param pInfo the info
123      */
124     private void validateInfoLength(final MoneyWiseLoanInfo pInfo) {
125         final char[] myArray = pInfo.getValue(char[].class);
126         final MoneyWiseAccountInfoClass myClass = pInfo.getInfoClass();
127         if (myArray.length > myClass.getMaximumLength()) {
128             getOwner().addError(PrometheusDataItem.ERROR_LENGTH, MoneyWiseLoanInfoSet.getFieldForClass(myClass));
129         }
130     }
131 
132     @Override
133     protected void autoCorrect(final PrometheusDataInfoClass pClass) throws OceanusException {
134         /* If the info is Opening balance */
135         if (MoneyWiseAccountInfoClass.OPENINGBALANCE.equals(pClass)) {
136             /* Access the value */
137             final MoneyWiseLoan myOwner = getOwner();
138             OceanusMoney myOpening = myOwner.getOpeningBalance();
139             final MoneyWiseCurrency myAssetCurrency = myOwner.getAssetCurrency();
140             final Currency myCurrency = myAssetCurrency.getCurrency();
141 
142             /* If we need to change currency */
143             if (!myCurrency.equals(myOpening.getCurrency())) {
144                 myOpening = myOpening.changeCurrency(myCurrency);
145                 getInfoSet().setValue(pClass, myOpening);
146             }
147         }
148     }
149 }