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.MetisDataDifference;
20  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataFieldId;
21  import io.github.tonywasher.joceanus.metis.data.MetisDataResource;
22  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
23  import io.github.tonywasher.joceanus.metis.field.MetisFieldVersionedSet;
24  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAssetCategory;
25  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseLoanCategoryClass;
26  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseLoanCategoryType;
27  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseLoanCategoryType.MoneyWiseLoanCategoryTypeList;
28  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseStaticDataType;
29  import io.github.tonywasher.joceanus.moneywise.exc.MoneyWiseDataException;
30  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
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.PrometheusDataSet;
34  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataValues;
35  import io.github.tonywasher.joceanus.prometheus.data.PrometheusListStyle;
36  import io.github.tonywasher.joceanus.prometheus.data.PrometheusStaticDataItem;
37  import io.github.tonywasher.joceanus.prometheus.views.PrometheusEditSet;
38  
39  import java.util.Iterator;
40  
41  /**
42   * Loan Category class.
43   */
44  public final class MoneyWiseLoanCategory
45          extends MoneyWiseCategoryBase
46          implements MoneyWiseAssetCategory {
47      /**
48       * Object name.
49       */
50      public static final String OBJECT_NAME = MoneyWiseBasicDataType.LOANCATEGORY.getItemName();
51  
52      /**
53       * List name.
54       */
55      public static final String LIST_NAME = MoneyWiseBasicDataType.LOANCATEGORY.getListName();
56  
57      /**
58       * Local Report fields.
59       */
60      private static final MetisFieldVersionedSet<MoneyWiseLoanCategory> FIELD_DEFS = MetisFieldVersionedSet.newVersionedFieldSet(MoneyWiseLoanCategory.class);
61  
62      /*
63       * FieldIds.
64       */
65      static {
66          FIELD_DEFS.declareLinkField(MoneyWiseStaticDataType.LOANTYPE);
67      }
68  
69      /**
70       * Copy Constructor.
71       *
72       * @param pList     the list
73       * @param pCategory The Category to copy
74       */
75      MoneyWiseLoanCategory(final MoneyWiseLoanCategoryList pList,
76                            final MoneyWiseLoanCategory pCategory) {
77          /* Set standard values */
78          super(pList, pCategory);
79      }
80  
81      /**
82       * Values constructor.
83       *
84       * @param pList   the List to add to
85       * @param pValues the values constructor
86       * @throws OceanusException on error
87       */
88      private MoneyWiseLoanCategory(final MoneyWiseLoanCategoryList pList,
89                                    final PrometheusDataValues pValues) throws OceanusException {
90          /* Initialise the item */
91          super(pList, pValues);
92  
93          /* Store the Category Type */
94          final Object myValue = pValues.getValue(MoneyWiseStaticDataType.LOANTYPE);
95          if (myValue instanceof Integer i) {
96              setValueType(i);
97          } else if (myValue instanceof String s) {
98              setValueType(s);
99          }
100     }
101 
102     /**
103      * Edit Constructor.
104      *
105      * @param pList the list
106      */
107     public MoneyWiseLoanCategory(final MoneyWiseLoanCategoryList pList) {
108         super(pList);
109     }
110 
111     @Override
112     public MetisFieldSetDef getDataFieldSet() {
113         return FIELD_DEFS;
114     }
115 
116     @Override
117     public boolean includeXmlField(final MetisDataFieldId pField) {
118         /* Determine whether fields should be included */
119         if (MoneyWiseStaticDataType.LOANTYPE.equals(pField)) {
120             return true;
121         }
122 
123         /* Pass call on */
124         return super.includeXmlField(pField);
125     }
126 
127     @Override
128     public MoneyWiseLoanCategoryType getCategoryType() {
129         return getValues().getValue(MoneyWiseStaticDataType.LOANTYPE, MoneyWiseLoanCategoryType.class);
130     }
131 
132     @Override
133     public MoneyWiseLoanCategoryClass getCategoryTypeClass() {
134         final MoneyWiseLoanCategoryType myType = getCategoryType();
135         return myType == null
136                 ? null
137                 : myType.getLoanClass();
138     }
139 
140     @Override
141     public MoneyWiseLoanCategory getParentCategory() {
142         return getValues().getValue(PrometheusDataResource.DATAGROUP_PARENT, MoneyWiseLoanCategory.class);
143     }
144 
145 
146     /**
147      * Set account type value.
148      *
149      * @param pValue the value
150      */
151     private void setValueType(final MoneyWiseLoanCategoryType pValue) {
152         getValues().setUncheckedValue(MoneyWiseStaticDataType.LOANTYPE, pValue);
153     }
154 
155     /**
156      * Set account type id.
157      *
158      * @param pValue the value
159      */
160     private void setValueType(final Integer pValue) {
161         getValues().setUncheckedValue(MoneyWiseStaticDataType.LOANTYPE, pValue);
162     }
163 
164     /**
165      * Set account type name.
166      *
167      * @param pValue the value
168      */
169     private void setValueType(final String pValue) {
170         getValues().setUncheckedValue(MoneyWiseStaticDataType.LOANTYPE, pValue);
171     }
172 
173     /**
174      * Is this loan category the required class.
175      *
176      * @param pClass the required category class.
177      * @return true/false
178      */
179     public boolean isCategoryClass(final MoneyWiseLoanCategoryClass pClass) {
180         /* Check for match */
181         return getCategoryTypeClass() == pClass;
182     }
183 
184     @Override
185     public MoneyWiseLoanCategoryList getList() {
186         return (MoneyWiseLoanCategoryList) super.getList();
187     }
188 
189     /**
190      * Set defaults.
191      *
192      * @param pParent the parent
193      * @throws OceanusException on error
194      */
195     public void setDefaults(final MoneyWiseLoanCategory pParent) throws OceanusException {
196         getList().getValidator().setDefaults(pParent, this);
197     }
198 
199     @Override
200     public void resolveDataSetLinks() throws OceanusException {
201         /* Update the Underlying details */
202         super.resolveDataSetLinks();
203 
204         /* Resolve category type and parent */
205         resolveDataLink(MoneyWiseStaticDataType.LOANTYPE, MoneyWiseStaticDataType.LOANTYPE);
206     }
207 
208     @Override
209     protected void resolveEditSetLinks() throws OceanusException {
210         /* Resolve parent within list */
211         resolveDataLink(PrometheusDataResource.DATAGROUP_PARENT, getList());
212 
213         /* Resolve StaticType if required */
214         final PrometheusEditSet myEditSet = getList().getEditSet();
215         if (myEditSet.hasDataType(MoneyWiseStaticDataType.LOANTYPE)) {
216             resolveDataLink(MoneyWiseStaticDataType.LOANTYPE, myEditSet.getDataList(MoneyWiseStaticDataType.LOANTYPE, MoneyWiseLoanCategoryTypeList.class));
217         }
218     }
219 
220     @Override
221     public void setCategoryType(final PrometheusStaticDataItem pType) {
222         setValueType((MoneyWiseLoanCategoryType) pType);
223     }
224 
225     /**
226      * Update base category from an edited category.
227      *
228      * @param pCategory the edited category
229      * @return whether changes have been made
230      */
231     @Override
232     public boolean applyChanges(final PrometheusDataItem pCategory) {
233         /* Can only update from a loan category */
234         if (!(pCategory instanceof MoneyWiseLoanCategory myCategory)) {
235             return false;
236         }
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 PrometheusDataSet 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 }