/
ModifyFeaturesStrategy.java
103 lines (85 loc) · 4.43 KB
/
ModifyFeaturesStrategy.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
/*
* Copyright (c) 2017 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.services.things.persistence.actors.strategies.commands;
import java.util.Optional;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.headers.WithDittoHeaders;
import org.eclipse.ditto.model.things.Features;
import org.eclipse.ditto.model.things.Thing;
import org.eclipse.ditto.model.things.ThingId;
import org.eclipse.ditto.services.utils.persistentactors.results.Result;
import org.eclipse.ditto.services.utils.persistentactors.results.ResultFactory;
import org.eclipse.ditto.signals.commands.things.ThingCommandSizeValidator;
import org.eclipse.ditto.signals.commands.things.modify.ModifyFeatures;
import org.eclipse.ditto.signals.commands.things.modify.ModifyFeaturesResponse;
import org.eclipse.ditto.signals.events.things.FeaturesCreated;
import org.eclipse.ditto.signals.events.things.FeaturesModified;
import org.eclipse.ditto.signals.events.things.ThingEvent;
/**
* This strategy handles the {@link org.eclipse.ditto.signals.commands.things.modify.ModifyFeatures} command.
*/
@Immutable
final class ModifyFeaturesStrategy extends AbstractThingCommandStrategy<ModifyFeatures> {
/**
* Constructs a new {@code ModifyFeaturesStrategy} object.
*/
ModifyFeaturesStrategy() {
super(ModifyFeatures.class);
}
@Override
protected Result<ThingEvent> doApply(final Context<ThingId> context, @Nullable final Thing thing,
final long nextRevision, final ModifyFeatures command) {
final Thing nonNullThing = getEntityOrThrow(thing);
ThingCommandSizeValidator.getInstance().ensureValidSize(() -> {
final long lengthWithOutFeatures = nonNullThing.removeFeatures()
.toJsonString()
.length();
final long featuresLength = command.getFeatures().toJsonString().length() + "features".length() + 5L;
return lengthWithOutFeatures + featuresLength;
}, command::getDittoHeaders);
return nonNullThing.getFeatures()
.map(features -> getModifyResult(context, nextRevision, command, thing))
.orElseGet(() -> getCreateResult(context, nextRevision, command, thing));
}
private Result<ThingEvent> getModifyResult(final Context<ThingId> context, final long nextRevision,
final ModifyFeatures command, @Nullable final Thing thing) {
final DittoHeaders dittoHeaders = command.getDittoHeaders();
final ThingEvent event =
FeaturesModified.of(command.getThingEntityId(), command.getFeatures(), nextRevision,
getEventTimestamp(), dittoHeaders);
final WithDittoHeaders response = appendETagHeaderIfProvided(command,
ModifyFeaturesResponse.modified(context.getState(), dittoHeaders), thing);
return ResultFactory.newMutationResult(command, event, response);
}
private Result<ThingEvent> getCreateResult(final Context<ThingId> context, final long nextRevision,
final ModifyFeatures command, @Nullable final Thing thing) {
final Features features = command.getFeatures();
final DittoHeaders dittoHeaders = command.getDittoHeaders();
final ThingEvent event =
FeaturesCreated.of(command.getThingEntityId(), features, nextRevision, getEventTimestamp(),
dittoHeaders);
final WithDittoHeaders response = appendETagHeaderIfProvided(command,
ModifyFeaturesResponse.created(context.getState(), features, dittoHeaders), thing);
return ResultFactory.newMutationResult(command, event, response);
}
@Override
public Optional<?> previousETagEntity(final ModifyFeatures command, @Nullable final Thing previousEntity) {
return Optional.ofNullable(previousEntity).flatMap(Thing::getFeatures);
}
@Override
public Optional<?> nextETagEntity(final ModifyFeatures command, @Nullable final Thing newEntity) {
return Optional.of(command.getFeatures());
}
}