/
ModifyAttributesStrategy.java
84 lines (72 loc) · 3.58 KB
/
ModifyAttributesStrategy.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
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* 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.things.Attributes;
import org.eclipse.ditto.model.things.Thing;
import org.eclipse.ditto.signals.commands.things.ThingCommandSizeValidator;
import org.eclipse.ditto.signals.commands.things.modify.ModifyAttributes;
import org.eclipse.ditto.signals.commands.things.modify.ModifyAttributesResponse;
import org.eclipse.ditto.signals.events.things.AttributesCreated;
import org.eclipse.ditto.signals.events.things.AttributesModified;
/**
* This strategy handles the {@link ModifyAttributes} command.
*/
@Immutable
public final class ModifyAttributesStrategy extends
AbstractConditionalHeadersCheckingCommandStrategy<ModifyAttributes, Attributes> {
/**
* Constructs a new {@code ModifyAttributesStrategy} object.
*/
ModifyAttributesStrategy() {
super(ModifyAttributes.class);
}
@Override
protected Result doApply(final Context context, @Nullable final Thing thing,
final long nextRevision, final ModifyAttributes command) {
final Thing nonNullThing = getThingOrThrow(thing);
ThingCommandSizeValidator.getInstance().ensureValidSize(() -> {
final long lengthWithOutAttributes = nonNullThing.removeAttributes()
.toJsonString()
.length();
final long attributesLength = command.getAttributes().toJsonString().length()
+ "attributes".length() + 5L;
return lengthWithOutAttributes + attributesLength;
}, command::getDittoHeaders);
return nonNullThing.getAttributes()
.map(attributes -> getModifyResult(context, nextRevision, command))
.orElseGet(() -> getCreateResult(context, nextRevision, command));
}
private Result getModifyResult(final Context context, final long nextRevision,
final ModifyAttributes command) {
final String thingId = context.getThingId();
final DittoHeaders dittoHeaders = command.getDittoHeaders();
return ResultFactory.newMutationResult(command,
AttributesModified.of(thingId, command.getAttributes(), nextRevision, getEventTimestamp(),
dittoHeaders), ModifyAttributesResponse.modified(thingId, dittoHeaders), this);
}
private Result getCreateResult(final Context context, final long nextRevision,
final ModifyAttributes command) {
final String thingId = context.getThingId();
final Attributes attributes = command.getAttributes();
final DittoHeaders dittoHeaders = command.getDittoHeaders();
return ResultFactory.newMutationResult(command,
AttributesCreated.of(thingId, attributes, nextRevision, getEventTimestamp(), dittoHeaders),
ModifyAttributesResponse.created(thingId, attributes, dittoHeaders), this);
}
@Override
public Optional<Attributes> determineETagEntity(final ModifyAttributes command, @Nullable final Thing thing) {
return getThingOrThrow(thing).getAttributes();
}
}