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.MoneyWiseBasicDataType;
21  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseBasicResource;
22  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseCash;
23  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseCash.MoneyWiseCashList;
24  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseCashCategory;
25  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseCashCategory.MoneyWiseCashCategoryList;
26  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDataValidator.MoneyWiseDataValidatorAutoCorrect;
27  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWisePayee;
28  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseCashCategoryClass;
29  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseCurrency;
30  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseStaticDataType;
31  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
32  import io.github.tonywasher.joceanus.prometheus.views.PrometheusEditSet;
33  
34  import java.util.Iterator;
35  
36  /**
37   * Validator for Cash.
38   */
39  public class MoneyWiseValidateCash
40          extends MoneyWiseValidateAccount<MoneyWiseCash>
41          implements MoneyWiseDataValidatorAutoCorrect<MoneyWiseCash> {
42      /**
43       * New Account name.
44       */
45      private static final String NAME_NEWACCOUNT = MoneyWiseBasicResource.CASH_NEWACCOUNT.getValue();
46  
47      /**
48       * The infoSet validator.
49       */
50      private final MoneyWiseValidateCashInfoSet theInfoSet;
51  
52      /**
53       * Constructor.
54       */
55      MoneyWiseValidateCash() {
56          theInfoSet = new MoneyWiseValidateCashInfoSet();
57      }
58  
59      @Override
60      public void setEditSet(final PrometheusEditSet pEditSet) {
61          super.setEditSet(pEditSet);
62          theInfoSet.storeEditSet(pEditSet);
63      }
64  
65      @Override
66      public void validate(final PrometheusDataItem pCash) {
67          final MoneyWiseCash myCash = (MoneyWiseCash) pCash;
68          final MoneyWisePayee myParent = myCash.getParent();
69          final MoneyWiseCashCategory myCategory = myCash.getCategory();
70          final MoneyWiseCurrency myCurrency = myCash.getAssetCurrency();
71  
72          /* Validate base components */
73          super.validate(pCash);
74  
75          /* Category must be non-null */
76          if (myCategory == null) {
77              pCash.addError(PrometheusDataItem.ERROR_MISSING, MoneyWiseBasicResource.CATEGORY_NAME);
78          } else if (myCategory.getCategoryTypeClass().isParentCategory()) {
79              pCash.addError(ERROR_BADCATEGORY, MoneyWiseBasicResource.CATEGORY_NAME);
80          }
81  
82          /* Parent must be null */
83          if (myParent != null) {
84              pCash.addError(PrometheusDataItem.ERROR_EXIST, MoneyWiseBasicResource.ASSET_PARENT);
85          }
86  
87          /* Currency must be non-null and enabled */
88          if (myCurrency == null) {
89              pCash.addError(PrometheusDataItem.ERROR_MISSING, MoneyWiseStaticDataType.CURRENCY);
90          } else if (!myCurrency.getEnabled()) {
91              pCash.addError(PrometheusDataItem.ERROR_DISABLED, MoneyWiseStaticDataType.CURRENCY);
92          }
93  
94          /* If we have an infoSet */
95          if (myCash.getInfoSet() != null) {
96              /* Validate the InfoSet */
97              theInfoSet.validate(myCash.getInfoSet());
98          }
99  
100         /* Set validation flag */
101         if (!pCash.hasErrors()) {
102             pCash.setValidEdit();
103         }
104     }
105 
106     @Override
107     public void setDefaults(final MoneyWiseCash pCash) throws OceanusException {
108         /* Set values */
109         final MoneyWiseCashList myList = pCash.getList();
110         pCash.setName(getUniqueName(myList, NAME_NEWACCOUNT));
111         pCash.setCategory(getDefaultCategory());
112         pCash.setAssetCurrency(getReportingCurrency());
113         pCash.setClosed(Boolean.FALSE);
114         autoCorrect(pCash);
115     }
116 
117     @Override
118     public void autoCorrect(final MoneyWiseCash pCash) throws OceanusException {
119         /* autoCorrect the infoSet */
120         theInfoSet.autoCorrect(pCash.getInfoSet());
121     }
122 
123     /**
124      * Obtain default category for new cash account.
125      *
126      * @return the default category
127      */
128     private MoneyWiseCashCategory getDefaultCategory() {
129         /* loop through the categories */
130         final MoneyWiseCashCategoryList myCategories
131                 = getEditSet().getDataList(MoneyWiseBasicDataType.CASHCATEGORY, MoneyWiseCashCategoryList.class);
132         final Iterator<MoneyWiseCashCategory> myIterator = myCategories.iterator();
133         while (myIterator.hasNext()) {
134             final MoneyWiseCashCategory myCategory = myIterator.next();
135 
136             /* Ignore deleted categories */
137             if (myCategory.isDeleted()) {
138                 continue;
139             }
140 
141             /* If the category is not a parent */
142             if (!myCategory.isCategoryClass(MoneyWiseCashCategoryClass.PARENT)) {
143                 return myCategory;
144             }
145         }
146 
147         /* Return no category */
148         return null;
149     }
150 }