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.MoneyWiseDeposit;
21  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDepositCategory;
22  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDepositInfo;
23  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDepositInfoSet;
24  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAccountInfoClass;
25  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseCurrency;
26  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseDepositCategoryClass;
27  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
28  import io.github.tonywasher.joceanus.oceanus.date.OceanusDate;
29  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusMoney;
30  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataInfoClass;
31  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
32  import io.github.tonywasher.joceanus.prometheus.validate.PrometheusValidateInfoSet;
33  
34  import java.util.Currency;
35  
36  /**
37   * Validate DepositInfoSet.
38   */
39  public class MoneyWiseValidateDepositInfoSet
40          extends PrometheusValidateInfoSet<MoneyWiseDepositInfo> {
41      @Override
42      public MoneyWiseDeposit getOwner() {
43          return (MoneyWiseDeposit) super.getOwner();
44      }
45  
46      @Override
47      public MoneyWiseDepositInfoSet getInfoSet() {
48          return (MoneyWiseDepositInfoSet) super.getInfoSet();
49      }
50  
51      @Override
52      public MetisFieldRequired isClassRequired(final PrometheusDataInfoClass pClass) {
53          /* Access details about the Deposit */
54          final MoneyWiseDeposit myDeposit = getOwner();
55          final MoneyWiseDepositCategory myCategory = myDeposit.getCategory();
56  
57          /* If we have no Category, no class is allowed */
58          if (myCategory == null) {
59              return MetisFieldRequired.NOTALLOWED;
60          }
61          final MoneyWiseDepositCategoryClass myClass = myCategory.getCategoryTypeClass();
62  
63          /* Switch on class */
64          return switch ((MoneyWiseAccountInfoClass) pClass) {
65              /* Allowed set */
66              case NOTES, SORTCODE, ACCOUNT, REFERENCE, OPENINGBALANCE -> MetisFieldRequired.CANEXIST;
67  
68              /* Handle Maturity */
69              case MATURITY -> myClass.hasMaturity()
70                      ? MetisFieldRequired.MUSTEXIST
71                      : MetisFieldRequired.NOTALLOWED;
72  
73              /* Not allowed */
74              default -> MetisFieldRequired.NOTALLOWED;
75          };
76      }
77  
78      @Override
79      public void validateClass(final MoneyWiseDepositInfo pInfo,
80                                final PrometheusDataInfoClass pClass) {
81          /* Switch on class */
82          switch ((MoneyWiseAccountInfoClass) pClass) {
83              case OPENINGBALANCE:
84                  validateOpeningBalance(pInfo);
85                  break;
86              case SORTCODE, ACCOUNT, NOTES, REFERENCE:
87                  validateInfoLength(pInfo);
88                  break;
89              default:
90                  break;
91          }
92      }
93  
94      /**
95       * Validate the opening balance.
96       *
97       * @param pInfo the info
98       */
99      private void validateOpeningBalance(final MoneyWiseDepositInfo pInfo) {
100         final OceanusMoney myBalance = pInfo.getValue(OceanusMoney.class);
101         if (!myBalance.getCurrency().equals(getOwner().getCurrency())) {
102             getOwner().addError(MoneyWiseDepositInfoSet.ERROR_CURRENCY, MoneyWiseDepositInfoSet.getFieldForClass(MoneyWiseAccountInfoClass.OPENINGBALANCE));
103         }
104     }
105 
106     /**
107      * Validate the info length.
108      *
109      * @param pInfo the info
110      */
111     private void validateInfoLength(final MoneyWiseDepositInfo pInfo) {
112         final char[] myArray = pInfo.getValue(char[].class);
113         final MoneyWiseAccountInfoClass myClass = pInfo.getInfoClass();
114         if (myArray.length > myClass.getMaximumLength()) {
115             getOwner().addError(PrometheusDataItem.ERROR_LENGTH, MoneyWiseDepositInfoSet.getFieldForClass(myClass));
116         }
117     }
118 
119     @Override
120     protected void setDefault(final PrometheusDataInfoClass pClass) throws OceanusException {
121         /* If this is maturity */
122         if (MoneyWiseAccountInfoClass.MATURITY.equals(pClass)) {
123             final OceanusDate myDate = new OceanusDate();
124             myDate.adjustYear(1);
125             getOwner().setMaturity(myDate);
126         }
127     }
128 
129     @Override
130     protected void autoCorrect(final PrometheusDataInfoClass pClass) throws OceanusException {
131         /* If the info is Opening balance */
132         if (MoneyWiseAccountInfoClass.OPENINGBALANCE.equals(pClass)) {
133             /* Access the value */
134             final MoneyWiseDeposit myOwner = getOwner();
135             OceanusMoney myOpening = myOwner.getOpeningBalance();
136             final MoneyWiseCurrency myAssetCurrency = myOwner.getAssetCurrency();
137             final Currency myCurrency = myAssetCurrency.getCurrency();
138 
139             /* If we need to change currency */
140             if (!myCurrency.equals(myOpening.getCurrency())) {
141                 myOpening = myOpening.changeCurrency(myCurrency);
142                 getInfoSet().setValue(pClass, myOpening);
143             }
144         }
145     }
146 }