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.MetisFieldRequired;
23  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
24  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseTransInfo.MoneyWiseTransInfoList;
25  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseTransCategoryClass;
26  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseTransInfoClass;
27  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseTransInfoType.MoneyWiseTransInfoTypeList;
28  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataInfoClass;
29  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataInfoSet;
30  import io.github.tonywasher.joceanus.prometheus.views.PrometheusEditSet;
31  
32  import java.util.Arrays;
33  import java.util.Iterator;
34  import java.util.Map;
35  
36  /**
37   * TransactionInfoSet class.
38   *
39   * @author Tony Washer
40   */
41  public class MoneyWiseTransInfoSet
42          extends PrometheusDataInfoSet<MoneyWiseTransInfo> {
43      /**
44       * Report fields.
45       */
46      private static final MetisFieldSet<MoneyWiseTransInfoSet> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseTransInfoSet.class);
47  
48      /**
49       * FieldSet map.
50       */
51      private static final Map<MetisDataFieldId, MoneyWiseTransInfoClass> FIELDSET_MAP = FIELD_DEFS.buildFieldMap(MoneyWiseTransInfoClass.class, MoneyWiseTransInfoSet::getFieldValue);
52  
53      /**
54       * Reverse FieldSet map.
55       */
56      private static final Map<MoneyWiseTransInfoClass, MetisDataFieldId> REVERSE_FIELDMAP = MetisFieldSet.reverseFieldMap(FIELDSET_MAP, MoneyWiseTransInfoClass.class);
57  
58      /**
59       * Constructor.
60       *
61       * @param pOwner    the Owner to which this Set belongs
62       * @param pTypeList the infoTypeList for the set
63       * @param pInfoList source InfoSet
64       */
65      protected MoneyWiseTransInfoSet(final MoneyWiseTransaction pOwner,
66                                      final MoneyWiseTransInfoTypeList pTypeList,
67                                      final MoneyWiseTransInfoList pInfoList) {
68          /* Store the Owner and Info List */
69          super(pOwner, pTypeList, pInfoList);
70      }
71  
72      @Override
73      public MetisFieldSetDef getDataFieldSet() {
74          return FIELD_DEFS;
75      }
76  
77      @Override
78      public MoneyWiseTransaction getOwner() {
79          return (MoneyWiseTransaction) super.getOwner();
80      }
81  
82      /**
83       * Obtain fieldValue for infoSet.
84       *
85       * @param pFieldId the fieldId
86       * @return the value
87       */
88      public Object getFieldValue(final MetisDataFieldId pFieldId) {
89          /* Handle InfoSet fields */
90          final MoneyWiseTransInfoClass myClass = getClassForField(pFieldId);
91          if (myClass != null) {
92              return getInfoSetValue(myClass);
93          }
94  
95          /* Pass onwards */
96          return null;
97      }
98  
99      /**
100      * Get an infoSet value.
101      *
102      * @param pInfoClass the class of info to get
103      * @return the value to set
104      */
105     private Object getInfoSetValue(final MoneyWiseTransInfoClass pInfoClass) {
106         final Object myValue;
107 
108         switch (pInfoClass) {
109             case RETURNEDCASHACCOUNT:
110                 /* Access deposit of object */
111                 myValue = getTransAsset(pInfoClass);
112                 break;
113             case TRANSTAG:
114                 /* Access InfoSetList */
115                 myValue = getListValue(pInfoClass);
116                 break;
117             default:
118                 /* Access value of object */
119                 myValue = getField(pInfoClass);
120                 break;
121         }
122 
123         /* Return the value */
124         return myValue != null
125                 ? myValue
126                 : MetisDataFieldValue.SKIP;
127     }
128 
129     /**
130      * Obtain the class of the field if it is an infoSet field.
131      *
132      * @param pField the field
133      * @return the class
134      */
135     public static MoneyWiseTransInfoClass getClassForField(final MetisDataFieldId pField) {
136         /* Look up field in map */
137         return FIELDSET_MAP.get(pField);
138     }
139 
140     /**
141      * Obtain the field for the infoSet class.
142      *
143      * @param pClass the class
144      * @return the field
145      */
146     public static MetisDataFieldId getFieldForClass(final MoneyWiseTransInfoClass pClass) {
147         /* Look up field in map */
148         return REVERSE_FIELDMAP.get(pClass);
149     }
150 
151     @Override
152     public MetisDataFieldId getFieldForClass(final PrometheusDataInfoClass pClass) {
153         return getFieldForClass((MoneyWiseTransInfoClass) pClass);
154     }
155 
156     @Override
157     public Iterator<PrometheusDataInfoClass> classIterator() {
158         final PrometheusDataInfoClass[] myValues = MoneyWiseTransInfoClass.values();
159         return Arrays.stream(myValues).iterator();
160     }
161 
162     /**
163      * Clone the dataInfoSet.
164      *
165      * @param pSource the InfoSet to clone
166      */
167     protected void cloneDataInfoSet(final MoneyWiseTransInfoSet pSource) {
168         /* Clone the dataInfoSet */
169         cloneTheDataInfoSet(pSource);
170     }
171 
172     /**
173      * Resolve editSetLinks.
174      *
175      * @param pEditSet the editSet
176      * @throws OceanusException on error
177      */
178     void resolveEditSetLinks(final PrometheusEditSet pEditSet) throws OceanusException {
179         /* Loop through the items */
180         for (MoneyWiseTransInfo myInfo : this) {
181             myInfo.resolveEditSetLinks(pEditSet);
182         }
183     }
184 
185     /**
186      * Obtain the deposit for the infoClass.
187      *
188      * @param pInfoClass the Info Class
189      * @return the deposit
190      */
191     public MoneyWiseTransAsset getTransAsset(final MoneyWiseTransInfoClass pInfoClass) {
192         /* Access existing entry */
193         final MoneyWiseTransInfo myValue = getInfo(pInfoClass);
194 
195         /* If we have no entry, return null */
196         if (myValue == null) {
197             return null;
198         }
199 
200         /* Return the asset */
201         return myValue.getTransAsset();
202     }
203 
204     /**
205      * Determine if an infoSet class is metaData.
206      *
207      * @param pClass the infoSet class
208      * @return the status
209      */
210     public boolean isMetaData(final MoneyWiseTransInfoClass pClass) {
211         /* Switch on class */
212         switch (pClass) {
213             /* Can always change reference/comments/tags */
214             case REFERENCE:
215             case COMMENTS:
216             case TRANSTAG:
217                 return true;
218 
219             /* All others are locked */
220             default:
221                 return false;
222         }
223     }
224 
225     /**
226      * Determine if AccountDeltaUnits can/mustBe/mustNotBe positive.
227      *
228      * @param pDir   the direction
229      * @param pClass the category class
230      * @return the status
231      */
232     public static MetisFieldRequired isAccountUnitsPositive(final MoneyWiseAssetDirection pDir,
233                                                             final MoneyWiseTransCategoryClass pClass) {
234         switch (pClass) {
235             case TRANSFER:
236                 return pDir.isFrom()
237                         ? MetisFieldRequired.MUSTEXIST
238                         : MetisFieldRequired.NOTALLOWED;
239             case UNITSADJUST:
240             case STOCKSPLIT:
241                 return MetisFieldRequired.CANEXIST;
242             case INHERITED:
243             case DIVIDEND:
244             case STOCKRIGHTSISSUE:
245                 return MetisFieldRequired.MUSTEXIST;
246             case STOCKDEMERGER:
247             default:
248                 return MetisFieldRequired.NOTALLOWED;
249         }
250     }
251 
252     /**
253      * Determine if PartnerDeltaUnits can/mustBe/mustNotBe positive.
254      *
255      * @param pDir   the direction
256      * @param pClass the category class
257      * @return the status
258      */
259     public static MetisFieldRequired isPartnerUnitsPositive(final MoneyWiseAssetDirection pDir,
260                                                             final MoneyWiseTransCategoryClass pClass) {
261         switch (pClass) {
262             case TRANSFER:
263                 return pDir.isTo()
264                         ? MetisFieldRequired.MUSTEXIST
265                         : MetisFieldRequired.NOTALLOWED;
266             case STOCKDEMERGER:
267             case SECURITYREPLACE:
268             case STOCKTAKEOVER:
269             case STOCKRIGHTSISSUE:
270                 return MetisFieldRequired.MUSTEXIST;
271             default:
272                 return MetisFieldRequired.NOTALLOWED;
273         }
274     }
275 }