/
RetrieveFeatureDesiredPropertyStrategy.java
109 lines (93 loc) · 5.13 KB
/
RetrieveFeatureDesiredPropertyStrategy.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
104
105
106
107
108
109
/*
* Copyright (c) 2020 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.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.model.base.entity.metadata.Metadata;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.headers.entitytag.EntityTag;
import org.eclipse.ditto.model.things.Feature;
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.query.RetrieveFeatureDesiredProperty;
import org.eclipse.ditto.signals.commands.things.query.RetrieveFeatureDesiredPropertyResponse;
import org.eclipse.ditto.signals.events.things.ThingEvent;
/**
* This strategy handles the {@link org.eclipse.ditto.signals.commands.things.query.RetrieveFeatureDesiredProperty} command.
*/
@Immutable
final class RetrieveFeatureDesiredPropertyStrategy extends AbstractThingCommandStrategy<RetrieveFeatureDesiredProperty> {
/**
* Constructs a new {@code RetrieveFeatureDesiredPropertyStrategy} object.
*/
RetrieveFeatureDesiredPropertyStrategy() {
super(RetrieveFeatureDesiredProperty.class);
}
@Override
protected Result<ThingEvent> doApply(final Context<ThingId> context,
@Nullable final Thing thing,
final long nextRevision,
final RetrieveFeatureDesiredProperty command,
@Nullable final Metadata metadata) {
final String featureId = command.getFeatureId();
return extractFeature(command, thing)
.map(feature -> getRetrieveFeatureDesiredPropertyResult(feature, context, command, thing))
.orElseGet(
() -> ResultFactory.newErrorResult(ExceptionFactory.featureNotFound(context.getState(),
featureId, command.getDittoHeaders()), command));
}
private Optional<Feature> extractFeature(final RetrieveFeatureDesiredProperty command, final @Nullable Thing thing) {
return getEntityOrThrow(thing).getFeatures()
.flatMap(features -> features.getFeature(command.getFeatureId()));
}
private Result<ThingEvent> getRetrieveFeatureDesiredPropertyResult(final Feature feature, final Context<ThingId> context,
final RetrieveFeatureDesiredProperty command, @Nullable final Thing thing) {
return feature.getDesiredProperties()
.map(desiredProperties -> getRetrieveFeatureDesiredPropertyResult(desiredProperties, context, command, thing))
.orElseGet(() -> ResultFactory.newErrorResult(
ExceptionFactory.featureDesiredPropertiesNotFound(context.getState(), feature.getId(),
command.getDittoHeaders()), command));
}
private Result<ThingEvent> getRetrieveFeatureDesiredPropertyResult(final JsonObject featureProperties,
final Context<ThingId> context,
final RetrieveFeatureDesiredProperty command, @Nullable final Thing thing) {
final String featureId = command.getFeatureId();
final JsonPointer propertyPointer = command.getDesiredPropertyPointer();
final DittoHeaders dittoHeaders = command.getDittoHeaders();
return featureProperties.getValue(propertyPointer)
.map(featureDesiredProperty -> RetrieveFeatureDesiredPropertyResponse.of(context.getState(), featureId,
propertyPointer, featureDesiredProperty, dittoHeaders))
.<Result<ThingEvent>>map(response ->
ResultFactory.newQueryResult(command, appendETagHeaderIfProvided(command, response, thing)))
.orElseGet(() -> ResultFactory.newErrorResult(
ExceptionFactory.featureDesiredPropertyNotFound(context.getState(), featureId, propertyPointer,
dittoHeaders), command));
}
@Override
public Optional<EntityTag> previousEntityTag(final RetrieveFeatureDesiredProperty command,
@Nullable final Thing previousEntity) {
return nextEntityTag(command, previousEntity);
}
@Override
public Optional<EntityTag> nextEntityTag(final RetrieveFeatureDesiredProperty command, @Nullable final Thing newEntity) {
return extractFeature(command, newEntity)
.flatMap(Feature::getDesiredProperties)
.flatMap(featureProperties -> featureProperties.getValue(command.getDesiredPropertyPointer()))
.flatMap(EntityTag::fromEntity);
}
}