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.moneywise.data.basic.MoneyWiseBasicResource;
20  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDataValidator.MoneyWiseDataValidatorDefaults;
21  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseRegion;
22  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseRegion.MoneyWiseRegionDataMap;
23  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseRegion.MoneyWiseRegionList;
24  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
25  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataInfoLinkSet;
26  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
27  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataResource;
28  import io.github.tonywasher.joceanus.prometheus.views.PrometheusEditSet;
29  
30  /**
31   * Validator for region.
32   */
33  public class MoneyWiseValidateRegion
34          implements MoneyWiseDataValidatorDefaults<MoneyWiseRegion> {
35      /**
36       * New Region name.
37       */
38      private static final String NAME_NEWREGION = MoneyWiseBasicResource.REGION_NEWREGION.getValue();
39  
40      @Override
41      public void setEditSet(final PrometheusEditSet pEditSet) {
42          /* NoOp */
43      }
44  
45      @Override
46      public void validate(final PrometheusDataItem pRegion) {
47          final MoneyWiseRegion myRegion = (MoneyWiseRegion) pRegion;
48          final MoneyWiseRegionList myList = myRegion.getList();
49          final String myName = myRegion.getName();
50          final String myDesc = myRegion.getDesc();
51          final MoneyWiseRegionDataMap myMap = myList.getDataMap();
52  
53          /* Name must be non-null */
54          if (myName == null) {
55              pRegion.addError(PrometheusDataItem.ERROR_MISSING, PrometheusDataResource.DATAITEM_FIELD_NAME);
56  
57              /* Else check the name */
58          } else {
59              /* The description must not be too long */
60              if (myName.length() > PrometheusDataItem.NAMELEN) {
61                  pRegion.addError(PrometheusDataItem.ERROR_LENGTH, PrometheusDataResource.DATAITEM_FIELD_NAME);
62              }
63  
64              /* Check that the name is unique */
65              if (!myMap.validNameCount(myName)) {
66                  pRegion.addError(PrometheusDataItem.ERROR_DUPLICATE, PrometheusDataResource.DATAITEM_FIELD_NAME);
67              }
68  
69              /* Check that the name does not contain invalid characters */
70              if (myName.contains(PrometheusDataInfoLinkSet.ITEM_SEP)) {
71                  pRegion.addError(PrometheusDataItem.ERROR_INVALIDCHAR, PrometheusDataResource.DATAITEM_FIELD_NAME);
72              }
73          }
74  
75          /* Check description length */
76          if (myDesc != null
77                  && myDesc.length() > PrometheusDataItem.DESCLEN) {
78              pRegion.addError(PrometheusDataItem.ERROR_LENGTH, PrometheusDataResource.DATAITEM_FIELD_DESC);
79          }
80  
81          /* Set validation flag */
82          if (!pRegion.hasErrors()) {
83              pRegion.setValidEdit();
84          }
85      }
86  
87      @Override
88      public void setDefaults(final MoneyWiseRegion pRegion) throws OceanusException {
89          /* Set values */
90          final MoneyWiseRegionList myList = pRegion.getList();
91          pRegion.setName(getUniqueName(myList));
92      }
93  
94      /**
95       * Obtain unique name for new region.
96       *
97       * @param pList the region list
98       * @return The new name
99       */
100     private String getUniqueName(final MoneyWiseRegionList pList) {
101         /* Set up base constraints */
102         final String myBase = NAME_NEWREGION;
103         int iNextId = 1;
104 
105         /* Loop until we found a name */
106         String myName = myBase;
107         while (true) {
108             /* try out the name */
109             if (pList.findItemByName(myName) == null) {
110                 return myName;
111             }
112 
113             /* Build next name */
114             myName = myBase.concat(Integer.toString(iNextId++));
115         }
116     }
117 }