/
DeleteFeaturesStrategy.java
79 lines (68 loc) · 3.23 KB
/
DeleteFeaturesStrategy.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
/*
* 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.modify.DeleteFeatures;
import org.eclipse.ditto.signals.commands.things.modify.DeleteFeaturesResponse;
import org.eclipse.ditto.signals.events.things.FeaturesDeleted;
import org.eclipse.ditto.signals.events.things.ThingEvent;
/**
* This strategy handles the {@link org.eclipse.ditto.signals.commands.things.modify.DeleteFeatures} command.
*/
@Immutable
final class DeleteFeaturesStrategy extends AbstractConditionalHeadersCheckingCommandStrategy<DeleteFeatures, Features> {
/**
* Constructs a new {@code DeleteFeaturesStrategy} object.
*/
DeleteFeaturesStrategy() {
super(DeleteFeatures.class);
}
@Override
protected Result<ThingEvent> doApply(final Context<ThingId> context, @Nullable final Thing thing,
final long nextRevision, final DeleteFeatures command) {
final DittoHeaders dittoHeaders = command.getDittoHeaders();
return extractFeatures(thing)
.map(features ->
ResultFactory.newMutationResult(command, getEventToPersist(context, nextRevision, dittoHeaders),
getResponse(context, command, thing))
)
.orElseGet(() ->
ResultFactory.newErrorResult(ExceptionFactory.featuresNotFound(context.getEntityId(),
dittoHeaders)));
}
private Optional<Features> extractFeatures(final @Nullable Thing thing) {
return getEntityOrThrow(thing).getFeatures();
}
private static ThingEvent getEventToPersist(final Context<ThingId> context, final long nextRevision,
final DittoHeaders dittoHeaders) {
return FeaturesDeleted.of(context.getEntityId(), nextRevision, getEventTimestamp(), dittoHeaders);
}
private WithDittoHeaders getResponse(final Context<ThingId> context, final DeleteFeatures command,
@Nullable final Thing thing) {
return appendETagHeaderIfProvided(command,
DeleteFeaturesResponse.of(context.getEntityId(), command.getDittoHeaders()), thing);
}
@Override
public Optional<Features> determineETagEntity(final DeleteFeatures command, @Nullable final Thing thing) {
return Optional.empty();
}
}