[New] FromFlatJsonVisitor is now a StrolchRootElementVisitor
This commit is contained in:
parent
f3ea159052
commit
d95c3913d8
|
@ -5,12 +5,12 @@ import java.util.Set;
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import li.strolch.exception.StrolchModelException;
|
import li.strolch.exception.StrolchModelException;
|
||||||
import li.strolch.model.ParameterBag;
|
import li.strolch.model.*;
|
||||||
import li.strolch.model.StrolchRootElement;
|
|
||||||
import li.strolch.model.Tags.Json;
|
import li.strolch.model.Tags.Json;
|
||||||
|
import li.strolch.model.activity.Activity;
|
||||||
import li.strolch.model.parameter.Parameter;
|
import li.strolch.model.parameter.Parameter;
|
||||||
|
import li.strolch.model.visitor.StrolchRootElementVisitor;
|
||||||
import li.strolch.utils.collections.MapOfSets;
|
import li.strolch.utils.collections.MapOfSets;
|
||||||
import li.strolch.utils.dbc.DBC;
|
import li.strolch.utils.dbc.DBC;
|
||||||
|
|
||||||
|
@ -19,30 +19,36 @@ import li.strolch.utils.dbc.DBC;
|
||||||
* Maps a given {@link JsonObject} to a {@link StrolchRootElement}. All {@link Parameter Parameters} on the element are
|
* Maps a given {@link JsonObject} to a {@link StrolchRootElement}. All {@link Parameter Parameters} on the element are
|
||||||
* iterated and expected to be found as a member on the {@link JsonObject}.
|
* iterated and expected to be found as a member on the {@link JsonObject}.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
* <p>
|
||||||
* <p>
|
* <p>
|
||||||
* To ignore {@link Parameter Parameters} or {@link ParameterBag ParameterBags} use the
|
* To ignore {@link Parameter Parameters} or {@link ParameterBag ParameterBags} use the
|
||||||
* {@link #ignoreParameter(String, String)} and {@link #ignoreBag(String)} methods
|
* {@link #ignoreParameter(String, String)} and {@link #ignoreBag(String)} methods
|
||||||
* </p>
|
* </p>
|
||||||
*
|
* <p>
|
||||||
* <p>
|
* <p>
|
||||||
* {@link Parameter} can be made optional by using the {@link #optionalParameter(String, String)} method
|
* {@link Parameter} can be made optional by using the {@link #optionalParameter(String, String)} method
|
||||||
* </p>
|
* </p>
|
||||||
*
|
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
|
||||||
*
|
*
|
||||||
* @param <T>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*/
|
*/
|
||||||
public class FromFlatJsonVisitor {
|
public class FromFlatJsonVisitor implements StrolchRootElementVisitor<Void> {
|
||||||
|
|
||||||
private MapOfSets<String, String> ignoredKeys;
|
private MapOfSets<String, String> ignoredKeys;
|
||||||
private MapOfSets<String, String> optionalKeys;
|
private MapOfSets<String, String> optionalKeys;
|
||||||
|
|
||||||
|
private JsonObject srcObject;
|
||||||
|
|
||||||
public FromFlatJsonVisitor() {
|
public FromFlatJsonVisitor() {
|
||||||
this.ignoredKeys = new MapOfSets<>();
|
this.ignoredKeys = new MapOfSets<>();
|
||||||
this.optionalKeys = new MapOfSets<>();
|
this.optionalKeys = new MapOfSets<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public FromFlatJsonVisitor(JsonObject srcObject) {
|
||||||
|
this.srcObject = srcObject;
|
||||||
|
this.ignoredKeys = new MapOfSets<>();
|
||||||
|
this.optionalKeys = new MapOfSets<>();
|
||||||
|
}
|
||||||
|
|
||||||
public FromFlatJsonVisitor(MapOfSets<String, String> ignoredParams) {
|
public FromFlatJsonVisitor(MapOfSets<String, String> ignoredParams) {
|
||||||
this.ignoredKeys = new MapOfSets<>();
|
this.ignoredKeys = new MapOfSets<>();
|
||||||
this.optionalKeys = new MapOfSets<>();
|
this.optionalKeys = new MapOfSets<>();
|
||||||
|
@ -63,17 +69,58 @@ public class FromFlatJsonVisitor {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visit(StrolchRootElement element, JsonObject jsonObject) {
|
@Override
|
||||||
|
public Void visitResource(Resource dstElement) {
|
||||||
|
if (this.srcObject.has(Json.OBJECT_TYPE)) {
|
||||||
|
DBC.PRE.assertEquals("objectType must be the same, if set on JsonObject!", dstElement.getObjectType(),
|
||||||
|
srcObject.get(Json.OBJECT_TYPE).getAsString());
|
||||||
|
}
|
||||||
|
|
||||||
DBC.PRE.assertTrue("objectType must be set!", jsonObject.has(Json.OBJECT_TYPE));
|
visit(dstElement);
|
||||||
DBC.PRE.assertEquals("objectType must be the same!", element.getObjectType(),
|
return null;
|
||||||
jsonObject.get(Json.OBJECT_TYPE).getAsString());
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visitOrder(Order dstElement) {
|
||||||
|
if (this.srcObject.has(Json.OBJECT_TYPE)) {
|
||||||
|
DBC.PRE.assertEquals("objectType must be the same, if set on JsonObject!", dstElement.getObjectType(),
|
||||||
|
srcObject.get(Json.OBJECT_TYPE).getAsString());
|
||||||
|
}
|
||||||
|
|
||||||
|
visit(dstElement);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visitActivity(Activity dstElement) {
|
||||||
|
throw new UnsupportedOperationException("Activity elements are not supported by this visitor!");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void visit(StrolchRootElement dstElement, JsonObject srcObject) {
|
||||||
|
if (this.srcObject != null && this.srcObject != srcObject) {
|
||||||
|
throw new IllegalStateException("The srcObject was already set with a different value in the constructor!");
|
||||||
|
}
|
||||||
|
|
||||||
|
DBC.PRE.assertTrue("objectType must be set!", srcObject.has(Json.OBJECT_TYPE));
|
||||||
|
DBC.PRE.assertEquals("objectType must be the same!", dstElement.getObjectType(),
|
||||||
|
srcObject.get(Json.OBJECT_TYPE).getAsString());
|
||||||
|
|
||||||
|
this.srcObject = srcObject;
|
||||||
|
visit(dstElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void visit(StrolchRootElement dstElement) {
|
||||||
|
|
||||||
|
// types must be the same, if exists on source element
|
||||||
|
if (this.srcObject.has(Json.TYPE))
|
||||||
|
DBC.PRE.assertEquals("type must be the same!", dstElement.getObjectType(),
|
||||||
|
this.srcObject.get(Json.TYPE).getAsString());
|
||||||
|
|
||||||
// update name if possible
|
// update name if possible
|
||||||
if (jsonObject.has(Json.NAME))
|
if (this.srcObject.has(Json.NAME))
|
||||||
element.setName(jsonObject.get(Json.NAME).getAsString());
|
dstElement.setName(srcObject.get(Json.NAME).getAsString());
|
||||||
|
|
||||||
Set<String> bagKeySet = element.getParameterBagKeySet();
|
Set<String> bagKeySet = dstElement.getParameterBagKeySet();
|
||||||
for (String bagId : bagKeySet) {
|
for (String bagId : bagKeySet) {
|
||||||
|
|
||||||
// see if we have to ignore this bag i.e. empty set existing
|
// see if we have to ignore this bag i.e. empty set existing
|
||||||
|
@ -81,7 +128,7 @@ public class FromFlatJsonVisitor {
|
||||||
if (ignoredParamIds != null && ignoredParamIds.isEmpty())
|
if (ignoredParamIds != null && ignoredParamIds.isEmpty())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
ParameterBag parameterBag = element.getParameterBag(bagId);
|
ParameterBag parameterBag = dstElement.getParameterBag(bagId);
|
||||||
|
|
||||||
Set<String> parameterKeySet = parameterBag.getParameterKeySet();
|
Set<String> parameterKeySet = parameterBag.getParameterKeySet();
|
||||||
for (String paramId : parameterKeySet) {
|
for (String paramId : parameterKeySet) {
|
||||||
|
@ -90,7 +137,7 @@ public class FromFlatJsonVisitor {
|
||||||
if (ignoredParamIds != null && ignoredParamIds.contains(paramId))
|
if (ignoredParamIds != null && ignoredParamIds.contains(paramId))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
JsonElement jsonElement = jsonObject.get(paramId);
|
JsonElement jsonElement = this.srcObject.get(paramId);
|
||||||
if (jsonElement == null) {
|
if (jsonElement == null) {
|
||||||
if (this.optionalKeys.containsElement(bagId, paramId))
|
if (this.optionalKeys.containsElement(bagId, paramId))
|
||||||
continue;
|
continue;
|
||||||
|
@ -98,8 +145,9 @@ public class FromFlatJsonVisitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!jsonElement.isJsonPrimitive()) {
|
if (!jsonElement.isJsonPrimitive()) {
|
||||||
throw new StrolchModelException("JsonElement " + paramId + " is not a json primitive but a "
|
throw new StrolchModelException(
|
||||||
+ jsonElement.getClass().getName());
|
"JsonElement " + paramId + " is not a json primitive but a " + jsonElement.getClass()
|
||||||
|
.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
Parameter<?> parameter = parameterBag.getParameter(paramId);
|
Parameter<?> parameter = parameterBag.getParameter(paramId);
|
||||||
|
|
Loading…
Reference in New Issue