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