-
Notifications
You must be signed in to change notification settings - Fork 214
/
ThingMongoEventAdapter.java
60 lines (52 loc) · 2.69 KB
/
ThingMongoEventAdapter.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
/*
* 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.serializer;
import org.eclipse.ditto.base.model.signals.events.Event;
import org.eclipse.ditto.base.model.signals.events.GlobalEventRegistry;
import org.eclipse.ditto.base.service.config.DittoServiceConfig;
import org.eclipse.ditto.internal.utils.config.DefaultScopedConfig;
import org.eclipse.ditto.internal.utils.persistence.mongo.AbstractMongoEventAdapter;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.things.model.signals.events.ThingMerged;
import org.eclipse.ditto.things.service.common.config.DefaultThingConfig;
import akka.actor.ExtendedActorSystem;
/**
* EventAdapter for {@link Event}s persisted into akka-persistence event-journal. Converts Event to MongoDB
* BSON objects and vice versa.
*/
public final class ThingMongoEventAdapter extends AbstractMongoEventAdapter<ThingEvent<?>> {
private static final JsonPointer POLICY_IN_THING_EVENT_PAYLOAD = ThingEvent.JsonFields.THING.getPointer()
.append(JsonPointer.of(Policy.INLINED_FIELD_NAME));
private static final JsonPointer POLICY_IN_THING_MERGED_VALUE_PAYLOAD =
ThingMerged.JsonFields.JSON_VALUE.getPointer()
.append(JsonPointer.of(Policy.INLINED_FIELD_NAME));
public ThingMongoEventAdapter(final ExtendedActorSystem system) {
super(system, GlobalEventRegistry.getInstance(), DefaultThingConfig.of(
DittoServiceConfig.of(DefaultScopedConfig.dittoScoped(system.settings().config()), "things")
).getEventConfig());
}
@Override
protected JsonObjectBuilder performToJournalMigration(final Event<?> event, final JsonObject jsonObject) {
if (event instanceof ThingMerged) {
return super.performToJournalMigration(event, jsonObject)
.remove(POLICY_IN_THING_MERGED_VALUE_PAYLOAD); // remove the policy entries from thing merged payload
} else {
return super.performToJournalMigration(event, jsonObject)
.remove(POLICY_IN_THING_EVENT_PAYLOAD); // remove the policy entries from thing event payload
}
}
}