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.basic;
18  
19  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20  import io.github.tonywasher.joceanus.metis.data.MetisDataFieldValue;
21  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataFieldId;
22  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
23  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseCashInfo.MoneyWiseCashInfoList;
24  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAccountInfoClass;
25  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAccountInfoType.MoneyWiseAccountInfoTypeList;
26  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataInfoClass;
27  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataInfoSet;
28  import io.github.tonywasher.joceanus.prometheus.views.PrometheusEditSet;
29  
30  import java.util.Arrays;
31  import java.util.Iterator;
32  import java.util.Map;
33  
34  /**
35   * CashInfoSet class.
36   *
37   * @author Tony Washer
38   */
39  public class MoneyWiseCashInfoSet
40          extends PrometheusDataInfoSet<MoneyWiseCashInfo> {
41      /**
42       * Report fields.
43       */
44      private static final MetisFieldSet<MoneyWiseCashInfoSet> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseCashInfoSet.class);
45  
46      /**
47       * FieldSet map.
48       */
49      private static final Map<MetisDataFieldId, MoneyWiseAccountInfoClass> FIELDSET_MAP = FIELD_DEFS.buildFieldMap(MoneyWiseAccountInfoClass.class, MoneyWiseCashInfoSet::getFieldValue);
50  
51      /**
52       * Reverse FieldSet map.
53       */
54      private static final Map<MoneyWiseAccountInfoClass, MetisDataFieldId> REVERSE_FIELDMAP = MetisFieldSet.reverseFieldMap(FIELDSET_MAP, MoneyWiseAccountInfoClass.class);
55  
56      /**
57       * AutoExpense Not Expense Error Text.
58       */
59      public static final String ERROR_AUTOEXP = MoneyWiseBasicResource.CASH_ERROR_AUTOEXPENSE.getValue();
60  
61      /**
62       * Constructor.
63       *
64       * @param pOwner    the Owner to which this Set belongs
65       * @param pTypeList the infoTypeList for the set
66       * @param pInfoList the InfoList for the set
67       */
68      protected MoneyWiseCashInfoSet(final MoneyWiseCash pOwner,
69                                     final MoneyWiseAccountInfoTypeList pTypeList,
70                                     final MoneyWiseCashInfoList pInfoList) {
71          /* Store the Owner and Info List */
72          super(pOwner, pTypeList, pInfoList);
73      }
74  
75      @Override
76      public MetisFieldSetDef getDataFieldSet() {
77          return FIELD_DEFS;
78      }
79  
80      @Override
81      public MoneyWiseCash getOwner() {
82          return (MoneyWiseCash) super.getOwner();
83      }
84  
85      /**
86       * Obtain fieldValue for infoSet.
87       *
88       * @param pFieldId the fieldId
89       * @return the value
90       */
91      public Object getFieldValue(final MetisDataFieldId pFieldId) {
92          /* Handle InfoSet fields */
93          final MoneyWiseAccountInfoClass myClass = getClassForField(pFieldId);
94          if (myClass != null) {
95              return getInfoSetValue(myClass);
96          }
97  
98          /* Pass onwards */
99          return null;
100     }
101 
102     /**
103      * Get an infoSet value.
104      *
105      * @param pInfoClass the class of info to get
106      * @return the value to set
107      */
108     private Object getInfoSetValue(final MoneyWiseAccountInfoClass pInfoClass) {
109         final Object myValue;
110 
111         switch (pInfoClass) {
112             case AUTOPAYEE:
113                 /* Access payee of object */
114                 myValue = getPayee(pInfoClass);
115                 break;
116             case AUTOEXPENSE:
117                 /* Access event category of object */
118                 myValue = getEventCategory(pInfoClass);
119                 break;
120             default:
121                 /* Access value of object */
122                 myValue = getField(pInfoClass);
123                 break;
124         }
125 
126         /* Return the value */
127         return myValue != null
128                 ? myValue
129                 : MetisDataFieldValue.SKIP;
130     }
131 
132     /**
133      * Obtain the class of the field if it is an infoSet field.
134      *
135      * @param pField the field
136      * @return the class
137      */
138     public static MoneyWiseAccountInfoClass getClassForField(final MetisDataFieldId pField) {
139         /* Look up field in map */
140         return FIELDSET_MAP.get(pField);
141     }
142 
143     /**
144      * Obtain the field for the infoSet class.
145      *
146      * @param pClass the class
147      * @return the field
148      */
149     public static MetisDataFieldId getFieldForClass(final MoneyWiseAccountInfoClass pClass) {
150         /* Look up field in map */
151         return REVERSE_FIELDMAP.get(pClass);
152     }
153 
154     @Override
155     public MetisDataFieldId getFieldForClass(final PrometheusDataInfoClass pClass) {
156         return getFieldForClass((MoneyWiseAccountInfoClass) pClass);
157     }
158 
159     /**
160      * Obtain the payee for the infoClass.
161      *
162      * @param pInfoClass the Info Class
163      * @return the payee
164      */
165     public MoneyWisePayee getPayee(final MoneyWiseAccountInfoClass pInfoClass) {
166         /* Access existing entry */
167         final MoneyWiseCashInfo myValue = getInfo(pInfoClass);
168 
169         /* If we have no entry, return null */
170         if (myValue == null) {
171             return null;
172         }
173 
174         /* Return the payee */
175         return myValue.getPayee();
176     }
177 
178     /**
179      * Obtain the event category for the infoClass.
180      *
181      * @param pInfoClass the Info Class
182      * @return the event category
183      */
184     public MoneyWiseTransCategory getEventCategory(final MoneyWiseAccountInfoClass pInfoClass) {
185         /* Access existing entry */
186         final MoneyWiseCashInfo myValue = getInfo(pInfoClass);
187 
188         /* If we have no entry, return null */
189         if (myValue == null) {
190             return null;
191         }
192 
193         /* Return the event category */
194         return myValue.getEventCategory();
195     }
196 
197     @Override
198     public Iterator<PrometheusDataInfoClass> classIterator() {
199         final PrometheusDataInfoClass[] myValues = MoneyWiseAccountInfoClass.values();
200         return Arrays.stream(myValues).iterator();
201     }
202 
203     /**
204      * Clone the dataInfoSet.
205      *
206      * @param pSource the InfoSet to clone
207      */
208     protected void cloneDataInfoSet(final MoneyWiseCashInfoSet pSource) {
209         /* Clone the dataInfoSet */
210         cloneTheDataInfoSet(pSource);
211     }
212 
213     /**
214      * Resolve editSetLinks.
215      *
216      * @param pEditSet the editSet
217      * @throws OceanusException on error
218      */
219     void resolveEditSetLinks(final PrometheusEditSet pEditSet) throws OceanusException {
220         /* Loop through the items */
221         for (MoneyWiseCashInfo myInfo : this) {
222             myInfo.resolveEditSetLinks(pEditSet);
223         }
224     }
225 }