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.moneywise.data.basic.MoneyWiseCashCategory;
21  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseCashCategory.MoneyWiseCashCategoryList;
22  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseCategoryBase;
23  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseCashCategoryClass;
24  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseCashCategoryType;
25  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseCashCategoryType.MoneyWiseCashCategoryTypeList;
26  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseStaticDataType;
27  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
28  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataResource;
29  
30  /**
31   * Validator for CashCategory.
32   */
33  public class MoneyWiseValidateCashCategory
34          extends MoneyWiseValidateCategory<MoneyWiseCashCategory> {
35      @Override
36      public void validate(final PrometheusDataItem pCategory) {
37          /* Validate the base */
38          super.validate(pCategory);
39  
40          /* Access details */
41          final MoneyWiseCashCategory myCategory = (MoneyWiseCashCategory) pCategory;
42          final MoneyWiseCashCategoryType myCatType = myCategory.getCategoryType();
43          final MoneyWiseCashCategory myParent = myCategory.getParentCategory();
44  
45          /* CashCategoryType must be non-null */
46          if (myCatType == null) {
47              pCategory.addError(PrometheusDataItem.ERROR_MISSING, MoneyWiseStaticDataType.CASHTYPE);
48          } else {
49              /* Access the class */
50              final MoneyWiseCashCategoryClass myClass = myCatType.getCashClass();
51  
52              /* CashCategoryType must be enabled */
53              if (!myCatType.getEnabled()) {
54                  pCategory.addError(PrometheusDataItem.ERROR_DISABLED, MoneyWiseStaticDataType.CASHTYPE);
55              }
56  
57              /* Switch on the account class */
58              if (MoneyWiseCashCategoryClass.PARENT.equals(myClass)) {
59                  /* If parent exists */
60                  if (myParent != null) {
61                      pCategory.addError(PrometheusDataItem.ERROR_EXIST, PrometheusDataResource.DATAGROUP_PARENT);
62                  }
63              } else {
64                  /* Check parent */
65                  if (myParent == null) {
66                      pCategory.addError(PrometheusDataItem.ERROR_MISSING, PrometheusDataResource.DATAGROUP_PARENT);
67                  } else if (!myParent.isCategoryClass(MoneyWiseCashCategoryClass.PARENT)) {
68                      pCategory.addError(ERROR_BADPARENT, PrometheusDataResource.DATAGROUP_PARENT);
69                  } else {
70                      final String myName = myCategory.getName();
71  
72                      /* Check validity of parent */
73                      final MoneyWiseCashCategoryClass myParentClass = myParent.getCategoryTypeClass();
74                      if (!MoneyWiseCashCategoryClass.PARENT.equals(myParentClass)) {
75                          pCategory.addError(ERROR_BADPARENT, PrometheusDataResource.DATAGROUP_PARENT);
76                      }
77                      /* Check that name reflects parent */
78                      if ((myName != null) && !myName.startsWith(myParent.getName() + MoneyWiseCategoryBase.STR_SEP)) {
79                          pCategory.addError(ERROR_MATCHPARENT, PrometheusDataResource.DATAGROUP_PARENT);
80                      }
81                  }
82              }
83          }
84  
85          /* Set validation flag */
86          if (!pCategory.hasErrors()) {
87              pCategory.setValidEdit();
88          }
89      }
90  
91      @Override
92      public void setDefaults(final MoneyWiseCashCategory pParent,
93                              final MoneyWiseCashCategory pCategory) throws OceanusException {
94          /* Set values */
95          final MoneyWiseCashCategoryList myList = pCategory.getList();
96          final MoneyWiseCashCategoryTypeList myTypes
97                  = getEditSet().getDataList(MoneyWiseStaticDataType.CASHTYPE, MoneyWiseCashCategoryTypeList.class);
98          pCategory.setCategoryType(myTypes.findItemByClass(pParent == null
99                  ? MoneyWiseCashCategoryClass.PARENT
100                 : MoneyWiseCashCategoryClass.CASH));
101         pCategory.setParentCategory(pParent);
102         pCategory.setSubCategoryName(getUniqueName(myList, pParent));
103     }
104 }