[New] New classes to flat map elements to/from Json
This commit is contained in:
parent
d2de579b84
commit
313eaf0026
|
@ -0,0 +1,14 @@
|
||||||
|
package li.strolch.rest.visitor;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
import li.strolch.model.activity.Activity;
|
||||||
|
import li.strolch.model.visitor.ActivityVisitor;
|
||||||
|
|
||||||
|
public class ActivityToFlatJsonVisitor extends ToFlatJsonVisitor implements ActivityVisitor<JsonObject> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JsonObject visit(Activity element) {
|
||||||
|
return toJson(element);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,83 @@
|
||||||
|
package li.strolch.rest.visitor;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
import li.strolch.exception.StrolchModelException;
|
||||||
|
import li.strolch.model.ParameterBag;
|
||||||
|
import li.strolch.model.StrolchRootElement;
|
||||||
|
import li.strolch.model.parameter.Parameter;
|
||||||
|
import li.strolch.utils.collections.MapOfSets;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Maps a given {@link JsonObject} to a {@link StrolchRootElement}. All {@link Parameter Parameters} on the element are
|
||||||
|
* iterated expected to be found as a member on the {@link JsonObject}.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* To not map a {@link Parameter} add it to the {@link MapOfSets}, to ignore a whole {@link ParameterBag} then add an
|
||||||
|
* empty set with the bag id.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
|
*
|
||||||
|
* @param <T>
|
||||||
|
*/
|
||||||
|
public class FromFlatJsonVisitor<T extends StrolchRootElement> {
|
||||||
|
|
||||||
|
private MapOfSets<String, String> ignoredKeys;
|
||||||
|
|
||||||
|
public FromFlatJsonVisitor() {
|
||||||
|
this.ignoredKeys = new MapOfSets<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public FromFlatJsonVisitor(MapOfSets<String, String> ignoredParams) {
|
||||||
|
this.ignoredKeys = new MapOfSets<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ignoreBag(String bagId) {
|
||||||
|
this.ignoredKeys.addSet(bagId, Collections.emptySet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ignoreParameter(String bagId, String paramId) {
|
||||||
|
this.ignoredKeys.addElement(bagId, paramId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void visit(T element, JsonObject jsonObject) {
|
||||||
|
|
||||||
|
Set<String> bagKeySet = element.getParameterBagKeySet();
|
||||||
|
for (String bagId : bagKeySet) {
|
||||||
|
|
||||||
|
// see if we have to ignore this bag i.e. empty set existing
|
||||||
|
Set<String> paramIds = this.ignoredKeys.getSet(bagId);
|
||||||
|
if (paramIds != null && paramIds.isEmpty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
ParameterBag parameterBag = element.getParameterBag(bagId);
|
||||||
|
|
||||||
|
Set<String> parameterKeySet = parameterBag.getParameterKeySet();
|
||||||
|
for (String paramId : parameterKeySet) {
|
||||||
|
|
||||||
|
// see if this parameter must be ignored
|
||||||
|
if (paramIds != null && paramIds.contains(paramId))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
JsonElement jsonElement = jsonObject.get(paramId);
|
||||||
|
if (jsonElement == null)
|
||||||
|
throw new StrolchModelException("JsonObject is missing member with ID " + paramId);
|
||||||
|
|
||||||
|
if (!jsonElement.isJsonPrimitive()) {
|
||||||
|
throw new StrolchModelException("JsonElement " + paramId + " is not a json primitive but a "
|
||||||
|
+ jsonElement.getClass().getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
Parameter<?> parameter = parameterBag.getParameter(paramId);
|
||||||
|
parameter.setValueFromString(jsonElement.getAsString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package li.strolch.rest.visitor;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
import li.strolch.model.Order;
|
||||||
|
import li.strolch.model.visitor.OrderVisitor;
|
||||||
|
|
||||||
|
public class OrderToFlatJsonVisitor extends ToFlatJsonVisitor implements OrderVisitor<JsonObject> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JsonObject visit(Order element) {
|
||||||
|
return toJson(element);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package li.strolch.rest.visitor;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
import li.strolch.model.Resource;
|
||||||
|
import li.strolch.model.visitor.ResourceVisitor;
|
||||||
|
|
||||||
|
public class ResourceToFlatJsonVisitor extends ToFlatJsonVisitor implements ResourceVisitor<JsonObject> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JsonObject visit(Resource element) {
|
||||||
|
return toJson(element);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,88 @@
|
||||||
|
package li.strolch.rest.visitor;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
import li.strolch.exception.StrolchModelException;
|
||||||
|
import li.strolch.model.ParameterBag;
|
||||||
|
import li.strolch.model.StrolchRootElement;
|
||||||
|
import li.strolch.model.StrolchValueType;
|
||||||
|
import li.strolch.model.parameter.Parameter;
|
||||||
|
import li.strolch.utils.collections.MapOfSets;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Maps a given {@link StrolchRootElement} to a {@link JsonObject}. All {@link Parameter Parameters} are mapped to a
|
||||||
|
* member of the {@link JsonObject}.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* To not map a {@link Parameter} add it to the {@link MapOfSets}, to ignore a whole {@link ParameterBag} then add an
|
||||||
|
* empty set with the bag id.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
|
*/
|
||||||
|
public class ToFlatJsonVisitor {
|
||||||
|
|
||||||
|
private MapOfSets<String, String> ignoredKeys;
|
||||||
|
|
||||||
|
public ToFlatJsonVisitor() {
|
||||||
|
this.ignoredKeys = new MapOfSets<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ToFlatJsonVisitor(MapOfSets<String, String> ignoredParams) {
|
||||||
|
this.ignoredKeys = new MapOfSets<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ignoreBag(String bagId) {
|
||||||
|
this.ignoredKeys.addSet(bagId, Collections.emptySet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ignoreParameter(String bagId, String paramId) {
|
||||||
|
this.ignoredKeys.addElement(bagId, paramId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonObject toJson(StrolchRootElement element) {
|
||||||
|
|
||||||
|
JsonObject jsonObject = new JsonObject();
|
||||||
|
|
||||||
|
Set<String> bagKeySet = element.getParameterBagKeySet();
|
||||||
|
for (String bagId : bagKeySet) {
|
||||||
|
|
||||||
|
// see if we have to ignore this bag i.e. empty set existing
|
||||||
|
Set<String> paramIds = this.ignoredKeys.getSet(bagId);
|
||||||
|
if (paramIds != null && paramIds.isEmpty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
ParameterBag parameterBag = element.getParameterBag(bagId);
|
||||||
|
|
||||||
|
Set<String> parameterKeySet = parameterBag.getParameterKeySet();
|
||||||
|
for (String paramId : parameterKeySet) {
|
||||||
|
|
||||||
|
// see if this parameter must be ignored
|
||||||
|
if (paramIds != null && paramIds.contains(paramId))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (jsonObject.has(paramId)) {
|
||||||
|
throw new StrolchModelException("JsonObject already has a member with ID " + paramId);
|
||||||
|
}
|
||||||
|
|
||||||
|
Parameter<?> param = parameterBag.getParameter(paramId);
|
||||||
|
|
||||||
|
StrolchValueType type = StrolchValueType.parse(param.getType());
|
||||||
|
if (type.isBoolean()) {
|
||||||
|
jsonObject.addProperty(paramId, (Boolean) param.getValue());
|
||||||
|
} else if (type.isNumber()) {
|
||||||
|
jsonObject.addProperty(paramId, (Number) param.getValue());
|
||||||
|
} else {
|
||||||
|
jsonObject.addProperty(paramId, param.getValueAsString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue