/
AssetAttribute.java
426 lines (365 loc) · 13.7 KB
/
AssetAttribute.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
* Copyright 2017, OpenRemote Inc.
*
* See the CONTRIBUTORS.txt file in the distribution for a
* full listing of individual contributors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.openremote.model.asset;
import org.openremote.model.AbstractValueHolder;
import org.openremote.model.ValidationFailure;
import org.openremote.model.attribute.*;
import org.openremote.model.util.Pair;
import org.openremote.model.value.ObjectValue;
import org.openremote.model.value.Value;
import org.openremote.model.value.Values;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;
import static org.openremote.model.asset.AssetMeta.*;
import static org.openremote.model.attribute.MetaItem.isMetaNameEqualTo;
import static org.openremote.model.attribute.MetaItem.replaceMetaByName;
import static org.openremote.model.util.TextUtil.isNullOrEmpty;
import static org.openremote.model.util.TextUtil.requireNonNullAndNonEmpty;
public class AssetAttribute extends Attribute {
protected String assetId;
public AssetAttribute() {
super(Values.createObject());
}
protected AssetAttribute(ObjectValue objectValue) {
super(objectValue);
}
public AssetAttribute(String name) {
super(name);
}
public AssetAttribute(String name, AttributeType type) {
super(name, type);
}
public AssetAttribute(String name, AttributeType type, Value value) {
super(name, type, value);
}
public AssetAttribute(String assetId, String name) {
super(name);
setAssetId(assetId);
}
public AssetAttribute(String assetId, String name, AttributeType type) {
super(name, type);
setAssetId(assetId);
}
public AssetAttribute(String assetId, String name, AttributeType type, Value value) {
super(name, type, value);
setAssetId(assetId);
}
public Optional<String> getAssetId() {
return Optional.ofNullable(assetId);
}
protected void setAssetId(String assetId) {
requireNonNullAndNonEmpty(assetId);
this.assetId = assetId;
}
public Optional<AttributeRef> getReference() {
return Optional.ofNullable(
getAssetId().isPresent() && getName().isPresent()
? new AttributeRef(getAssetId().get(), getName().get())
: null
);
}
/**
* The below is used in a sufficient number of places to provide it here as a utility method
* with a standardised exception message.
*/
public AttributeRef getReferenceOrThrow() {
return getReference().orElseThrow(() -> new IllegalStateException("Attribute doesn't have an attribute ref"));
}
public Optional<AttributeState> getState() {
return getReference().map(ref -> new AttributeState(ref, getValue().orElse(null)));
}
@Override
public List<ValidationFailure> getMetaItemValidationFailures(MetaItem item) {
List<ValidationFailure> failures = super.getMetaItemValidationFailures(item);
// We validation well-known meta items
AssetMeta.getValidationFailure(item).ifPresent(failures::add);
return failures;
}
@Override
public AssetAttribute setMeta(Meta meta) {
super.setMeta(meta);
return this;
}
@Override
public AssetAttribute setMeta(MetaItem... meta) {
super.setMeta(meta);
return this;
}
public AssetAttribute addMeta(MetaItem... meta) {
if (meta != null) {
getMeta().addAll(Arrays.asList(meta));
}
return this;
}
/**
* @return The current value and its timestamp represented as an attribute event.
*/
public Optional<AttributeEvent> getStateEvent() {
return getState().map(state -> new AttributeEvent(state, getValueTimestamp()));
}
public boolean hasLabel() {
return getMetaStream().anyMatch(isMetaNameEqualTo(LABEL));
}
public Optional<String> getLabel() {
return Optional.ofNullable(getMetaStream()
.filter(isMetaNameEqualTo(LABEL))
.findFirst()
.flatMap(AbstractValueHolder::getValueAsString)
.orElseGet(() -> getName().orElse(null)));
}
public Optional<String> getLabelOrName() {
return getLabel().map(Optional::of).orElseGet(this::getName);
}
public void setLabel(String label) {
if (!isNullOrEmpty(label)) {
replaceMetaByName(getMeta(), LABEL, Values.create(label));
} else {
getMeta().removeIf(isMetaNameEqualTo(LABEL));
}
}
public boolean isExecutable() {
return getMetaStream()
.filter(isMetaNameEqualTo(EXECUTABLE))
.findFirst()
.map(metaItem -> metaItem.getValueAsBoolean().orElse(false))
.orElse(false);
}
public void setExecutable(boolean executable) {
if (executable) {
replaceMetaByName(getMeta(), EXECUTABLE, Values.create(true));
} else {
getMeta().removeIf(isMetaNameEqualTo(EXECUTABLE));
}
}
public boolean isShowOnDashboard() {
return getMetaStream()
.filter(isMetaNameEqualTo(SHOW_ON_DASHBOARD))
.findFirst()
.map(metaItem -> metaItem.getValueAsBoolean().orElse(false))
.orElse(false);
}
public void setShowOnDashboard(boolean show) {
if (show) {
replaceMetaByName(getMeta(), SHOW_ON_DASHBOARD, Values.create(true));
} else {
getMeta().removeIf(isMetaNameEqualTo(SHOW_ON_DASHBOARD));
}
}
public boolean hasFormat() {
return getMetaStream().anyMatch(isMetaNameEqualTo(FORMAT));
}
public Optional<String> getFormat() {
return getMetaStream()
.filter(isMetaNameEqualTo(FORMAT))
.findFirst()
.flatMap(AbstractValueHolder::getValueAsString);
}
public void setFormat(String format) {
if (!isNullOrEmpty(format)) {
replaceMetaByName(getMeta(), FORMAT, Values.create(format));
} else {
getMeta().removeIf(isMetaNameEqualTo(FORMAT));
}
}
public boolean hasDescription() {
return getMetaStream().anyMatch(isMetaNameEqualTo(DESCRIPTION));
}
public Optional<String> getDescription() {
return getMetaStream()
.filter(isMetaNameEqualTo(DESCRIPTION))
.findFirst()
.flatMap(AbstractValueHolder::getValueAsString);
}
public void setDescription(String description) {
if (!isNullOrEmpty(description)) {
replaceMetaByName(getMeta(), DESCRIPTION, Values.create(description));
} else {
getMeta().removeIf(isMetaNameEqualTo(DESCRIPTION));
}
}
/**
* Defaults to <code>true</code> if there is no {@link AssetMeta#ENABLED} item.
*/
public boolean isEnabled() {
return getMetaStream()
.filter(isMetaNameEqualTo(ENABLED))
.findFirst()
.map(metaItem -> metaItem.getValueAsBoolean().orElse(false))
.orElse(true);
}
public void setEnabled(boolean enabled) {
if (!enabled) {
replaceMetaByName(getMeta(), ENABLED, Values.create(false));
} else {
getMeta().removeIf(isMetaNameEqualTo(ENABLED));
}
}
public boolean isProtected() {
return getMetaStream()
.filter(isMetaNameEqualTo(PROTECTED))
.findFirst()
.map(metaItem -> metaItem.getValueAsBoolean().orElse(false))
.orElse(false);
}
public void setProtected(boolean protect) {
if (protect) {
replaceMetaByName(getMeta(), PROTECTED, Values.create(true));
} else {
getMeta().removeIf(isMetaNameEqualTo(PROTECTED));
}
}
public boolean isReadOnly() {
return getMetaStream()
.filter(isMetaNameEqualTo(READ_ONLY))
.findFirst()
.map(metaItem -> metaItem.getValueAsBoolean().orElse(false))
.orElse(false);
}
public void setReadOnly(boolean readOnly) {
if (readOnly) {
replaceMetaByName(getMeta(), READ_ONLY, Values.create(true));
} else {
getMeta().removeIf(isMetaNameEqualTo(READ_ONLY));
}
}
public boolean isStoreDatapoints() {
return getMetaStream()
.filter(isMetaNameEqualTo(STORE_DATA_POINTS))
.findFirst()
.map(metaItem -> metaItem.getValueAsBoolean().orElse(false))
.orElse(false);
}
public void setStoreDatapoints(boolean storeDatapoints) {
if (storeDatapoints) {
replaceMetaByName(getMeta(), STORE_DATA_POINTS, Values.create(true));
} else {
getMeta().removeIf(isMetaNameEqualTo(STORE_DATA_POINTS));
}
}
public boolean isRuleState() {
return getMetaStream()
.filter(isMetaNameEqualTo(RULE_STATE))
.findFirst()
.map(metaItem -> metaItem.getValueAsBoolean().orElse(false))
.orElse(false);
}
public void setRuleState(boolean ruleState) {
if (ruleState) {
replaceMetaByName(getMeta(), RULE_STATE, Values.create(true));
} else {
getMeta().removeIf(isMetaNameEqualTo(RULE_STATE));
}
}
public boolean isRuleEvent() {
return getMetaStream()
.filter(isMetaNameEqualTo(RULE_EVENT))
.findFirst()
.map(metaItem -> metaItem.getValueAsBoolean().orElse(false))
.orElse(false);
}
public void setRuleEvent(boolean ruleEvent) {
if (ruleEvent) {
replaceMetaByName(getMeta(), RULE_EVENT, Values.create(true));
} else {
getMeta().removeIf(isMetaNameEqualTo(RULE_EVENT));
}
}
public Optional<String> getRuleEventExpires() {
return getMetaStream()
.filter(isMetaNameEqualTo(RULE_EVENT_EXPIRES))
.findFirst()
.flatMap(AbstractValueHolder::getValueAsString);
}
public void setRuleEventExpires(String expiry) {
if (!isNullOrEmpty(expiry)) {
replaceMetaByName(getMeta(), RULE_EVENT_EXPIRES, Values.create(expiry));
} else {
getMeta().removeIf(isMetaNameEqualTo(RULE_EVENT_EXPIRES));
}
}
public AssetAttribute deepCopy() {
AssetAttribute copy = new AssetAttribute(getObjectValue().deepCopy());
copy.assetId = assetId;
copy.name = name;
return copy;
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"assetId='" + assetId + '\'' +
", name='" + name + '\'' +
"} " + objectValue.toJson();
}
// ---------------------------------------------------
// FUNCTIONAL METHODS BELOW
// ---------------------------------------------------
/**
* Returns non-private attributes with non-private meta items.
*
* @see #isProtected()
* @see AssetMeta#isMetaItemProtectedReadable(MetaItem)
*/
public static Stream<AssetAttribute> filterProtectedAttributes(Stream<AssetAttribute> attributes) {
return attributes == null ? Stream.empty() : attributes
.filter(AssetAttribute::isProtected)
.map(AssetAttribute::deepCopy)
.peek(attribute -> attribute.getMeta().removeIf(((Predicate<MetaItem>) AssetMeta::isMetaItemProtectedReadable).negate()));
}
public static Optional<AssetAttribute> attributeFromJson(ObjectValue objectValue, String assetId, String name) {
if (objectValue == null || objectValue.keys().length == 0 || isNullOrEmpty(assetId) || isNullOrEmpty(name)) {
return Optional.empty();
}
AssetAttribute attribute = new AssetAttribute(objectValue);
attribute.setAssetId(assetId);
attribute.setName(name);
return Optional.of(attribute);
}
public static Stream<AssetAttribute> attributesFromJson(ObjectValue objectValue, String assetId) {
if (objectValue == null || objectValue.keys().length == 0 || isNullOrEmpty(assetId)) {
return Stream.empty();
}
return Arrays
.stream(objectValue.keys())
.map(key -> new Pair<>(key, objectValue.getObject(key)))
.filter(pair -> pair.value.isPresent())
.map(pair -> attributeFromJson(pair.value.get(), assetId, pair.key))
.filter(Optional::isPresent)
.map(Optional::get);
}
/**
* Maps the attribute stream to a {@link ObjectValue}, duplicate attribute names are not
* allowed (internally this is a hash map, thus not allowing duplicate JSON object property
* names).
*/
public static <A extends Attribute> Optional<ObjectValue> attributesToJson(Collection<A> attributes) {
if (attributes.size() == 0)
return Optional.empty();
ObjectValue objectValue = Values.createObject();
for (A attribute : attributes) {
if (attribute.getName().isPresent())
objectValue.put(attribute.getName().get(), attribute.getObjectValue());
}
return Optional.of(objectValue);
}
}