-
Notifications
You must be signed in to change notification settings - Fork 215
/
AbstractThingEventStrategy.java
106 lines (95 loc) · 4.55 KB
/
AbstractThingEventStrategy.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
/*
* 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.things.service.persistence.actors.strategies.events;
import java.util.Optional;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.entity.metadata.Metadata;
import org.eclipse.ditto.base.model.entity.metadata.MetadataBuilder;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.internal.utils.persistentactors.events.EventStrategy;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingBuilder;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.things.model.signals.events.ThingModifiedEvent;
/**
* This abstract implementation of {@code EventStrategy} checks if the Thing to be handled is {@code null}.
* If the Thing is {@code null} the {@code handle} method returns with {@code null}; otherwise a ThingBuilder will be
* derived from the Thing with the revision and modified timestamp set.
* This builder is then passed to the {@link #applyEvent(T, org.eclipse.ditto.things.model.ThingBuilder.FromCopy)}
* method for further handling.
* However, sub-classes are free to implement the {@code handle} method directly and thus completely circumvent the
* {@code applyEvent} method.
*
* @param <T> the type of the handled ThingEvent.
*/
@Immutable
abstract class AbstractThingEventStrategy<T extends ThingEvent<T>> implements EventStrategy<T, Thing> {
/**
* Constructs a new {@code AbstractThingEventStrategy} object.
*/
protected AbstractThingEventStrategy() {
super();
}
@Nullable
@Override
public Thing handle(final T event, @Nullable final Thing thing, final long revision) {
if (null != thing) {
final ThingBuilder.FromCopy thingBuilder = thing.toBuilder()
.setRevision(revision)
.setModified(event.getTimestamp().orElse(null))
.setMetadata(mergeMetadata(thing, event));
return applyEvent(event, thingBuilder).build();
}
return null;
}
@Nullable
protected Metadata mergeMetadata(@Nullable final Thing thing, final T event) {
final JsonPointer eventMetadataResourcePath = event.getResourcePath();
final Optional<Metadata> eventMetadataOpt = event.getMetadata();
final Optional<Metadata> thingMetadata = deleteMetadataForDeletedEvent(thing, event);
if (eventMetadataResourcePath.isEmpty() && eventMetadataOpt.isPresent()) {
return eventMetadataOpt.get();
} else if (eventMetadataOpt.isPresent()) {
final Metadata eventMetadata = eventMetadataOpt.get();
final MetadataBuilder metadataBuilder =
thingMetadata.map(Metadata::toBuilder).orElseGet(Metadata::newBuilder);
metadataBuilder.set(eventMetadataResourcePath, eventMetadata.toJson());
return metadataBuilder.build();
} else {
return thingMetadata.orElse(null);
}
}
/**
* Apply the specified event to the also specified ThingBuilder.
* The builder has already the specified revision set as well as the event's timestamp.
*
* @param event the ThingEvent<?>to be applied.
* @param thingBuilder builder which is derived from the {@code event}'s Thing with the revision and event
* timestamp already set.
* @return the updated {@code thingBuilder} after applying {@code event}.
*/
protected ThingBuilder.FromCopy applyEvent(final T event, final ThingBuilder.FromCopy thingBuilder) {
return thingBuilder;
}
private Optional<Metadata> deleteMetadataForDeletedEvent(@Nullable final Thing thing, final T event) {
final Optional<Metadata> optionalMetadata = Optional.ofNullable(thing).flatMap(Thing::getMetadata);
// delete metadata for delete events
if (event instanceof ThingModifiedEvent && event.getCommandCategory().equals(Command.Category.DELETE)) {
return optionalMetadata.map(metadata -> metadata.toBuilder().remove(event.getResourcePath()).build());
} else {
return optionalMetadata;
}
}
}