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.MoneyWisePortfolio;
21  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWisePortfolioInfo;
22  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWisePortfolioInfoSet;
23  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAccountInfoClass;
24  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataInfoClass;
25  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
26  import io.github.tonywasher.joceanus.prometheus.validate.PrometheusValidateInfoSet;
27  
28  /**
29   * Validate PortfolioInfoSet.
30   */
31  public class MoneyWiseValidatePortfolioInfoSet
32          extends PrometheusValidateInfoSet<MoneyWisePortfolioInfo> {
33      @Override
34      public MoneyWisePortfolio getOwner() {
35          return (MoneyWisePortfolio) super.getOwner();
36      }
37  
38      @Override
39      public MoneyWisePortfolioInfoSet getInfoSet() {
40          return (MoneyWisePortfolioInfoSet) super.getInfoSet();
41      }
42  
43      @Override
44      public MetisFieldRequired isClassRequired(final PrometheusDataInfoClass pClass) {
45          /* Switch on class */
46          return switch ((MoneyWiseAccountInfoClass) pClass) {
47              /* Allowed set */
48              case NOTES, SORTCODE, ACCOUNT, REFERENCE, WEBSITE, CUSTOMERNO, USERID, PASSWORD ->
49                      MetisFieldRequired.CANEXIST;
50  
51              /* Not Allowed */
52              default -> MetisFieldRequired.NOTALLOWED;
53          };
54      }
55  
56      @Override
57      public void validateClass(final MoneyWisePortfolioInfo pInfo,
58                                final PrometheusDataInfoClass pClass) {
59          /* Switch on class */
60          switch ((MoneyWiseAccountInfoClass) pClass) {
61              case WEBSITE, CUSTOMERNO, USERID, PASSWORD, SORTCODE, ACCOUNT, NOTES, REFERENCE:
62                  validateInfoLength(pInfo);
63                  break;
64              default:
65                  break;
66          }
67      }
68  
69      /**
70       * Validate the info length.
71       *
72       * @param pInfo the info
73       */
74      private void validateInfoLength(final MoneyWisePortfolioInfo pInfo) {
75          final char[] myArray = pInfo.getValue(char[].class);
76          final MoneyWiseAccountInfoClass myClass = pInfo.getInfoClass();
77          if (myArray.length > myClass.getMaximumLength()) {
78              getOwner().addError(PrometheusDataItem.ERROR_LENGTH, MoneyWisePortfolioInfoSet.getFieldForClass(myClass));
79          }
80      }
81  }