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