1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.prometheus.data;
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.field.MetisFieldSet;
22 import io.github.tonywasher.joceanus.metis.field.MetisFieldVersionedSet;
23 import io.github.tonywasher.joceanus.metis.list.MetisListKey;
24 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
25 import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
26 import io.github.tonywasher.joceanus.prometheus.data.PrometheusCryptographyData.PrometheusControlDataCtl;
27 import io.github.tonywasher.joceanus.prometheus.data.PrometheusCryptographyData.PrometheusCryptographicDataSet;
28 import io.github.tonywasher.joceanus.prometheus.data.PrometheusData.PrometheusDataSetCtl;
29 import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataKeySet.PrometheusDataKeySetList;
30 import io.github.tonywasher.joceanus.prometheus.data.PrometheusEncrypted.PrometheusEncryptedDataItemCtl;
31 import io.github.tonywasher.joceanus.tethys.api.thread.TethysUIThreadStatusReport;
32
33 import java.util.Iterator;
34
35
36
37
38
39
40 public abstract class PrometheusEncryptedDataItem
41 extends PrometheusDataItem
42 implements PrometheusEncryptedDataItemCtl {
43
44
45
46 private static final PrometheusEncryptor NULL_ENCRYPTOR = new PrometheusEncryptor(new OceanusDataFormatter(), null);
47
48
49
50
51 private static final MetisFieldVersionedSet<PrometheusEncryptedDataItem> FIELD_DEFS = MetisFieldVersionedSet.newVersionedFieldSet(PrometheusEncryptedDataItem.class);
52
53
54
55
56 static {
57 FIELD_DEFS.declareLinkField(PrometheusCryptographyDataType.DATAKEYSET);
58 }
59
60
61
62
63
64
65
66
67 protected PrometheusEncryptedDataItem(final PrometheusEncryptedList<?> pList,
68 final Integer pId) {
69 super(pList, pId);
70 }
71
72
73
74
75
76
77
78 protected PrometheusEncryptedDataItem(final PrometheusEncryptedList<?> pList,
79 final PrometheusEncryptedDataItem pSource) {
80 super(pList, pSource);
81 }
82
83
84
85
86
87
88
89
90
91 protected PrometheusEncryptedDataItem(final PrometheusEncryptedList<?> pList,
92 final PrometheusDataValues pValues) throws OceanusException {
93 super(pList, pValues);
94
95
96 final Integer myId = pValues.getValue(PrometheusCryptographyDataType.DATAKEYSET, Integer.class);
97 if (myId != null) {
98 setDataKeySet(myId);
99 }
100 }
101
102 @Override
103 protected PrometheusEncryptedValues newVersionValues() {
104 return new PrometheusEncryptedValues(this);
105 }
106
107 @Override
108 public PrometheusEncryptedValues getValues() {
109 return (PrometheusEncryptedValues) super.getValues();
110 }
111
112 @Override
113 public PrometheusEncryptedValues getOriginalValues() {
114 return (PrometheusEncryptedValues) super.getOriginalValues();
115 }
116
117
118
119
120
121
122 public final PrometheusDataKeySet getDataKeySet() {
123 return getValues().getValue(PrometheusCryptographyDataType.DATAKEYSET, PrometheusDataKeySet.class);
124 }
125
126
127
128
129
130
131 public final Integer getDataKeySetId() {
132 final PrometheusDataKeySet mySet = getDataKeySet();
133 return (mySet == null)
134 ? null
135 : mySet.getIndexedId();
136 }
137
138
139
140
141
142
143 private void setValueDataKeySet(final PrometheusDataKeySet pSet) {
144 getValues().setUncheckedValue(PrometheusCryptographyDataType.DATAKEYSET, pSet);
145 }
146
147
148
149
150
151
152 private void setValueDataKeySet(final Integer pId) {
153 getValues().setUncheckedValue(PrometheusCryptographyDataType.DATAKEYSET, pId);
154 }
155
156 @Override
157 public PrometheusEncryptor getEncryptor() {
158 final PrometheusDataKeySet myKeySet = getDataKeySet();
159 return myKeySet == null ? NULL_ENCRYPTOR : myKeySet.getEncryptor();
160 }
161
162 @Override
163 public PrometheusEncryptedList<?> getList() {
164 return (PrometheusEncryptedList<?>) super.getList();
165 }
166
167
168
169
170
171
172 protected final void setDataKeySet(final PrometheusDataKeySet pKeySet) {
173 setValueDataKeySet(pKeySet);
174 }
175
176
177
178
179 protected final void setNextDataKeySet() {
180 setDataKeySet(getList().getNextDataKeySet());
181 }
182
183
184
185
186
187
188
189 protected final void setDataKeySet(final Integer pKeySetId) throws OceanusException {
190
191 setValueDataKeySet(pKeySetId);
192
193
194 final PrometheusDataSetCtl myData = getDataSet();
195 resolveDataLink(PrometheusCryptographyDataType.DATAKEYSET, PrometheusCryptographyDataType.DATAKEYSET);
196 }
197
198
199
200
201
202
203
204
205 protected final void setEncryptedValue(final MetisDataFieldId pFieldId,
206 final Object pValue) throws OceanusException {
207
208 final MetisFieldDef myFieldDef = getDataFieldSet().getField(pFieldId);
209 final PrometheusEncryptedValues myValueSet = getValues();
210 Object myCurrent = myValueSet.getValue(myFieldDef);
211
212
213 if (myCurrent != null && !(myCurrent instanceof PrometheusEncryptedPair)) {
214 myCurrent = null;
215 }
216
217
218 final PrometheusEncryptedPair myCurr = (PrometheusEncryptedPair) myCurrent;
219 final PrometheusEncryptedPair myField = getEncryptor().encryptValue(myCurr, pValue);
220
221
222 myValueSet.setUncheckedValue(myFieldDef, myField);
223 }
224
225
226
227
228
229
230
231
232 protected final void setEncryptedValue(final MetisDataFieldId pFieldId,
233 final byte[] pEncrypted) throws OceanusException {
234
235 final MetisFieldDef myFieldDef = getDataFieldSet().getField(pFieldId);
236 final PrometheusEncryptedPair myField = getEncryptor().decryptValue(pEncrypted, myFieldDef);
237
238
239 getValues().setValue(myFieldDef, myField);
240 }
241
242
243
244
245
246
247
248
249
250 protected final void setEncryptedValue(final MetisDataFieldId pFieldId,
251 final byte[] pEncrypted,
252 final Class<?> pClazz) throws OceanusException {
253
254 final MetisFieldDef myFieldDef = getDataFieldSet().getField(pFieldId);
255 final PrometheusEncryptedPair myField = getEncryptor().decryptValue(pEncrypted, pClazz);
256
257
258 getValues().setUncheckedValue(myFieldDef, myField);
259 }
260
261
262
263
264
265
266
267
268 public static MetisDataDifference getDifference(final PrometheusEncryptedPair pCurr,
269 final PrometheusEncryptedPair pNew) {
270
271 if (pCurr == null) {
272 return (pNew != null)
273 ? MetisDataDifference.DIFFERENT
274 : MetisDataDifference.IDENTICAL;
275 }
276
277
278 if (pNew == null) {
279 return MetisDataDifference.DIFFERENT;
280 }
281
282
283 return pCurr.differs(pNew);
284 }
285
286 @Override
287 public void resolveDataSetLinks() throws OceanusException {
288
289 final PrometheusDataSetCtl myData = getDataSet();
290 resolveDataLink(PrometheusCryptographyDataType.DATAKEYSET, PrometheusCryptographyDataType.DATAKEYSET);
291 }
292
293
294
295
296
297
298
299
300 protected void adoptSecurity(final PrometheusDataKeySet pKeySet,
301 final PrometheusEncryptedDataItem pBase) throws OceanusException {
302
303 setValueDataKeySet(pKeySet);
304
305
306 final PrometheusEncryptedValues myBaseValues = pBase.getValues();
307
308
309 getValues().adoptSecurity(myBaseValues);
310 }
311
312
313
314
315
316
317
318 protected void initialiseSecurity(final PrometheusDataKeySet pKeySet) throws OceanusException {
319
320 setValueDataKeySet(pKeySet);
321
322
323 getValues().adoptSecurity(null);
324 }
325
326
327
328
329
330
331
332 protected void updateSecurity(final PrometheusDataKeySet pKeySet) throws OceanusException {
333
334 if (pKeySet.equals(getDataKeySet())) {
335 return;
336 }
337
338
339 pushHistory();
340
341
342 setDataKeySet(pKeySet);
343
344
345 getValues().updateSecurity();
346 }
347
348
349
350
351
352
353 public abstract static class PrometheusEncryptedList<T extends PrometheusEncryptedDataItem>
354 extends PrometheusDataList<T> {
355
356
357
358 static {
359 MetisFieldSet.newFieldSet(PrometheusEncryptedList.class);
360 }
361
362
363
364
365
366
367
368
369 protected PrometheusEncryptedList(final Class<T> pBaseClass,
370 final PrometheusCryptographicDataSet pData,
371 final MetisListKey pItemType) {
372 this(pBaseClass, pData, pItemType, PrometheusListStyle.CORE);
373 }
374
375
376
377
378
379
380
381
382
383 protected PrometheusEncryptedList(final Class<T> pBaseClass,
384 final PrometheusCryptographicDataSet pData,
385 final MetisListKey pItemType,
386 final PrometheusListStyle pStyle) {
387 super(pBaseClass, pData, pItemType, pStyle);
388 }
389
390
391
392
393
394
395 protected PrometheusEncryptedList(final PrometheusEncryptedList<T> pSource) {
396 super(pSource);
397 }
398
399 @Override
400 public PrometheusCryptographicDataSet getDataSet() {
401 return (PrometheusCryptographicDataSet) super.getDataSet();
402 }
403
404
405
406
407
408
409 private PrometheusControlKey getControlKey() {
410 final PrometheusControlDataCtl myControl = getDataSet().getControl();
411 return myControl == null
412 ? null
413 : (PrometheusControlKey) myControl.getControlKey();
414 }
415
416
417
418
419
420
421 private PrometheusDataKeySet getNextDataKeySet() {
422 final PrometheusControlKey myKey = getControlKey();
423 return (myKey == null)
424 ? null
425 : myKey.getNextDataKeySet();
426 }
427
428
429
430
431
432
433
434
435 public void updateSecurity(final TethysUIThreadStatusReport pReport,
436 final PrometheusControlKey pControl) throws OceanusException {
437
438 pReport.setNewStage(listName());
439
440
441 pReport.setNumSteps(size());
442
443
444 final Iterator<T> myIterator = iterator();
445 while (myIterator.hasNext()) {
446 final T myCurr = myIterator.next();
447
448
449 if (!pControl.equals(myCurr.getDataKeySet().getControlKey())) {
450
451 myCurr.updateSecurity(pControl.getNextDataKeySet());
452 }
453
454
455 pReport.setNextStep();
456 }
457 }
458
459
460
461
462
463
464
465
466
467 protected void adoptSecurity(final TethysUIThreadStatusReport pReport,
468 final PrometheusEncryptedList<?> pBase) throws OceanusException {
469
470 pReport.setNewStage(listName());
471
472
473 pReport.setNumSteps(size());
474
475
476 final PrometheusDataSetCtl myData = getDataSet();
477 final PrometheusDataKeySetList mySets
478 = myData.getDataList(PrometheusCryptographyDataType.DATAKEYSET, PrometheusDataKeySetList.class);
479
480
481 final Iterator<T> myIterator = iterator();
482 final Class<T> myClass = getBaseClass();
483
484
485 while (myIterator.hasNext()) {
486
487 final PrometheusEncryptedDataItem myCurr = myIterator.next();
488 final PrometheusEncryptedDataItem myBase = pBase.findItemById(myCurr.getIndexedId());
489
490
491 final T myTarget = myClass.cast(myCurr);
492
493
494 if (myBase != null) {
495
496 final T mySource = myClass.cast(myBase);
497
498
499 PrometheusDataKeySet mySet = myBase.getDataKeySet();
500 mySet = mySets.findItemById(mySet.getIndexedId());
501
502
503 myTarget.adoptSecurity(mySet, mySource);
504 } else {
505
506 myTarget.initialiseSecurity(getNextDataKeySet());
507 }
508
509
510 pReport.setNextStep();
511 }
512 }
513 }
514 }