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.MetisFieldRequired;
22  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
23  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseTransInfo.MoneyWiseTransInfoList;
24  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseTransCategoryClass;
25  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseTransInfoClass;
26  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseTransInfoType.MoneyWiseTransInfoTypeList;
27  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
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 = switch (pInfoClass) {
107             case RETURNEDCASHACCOUNT ->
108                 /* Access deposit of object */
109                     getTransAsset(pInfoClass);
110             case TRANSTAG ->
111                 /* Access InfoSetList */
112                     getListValue(pInfoClass);
113             default ->
114                 /* Access value of object */
115                     getField(pInfoClass);
116         };
117 
118         /* Return the value */
119         return myValue != null
120                 ? myValue
121                 : MetisDataFieldValue.SKIP;
122     }
123 
124     /**
125      * Obtain the class of the field if it is an infoSet field.
126      *
127      * @param pField the field
128      * @return the class
129      */
130     public static MoneyWiseTransInfoClass getClassForField(final MetisDataFieldId pField) {
131         /* Look up field in map */
132         return FIELDSET_MAP.get(pField);
133     }
134 
135     /**
136      * Obtain the field for the infoSet class.
137      *
138      * @param pClass the class
139      * @return the field
140      */
141     public static MetisDataFieldId getFieldForClass(final MoneyWiseTransInfoClass pClass) {
142         /* Look up field in map */
143         return REVERSE_FIELDMAP.get(pClass);
144     }
145 
146     @Override
147     public MetisDataFieldId getFieldForClass(final PrometheusDataInfoClass pClass) {
148         return getFieldForClass((MoneyWiseTransInfoClass) pClass);
149     }
150 
151     @Override
152     public Iterator<PrometheusDataInfoClass> classIterator() {
153         final PrometheusDataInfoClass[] myValues = MoneyWiseTransInfoClass.values();
154         return Arrays.stream(myValues).iterator();
155     }
156 
157     /**
158      * Clone the dataInfoSet.
159      *
160      * @param pSource the InfoSet to clone
161      */
162     protected void cloneDataInfoSet(final MoneyWiseTransInfoSet pSource) {
163         /* Clone the dataInfoSet */
164         cloneTheDataInfoSet(pSource);
165     }
166 
167     /**
168      * Resolve editSetLinks.
169      *
170      * @param pEditSet the editSet
171      * @throws OceanusException on error
172      */
173     void resolveEditSetLinks(final PrometheusEditSet pEditSet) throws OceanusException {
174         /* Loop through the items */
175         for (MoneyWiseTransInfo myInfo : this) {
176             myInfo.resolveEditSetLinks(pEditSet);
177         }
178     }
179 
180     /**
181      * Obtain the deposit for the infoClass.
182      *
183      * @param pInfoClass the Info Class
184      * @return the deposit
185      */
186     public MoneyWiseTransAsset getTransAsset(final MoneyWiseTransInfoClass pInfoClass) {
187         /* Access existing entry */
188         final MoneyWiseTransInfo myValue = getInfo(pInfoClass);
189 
190         /* If we have no entry, return null */
191         if (myValue == null) {
192             return null;
193         }
194 
195         /* Return the asset */
196         return myValue.getTransAsset();
197     }
198 
199     /**
200      * Determine if an infoSet class is metaData.
201      *
202      * @param pClass the infoSet class
203      * @return the status
204      */
205     public boolean isMetaData(final MoneyWiseTransInfoClass pClass) {
206         /* Switch on class */
207         return switch (pClass) {
208             /* Can always change reference/comments/tags */
209             case REFERENCE, COMMENTS, TRANSTAG -> true;
210 
211             /* All others are locked */
212             default -> false;
213         };
214     }
215 
216     /**
217      * Determine if AccountDeltaUnits can/mustBe/mustNotBe positive.
218      *
219      * @param pDir   the direction
220      * @param pClass the category class
221      * @return the status
222      */
223     public static MetisFieldRequired isAccountUnitsPositive(final MoneyWiseAssetDirection pDir,
224                                                             final MoneyWiseTransCategoryClass pClass) {
225         return switch (pClass) {
226             case TRANSFER -> pDir.isFrom()
227                     ? MetisFieldRequired.MUSTEXIST
228                     : MetisFieldRequired.NOTALLOWED;
229             case UNITSADJUST, STOCKSPLIT -> MetisFieldRequired.CANEXIST;
230             case INHERITED, DIVIDEND, STOCKRIGHTSISSUE -> MetisFieldRequired.MUSTEXIST;
231             default -> MetisFieldRequired.NOTALLOWED;
232         };
233     }
234 
235     /**
236      * Determine if PartnerDeltaUnits can/mustBe/mustNotBe positive.
237      *
238      * @param pDir   the direction
239      * @param pClass the category class
240      * @return the status
241      */
242     public static MetisFieldRequired isPartnerUnitsPositive(final MoneyWiseAssetDirection pDir,
243                                                             final MoneyWiseTransCategoryClass pClass) {
244         return switch (pClass) {
245             case TRANSFER -> pDir.isTo()
246                     ? MetisFieldRequired.MUSTEXIST
247                     : MetisFieldRequired.NOTALLOWED;
248             case STOCKDEMERGER, SECURITYREPLACE, STOCKTAKEOVER, STOCKRIGHTSISSUE -> MetisFieldRequired.MUSTEXIST;
249             default -> MetisFieldRequired.NOTALLOWED;
250         };
251     }
252 }