12
12
*/
13
13
package org .eclipse .ditto .things .model .signals .events ;
14
14
15
- import static org .eclipse .ditto .base .model .common .ConditionChecker .checkNotNull ;
16
15
17
16
import java .time .Instant ;
18
17
import java .util .Objects ;
27
26
import org .eclipse .ditto .base .model .json .FieldType ;
28
27
import org .eclipse .ditto .base .model .json .JsonParsableEvent ;
29
28
import org .eclipse .ditto .base .model .json .JsonSchemaVersion ;
30
- import org .eclipse .ditto .base .model .signals .FeatureToggle ;
31
29
import org .eclipse .ditto .base .model .signals .UnsupportedSchemaVersionException ;
32
30
import org .eclipse .ditto .base .model .signals .commands .Command ;
33
31
import org .eclipse .ditto .base .model .signals .events .EventJsonDeserializer ;
38
36
import org .eclipse .ditto .json .JsonObjectBuilder ;
39
37
import org .eclipse .ditto .json .JsonPointer ;
40
38
import org .eclipse .ditto .json .JsonValue ;
41
- import org .eclipse .ditto .things .model .ThingId ;
39
+ import org .eclipse .ditto .things .model .Thing ;
40
+ import org .eclipse .ditto .things .model .ThingsModelFactory ;
42
41
43
42
/**
44
43
* This event is emitted after a {@link org.eclipse.ditto.things.model.Thing} was successfully migrated.
@@ -59,44 +58,34 @@ public final class ThingMigrated extends AbstractThingEvent<ThingMigrated> imple
59
58
*/
60
59
public static final String TYPE = TYPE_PREFIX + NAME ;
61
60
62
- private final ThingId thingId ;
63
- private final JsonPointer path ;
64
- private final JsonValue value ;
61
+ private final Thing thing ;
65
62
66
- private ThingMigrated (final ThingId thingId ,
67
- final JsonPointer path ,
68
- final JsonValue value ,
69
- final long revision ,
70
- @ Nullable final Instant timestamp ,
71
- final DittoHeaders dittoHeaders ,
72
- @ Nullable final Metadata metadata ) {
73
- super (TYPE , thingId , revision , timestamp , FeatureToggle .checkMergeFeatureEnabled (TYPE , dittoHeaders ), metadata );
74
- this .thingId = checkNotNull (thingId , "thingId" );
75
- this .path = checkNotNull (path , "path" );
76
- this .value = checkNotNull (value , "value" );
63
+ private ThingMigrated (final Thing thing ,
64
+ final long revision ,
65
+ @ Nullable final Instant timestamp ,
66
+ final DittoHeaders dittoHeaders ,
67
+ @ Nullable final Metadata metadata ) {
68
+ super (TYPE , thing .getEntityId ().orElseThrow (() -> new NullPointerException ("Thing has no ID!" )), revision , timestamp , dittoHeaders , metadata );
69
+ this .thing = thing ;
77
70
checkSchemaVersion ();
78
71
}
79
72
80
73
/**
81
74
* Creates an event of a migrated thing.
82
75
*
83
- * @param thingId The thing ID.
84
- * @param path The path where the changes were applied.
85
- * @param value The value describing the changes that were migrated.
76
+ * @param thing the created {@link org.eclipse.ditto.things.model.Thing}.
86
77
* @param revision The revision number of the thing.
87
78
* @param timestamp The event timestamp.
88
79
* @param dittoHeaders The Ditto headers.
89
80
* @param metadata The metadata associated with the event.
90
81
* @return The created {@code ThingMigrated} event.
91
82
*/
92
- public static ThingMigrated of (final ThingId thingId ,
93
- final JsonPointer path ,
94
- final JsonValue value ,
83
+ public static ThingMigrated of (final Thing thing ,
95
84
final long revision ,
96
85
@ Nullable final Instant timestamp ,
97
86
final DittoHeaders dittoHeaders ,
98
87
@ Nullable final Metadata metadata ) {
99
- return new ThingMigrated (thingId , path , value , revision , timestamp , dittoHeaders , metadata );
88
+ return new ThingMigrated (thing , revision , timestamp , dittoHeaders , metadata );
100
89
}
101
90
102
91
/**
@@ -109,25 +98,23 @@ public static ThingMigrated of(final ThingId thingId,
109
98
public static ThingMigrated fromJson (final JsonObject jsonObject , final DittoHeaders dittoHeaders ) {
110
99
return new EventJsonDeserializer <ThingMigrated >(TYPE , jsonObject ).deserialize (
111
100
(revision , timestamp , metadata ) -> {
112
- final ThingId thingId = ThingId .of (jsonObject .getValueOrThrow (ThingEvent .JsonFields .THING_ID ));
113
- final JsonPointer path = JsonPointer .of (jsonObject .getValueOrThrow (JsonFields .JSON_PATH ));
114
- final JsonValue value = jsonObject .getValueOrThrow (JsonFields .JSON_VALUE );
101
+ final JsonObject thingJsonObject = jsonObject .getValueOrThrow (ThingEvent .JsonFields .THING );
102
+ final Thing extractedThing = ThingsModelFactory .newThing (thingJsonObject );
115
103
116
- return of (thingId , path , value , revision , timestamp , dittoHeaders , metadata );
104
+ return of (extractedThing , revision , timestamp , dittoHeaders , metadata );
117
105
});
118
106
}
119
107
120
108
@ Override
121
109
protected void appendPayload (final JsonObjectBuilder jsonObjectBuilder ,
122
- final JsonSchemaVersion schemaVersion , final Predicate <JsonField > predicate ) {
123
- jsonObjectBuilder .set (ThingEvent .JsonFields .THING_ID , thingId .toString (), predicate );
124
- jsonObjectBuilder .set (JsonFields .JSON_PATH , path .toString (), predicate );
125
- jsonObjectBuilder .set (JsonFields .JSON_VALUE , value , predicate );
110
+ final JsonSchemaVersion schemaVersion , final Predicate <JsonField > thePredicate ) {
111
+ final Predicate <JsonField > predicate = schemaVersion .and (thePredicate );
112
+ jsonObjectBuilder .set (ThingEvent .JsonFields .THING , thing .toJson (schemaVersion , thePredicate ), predicate );
126
113
}
127
114
128
115
@ Override
129
116
public ThingMigrated setDittoHeaders (final DittoHeaders dittoHeaders ) {
130
- return of (thingId , path , value , getRevision (), getTimestamp ().orElse (null ), dittoHeaders ,
117
+ return of (thing , getRevision (), getTimestamp ().orElse (null ), dittoHeaders ,
131
118
getMetadata ().orElse (null ));
132
119
}
133
120
@@ -138,28 +125,26 @@ public Command.Category getCommandCategory() {
138
125
139
126
@ Override
140
127
public ThingMigrated setRevision (final long revision ) {
141
- return of (thingId , path , value , revision , getTimestamp ().orElse (null ), getDittoHeaders (),
128
+ return of (thing , revision , getTimestamp ().orElse (null ), getDittoHeaders (),
142
129
getMetadata ().orElse (null ));
143
130
}
144
131
145
- @ Override
146
- public JsonPointer getResourcePath () {
147
- return path ;
148
- }
149
-
150
- public JsonValue getValue () {
151
- return value ;
132
+ /**
133
+ * @return the value describing the changes that were applied to the existing thing.
134
+ */
135
+ public Thing getThing () {
136
+ return thing ;
152
137
}
153
138
154
139
@ Override
155
- public Optional <JsonValue > getEntity () {
156
- return Optional .of (value );
140
+ public Optional <JsonValue > getEntity (final JsonSchemaVersion schemaVersion ) {
141
+ return Optional .of (thing . toJson ( schemaVersion , FieldType . notHidden ()) );
157
142
}
158
143
159
144
@ Override
160
145
public ThingMigrated setEntity (final JsonValue entity ) {
161
- return of (thingId , path , entity , getRevision (), getTimestamp ().orElse (null ), getDittoHeaders ( ),
162
- getMetadata ().orElse (null ));
146
+ return of (ThingsModelFactory . newThing ( entity . asObject ()) , getRevision (), getTimestamp ().orElse (null ),
147
+ getDittoHeaders (), getMetadata ().orElse (null ));
163
148
}
164
149
165
150
@ Override
@@ -186,23 +171,25 @@ public boolean equals(final Object o) {
186
171
return false ;
187
172
}
188
173
final ThingMigrated that = (ThingMigrated ) o ;
189
- return that .canEqual (this ) && thingId .equals (that .thingId ) &&
190
- path .equals (that .path ) &&
191
- value .equals (that .value );
174
+ return that .canEqual (this ) && thing .equals (that .thing );
192
175
}
193
176
194
177
@ Override
195
178
public int hashCode () {
196
- return Objects .hash (super .hashCode (), thingId , path , value );
179
+ return Objects .hash (super .hashCode (), thing );
197
180
}
198
181
@ Override
199
182
public String toString () {
200
183
return getClass ().getSimpleName () + " [" + super .toString () +
201
- ", path=" + path +
202
- ", value=" + value +
184
+ ", thing=" + thing +
203
185
"]" ;
204
186
}
205
187
188
+ @ Override
189
+ public JsonPointer getResourcePath () {
190
+ return JsonPointer .empty ();
191
+ }
192
+
206
193
/**
207
194
* An enumeration of the JSON fields of a {@code ThingMigrated} event.
208
195
*/
@@ -212,9 +199,6 @@ private JsonFields() {
212
199
throw new AssertionError ();
213
200
}
214
201
215
- public static final JsonFieldDefinition <String > JSON_PATH =
216
- JsonFactory .newStringFieldDefinition ("path" , FieldType .REGULAR , JsonSchemaVersion .V_2 );
217
-
218
202
public static final JsonFieldDefinition <JsonValue > JSON_VALUE =
219
203
JsonFactory .newJsonValueFieldDefinition ("value" , FieldType .REGULAR , JsonSchemaVersion .V_2 );
220
204
}
0 commit comments