-
Notifications
You must be signed in to change notification settings - Fork 214
/
ModifyToCreateThingTransformer.java
90 lines (78 loc) · 3.88 KB
/
ModifyToCreateThingTransformer.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
/*
* Copyright (c) 2022 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.service.enforcement.pre;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.service.signaltransformer.SignalTransformer;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingsModelFactory;
import org.eclipse.ditto.things.model.signals.commands.modify.CreateThing;
import org.eclipse.ditto.things.model.signals.commands.modify.MergeThing;
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyThing;
import org.eclipse.ditto.things.model.signals.commands.modify.ThingModifyCommand;
import com.typesafe.config.Config;
import akka.actor.ActorSystem;
/**
* Transforms a ModifyThing and a MergeThing command into a CreateThing if the thing does not exist already.
*/
public final class ModifyToCreateThingTransformer implements SignalTransformer {
private final ThingExistenceChecker existenceChecker;
ModifyToCreateThingTransformer(final ActorSystem actorSystem, final Config config) {
this(new ThingExistenceChecker(actorSystem));
}
ModifyToCreateThingTransformer(final ThingExistenceChecker existenceChecker) {
this.existenceChecker = existenceChecker;
}
@Override
public CompletionStage<Signal<?>> apply(final Signal<?> signal) {
return calculateInputParams(signal)
.map(input -> existenceChecker.checkExistence(input.thingModifyCommand())
.thenApply(exists -> {
if (Boolean.FALSE.equals(exists)) {
final var newThing = input.thing().toBuilder()
.setId(input.thingModifyCommand().getEntityId())
.build();
return CreateThing.of(newThing, input.initialPolicy(), input.policyIdOrPlaceholder(),
input.thingModifyCommand().getDittoHeaders());
} else {
return (Signal<?>) input.thingModifyCommand();
}
})
).orElse(CompletableFuture.completedStage(signal));
}
private static Optional<InputParams> calculateInputParams(final Signal<?> signal) {
if (signal instanceof ModifyThing modifyThing) {
return Optional.of(new InputParams(modifyThing,
modifyThing.getThing(),
modifyThing.getInitialPolicy().orElse(null),
modifyThing.getPolicyIdOrPlaceholder().orElse(null)
));
} else if (signal instanceof MergeThing mergeThing && mergeThing.getPath().isEmpty()) {
final JsonObject mergeThingObject = mergeThing.getEntity().orElseGet(mergeThing::getValue).asObject();
return Optional.of(new InputParams(mergeThing,
ThingsModelFactory.newThing(mergeThingObject),
mergeThing.getInitialPolicy().orElse(null),
mergeThing.getPolicyIdOrPlaceholder().orElse(null)
));
} else {
return Optional.empty();
}
}
private record InputParams(ThingModifyCommand<?> thingModifyCommand, Thing thing,
@Nullable JsonObject initialPolicy,
@Nullable String policyIdOrPlaceholder) {}
}