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