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