1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.MetisDataItem.MetisDataNamedItem;
22 import io.github.tonywasher.joceanus.metis.data.MetisDataResource;
23 import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
24 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDataValidator.MoneyWiseDataValidatorDefaults;
25 import io.github.tonywasher.joceanus.moneywise.exc.MoneyWiseDataException;
26 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
27 import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
28 import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataInstanceMap;
29 import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
30 import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataResource;
31 import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataValues;
32 import io.github.tonywasher.joceanus.prometheus.data.PrometheusEncryptedDataItem;
33 import io.github.tonywasher.joceanus.prometheus.data.PrometheusEncryptedFieldSet;
34 import io.github.tonywasher.joceanus.prometheus.data.PrometheusEncryptedPair;
35 import io.github.tonywasher.joceanus.prometheus.views.PrometheusEditSet;
36
37 import java.util.Iterator;
38
39
40
41
42 public class MoneyWiseRegion
43 extends PrometheusEncryptedDataItem
44 implements MetisDataNamedItem {
45
46
47
48 public static final String OBJECT_NAME = MoneyWiseBasicDataType.REGION.getItemName();
49
50
51
52
53 public static final String LIST_NAME = MoneyWiseBasicDataType.REGION.getListName();
54
55
56
57
58 private static final PrometheusEncryptedFieldSet<MoneyWiseRegion> FIELD_DEFS = PrometheusEncryptedFieldSet.newEncryptedFieldSet(MoneyWiseRegion.class);
59
60
61
62
63 static {
64 FIELD_DEFS.declareEncryptedStringField(PrometheusDataResource.DATAITEM_FIELD_NAME, NAMELEN);
65 FIELD_DEFS.declareEncryptedStringField(PrometheusDataResource.DATAITEM_FIELD_DESC, DESCLEN);
66 }
67
68
69
70
71
72
73
74 protected MoneyWiseRegion(final MoneyWiseRegionList pList,
75 final MoneyWiseRegion pRegion) {
76
77 super(pList, pRegion);
78 }
79
80
81
82
83
84
85
86
87 private MoneyWiseRegion(final MoneyWiseRegionList pList,
88 final PrometheusDataValues pValues) throws OceanusException {
89
90 super(pList, pValues);
91
92
93 try {
94
95 Object myValue = pValues.getValue(PrometheusDataResource.DATAITEM_FIELD_NAME);
96 if (myValue instanceof String s) {
97 setValueName(s);
98 } else if (myValue instanceof byte[] ba) {
99 setValueName(ba);
100 }
101
102
103 myValue = pValues.getValue(PrometheusDataResource.DATAITEM_FIELD_DESC);
104 if (myValue instanceof String s) {
105 setValueDesc(s);
106 } else if (myValue instanceof byte[] ba) {
107 setValueDesc(ba);
108 }
109
110
111 } catch (OceanusException e) {
112
113 throw new MoneyWiseDataException(this, ERROR_CREATEITEM, e);
114 }
115 }
116
117
118
119
120
121
122 public MoneyWiseRegion(final MoneyWiseRegionList pList) {
123 super(pList, 0);
124 setNextDataKeySet();
125 }
126
127 @Override
128 public MetisFieldSetDef getDataFieldSet() {
129 return FIELD_DEFS;
130 }
131
132 @Override
133 public String formatObject(final OceanusDataFormatter pFormatter) {
134 return toString();
135 }
136
137 @Override
138 public String toString() {
139 return getName();
140 }
141
142 @Override
143 public boolean includeXmlField(final MetisDataFieldId pField) {
144
145 if (PrometheusDataResource.DATAITEM_FIELD_NAME.equals(pField)) {
146 return true;
147 }
148 if (PrometheusDataResource.DATAITEM_FIELD_DESC.equals(pField)) {
149 return getDesc() != null;
150 }
151
152
153 return super.includeXmlField(pField);
154 }
155
156 @Override
157 public String getName() {
158 return getValues().getValue(PrometheusDataResource.DATAITEM_FIELD_NAME, String.class);
159 }
160
161
162
163
164
165
166 public byte[] getNameBytes() {
167 return getValues().getEncryptedBytes(PrometheusDataResource.DATAITEM_FIELD_NAME);
168 }
169
170
171
172
173
174
175 private PrometheusEncryptedPair getNameField() {
176 return getValues().getEncryptedPair(PrometheusDataResource.DATAITEM_FIELD_NAME);
177 }
178
179
180
181
182
183
184 public String getDesc() {
185 return getValues().getValue(PrometheusDataResource.DATAITEM_FIELD_DESC, String.class);
186 }
187
188
189
190
191
192
193 public byte[] getDescBytes() {
194 return getValues().getEncryptedBytes(PrometheusDataResource.DATAITEM_FIELD_DESC);
195 }
196
197
198
199
200
201
202 private PrometheusEncryptedPair getDescField() {
203 return getValues().getEncryptedPair(PrometheusDataResource.DATAITEM_FIELD_DESC);
204 }
205
206
207
208
209
210
211
212 private void setValueName(final String pValue) throws OceanusException {
213 setEncryptedValue(PrometheusDataResource.DATAITEM_FIELD_NAME, pValue);
214 }
215
216
217
218
219
220
221
222 private void setValueName(final byte[] pBytes) throws OceanusException {
223 setEncryptedValue(PrometheusDataResource.DATAITEM_FIELD_NAME, pBytes, String.class);
224 }
225
226
227
228
229
230
231 private void setValueName(final PrometheusEncryptedPair pValue) {
232 getValues().setUncheckedValue(PrometheusDataResource.DATAITEM_FIELD_NAME, pValue);
233 }
234
235
236
237
238
239
240
241 private void setValueDesc(final String pValue) throws OceanusException {
242 setEncryptedValue(PrometheusDataResource.DATAITEM_FIELD_DESC, pValue);
243 }
244
245
246
247
248
249
250
251 private void setValueDesc(final byte[] pBytes) throws OceanusException {
252 setEncryptedValue(PrometheusDataResource.DATAITEM_FIELD_DESC, pBytes, String.class);
253 }
254
255
256
257
258
259
260 private void setValueDesc(final PrometheusEncryptedPair pValue) {
261 getValues().setUncheckedValue(PrometheusDataResource.DATAITEM_FIELD_DESC, pValue);
262 }
263
264 @Override
265 public MoneyWiseDataSet getDataSet() {
266 return (MoneyWiseDataSet) super.getDataSet();
267 }
268
269 @Override
270 public MoneyWiseRegion getBase() {
271 return (MoneyWiseRegion) super.getBase();
272 }
273
274 @Override
275 public MoneyWiseRegionList getList() {
276 return (MoneyWiseRegionList) super.getList();
277 }
278
279
280
281
282
283
284 public void setDefaults() throws OceanusException {
285 getList().getValidator().setDefaults(this);
286 }
287
288 @Override
289 public int compareValues(final PrometheusDataItem pThat) {
290
291 final MoneyWiseRegion myThat = (MoneyWiseRegion) pThat;
292 return MetisDataDifference.compareObject(getName(), myThat.getName());
293 }
294
295
296
297
298
299
300
301 public void setName(final String pName) throws OceanusException {
302 setValueName(pName);
303 }
304
305
306
307
308
309
310
311 public void setDescription(final String pDesc) throws OceanusException {
312 setValueDesc(pDesc);
313 }
314
315
316
317
318
319
320
321 @Override
322 public boolean applyChanges(final PrometheusDataItem pTag) {
323
324 if (!(pTag instanceof MoneyWiseRegion myRegion)) {
325 return false;
326 }
327
328
329 pushHistory();
330
331
332 if (!MetisDataDifference.isEqual(getName(), myRegion.getName())) {
333 setValueName(myRegion.getNameField());
334 }
335
336
337 if (!MetisDataDifference.isEqual(getDesc(), myRegion.getDesc())) {
338 setValueDesc(myRegion.getDescField());
339 }
340
341
342 return checkForHistory();
343 }
344
345 @Override
346 public void adjustMapForItem() {
347 final MoneyWiseRegionList myList = getList();
348 final MoneyWiseRegionDataMap myMap = myList.getDataMap();
349 myMap.adjustForItem(this);
350 }
351
352
353
354
355 public static class MoneyWiseRegionList
356 extends PrometheusEncryptedList<MoneyWiseRegion> {
357
358
359
360 private static final MetisFieldSet<MoneyWiseRegionList> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseRegionList.class);
361
362
363
364
365
366
367 protected MoneyWiseRegionList(final MoneyWiseDataSet pData) {
368 super(MoneyWiseRegion.class, pData, MoneyWiseBasicDataType.REGION, PrometheusListStyle.CORE);
369 }
370
371
372
373
374
375
376 protected MoneyWiseRegionList(final MoneyWiseRegionList pSource) {
377 super(pSource);
378 }
379
380 @Override
381 public MetisFieldSet<MoneyWiseRegionList> getDataFieldSet() {
382 return FIELD_DEFS;
383 }
384
385 @Override
386 public String listName() {
387 return LIST_NAME;
388 }
389
390 @Override
391 public MetisFieldSetDef getItemFields() {
392 return MoneyWiseRegion.FIELD_DEFS;
393 }
394
395 @Override
396 public MoneyWiseDataSet getDataSet() {
397 return (MoneyWiseDataSet) super.getDataSet();
398 }
399
400 @Override
401 public MoneyWiseRegionDataMap getDataMap() {
402 return (MoneyWiseRegionDataMap) super.getDataMap();
403 }
404
405 @Override
406 @SuppressWarnings("unchecked")
407 public MoneyWiseDataValidatorDefaults<MoneyWiseRegion> getValidator() {
408 return (MoneyWiseDataValidatorDefaults<MoneyWiseRegion>) super.getValidator();
409 }
410
411 @Override
412 protected MoneyWiseRegionList getEmptyList(final PrometheusListStyle pStyle) {
413 final MoneyWiseRegionList myList = new MoneyWiseRegionList(this);
414 myList.setStyle(pStyle);
415 return myList;
416 }
417
418
419
420
421
422
423
424 public MoneyWiseRegionList deriveEditList(final PrometheusEditSet pEditSet) {
425
426 final MoneyWiseRegionList myList = getEmptyList(PrometheusListStyle.EDIT);
427 myList.ensureMap();
428 pEditSet.setEditEntryList(MoneyWiseBasicDataType.REGION, myList);
429 myList.getValidator().setEditSet(pEditSet);
430
431
432 final Iterator<MoneyWiseRegion> myIterator = iterator();
433 while (myIterator.hasNext()) {
434 final MoneyWiseRegion myCurr = myIterator.next();
435
436
437 if (myCurr.isDeleted()) {
438 continue;
439 }
440
441
442 final MoneyWiseRegion myRegion = new MoneyWiseRegion(myList, myCurr);
443 myList.add(myRegion);
444
445
446 myRegion.adjustMapForItem();
447 }
448
449
450 return myList;
451 }
452
453
454
455
456
457
458
459 @Override
460 public MoneyWiseRegion addCopyItem(final PrometheusDataItem pRegion) {
461
462 if (!(pRegion instanceof MoneyWiseRegion)) {
463 throw new UnsupportedOperationException();
464 }
465
466 final MoneyWiseRegion myRegion = new MoneyWiseRegion(this, (MoneyWiseRegion) pRegion);
467 add(myRegion);
468 return myRegion;
469 }
470
471
472
473
474
475
476 @Override
477 public MoneyWiseRegion addNewItem() {
478 final MoneyWiseRegion myRegion = new MoneyWiseRegion(this);
479 add(myRegion);
480 return myRegion;
481 }
482
483 @Override
484 public MoneyWiseRegion findItemByName(final String pName) {
485
486 return getDataMap().findItemByName(pName);
487 }
488
489 @Override
490 public MoneyWiseRegion addValuesItem(final PrometheusDataValues pValues) throws OceanusException {
491
492 final MoneyWiseRegion myRegion = new MoneyWiseRegion(this, pValues);
493
494
495 if (!isIdUnique(myRegion.getIndexedId())) {
496 myRegion.addError(ERROR_DUPLICATE, MetisDataResource.DATA_ID);
497 throw new MoneyWiseDataException(myRegion, ERROR_VALIDATION);
498 }
499
500
501 add(myRegion);
502
503
504 return myRegion;
505 }
506
507 @Override
508 protected MoneyWiseRegionDataMap allocateDataMap() {
509 return new MoneyWiseRegionDataMap();
510 }
511 }
512
513
514
515
516 public static class MoneyWiseRegionDataMap
517 extends PrometheusDataInstanceMap<MoneyWiseRegion, String> {
518 @Override
519 public void adjustForItem(final PrometheusDataItem pItem) {
520
521 final MoneyWiseRegion myItem = (MoneyWiseRegion) pItem;
522
523
524 adjustForItem(myItem, myItem.getName());
525 }
526
527
528
529
530
531
532
533 public MoneyWiseRegion findItemByName(final String pName) {
534 return findItemByKey(pName);
535 }
536
537
538
539
540
541
542
543 public boolean validNameCount(final String pName) {
544 return validKeyCount(pName);
545 }
546
547
548
549
550
551
552
553 public boolean availableName(final String pName) {
554 return availableKey(pName);
555 }
556 }
557 }