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