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