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.MetisDataDifference;
21  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataFieldId;
22  import io.github.tonywasher.joceanus.metis.data.MetisDataResource;
23  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
24  import io.github.tonywasher.joceanus.metis.field.MetisFieldVersionedSet;
25  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAssetCategory;
26  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseLoanCategoryClass;
27  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseLoanCategoryType;
28  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseLoanCategoryType.MoneyWiseLoanCategoryTypeList;
29  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseStaticDataType;
30  import io.github.tonywasher.joceanus.moneywise.exc.MoneyWiseDataException;
31  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
32  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataResource;
33  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataValues;
34  import io.github.tonywasher.joceanus.prometheus.data.PrometheusStaticDataItem;
35  import io.github.tonywasher.joceanus.prometheus.views.PrometheusEditSet;
36  
37  import java.util.Iterator;
38  
39  /**
40   * Loan Category class.
41   */
42  public final class MoneyWiseLoanCategory
43          extends MoneyWiseCategoryBase
44          implements MoneyWiseAssetCategory {
45      /**
46       * Object name.
47       */
48      public static final String OBJECT_NAME = MoneyWiseBasicDataType.LOANCATEGORY.getItemName();
49  
50      /**
51       * List name.
52       */
53      public static final String LIST_NAME = MoneyWiseBasicDataType.LOANCATEGORY.getListName();
54  
55      /**
56       * Local Report fields.
57       */
58      private static final MetisFieldVersionedSet<MoneyWiseLoanCategory> FIELD_DEFS = MetisFieldVersionedSet.newVersionedFieldSet(MoneyWiseLoanCategory.class);
59  
60      /*
61       * FieldIds.
62       */
63      static {
64          FIELD_DEFS.declareLinkField(MoneyWiseStaticDataType.LOANTYPE);
65      }
66  
67      /**
68       * Copy Constructor.
69       *
70       * @param pList     the list
71       * @param pCategory The Category to copy
72       */
73      MoneyWiseLoanCategory(final MoneyWiseLoanCategoryList pList,
74                            final MoneyWiseLoanCategory pCategory) {
75          /* Set standard values */
76          super(pList, pCategory);
77      }
78  
79      /**
80       * Values constructor.
81       *
82       * @param pList   the List to add to
83       * @param pValues the values constructor
84       * @throws OceanusException on error
85       */
86      private MoneyWiseLoanCategory(final MoneyWiseLoanCategoryList pList,
87                                    final PrometheusDataValues pValues) throws OceanusException {
88          /* Initialise the item */
89          super(pList, pValues);
90  
91          /* Store the Category Type */
92          final Object myValue = pValues.getValue(MoneyWiseStaticDataType.LOANTYPE);
93          if (myValue instanceof Integer i) {
94              setValueType(i);
95          } else if (myValue instanceof String s) {
96              setValueType(s);
97          }
98      }
99  
100     /**
101      * Edit Constructor.
102      *
103      * @param pList the list
104      */
105     public MoneyWiseLoanCategory(final MoneyWiseLoanCategoryList pList) {
106         super(pList);
107     }
108 
109     @Override
110     public MetisFieldSetDef getDataFieldSet() {
111         return FIELD_DEFS;
112     }
113 
114     @Override
115     public boolean includeXmlField(final MetisDataFieldId pField) {
116         /* Determine whether fields should be included */
117         if (MoneyWiseStaticDataType.LOANTYPE.equals(pField)) {
118             return true;
119         }
120 
121         /* Pass call on */
122         return super.includeXmlField(pField);
123     }
124 
125     @Override
126     public MoneyWiseLoanCategoryType getCategoryType() {
127         return getValues().getValue(MoneyWiseStaticDataType.LOANTYPE, MoneyWiseLoanCategoryType.class);
128     }
129 
130     @Override
131     public MoneyWiseLoanCategoryClass getCategoryTypeClass() {
132         final MoneyWiseLoanCategoryType myType = getCategoryType();
133         return myType == null
134                 ? null
135                 : myType.getLoanClass();
136     }
137 
138     @Override
139     public MoneyWiseLoanCategory getParentCategory() {
140         return getValues().getValue(PrometheusDataResource.DATAGROUP_PARENT, MoneyWiseLoanCategory.class);
141     }
142 
143 
144     /**
145      * Set account type value.
146      *
147      * @param pValue the value
148      */
149     private void setValueType(final MoneyWiseLoanCategoryType pValue) {
150         getValues().setUncheckedValue(MoneyWiseStaticDataType.LOANTYPE, pValue);
151     }
152 
153     /**
154      * Set account type id.
155      *
156      * @param pValue the value
157      */
158     private void setValueType(final Integer pValue) {
159         getValues().setUncheckedValue(MoneyWiseStaticDataType.LOANTYPE, pValue);
160     }
161 
162     /**
163      * Set account type name.
164      *
165      * @param pValue the value
166      */
167     private void setValueType(final String pValue) {
168         getValues().setUncheckedValue(MoneyWiseStaticDataType.LOANTYPE, pValue);
169     }
170 
171     /**
172      * Is this loan category the required class.
173      *
174      * @param pClass the required category class.
175      * @return true/false
176      */
177     public boolean isCategoryClass(final MoneyWiseLoanCategoryClass pClass) {
178         /* Check for match */
179         return getCategoryTypeClass() == pClass;
180     }
181 
182     @Override
183     public MoneyWiseLoanCategoryList getList() {
184         return (MoneyWiseLoanCategoryList) super.getList();
185     }
186 
187     /**
188      * Set defaults.
189      *
190      * @param pParent the parent
191      * @throws OceanusException on error
192      */
193     public void setDefaults(final MoneyWiseLoanCategory pParent) throws OceanusException {
194         getList().getValidator().setDefaults(pParent, this);
195     }
196 
197     @Override
198     public void resolveDataSetLinks() throws OceanusException {
199         /* Update the Underlying details */
200         super.resolveDataSetLinks();
201 
202         /* Resolve category type and parent */
203         final MoneyWiseDataSet myData = getDataSet();
204         resolveDataLink(MoneyWiseStaticDataType.LOANTYPE, myData.getLoanCategoryTypes());
205     }
206 
207     @Override
208     protected void resolveEditSetLinks() throws OceanusException {
209         /* Resolve parent within list */
210         resolveDataLink(PrometheusDataResource.DATAGROUP_PARENT, getList());
211 
212         /* Resolve StaticType if required */
213         final PrometheusEditSet myEditSet = getList().getEditSet();
214         if (myEditSet.hasDataType(MoneyWiseStaticDataType.LOANTYPE)) {
215             resolveDataLink(MoneyWiseStaticDataType.LOANTYPE, myEditSet.getDataList(MoneyWiseStaticDataType.LOANTYPE, MoneyWiseLoanCategoryTypeList.class));
216         }
217     }
218 
219     @Override
220     public void setCategoryType(final PrometheusStaticDataItem pType) {
221         setValueType((MoneyWiseLoanCategoryType) pType);
222     }
223 
224     /**
225      * Update base category from an edited category.
226      *
227      * @param pCategory the edited category
228      * @return whether changes have been made
229      */
230     @Override
231     public boolean applyChanges(final PrometheusDataItem pCategory) {
232         /* Can only update from a loan category */
233         if (!(pCategory instanceof MoneyWiseLoanCategory)) {
234             return false;
235         }
236         final MoneyWiseLoanCategory myCategory = (MoneyWiseLoanCategory) pCategory;
237 
238         /* Store the current detail into history */
239         pushHistory();
240 
241         /* Apply basic changes */
242         applyBasicChanges(myCategory);
243 
244         /* Update the category type if required */
245         if (!MetisDataDifference.isEqual(getCategoryType(), myCategory.getCategoryType())) {
246             setValueType(myCategory.getCategoryType());
247         }
248 
249         /* Check for changes */
250         return checkForHistory();
251     }
252 
253     /**
254      * The Loan Category List class.
255      */
256     public static class MoneyWiseLoanCategoryList
257             extends MoneyWiseCategoryBaseList<MoneyWiseLoanCategory> {
258         /**
259          * Report fields.
260          */
261         private static final MetisFieldSet<MoneyWiseLoanCategoryList> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseLoanCategoryList.class);
262 
263         /**
264          * The EditSet.
265          */
266         private PrometheusEditSet theEditSet;
267 
268         /**
269          * Construct an empty CORE Category list.
270          *
271          * @param pData the DataSet for the list
272          */
273         protected MoneyWiseLoanCategoryList(final MoneyWiseDataSet pData) {
274             super(pData, MoneyWiseLoanCategory.class, MoneyWiseBasicDataType.LOANCATEGORY);
275         }
276 
277         /**
278          * Constructor for a cloned List.
279          *
280          * @param pSource the source List
281          */
282         protected MoneyWiseLoanCategoryList(final MoneyWiseLoanCategoryList pSource) {
283             super(pSource);
284         }
285 
286         @Override
287         public MetisFieldSet<MoneyWiseLoanCategoryList> getDataFieldSet() {
288             return FIELD_DEFS;
289         }
290 
291         @Override
292         public String listName() {
293             return LIST_NAME;
294         }
295 
296         @Override
297         public MetisFieldSetDef getItemFields() {
298             return MoneyWiseLoanCategory.FIELD_DEFS;
299         }
300 
301         /**
302          * Obtain editSet.
303          *
304          * @return the editSet
305          */
306         public PrometheusEditSet getEditSet() {
307             return theEditSet;
308         }
309 
310         @Override
311         protected MoneyWiseLoanCategoryList getEmptyList(final PrometheusListStyle pStyle) {
312             final MoneyWiseLoanCategoryList myList = new MoneyWiseLoanCategoryList(this);
313             myList.setStyle(pStyle);
314             return myList;
315         }
316 
317         /**
318          * Derive Edit list.
319          *
320          * @param pEditSet the editSet
321          * @return the edit list
322          * @throws OceanusException on error
323          */
324         public MoneyWiseLoanCategoryList deriveEditList(final PrometheusEditSet pEditSet) throws OceanusException {
325             /* Build an empty List */
326             final MoneyWiseLoanCategoryList myList = getEmptyList(PrometheusListStyle.EDIT);
327             myList.ensureMap();
328             pEditSet.setEditEntryList(MoneyWiseBasicDataType.LOANCATEGORY, myList);
329             myList.getValidator().setEditSet(pEditSet);
330 
331             /* Store the editSet */
332             myList.theEditSet = pEditSet;
333 
334             /* Loop through the categories */
335             final Iterator<MoneyWiseLoanCategory> myIterator = iterator();
336             while (myIterator.hasNext()) {
337                 final MoneyWiseLoanCategory myCurr = myIterator.next();
338 
339                 /* Ignore deleted events */
340                 if (myCurr.isDeleted()) {
341                     continue;
342                 }
343 
344                 /* Build the new linked category and add it to the list */
345                 final MoneyWiseLoanCategory myCategory = new MoneyWiseLoanCategory(myList, myCurr);
346                 myList.add(myCategory);
347                 myCategory.resolveEditSetLinks();
348 
349                 /* Adjust the map */
350                 myCategory.adjustMapForItem();
351             }
352 
353             /* Return the list */
354             return myList;
355         }
356 
357         /**
358          * Add a new item to the core list.
359          *
360          * @param pCategory item
361          * @return the newly added item
362          */
363         @Override
364         public MoneyWiseLoanCategory addCopyItem(final PrometheusDataItem pCategory) {
365             /* Can only clone a LoanCategory */
366             if (!(pCategory instanceof MoneyWiseLoanCategory)) {
367                 throw new UnsupportedOperationException();
368             }
369 
370             final MoneyWiseLoanCategory myCategory = new MoneyWiseLoanCategory(this, (MoneyWiseLoanCategory) pCategory);
371             add(myCategory);
372             return myCategory;
373         }
374 
375         /**
376          * Add a new item to the edit list.
377          *
378          * @return the new item
379          */
380         @Override
381         public MoneyWiseLoanCategory addNewItem() {
382             final MoneyWiseLoanCategory myCategory = new MoneyWiseLoanCategory(this);
383             add(myCategory);
384             return myCategory;
385         }
386 
387         @Override
388         public MoneyWiseLoanCategory addValuesItem(final PrometheusDataValues pValues) throws OceanusException {
389             /* Create the category */
390             final MoneyWiseLoanCategory myCategory = new MoneyWiseLoanCategory(this, pValues);
391 
392             /* Check that this CategoryId has not been previously added */
393             if (!isIdUnique(myCategory.getIndexedId())) {
394                 myCategory.addError(ERROR_DUPLICATE, MetisDataResource.DATA_ID);
395                 throw new MoneyWiseDataException(myCategory, ERROR_VALIDATION);
396             }
397 
398             /* Add to the list */
399             add(myCategory);
400 
401             /* Return it */
402             return myCategory;
403         }
404     }
405 }