-
Notifications
You must be signed in to change notification settings - Fork 214
/
MergeThing.java
648 lines (583 loc) · 29.1 KB
/
MergeThing.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.things.model.signals.commands.modify;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.common.Placeholders;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.json.JsonParsableCommand;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.FeatureToggle;
import org.eclipse.ditto.base.model.signals.UnsupportedSchemaVersionException;
import org.eclipse.ditto.base.model.signals.commands.AbstractCommand;
import org.eclipse.ditto.base.model.signals.commands.CommandJsonDeserializer;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.things.model.Attributes;
import org.eclipse.ditto.things.model.AttributesModelFactory;
import org.eclipse.ditto.things.model.Feature;
import org.eclipse.ditto.things.model.FeatureDefinition;
import org.eclipse.ditto.things.model.FeatureProperties;
import org.eclipse.ditto.things.model.Features;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingDefinition;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.ThingsModelFactory;
import org.eclipse.ditto.things.model.signals.commands.ThingCommand;
import org.eclipse.ditto.things.model.signals.commands.ThingCommandSizeValidator;
import org.eclipse.ditto.things.model.signals.commands.exceptions.AttributePointerInvalidException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.PoliciesConflictingException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingIdNotExplicitlySettableException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingMergeInvalidException;
/**
* /**
* This command merges an existing Thing with the supplied modification. The command contains a {@code path} and a
* {@code value} describing the change that should be applied. The {@code value} at the given {@code path} is merged
* with the existing thing according to <a href="https://tools.ietf.org/html/rfc7396">RFC7396 - JSON Merge Patch</a>.
*
* @since 2.0.0
*/
@Immutable
@JsonParsableCommand(typePrefix = ThingCommand.TYPE_PREFIX, name = MergeThing.NAME)
public final class MergeThing extends AbstractCommand<MergeThing> implements ThingModifyCommand<MergeThing> {
/**
* Name of the "Merge Thing" command.
*/
public static final String NAME = "mergeThing";
/**
* Type of this command.
*/
public static final String TYPE = TYPE_PREFIX + NAME;
/**
* Json Field definition for the optional initial "inline" policy for usage in getEntity().
*/
public static final JsonFieldDefinition<JsonObject> JSON_INLINE_POLICY =
JsonFactory.newJsonObjectFieldDefinition(Policy.INLINED_FIELD_NAME, FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* Json Field definition for the optional feature to copy an existing policy.
*/
public static final JsonFieldDefinition<String> JSON_COPY_POLICY_FROM =
JsonFactory.newStringFieldDefinition("_copyPolicyFrom", FieldType.REGULAR, JsonSchemaVersion.V_2);
private static final JsonFieldDefinition<JsonObject> JSON_INITIAL_POLICY =
JsonFactory.newJsonObjectFieldDefinition("initialPolicy", FieldType.REGULAR, JsonSchemaVersion.V_2);
private static final JsonFieldDefinition<String> JSON_POLICY_ID_OR_PLACEHOLDER =
JsonFactory.newStringFieldDefinition("policyIdOrPlaceholder", FieldType.REGULAR, JsonSchemaVersion.V_2);
private final ThingId thingId;
private final JsonPointer path;
private final JsonValue value;
@Nullable private final JsonObject initialPolicy;
@Nullable private final String policyIdOrPlaceholder;
private MergeThing(final ThingId thingId, final JsonPointer path, final JsonValue value,
@Nullable final JsonObject initialPolicy,
@Nullable final String policyIdOrPlaceholder,
final DittoHeaders dittoHeaders) {
super(TYPE, FeatureToggle.checkMergeFeatureEnabled(TYPE, dittoHeaders));
this.thingId = checkNotNull(thingId, "thingId");
this.path = checkNotNull(path, "path");
this.value = checkJsonSize(checkNotNull(value, "value"), dittoHeaders);
if (policyIdOrPlaceholder != null && initialPolicy != null) {
throw PoliciesConflictingException.newBuilder(thingId).dittoHeaders(dittoHeaders).build();
}
if (policyIdOrPlaceholder != null && !Placeholders.containsAnyPlaceholder(policyIdOrPlaceholder)) {
PolicyId.of(policyIdOrPlaceholder); //validates
}
this.initialPolicy = initialPolicy;
this.policyIdOrPlaceholder = policyIdOrPlaceholder;
checkSchemaVersion();
}
/**
* Creates a command for merging the thing identified by {@code thingId} with the given
* {@code path} and {@code value}.
*
* @param thingId the thing id.
* @param path the path that is merged with the existing thing.
* @param value the value describing the changes that are merged with the existing thing.
* @param dittoHeaders the ditto headers.
* @return the created {@link MergeThing} command.
*/
public static MergeThing of(final ThingId thingId, final JsonPointer path, final JsonValue value,
final DittoHeaders dittoHeaders) {
if (path.isEmpty() && value.isObject()) {
final JsonObject object = value.asObject();
final Optional<JsonObject> initialPolicy = initialPolicyForMergeThingFrom(object);
final Optional<String> policyIdOrPlaceholder = policyIdOrPlaceholderForMergeThingFrom(object);
return new MergeThing(thingId, path, value, initialPolicy.orElse(null),
policyIdOrPlaceholder.orElse(null), dittoHeaders);
} else {
return new MergeThing(thingId, path, value, null, null, dittoHeaders);
}
}
/**
* Creates a command for merging the thing identified by {@code thingId} with the given {@code thing}.
*
* @param thingId the thing id.
* @param thing the thing that is merged with the existing thing.
* @param dittoHeaders the ditto headers.
* @return the created {@link MergeThing} command.
*/
public static MergeThing withThing(final ThingId thingId, final Thing thing, final DittoHeaders dittoHeaders) {
ensureThingIdMatches(thingId, thing);
ensureThingIsNotNullOrEmpty(thing, dittoHeaders);
final JsonObject mergePatch = thing.toJson();
return new MergeThing(thingId, JsonPointer.empty(), mergePatch, null, null, dittoHeaders);
}
/**
* Creates a command for merging the thing identified by {@code thingId} with the given {@code thing}.
* Only one of the arguments initialPolicy and policyIdOrPlaceholder must not be null. They are both allowed to be
* null, but not both to not be null at the same time.
*
* @param thingId the thing id.
* @param thing the thing that is merged with the existing thing.
* @param policyIdOrPlaceholder the policy id of the {@code Policy} to copy and set for the Thing when creating it.
* If it's a placeholder it will be resolved to a policy id.
* Placeholder must be of the syntax: {@code {{ ref:things/<theThingId>/policyId }} }.
* @param initialPolicy the initial {@code Policy} to set for the Thing when creating it - may be null.
* @param dittoHeaders the ditto headers.
* @return the created {@link MergeThing} command.
* @since 3.3.0
*/
public static MergeThing withThing(final ThingId thingId, final Thing thing,
@Nullable final JsonObject initialPolicy, @Nullable final String policyIdOrPlaceholder,
final DittoHeaders dittoHeaders) {
ensureThingIdMatches(thingId, thing);
ensureThingIsNotNullOrEmpty(thing, dittoHeaders);
final JsonObject mergePatch = thing.toJson();
return new MergeThing(thingId, JsonPointer.empty(), mergePatch, initialPolicy, policyIdOrPlaceholder,
dittoHeaders);
}
/**
* Creates a command for merging the thing identified by {@code thingId} with the given {@code policyId}.
*
* @param thingId the thing id.
* @param policyId the policyId that is merged with the existing thing.
* @param dittoHeaders the ditto headers.
* @return the created {@link MergeThing} command.
*/
public static MergeThing withPolicyId(final ThingId thingId, final PolicyId policyId,
final DittoHeaders dittoHeaders) {
checkNotNull(policyId, "policyId");
return new MergeThing(thingId, Thing.JsonFields.POLICY_ID.getPointer(), JsonValue.of(policyId),
null, null, dittoHeaders);
}
/**
* Creates a command for merging the thing identified by {@code thingId} with the given {@code thingDefinition}.
*
* @param thingId the thing id.
* @param thingDefinition the thing definition that is merged with the existing thing.
* @param dittoHeaders the ditto headers.
* @return the created {@link MergeThing} command.
*/
public static MergeThing withThingDefinition(final ThingId thingId, final ThingDefinition thingDefinition,
final DittoHeaders dittoHeaders) {
return new MergeThing(thingId, Thing.JsonFields.DEFINITION.getPointer(), thingDefinition.toJson(),
null, null, dittoHeaders);
}
/**
* Creates a command for merging the thing identified by {@code thingId} with the given {@code attributes}.
*
* @param thingId the thing id.
* @param attributes the attributes that are merged with the existing thing.
* @param dittoHeaders the ditto headers.
* @return the created {@link MergeThing} command.
*/
public static MergeThing withAttributes(final ThingId thingId, final Attributes attributes,
final DittoHeaders dittoHeaders) {
return new MergeThing(thingId, Thing.JsonFields.ATTRIBUTES.getPointer(), attributes.toJson(),
null, null, dittoHeaders);
}
/**
* Creates a command for merging the thing identified by {@code thingId} with the given
* {@code attributePath} and {@code attributeValue}.
*
* @param thingId the thing id.
* @param attributePath the path where the attribute value is merged with the existing thing.
* @param attributeValue the attribute value that is merged with the existing thing.
* @param dittoHeaders the ditto headers.
* @return the created {@link MergeThing} command.
*/
public static MergeThing withAttribute(final ThingId thingId, final JsonPointer attributePath,
final JsonValue attributeValue, final DittoHeaders dittoHeaders) {
final JsonPointer absolutePath =
Thing.JsonFields.ATTRIBUTES.getPointer().append(checkAttributePointer(attributePath, dittoHeaders));
return new MergeThing(thingId, absolutePath, checkAttributeValue(attributeValue), null, null, dittoHeaders);
}
/**
* Creates a command for merging the thing identified by {@code thingId} with the given {@code features}.
*
* @param thingId the thing id.
* @param features the features that are merged with the existing thing.
* @param dittoHeaders the ditto headers.
* @return the created {@link MergeThing} command.
*/
public static MergeThing withFeatures(final ThingId thingId, final Features features,
final DittoHeaders dittoHeaders) {
final JsonPointer absolutePath = Thing.JsonFields.FEATURES.getPointer();
return new MergeThing(thingId, absolutePath, features.toJson(), null, null, dittoHeaders);
}
/**
* Creates a command for merging the thing identified by {@code thingId} with the given {@code feature}.
*
* @param thingId the thing id.
* @param feature the feature that is merged with the existing thing.
* @param dittoHeaders the ditto headers.
* @return the created {@link MergeThing} command.
*/
public static MergeThing withFeature(final ThingId thingId, final Feature feature,
final DittoHeaders dittoHeaders) {
final JsonPointer absolutePath = Thing.JsonFields.FEATURES.getPointer().append(JsonPointer.of(feature.getId()));
return new MergeThing(thingId, absolutePath, feature.toJson(), null, null, dittoHeaders);
}
/**
* Creates a command for merging the feature identified by {@code thingId} and {@code featureId} with the given
* {@code featureDefinition}.
*
* @param thingId the thing id.
* @param featureId the feature id identifying the feature.
* @param featureDefinition the feature definition that is merged with the existing feature.
* @param dittoHeaders the ditto headers.
* @return the created {@link MergeThing} command.
*/
public static MergeThing withFeatureDefinition(final ThingId thingId,
final String featureId, final FeatureDefinition featureDefinition,
final DittoHeaders dittoHeaders) {
final JsonPointer absolutePath = Thing.JsonFields.FEATURES.getPointer()
.append(JsonPointer.of(featureId))
.append(Feature.JsonFields.DEFINITION.getPointer());
return new MergeThing(thingId, absolutePath, featureDefinition.toJson(), null, null, dittoHeaders);
}
/**
* Creates a command for merging the feature identified by {@code thingId} and {@code featureId} with
* the given {@code featureProperties}.
*
* @param thingId the thing id.
* @param featureId the feature id identifying the feature.
* @param featureProperties the featureProperties that are merged with the existing feature.
* @param dittoHeaders the ditto headers.
* @return the created {@link MergeThing} command.
*/
public static MergeThing withFeatureProperties(final ThingId thingId,
final String featureId, final FeatureProperties featureProperties,
final DittoHeaders dittoHeaders) {
final JsonPointer absolutePath = Thing.JsonFields.FEATURES.getPointer()
.append(JsonPointer.of(featureId))
.append(Feature.JsonFields.PROPERTIES.getPointer());
return new MergeThing(thingId, absolutePath, featureProperties.toJson(), null, null, dittoHeaders);
}
/**
* Creates a command for merging the feature identified by {@code thingId} and {@code featureId} with
* the given {@code propertyPath} and {@code propertyValue}.
*
* @param thingId the thing id.
* @param featureId the feature id identifying the feature.
* @param propertyPath the path where the property value is merged with the existing feature properties.
* @param propertyValue the property value that is merged with the existing feature properties.
* @param dittoHeaders the ditto headers.
* @return the created {@link MergeThing} command.
*/
public static MergeThing withFeatureProperty(final ThingId thingId,
final String featureId, final JsonPointer propertyPath, final JsonValue propertyValue,
final DittoHeaders dittoHeaders) {
final JsonPointer absolutePath = Thing.JsonFields.FEATURES.getPointer()
.append(JsonPointer.of(featureId))
.append(Feature.JsonFields.PROPERTIES.getPointer())
.append(checkPropertyPointer(propertyPath));
return new MergeThing(thingId, absolutePath, checkPropertyValue(propertyValue), null, null, dittoHeaders);
}
/**
* Creates a command for merging the feature identified by {@code thingId} and {@code featureId} with
* the given {@code desiredFeatureProperties}.
*
* @param thingId the thing id.
* @param featureId the feature id identifying the feature.
* @param desiredFeatureProperties the desired feature properties that are merged with the existing feature.
* @param dittoHeaders the ditto headers.
* @return the created {@link MergeThing} command.
*/
public static MergeThing withFeatureDesiredProperties(final ThingId thingId,
final String featureId, final FeatureProperties desiredFeatureProperties,
final DittoHeaders dittoHeaders) {
final JsonPointer absolutePath = Thing.JsonFields.FEATURES.getPointer()
.append(JsonPointer.of(featureId))
.append(Feature.JsonFields.DESIRED_PROPERTIES.getPointer());
return new MergeThing(thingId, absolutePath, desiredFeatureProperties.toJson(), null, null, dittoHeaders);
}
/**
* Creates a command for merging the feature identified by {@code thingId} and {@code featureId} with
* the given {@code propertyPath} and {@code propertyValue}.
*
* @param thingId the thing id.
* @param featureId the feature id identifying the feature.
* @param propertyPath the path where the property value is merged with the existing desired feature properties.
* @param propertyValue the property value that is merged with the existing desired feature properties.
* @param dittoHeaders the ditto headers.
* @return the created {@link MergeThing} command.
*/
public static MergeThing withFeatureDesiredProperty(final ThingId thingId,
final String featureId, final JsonPointer propertyPath, final JsonValue propertyValue,
final DittoHeaders dittoHeaders) {
final JsonPointer absolutePath = Thing.JsonFields.FEATURES.getPointer()
.append(JsonPointer.of(featureId))
.append(Feature.JsonFields.DESIRED_PROPERTIES.getPointer())
.append(checkPropertyPointer(propertyPath));
return new MergeThing(thingId, absolutePath, checkPropertyValue(propertyValue), null, null, dittoHeaders);
}
/**
* Retrieves a potentially included "inline policy" from the {@link #JSON_INLINE_POLICY _policy} field of the passed
* {@code jsonObject}.
*
* @param jsonObject the JSON object to look for an inline policy in.
* @return the potentially contained inline policy as JSON object.
*/
public static Optional<JsonObject> initialPolicyForMergeThingFrom(final JsonObject jsonObject) {
return jsonObject.getValue(JSON_INLINE_POLICY)
.map(JsonValue::asObject);
}
/**
* Retrieves a potentially included "policy id or placeholder" to copy a policy from the
* {@link #JSON_COPY_POLICY_FROM _copyPolicyFrom} field of the passed {@code jsonObject}.
*
* @param jsonObject the JSON object to look for the policy id or placeholder in.
* @return the potentially contained policy id or placeholder.
*/
public static Optional<String> policyIdOrPlaceholderForMergeThingFrom(final JsonObject jsonObject) {
return jsonObject.getValue(JSON_COPY_POLICY_FROM);
}
private static JsonPointer checkPropertyPointer(final JsonPointer propertyPointer) {
return ThingsModelFactory.validateFeaturePropertyPointer(propertyPointer);
}
private static JsonValue checkPropertyValue(final JsonValue value) {
if (value.isObject()) {
ThingsModelFactory.validateJsonKeys(value.asObject());
}
return value;
}
private static JsonPointer checkAttributePointer(final JsonPointer pointer, final DittoHeaders dittoHeaders) {
if (pointer.isEmpty()) {
throw AttributePointerInvalidException.newBuilder(pointer)
.dittoHeaders(dittoHeaders)
.build();
}
return AttributesModelFactory.validateAttributePointer(pointer);
}
private static JsonValue checkAttributeValue(final JsonValue value) {
if (value.isObject()) {
AttributesModelFactory.validateAttributeKeys(value.asObject());
}
return value;
}
/**
* Ensures that the thingId is consistent with the id of the thing.
*
* @throws org.eclipse.ditto.things.model.signals.commands.exceptions.ThingIdNotExplicitlySettableException if ids do not match.
*/
private static void ensureThingIdMatches(final ThingId thingId, final Thing thing) {
if (!thing.getEntityId().map(id -> id.equals(thingId)).orElse(true)) {
throw ThingIdNotExplicitlySettableException.forDittoProtocol().build();
}
}
/**
* Ensures that the thing is not null or empty.
*
* @throws org.eclipse.ditto.things.model.signals.commands.exceptions.ThingMergeInvalidException if the thing is null or empty.
*/
private static void ensureThingIsNotNullOrEmpty(final Thing thing, final DittoHeaders dittoHeaders) {
if (thing.toJson().isEmpty() &&
dittoHeaders.getSchemaVersion().filter(JsonSchemaVersion.V_2::equals).isPresent()) {
throw ThingMergeInvalidException.fromMessage(
"The provided json value can not be applied at this resource", dittoHeaders);
}
}
private JsonValue checkJsonSize(final JsonValue value, final DittoHeaders dittoHeaders) {
ThingCommandSizeValidator.getInstance().ensureValidSize(
value::getUpperBoundForStringSize,
() -> value.toString().length(),
() -> dittoHeaders);
return value;
}
/**
* Creates a new {@code MergeThing} from a JSON object.
*
* @param jsonObject the JSON object of which the command is to be created.
* @param dittoHeaders the headers of the command.
* @return the {@code MergeThing} command created from JSON.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected
* format.
* @throws org.eclipse.ditto.json.JsonMissingFieldException if {@code jsonObject} did not contain a field for
* {@link org.eclipse.ditto.things.model.signals.commands.ThingCommand.JsonFields#JSON_THING_ID}, {@link MergeThing.JsonFields#JSON_PATH} or {@link MergeThing.JsonFields#JSON_VALUE}.
*/
public static MergeThing fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
return new CommandJsonDeserializer<MergeThing>(TYPE, jsonObject).deserialize(() -> {
final String thingId = jsonObject.getValueOrThrow(ThingCommand.JsonFields.JSON_THING_ID);
final String path = jsonObject.getValueOrThrow(JsonFields.JSON_PATH);
final JsonValue jsonValue = jsonObject.getValueOrThrow(JsonFields.JSON_VALUE);
final JsonObject initialPolicyObject = jsonObject.getValue(JSON_INITIAL_POLICY).orElse(null);
final String policyIdOrPlaceholder = jsonObject.getValue(JSON_POLICY_ID_OR_PLACEHOLDER).orElse(null);
return new MergeThing(ThingId.of(thingId), JsonPointer.of(path), jsonValue, initialPolicyObject,
policyIdOrPlaceholder, dittoHeaders);
});
}
/**
* @return the path where the changes are applied.
*/
public JsonPointer getPath() {
return path;
}
@Override
public ThingId getEntityId() {
return thingId;
}
@Override
public Optional<JsonValue> getEntity() {
if (path.isEmpty()) {
final JsonObject thingJson = value.asObject();
final JsonObject withInlinePolicyThingJson =
getInitialPolicy().map(ip -> thingJson.set(JSON_INLINE_POLICY, ip)).orElse(thingJson);
final JsonObject fullThingJson = getPolicyIdOrPlaceholder().map(
containedPolicyIdOrPlaceholder -> withInlinePolicyThingJson.set(JSON_COPY_POLICY_FROM,
containedPolicyIdOrPlaceholder)).orElse(withInlinePolicyThingJson);
return Optional.of(fullThingJson);
} else {
return Optional.of(value);
}
}
@Override
public Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion) {
return getEntity();
}
@Override
public JsonPointer getResourcePath() {
return path;
}
/**
* @return the initial {@code Policy} if there should be one applied when creating the Thing.
* @since 3.3.0
*/
public Optional<JsonObject> getInitialPolicy() {
return Optional.ofNullable(initialPolicy);
}
/**
* @return The policy id of the {@code Policy} to copy and set for the Thing when creating it.
* Could also be a placeholder like: {{ ref:things/theThingId/policyId }}.
* @since 3.3.0
*/
public Optional<String> getPolicyIdOrPlaceholder() {
return Optional.ofNullable(policyIdOrPlaceholder);
}
@Override
public boolean changesAuthorization() {
return Thing.JsonFields.POLICY_ID.getPointer().equals(path) || path.isEmpty() && value.isObject() &&
value.asObject().contains(Thing.JsonFields.POLICY_ID.getPointer());
}
@Override
public Category getCategory() {
return Category.MERGE;
}
@Override
public MergeThing setDittoHeaders(final DittoHeaders dittoHeaders) {
return new MergeThing(thingId, path, value, initialPolicy, policyIdOrPlaceholder, dittoHeaders);
}
@Override
protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final JsonSchemaVersion schemaVersion,
final Predicate<JsonField> predicateParameter) {
final Predicate<JsonField> predicate = schemaVersion.and(predicateParameter);
jsonObjectBuilder.set(ThingCommand.JsonFields.JSON_THING_ID, thingId.toString(), predicate);
jsonObjectBuilder.set(JsonFields.JSON_PATH, path.toString(), predicate);
jsonObjectBuilder.set(JsonFields.JSON_VALUE, value, predicate);
if (initialPolicy != null) {
jsonObjectBuilder.set(JSON_INITIAL_POLICY, initialPolicy, predicate);
}
if (policyIdOrPlaceholder != null) {
jsonObjectBuilder.set(JSON_POLICY_ID_OR_PLACEHOLDER, policyIdOrPlaceholder, predicate);
}
}
@Override
public JsonSchemaVersion[] getSupportedSchemaVersions() {
return new JsonSchemaVersion[]{JsonSchemaVersion.V_2};
}
private void checkSchemaVersion() {
final JsonSchemaVersion implementedSchemaVersion = getImplementedSchemaVersion();
if (!implementsSchemaVersion(implementedSchemaVersion)) {
throw UnsupportedSchemaVersionException.newBuilder(implementedSchemaVersion).build();
}
}
/**
* @return the value describing the changes that are applied to the existing thing.
*/
public JsonValue getValue() {
return value;
}
/**
* An enumeration of the JSON fields of a {@code MergeThing} command.
*/
@Immutable
static final class JsonFields {
static final JsonFieldDefinition<String> JSON_PATH =
JsonFactory.newStringFieldDefinition("path", FieldType.REGULAR, JsonSchemaVersion.V_2);
static final JsonFieldDefinition<JsonValue> JSON_VALUE =
JsonFactory.newJsonValueFieldDefinition("value", FieldType.REGULAR, JsonSchemaVersion.V_2);
JsonFields() {
throw new AssertionError();
}
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
final MergeThing that = (MergeThing) o;
return that.canEqual(this) &&
Objects.equals(thingId, that.thingId) &&
Objects.equals(path, that.path) &&
Objects.equals(value, that.value) &&
Objects.equals(initialPolicy, that.initialPolicy) &&
Objects.equals(policyIdOrPlaceholder, that.policyIdOrPlaceholder);
}
@Override
protected boolean canEqual(@Nullable final Object other) {
return other instanceof MergeThing;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), thingId, path, value, initialPolicy, policyIdOrPlaceholder);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
super.toString() +
", thingId=" + thingId +
", path=" + path +
", value=" + value +
", initialPolicy=" + initialPolicy +
", policyIdOrPlaceholder=" + policyIdOrPlaceholder +
"]";
}
}