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.MoneyWiseDepositInfo.MoneyWiseDepositInfoList;
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   * DepositInfoSet class.
37   *
38   * @author Tony Washer
39   */
40  public class MoneyWiseDepositInfoSet
41          extends PrometheusDataInfoSet<MoneyWiseDepositInfo> {
42      /**
43       * Report fields.
44       */
45      private static final MetisFieldSet<MoneyWiseDepositInfoSet> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseDepositInfoSet.class);
46  
47      /**
48       * FieldSet map.
49       */
50      private static final Map<MetisDataFieldId, MoneyWiseAccountInfoClass> FIELDSET_MAP = FIELD_DEFS.buildFieldMap(MoneyWiseAccountInfoClass.class, MoneyWiseDepositInfoSet::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       * Opening Balance Currency Error Text.
59       */
60      public static final String ERROR_CURRENCY = MoneyWiseBasicResource.MONEYWISEDATA_ERROR_CURRENCY.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 MoneyWiseDepositInfoSet(final PrometheusDataItem pOwner,
70                                        final MoneyWiseAccountInfoTypeList pTypeList,
71                                        final MoneyWiseDepositInfoList 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       * Obtain the class of the field if it is an infoSet field.
100      *
101      * @param pField the field
102      * @return the class
103      */
104     public static MoneyWiseAccountInfoClass getClassForField(final MetisDataFieldId pField) {
105         /* Look up field in map */
106         return FIELDSET_MAP.get(pField);
107     }
108 
109     /**
110      * Obtain the field for the infoSet class.
111      *
112      * @param pClass the class
113      * @return the field
114      */
115     public static MetisDataFieldId getFieldForClass(final MoneyWiseAccountInfoClass pClass) {
116         /* Look up field in map */
117         return REVERSE_FIELDMAP.get(pClass);
118     }
119 
120     @Override
121     public MetisDataFieldId getFieldForClass(final PrometheusDataInfoClass pClass) {
122         return getFieldForClass((MoneyWiseAccountInfoClass) pClass);
123     }
124 
125     /**
126      * Get an infoSet value.
127      *
128      * @param pInfoClass the class of info to get
129      * @return the value to set
130      */
131     private Object getInfoSetValue(final MoneyWiseAccountInfoClass pInfoClass) {
132         /* Return the value */
133         final Object myValue = getField(pInfoClass);
134         return myValue != null
135                 ? myValue
136                 : MetisDataFieldValue.SKIP;
137     }
138 
139     @Override
140     public Iterator<PrometheusDataInfoClass> classIterator() {
141         final PrometheusDataInfoClass[] myValues = MoneyWiseAccountInfoClass.values();
142         return Arrays.stream(myValues).iterator();
143     }
144 
145     /**
146      * Clone the dataInfoSet.
147      *
148      * @param pSource the InfoSet to clone
149      */
150     protected void cloneDataInfoSet(final MoneyWiseDepositInfoSet pSource) {
151         /* Clone the dataInfoSet */
152         cloneTheDataInfoSet(pSource);
153     }
154 
155     /**
156      * Resolve editSetLinks.
157      *
158      * @param pEditSet the editSet
159      * @throws OceanusException on error
160      */
161     void resolveEditSetLinks(final PrometheusEditSet pEditSet) throws OceanusException {
162         /* Loop through the items */
163         for (MoneyWiseDepositInfo myInfo : this) {
164             myInfo.resolveEditSetLinks(pEditSet);
165         }
166     }
167 }