/
ModifyAttributesStrategy.java
61 lines (52 loc) · 2.44 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
/*
* Copyright (c) 2017 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
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*
*/
package org.eclipse.ditto.services.things.persistence.actors.strategies;
import javax.annotation.concurrent.NotThreadSafe;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.things.Thing;
import org.eclipse.ditto.signals.commands.things.modify.ModifyAttributes;
import org.eclipse.ditto.signals.commands.things.modify.ModifyAttributesResponse;
import org.eclipse.ditto.signals.commands.things.modify.ThingModifyCommandResponse;
import org.eclipse.ditto.signals.events.things.AttributesCreated;
import org.eclipse.ditto.signals.events.things.AttributesModified;
import org.eclipse.ditto.signals.events.things.ThingModifiedEvent;
/**
* This strategy handles the {@link ModifyAttributes} command.
*/
@NotThreadSafe
public final class ModifyAttributesStrategy extends AbstractThingCommandStrategy<ModifyAttributes> {
/**
* Constructs a new {@code ModifyAttributesStrategy} object.
*/
public ModifyAttributesStrategy() {
super(ModifyAttributes.class, null);
}
@Override
protected Result doApply(final Context context, final ModifyAttributes command) {
final DittoHeaders dittoHeaders = command.getDittoHeaders();
final ThingModifiedEvent eventToPersist;
final ThingModifyCommandResponse response;
final String thingId = context.getThingId();
final Thing thing = context.getThing();
final long nextRevision = context.nextRevision();
if (thing.getAttributes().isPresent()) {
eventToPersist = AttributesModified.of(thingId, command.getAttributes(), nextRevision,
eventTimestamp(), dittoHeaders);
response = ModifyAttributesResponse.modified(thingId, dittoHeaders);
} else {
eventToPersist = AttributesCreated.of(thingId, command.getAttributes(), nextRevision,
eventTimestamp(), dittoHeaders);
response = ModifyAttributesResponse.created(thingId, command.getAttributes(), dittoHeaders);
}
return ImmutableResult.of(eventToPersist, response);
}
}