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.MoneyWisePayeeInfo.MoneyWisePayeeInfoList;
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   * PayeeInfoSet class.
37   *
38   * @author Tony Washer
39   */
40  public class MoneyWisePayeeInfoSet
41          extends PrometheusDataInfoSet<MoneyWisePayeeInfo> {
42      /**
43       * Report fields.
44       */
45      private static final MetisFieldSet<MoneyWisePayeeInfoSet> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWisePayeeInfoSet.class);
46  
47      /**
48       * FieldSet map.
49       */
50      private static final Map<MetisDataFieldId, MoneyWiseAccountInfoClass> FIELDSET_MAP = FIELD_DEFS.buildFieldMap(MoneyWiseAccountInfoClass.class, MoneyWisePayeeInfoSet::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       * Constructor.
59       *
60       * @param pOwner    the Owner to which this Set belongs
61       * @param pTypeList the infoTypeList for the set
62       * @param pInfoList the InfoList for the set
63       */
64      protected MoneyWisePayeeInfoSet(final PrometheusDataItem pOwner,
65                                      final MoneyWiseAccountInfoTypeList pTypeList,
66                                      final MoneyWisePayeeInfoList pInfoList) {
67          /* Store the Owner and Info List */
68          super(pOwner, pTypeList, pInfoList);
69      }
70  
71      @Override
72      public MetisFieldSetDef getDataFieldSet() {
73          return FIELD_DEFS;
74      }
75  
76      /**
77       * Obtain fieldValue for infoSet.
78       *
79       * @param pFieldId the fieldId
80       * @return the value
81       */
82      public Object getFieldValue(final MetisDataFieldId pFieldId) {
83          /* Handle InfoSet fields */
84          final MoneyWiseAccountInfoClass myClass = getClassForField(pFieldId);
85          if (myClass != null) {
86              return getInfoSetValue(myClass);
87          }
88  
89          /* Pass onwards */
90          return null;
91      }
92  
93      /**
94       * Get an infoSet value.
95       *
96       * @param pInfoClass the class of info to get
97       * @return the value to set
98       */
99      private Object getInfoSetValue(final MoneyWiseAccountInfoClass pInfoClass) {
100         /* Return the value */
101         final Object myValue = getField(pInfoClass);
102         return myValue != null
103                 ? myValue
104                 : MetisDataFieldValue.SKIP;
105     }
106 
107     /**
108      * Obtain the class of the field if it is an infoSet field.
109      *
110      * @param pField the field
111      * @return the class
112      */
113     public static MoneyWiseAccountInfoClass getClassForField(final MetisDataFieldId pField) {
114         /* Look up field in map */
115         return FIELDSET_MAP.get(pField);
116     }
117 
118     /**
119      * Obtain the field for the infoSet class.
120      *
121      * @param pClass the class
122      * @return the field
123      */
124     public static MetisDataFieldId getFieldForClass(final MoneyWiseAccountInfoClass pClass) {
125         /* Look up field in map */
126         return REVERSE_FIELDMAP.get(pClass);
127     }
128 
129     @Override
130     public MetisDataFieldId getFieldForClass(final PrometheusDataInfoClass pClass) {
131         return getFieldForClass((MoneyWiseAccountInfoClass) pClass);
132     }
133 
134     @Override
135     public Iterator<PrometheusDataInfoClass> classIterator() {
136         final PrometheusDataInfoClass[] myValues = MoneyWiseAccountInfoClass.values();
137         return Arrays.stream(myValues).iterator();
138     }
139 
140     /**
141      * Clone the dataInfoSet.
142      *
143      * @param pSource the InfoSet to clone
144      */
145     protected void cloneDataInfoSet(final MoneyWisePayeeInfoSet pSource) {
146         /* Clone the dataInfoSet */
147         cloneTheDataInfoSet(pSource);
148     }
149 
150     /**
151      * Resolve editSetLinks.
152      *
153      * @param pEditSet the editSet
154      * @throws OceanusException on error
155      */
156     void resolveEditSetLinks(final PrometheusEditSet pEditSet) throws OceanusException {
157         /* Loop through the items */
158         for (MoneyWisePayeeInfo myInfo : this) {
159             myInfo.resolveEditSetLinks(pEditSet);
160         }
161     }
162 }