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.MetisDataItem.MetisDataFieldId;
20 import io.github.tonywasher.joceanus.metis.field.MetisFieldItem.MetisFieldDef;
21 import io.github.tonywasher.joceanus.metis.field.MetisFieldItem.MetisFieldSetDef;
22 import io.github.tonywasher.joceanus.metis.field.MetisFieldItem.MetisFieldVersionedDef;
23 import io.github.tonywasher.joceanus.metis.field.MetisFieldVersionValues;
24 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
25 import io.github.tonywasher.joceanus.prometheus.data.PrometheusEncrypted.PrometheusEncryptedDataItemCtl;
26
27 import java.util.Iterator;
28
29
30
31
32 public class PrometheusEncryptedValues
33 extends MetisFieldVersionValues {
34
35
36
37
38
39 public PrometheusEncryptedValues(final PrometheusEncryptedDataItemCtl pItem) {
40 super(pItem);
41 }
42
43 @Override
44 public PrometheusEncryptedValues cloneIt() {
45
46 final PrometheusEncryptedValues mySet = new PrometheusEncryptedValues(getItem());
47 mySet.copyFrom(this);
48 return mySet;
49 }
50
51 @Override
52 protected PrometheusEncryptedDataItemCtl getItem() {
53 return (PrometheusEncryptedDataItemCtl) super.getItem();
54 }
55
56 @Override
57 public Object getValue(final MetisFieldDef pField) {
58
59 final Object myValue = super.getValue(pField);
60
61
62 return myValue instanceof PrometheusEncryptedPair myPair
63 ? myPair.getValue()
64 : myValue;
65 }
66
67
68
69
70
71
72
73 public byte[] getEncryptedBytes(final MetisDataFieldId pFieldId) {
74
75 final MetisFieldDef myField = getItem().getDataFieldSet().getField(pFieldId);
76 return getEncryptedBytes(myField);
77 }
78
79
80
81
82
83
84
85 public byte[] getEncryptedBytes(final MetisFieldDef pField) {
86
87 final Object myValue = super.getValue(pField);
88
89
90 return myValue instanceof PrometheusEncryptedPair myPair
91 ? myPair.getBytes()
92 : null;
93 }
94
95
96
97
98
99
100
101 public PrometheusEncryptedPair getEncryptedPair(final MetisDataFieldId pFieldId) {
102
103 final MetisFieldDef myField = getItem().getDataFieldSet().getField(pFieldId);
104 return getEncryptedPair(myField);
105 }
106
107
108
109
110
111
112
113 public PrometheusEncryptedPair getEncryptedPair(final MetisFieldDef pField) {
114
115 final Object myValue = super.getValue(pField);
116
117
118 return myValue instanceof PrometheusEncryptedPair myPair
119 ? myPair
120 : null;
121 }
122
123 @Override
124 public void setValue(final MetisFieldDef pField,
125 final Object pValue) throws OceanusException {
126
127 if (!(pField instanceof MetisFieldVersionedDef)) {
128 throw new IllegalArgumentException(ERROR_NOTVERSIONED);
129 }
130
131
132 checkValueType(pField, pValue);
133
134
135 Object myValue = pValue;
136 if (pField instanceof PrometheusEncryptedField
137
138 && myValue != null) {
139
140 final PrometheusEncryptedDataItemCtl myItem = getItem();
141 final PrometheusEncryptor myEncryptor = myItem.getEncryptor();
142 myValue = myEncryptor.encryptValue(pValue, pField);
143 }
144
145
146 setUncheckedValue(pField, myValue);
147 }
148
149
150
151
152
153
154 public void updateSecurity() throws OceanusException {
155
156 final PrometheusEncryptedDataItemCtl myItem = getItem();
157 final PrometheusEncryptor myEncryptor = myItem.getEncryptor();
158 final MetisFieldSetDef myFieldSet = getFields();
159 final Iterator<MetisFieldDef> myIterator = myFieldSet.fieldIterator();
160 while (myIterator.hasNext()) {
161 final MetisFieldDef myField = myIterator.next();
162
163
164 if (!(myField instanceof PrometheusEncryptedField)) {
165 continue;
166 }
167
168
169 final Object myValue = getValue(myField);
170
171
172 setUncheckedValue(myField, myEncryptor.encryptValue(myValue, myField));
173 }
174 }
175
176
177
178
179
180
181
182 public void adoptSecurity(final PrometheusEncryptedValues pBaseValues) throws OceanusException {
183
184 final PrometheusEncryptedDataItemCtl myItem = getItem();
185 final PrometheusEncryptor myEncryptor = myItem.getEncryptor();
186 final MetisFieldSetDef myFieldSet = getFields();
187 final Iterator<MetisFieldDef> myIterator = myFieldSet.fieldIterator();
188 while (myIterator.hasNext()) {
189 final MetisFieldDef myField = myIterator.next();
190
191
192 if (!(myField instanceof PrometheusEncryptedField)) {
193 continue;
194 }
195 final PrometheusEncryptedPair myValue = getEncryptedPair(myField);
196 if (myValue == null) {
197 continue;
198 }
199
200
201 Object myBaseObj = pBaseValues == null
202 ? null
203 : pBaseValues.getValue(myField);
204 if (!(myBaseObj instanceof PrometheusEncryptedPair)) {
205 myBaseObj = null;
206 }
207
208
209 myEncryptor.adoptEncryption(myValue, (PrometheusEncryptedPair) myBaseObj);
210 }
211 }
212 }