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