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.MetisDataResource;
22  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
23  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseLoan.MoneyWiseLoanList;
24  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAccountInfoClass;
25  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAccountInfoType;
26  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAccountInfoType.MoneyWiseAccountInfoTypeList;
27  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseStaticDataType;
28  import io.github.tonywasher.joceanus.moneywise.exc.MoneyWiseDataException;
29  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataInfoClass;
30  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataInfoItem;
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  /**
38   * Representation of an information extension of a loan.
39   *
40   * @author Tony Washer
41   */
42  public class MoneyWiseLoanInfo
43          extends PrometheusDataInfoItem {
44      /**
45       * Object name.
46       */
47      public static final String OBJECT_NAME = MoneyWiseBasicDataType.LOANINFO.getItemName();
48  
49      /**
50       * List name.
51       */
52      public static final String LIST_NAME = MoneyWiseBasicDataType.LOANINFO.getListName();
53  
54      /**
55       * Report fields.
56       */
57      private static final MetisFieldSet<MoneyWiseLoanInfo> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseLoanInfo.class);
58  
59      /**
60       * Copy Constructor.
61       *
62       * @param pList the list
63       * @param pInfo The Info to copy
64       */
65      protected MoneyWiseLoanInfo(final MoneyWiseLoanInfoList pList,
66                                  final MoneyWiseLoanInfo pInfo) {
67          /* Set standard values */
68          super(pList, pInfo);
69      }
70  
71      /**
72       * Edit Constructor.
73       *
74       * @param pList the list
75       * @param pLoan the loan
76       * @param pType the type
77       */
78      private MoneyWiseLoanInfo(final MoneyWiseLoanInfoList pList,
79                                final MoneyWiseLoan pLoan,
80                                final MoneyWiseAccountInfoType pType) {
81          /* Initialise the item */
82          super(pList);
83          setNextDataKeySet();
84  
85          /* Record the Detail */
86          setValueInfoType(pType);
87          setValueOwner(pLoan);
88      }
89  
90      /**
91       * Values constructor.
92       *
93       * @param pList   the List to add to
94       * @param pValues the values constructor
95       * @throws OceanusException on error
96       */
97      private MoneyWiseLoanInfo(final MoneyWiseLoanInfoList pList,
98                                final PrometheusDataValues pValues) throws OceanusException {
99          /* Initialise the item */
100         super(pList, pValues);
101 
102         /* Protect against exceptions */
103         try {
104             /* Resolve links */
105             final MoneyWiseDataSet myData = getDataSet();
106             resolveDataLink(PrometheusDataResource.DATAINFO_TYPE, myData.getActInfoTypes());
107             resolveDataLink(PrometheusDataResource.DATAINFO_OWNER, myData.getLoans());
108 
109             /* Set the value */
110             setValue(pValues.getValue(PrometheusDataResource.DATAINFO_VALUE));
111 
112             /* Access the LoanInfoSet and register this data */
113             final MoneyWiseLoanInfoSet mySet = getOwner().getInfoSet();
114             mySet.registerInfo(this);
115 
116         } catch (OceanusException e) {
117             /* Pass on exception */
118             throw new MoneyWiseDataException(this, ERROR_CREATEITEM, e);
119         }
120     }
121 
122     @Override
123     public MetisFieldSetDef getDataFieldSet() {
124         return FIELD_DEFS;
125     }
126 
127     @Override
128     public MoneyWiseAccountInfoType getInfoType() {
129         return getValues().getValue(PrometheusDataResource.DATAINFO_TYPE, MoneyWiseAccountInfoType.class);
130     }
131 
132     @Override
133     public MoneyWiseAccountInfoClass getInfoClass() {
134         return getInfoType().getInfoClass();
135     }
136 
137     @Override
138     public MoneyWiseLoan getOwner() {
139         return getValues().getValue(PrometheusDataResource.DATAINFO_OWNER, MoneyWiseLoan.class);
140     }
141 
142     @Override
143     public MoneyWiseDataSet getDataSet() {
144         return (MoneyWiseDataSet) super.getDataSet();
145     }
146 
147     @Override
148     public MoneyWiseLoanInfo getBase() {
149         return (MoneyWiseLoanInfo) super.getBase();
150     }
151 
152     @Override
153     public MoneyWiseLoanInfoList getList() {
154         return (MoneyWiseLoanInfoList) super.getList();
155     }
156 
157     @Override
158     public void deRegister() {
159         /* Access the LoanInfoSet and register this value */
160         final MoneyWiseLoanInfoSet mySet = getOwner().getInfoSet();
161         mySet.deRegisterInfo(this);
162     }
163 
164     @Override
165     public void resolveDataSetLinks() throws OceanusException {
166         /* Update the Encryption details */
167         super.resolveDataSetLinks();
168 
169         /* Resolve data links */
170         final MoneyWiseDataSet myData = getDataSet();
171         resolveDataLink(PrometheusDataResource.DATAINFO_TYPE, myData.getActInfoTypes());
172         resolveDataLink(PrometheusDataResource.DATAINFO_OWNER, myData.getLoans());
173 
174         /* Access the LoanInfoSet and register this data */
175         final MoneyWiseLoanInfoSet mySet = getOwner().getInfoSet();
176         mySet.registerInfo(this);
177     }
178 
179     /**
180      * Resolve editSet links.
181      *
182      * @param pEditSet the editSet
183      * @throws OceanusException on error
184      */
185     public void resolveEditSetLinks(final PrometheusEditSet pEditSet) throws OceanusException {
186         /* Resolve data links */
187         resolveDataLink(PrometheusDataResource.DATAINFO_TYPE, pEditSet.getDataList(MoneyWiseStaticDataType.ACCOUNTINFOTYPE, MoneyWiseAccountInfoTypeList.class));
188         resolveDataLink(PrometheusDataResource.DATAINFO_OWNER, pEditSet.getDataList(MoneyWiseBasicDataType.LOAN, MoneyWiseLoanList.class));
189     }
190 
191     /**
192      * Update depositInfo from a depositInfo extract.
193      *
194      * @param pInfo the changed depositInfo
195      * @return whether changes have been made
196      */
197     @Override
198     public boolean applyChanges(final PrometheusDataItem pInfo) {
199         /* Can only update from LoanInfo */
200         if (!(pInfo instanceof MoneyWiseLoanInfo)) {
201             return false;
202         }
203 
204         /* Access as LoanInfo */
205         final MoneyWiseLoanInfo myLoanInfo = (MoneyWiseLoanInfo) pInfo;
206 
207         /* Store the current detail into history */
208         pushHistory();
209 
210         /* Update the value if required */
211         if (!MetisDataDifference.isEqual(getField(), myLoanInfo.getField())) {
212             setValueValue(myLoanInfo.getField());
213         }
214 
215         /* Check for changes */
216         return checkForHistory();
217     }
218 
219     /**
220      * LoanInfoList.
221      */
222     public static class MoneyWiseLoanInfoList
223             extends PrometheusDataInfoList<MoneyWiseLoanInfo> {
224         /**
225          * Report fields.
226          */
227         private static final MetisFieldSet<MoneyWiseLoanInfoList> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseLoanInfoList.class);
228 
229         /**
230          * Construct an empty CORE list.
231          *
232          * @param pData the DataSet for the list
233          */
234         protected MoneyWiseLoanInfoList(final MoneyWiseDataSet pData) {
235             super(MoneyWiseLoanInfo.class, pData, MoneyWiseBasicDataType.LOANINFO, PrometheusListStyle.CORE);
236         }
237 
238         /**
239          * Constructor for a cloned List.
240          *
241          * @param pSource the source List
242          */
243         private MoneyWiseLoanInfoList(final MoneyWiseLoanInfoList pSource) {
244             super(pSource);
245         }
246 
247         @Override
248         public MetisFieldSet<MoneyWiseLoanInfoList> getDataFieldSet() {
249             return FIELD_DEFS;
250         }
251 
252         @Override
253         public String listName() {
254             return LIST_NAME;
255         }
256 
257         @Override
258         public MetisFieldSetDef getItemFields() {
259             return MoneyWiseLoanInfo.FIELD_DEFS;
260         }
261 
262         @Override
263         public MoneyWiseDataSet getDataSet() {
264             return (MoneyWiseDataSet) super.getDataSet();
265         }
266 
267         /**
268          * Set base list for Edit InfoList.
269          *
270          * @param pBase the base list
271          */
272         protected void setBase(final MoneyWiseLoanInfoList pBase) {
273             /* Set the style and base */
274             setStyle(PrometheusListStyle.EDIT);
275             super.setBase(pBase);
276         }
277 
278         @Override
279         protected MoneyWiseLoanInfoList getEmptyList(final PrometheusListStyle pStyle) {
280             final MoneyWiseLoanInfoList myList = new MoneyWiseLoanInfoList(this);
281             myList.setStyle(pStyle);
282             return myList;
283         }
284 
285         @Override
286         public MoneyWiseLoanInfo addCopyItem(final PrometheusDataItem pItem) {
287             /* Can only clone a LoanInfo */
288             if (!(pItem instanceof MoneyWiseLoanInfo)) {
289                 throw new UnsupportedOperationException();
290             }
291 
292             final MoneyWiseLoanInfo myInfo = new MoneyWiseLoanInfo(this, (MoneyWiseLoanInfo) pItem);
293             add(myInfo);
294             return myInfo;
295         }
296 
297         @Override
298         public MoneyWiseLoanInfo addNewItem() {
299             throw new UnsupportedOperationException();
300         }
301 
302         @Override
303         protected MoneyWiseLoanInfo addNewItem(final PrometheusDataItem pOwner,
304                                                final PrometheusStaticDataItem pInfoType) {
305             /* Allocate the new entry and add to list */
306             final MoneyWiseLoanInfo myInfo = new MoneyWiseLoanInfo(this, (MoneyWiseLoan) pOwner, (MoneyWiseAccountInfoType) pInfoType);
307             add(myInfo);
308 
309             /* return it */
310             return myInfo;
311         }
312 
313         @Override
314         public void addInfoItem(final Integer pId,
315                                 final PrometheusDataItem pLoan,
316                                 final PrometheusDataInfoClass pInfoClass,
317                                 final Object pValue) throws OceanusException {
318             /* Ignore item if it is null */
319             if (pValue == null) {
320                 return;
321             }
322 
323             /* Access the data set */
324             final MoneyWiseDataSet myData = getDataSet();
325 
326             /* Look up the Info Type */
327             final MoneyWiseAccountInfoType myInfoType = myData.getActInfoTypes().findItemByClass(pInfoClass);
328             if (myInfoType == null) {
329                 throw new MoneyWiseDataException(pLoan, ERROR_BADINFOCLASS + " [" + pInfoClass + "]");
330             }
331 
332             /* Create the values */
333             final PrometheusDataValues myValues = new PrometheusDataValues(OBJECT_NAME);
334             myValues.addValue(MetisDataResource.DATA_ID, pId);
335             myValues.addValue(PrometheusDataResource.DATAINFO_TYPE, myInfoType);
336             myValues.addValue(PrometheusDataResource.DATAINFO_OWNER, pLoan);
337             myValues.addValue(PrometheusDataResource.DATAINFO_VALUE, pValue);
338 
339             /* Create a new Loan Info */
340             final MoneyWiseLoanInfo myInfo = new MoneyWiseLoanInfo(this, myValues);
341 
342             /* Check that this InfoTypeId has not been previously added */
343             if (!isIdUnique(pId)) {
344                 myInfo.addError(ERROR_DUPLICATE, MetisDataResource.DATA_ID);
345                 throw new MoneyWiseDataException(myInfo, ERROR_VALIDATION);
346             }
347 
348             /* Add the Info to the list */
349             add(myInfo);
350         }
351 
352         @Override
353         public MoneyWiseLoanInfo addValuesItem(final PrometheusDataValues pValues) throws OceanusException {
354             /* Create the info */
355             final MoneyWiseLoanInfo myInfo = new MoneyWiseLoanInfo(this, pValues);
356 
357             /* Check that this InfoId has not been previously added */
358             if (!isIdUnique(myInfo.getIndexedId())) {
359                 myInfo.addError(ERROR_DUPLICATE, MetisDataResource.DATA_ID);
360                 throw new MoneyWiseDataException(myInfo, ERROR_VALIDATION);
361             }
362 
363             /* Add to the list */
364             add(myInfo);
365 
366             /* Return it */
367             return myInfo;
368         }
369 
370         @Override
371         public void postProcessOnLoad() throws OceanusException {
372             /* Validate the LoanInfo */
373             validateOnLoad();
374 
375             /* Map and Validate the Loans */
376             final MoneyWiseLoanList myLoans = getDataSet().getLoans();
377             myLoans.mapData();
378             myLoans.validateOnLoad();
379         }
380     }
381 }