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.MetisDataResource;
21 import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
22 import io.github.tonywasher.joceanus.metis.field.MetisFieldVersionedSet;
23 import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataSet.PrometheusCryptographyDataType;
24 import io.github.tonywasher.joceanus.prometheus.exc.PrometheusDataException;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public class PrometheusControlData
40 extends PrometheusDataItem {
41
42
43
44 public static final String OBJECT_NAME = PrometheusCryptographyDataType.CONTROLDATA.getItemName();
45
46
47
48
49 public static final String LIST_NAME = PrometheusCryptographyDataType.CONTROLDATA.getListName();
50
51
52
53
54 private static final MetisFieldVersionedSet<PrometheusControlData> FIELD_DEFS = MetisFieldVersionedSet.newVersionedFieldSet(PrometheusControlData.class);
55
56
57
58
59 static {
60 FIELD_DEFS.declareIntegerField(PrometheusDataResource.CONTROLDATA_VERSION);
61 FIELD_DEFS.declareLinkField(PrometheusCryptographyDataType.CONTROLKEY);
62 }
63
64
65
66
67 public static final String ERROR_CTLEXISTS = PrometheusDataResource.CONTROLDATA_ERROR_EXISTS.getValue();
68
69
70
71
72
73
74
75 protected PrometheusControlData(final PrometheusControlDataList pList,
76 final PrometheusControlData pSource) {
77
78 super(pList, pSource);
79 }
80
81
82
83
84
85
86
87
88 private PrometheusControlData(final PrometheusControlDataList pList,
89 final PrometheusDataValues pValues) throws OceanusException {
90
91 super(pList, pValues);
92
93
94 Object myValue = pValues.getValue(PrometheusDataResource.CONTROLDATA_VERSION);
95 if (myValue instanceof Integer i) {
96 setValueDataVersion(i);
97 } else if (myValue instanceof String s) {
98 setValueDataVersion(Integer.valueOf(s));
99 }
100
101
102 myValue = pValues.getValue(PrometheusCryptographyDataType.CONTROLKEY);
103 if (myValue instanceof Integer myInt) {
104
105 setValueControlKey(myInt);
106
107
108 final PrometheusDataSet myData = getDataSet();
109 resolveDataLink(PrometheusCryptographyDataType.CONTROLKEY, myData.getControlKeys());
110 }
111 }
112
113 @Override
114 public MetisFieldSetDef getDataFieldSet() {
115 return FIELD_DEFS;
116 }
117
118
119
120
121
122
123 public Integer getDataVersion() {
124 return getValues().getValue(PrometheusDataResource.CONTROLDATA_VERSION, Integer.class);
125 }
126
127
128
129
130
131
132 public PrometheusControlKey getControlKey() {
133 return getValues().getValue(PrometheusCryptographyDataType.CONTROLKEY, PrometheusControlKey.class);
134 }
135
136
137
138
139
140
141 public Integer getControlKeyId() {
142 final PrometheusControlKey myKey = getControlKey();
143 return (myKey == null)
144 ? null
145 : myKey.getIndexedId();
146 }
147
148
149
150
151
152
153
154 private void setValueDataVersion(final Integer pValue) throws OceanusException {
155 getValues().setValue(PrometheusDataResource.CONTROLDATA_VERSION, pValue);
156 }
157
158
159
160
161
162
163
164 private void setValueControlKey(final PrometheusControlKey pValue) throws OceanusException {
165 getValues().setValue(PrometheusCryptographyDataType.CONTROLKEY, pValue);
166 }
167
168
169
170
171
172
173
174 private void setValueControlKey(final Integer pId) throws OceanusException {
175 getValues().setValue(PrometheusCryptographyDataType.CONTROLKEY, pId);
176 }
177
178 @Override
179 public PrometheusControlData getBase() {
180 return (PrometheusControlData) super.getBase();
181 }
182
183 @Override
184 public PrometheusControlDataList getList() {
185 return (PrometheusControlDataList) super.getList();
186 }
187
188 @Override
189 public int compareValues(final PrometheusDataItem pThat) {
190
191 final PrometheusControlData myThat = (PrometheusControlData) pThat;
192 return getDataVersion() - myThat.getDataVersion();
193 }
194
195 @Override
196 public void resolveDataSetLinks() throws OceanusException {
197
198 final PrometheusDataSet myData = getDataSet();
199 resolveDataLink(PrometheusCryptographyDataType.CONTROLKEY, myData.getControlKeys());
200 }
201
202
203
204
205
206
207
208 protected void setControlKey(final PrometheusControlKey pControl) throws OceanusException {
209
210 if (getControlKey() == null) {
211
212 setValueControlKey(pControl);
213 return;
214 }
215
216
217 pushHistory();
218
219
220 setValueControlKey(pControl);
221
222
223 checkForHistory();
224 }
225
226
227
228
229 public static class PrometheusControlDataList
230 extends PrometheusDataList<PrometheusControlData> {
231
232
233
234 private static final MetisFieldSet<PrometheusControlDataList> FIELD_DEFS = MetisFieldSet.newFieldSet(PrometheusControlDataList.class);
235
236
237
238
239
240
241 protected PrometheusControlDataList(final PrometheusDataSet pData) {
242 this(pData, PrometheusListStyle.CORE);
243 }
244
245
246
247
248
249
250
251 protected PrometheusControlDataList(final PrometheusDataSet pData,
252 final PrometheusListStyle pStyle) {
253 super(PrometheusControlData.class, pData, PrometheusCryptographyDataType.CONTROLDATA, pStyle);
254 }
255
256
257
258
259
260
261 private PrometheusControlDataList(final PrometheusControlDataList pSource) {
262 super(pSource);
263 }
264
265 @Override
266 public MetisFieldSet<PrometheusControlDataList> getDataFieldSet() {
267 return FIELD_DEFS;
268 }
269
270 @Override
271 public String listName() {
272 return LIST_NAME;
273 }
274
275 @Override
276 public MetisFieldSet<PrometheusControlData> getItemFields() {
277 return PrometheusControlData.FIELD_DEFS;
278 }
279
280 @Override
281 public boolean includeDataXML() {
282 return false;
283 }
284
285
286
287
288
289
290 public PrometheusControlData getControl() {
291 return isEmpty()
292 ? null
293 : get(0);
294 }
295
296 @Override
297 protected PrometheusControlDataList getEmptyList(final PrometheusListStyle pStyle) {
298 final PrometheusControlDataList myList = new PrometheusControlDataList(this);
299 myList.setStyle(pStyle);
300 return myList;
301 }
302
303 @Override
304 public PrometheusControlDataList deriveList(final PrometheusListStyle pStyle) throws OceanusException {
305 return (PrometheusControlDataList) super.deriveList(pStyle);
306 }
307
308 @Override
309 public PrometheusControlDataList deriveDifferences(final PrometheusDataSet pDataSet,
310 final PrometheusDataList<?> pOld) {
311 return (PrometheusControlDataList) super.deriveDifferences(pDataSet, pOld);
312 }
313
314 @Override
315 public PrometheusControlData addCopyItem(final PrometheusDataItem pItem) {
316
317 if (!(pItem instanceof PrometheusControlData)) {
318 return null;
319 }
320
321
322 final PrometheusControlData myControl = new PrometheusControlData(this, (PrometheusControlData) pItem);
323 add(myControl);
324 return myControl;
325 }
326
327 @Override
328 public PrometheusControlData addNewItem() {
329 throw new UnsupportedOperationException();
330 }
331
332
333
334
335
336
337
338 public void addNewControl(final Integer pVersion) throws OceanusException {
339
340 final PrometheusDataValues myValues = new PrometheusDataValues(OBJECT_NAME);
341 myValues.addValue(PrometheusDataResource.CONTROLDATA_VERSION, pVersion);
342
343
344 addValuesItem(myValues);
345 }
346
347 @Override
348 public PrometheusControlData addValuesItem(final PrometheusDataValues pValues) throws OceanusException {
349
350 final PrometheusControlData myControl = new PrometheusControlData(this, pValues);
351
352
353 if (!isIdUnique(myControl.getIndexedId())) {
354 myControl.addError(ERROR_DUPLICATE, MetisDataResource.DATA_ID);
355 throw new PrometheusDataException(myControl, ERROR_VALIDATION);
356 }
357
358
359 add(myControl);
360
361
362 return myControl;
363 }
364
365 @Override
366 public void postProcessOnLoad() throws OceanusException {
367
368 reSort();
369 }
370
371 @Override
372 protected PrometheusDataMapItem allocateDataMap() {
373
374 throw new UnsupportedOperationException();
375 }
376 }
377 }