[Fix] Don't write empty stateId, but care when parsing on ValueChange
This commit is contained in:
parent
277a57dda3
commit
62fb8671fb
|
@ -1,12 +1,12 @@
|
|||
/*
|
||||
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
@ -23,20 +23,9 @@ import java.util.Set;
|
|||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import li.strolch.exception.StrolchException;
|
||||
import li.strolch.model.AbstractStrolchElement;
|
||||
import li.strolch.model.GroupedParameterizedElement;
|
||||
import li.strolch.model.Order;
|
||||
import li.strolch.model.ParameterBag;
|
||||
import li.strolch.model.ParameterizedElement;
|
||||
import li.strolch.model.Resource;
|
||||
import li.strolch.model.State;
|
||||
import li.strolch.model.StrolchRootElement;
|
||||
import li.strolch.model.StrolchValueType;
|
||||
import li.strolch.model.Tags;
|
||||
import li.strolch.model.*;
|
||||
import li.strolch.model.Tags.Json;
|
||||
import li.strolch.model.Version;
|
||||
import li.strolch.model.activity.Action;
|
||||
import li.strolch.model.activity.Activity;
|
||||
import li.strolch.model.activity.TimeOrdering;
|
||||
|
@ -71,15 +60,15 @@ public class StrolchElementFromJsonVisitor {
|
|||
order.setPolicyDefs(defs);
|
||||
|
||||
// attributes
|
||||
if (jsonObject.has(Tags.Json.DATE)) {
|
||||
String date = jsonObject.get(Tags.Json.DATE).getAsString();
|
||||
if (jsonObject.has(Json.DATE)) {
|
||||
String date = jsonObject.get(Json.DATE).getAsString();
|
||||
order.setDate(ISO8601FormatFactory.getInstance().getDateFormat().parse(date));
|
||||
} else {
|
||||
order.setDate(ISO8601FormatFactory.getInstance().getDateFormat().parse(StringHelper.DASH)); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
if (jsonObject.has(Tags.Json.STATE)) {
|
||||
order.setState(State.parse(jsonObject.get(Tags.Json.STATE).getAsString()));
|
||||
if (jsonObject.has(Json.STATE)) {
|
||||
order.setState(State.parse(jsonObject.get(Json.STATE).getAsString()));
|
||||
} else {
|
||||
order.setState(State.CREATED);
|
||||
}
|
||||
|
@ -101,10 +90,10 @@ public class StrolchElementFromJsonVisitor {
|
|||
resource.setPolicyDefs(defs);
|
||||
|
||||
// time states
|
||||
if (!jsonObject.has(Tags.Json.TIMED_STATES))
|
||||
if (!jsonObject.has(Json.TIMED_STATES))
|
||||
return;
|
||||
|
||||
JsonObject timedStatesJ = jsonObject.getAsJsonObject(Tags.Json.TIMED_STATES);
|
||||
JsonObject timedStatesJ = jsonObject.getAsJsonObject(Json.TIMED_STATES);
|
||||
Set<Entry<String, JsonElement>> entrySet = timedStatesJ.entrySet();
|
||||
for (Entry<String, JsonElement> entry : entrySet) {
|
||||
|
||||
|
@ -112,7 +101,7 @@ public class StrolchElementFromJsonVisitor {
|
|||
JsonObject timeStateJ = entry.getValue().getAsJsonObject();
|
||||
|
||||
// evaluate type of TimedState
|
||||
String typeS = timeStateJ.get(Tags.Json.TYPE).getAsString();
|
||||
String typeS = timeStateJ.get(Json.TYPE).getAsString();
|
||||
DBC.PRE.assertNotEmpty("Type must be set on TimedState for resource with id " + resource.getId(), typeS);
|
||||
StrolchValueType valueType = StrolchValueType.parse(typeS);
|
||||
StrolchTimedState<? extends IValue<?>> timedState = valueType.timedStateInstance();
|
||||
|
@ -128,29 +117,29 @@ public class StrolchElementFromJsonVisitor {
|
|||
}
|
||||
|
||||
// further attributes
|
||||
if (timeStateJ.has(Tags.Json.INTERPRETATION))
|
||||
timedState.setInterpretation(timeStateJ.get(Tags.Json.INTERPRETATION).getAsString());
|
||||
if (timeStateJ.has(Tags.Json.UOM))
|
||||
timedState.setUom(timeStateJ.get(Tags.Json.UOM).getAsString());
|
||||
if (timeStateJ.has(Tags.Json.INDEX))
|
||||
timedState.setIndex(timeStateJ.get(Tags.Json.INDEX).getAsInt());
|
||||
if (timeStateJ.has(Tags.Json.HIDDEN))
|
||||
timedState.setHidden(timeStateJ.get(Tags.Json.HIDDEN).getAsBoolean());
|
||||
if (timeStateJ.has(Json.INTERPRETATION))
|
||||
timedState.setInterpretation(timeStateJ.get(Json.INTERPRETATION).getAsString());
|
||||
if (timeStateJ.has(Json.UOM))
|
||||
timedState.setUom(timeStateJ.get(Json.UOM).getAsString());
|
||||
if (timeStateJ.has(Json.INDEX))
|
||||
timedState.setIndex(timeStateJ.get(Json.INDEX).getAsInt());
|
||||
if (timeStateJ.has(Json.HIDDEN))
|
||||
timedState.setHidden(timeStateJ.get(Json.HIDDEN).getAsBoolean());
|
||||
|
||||
resource.addTimedState(timedState);
|
||||
|
||||
if (!timeStateJ.has(Tags.Json.VALUES))
|
||||
if (!timeStateJ.has(Json.VALUES))
|
||||
continue;
|
||||
|
||||
JsonArray valuesJ = timeStateJ.getAsJsonArray(Tags.Json.VALUES);
|
||||
JsonArray valuesJ = timeStateJ.getAsJsonArray(Json.VALUES);
|
||||
valuesJ.forEach(e -> {
|
||||
|
||||
JsonObject timeValueJ = e.getAsJsonObject();
|
||||
|
||||
String timeS = timeValueJ.get(Tags.Json.TIME).getAsString();
|
||||
String timeS = timeValueJ.get(Json.TIME).getAsString();
|
||||
long time = ISO8601FormatFactory.getInstance().parseDate(timeS).getTime();
|
||||
|
||||
String valueS = timeValueJ.get(Tags.Json.VALUE).getAsString();
|
||||
String valueS = timeValueJ.get(Json.VALUE).getAsString();
|
||||
timedState.setStateFromStringAt(time, valueS);
|
||||
});
|
||||
}
|
||||
|
@ -164,9 +153,9 @@ public class StrolchElementFromJsonVisitor {
|
|||
|
||||
fillElement(jsonObject, (GroupedParameterizedElement) activity);
|
||||
|
||||
if (!jsonObject.has(Tags.Json.TIME_ORDERING))
|
||||
if (!jsonObject.has(Json.TIME_ORDERING))
|
||||
throw new StrolchException("TimeOrdering not set on " + activity.getLocator());
|
||||
String timeOrderingS = jsonObject.get(Tags.Json.TIME_ORDERING).getAsString();
|
||||
String timeOrderingS = jsonObject.get(Json.TIME_ORDERING).getAsString();
|
||||
TimeOrdering timeOrdering = TimeOrdering.parse(timeOrderingS);
|
||||
activity.setTimeOrdering(timeOrdering);
|
||||
|
||||
|
@ -177,23 +166,23 @@ public class StrolchElementFromJsonVisitor {
|
|||
if (defs.hasPolicyDefs())
|
||||
activity.setPolicyDefs(defs);
|
||||
|
||||
if (!jsonObject.has(Tags.Json.ELEMENTS))
|
||||
if (!jsonObject.has(Json.ELEMENTS))
|
||||
return;
|
||||
|
||||
JsonArray elementsJsonArray = jsonObject.getAsJsonArray(Tags.Json.ELEMENTS);
|
||||
JsonArray elementsJsonArray = jsonObject.getAsJsonArray(Json.ELEMENTS);
|
||||
elementsJsonArray.forEach(e -> {
|
||||
|
||||
JsonObject elementJsonObject = e.getAsJsonObject();
|
||||
String objectType = elementJsonObject.get(Tags.Json.OBJECT_TYPE).getAsString();
|
||||
String objectType = elementJsonObject.get(Json.OBJECT_TYPE).getAsString();
|
||||
|
||||
switch (objectType) {
|
||||
case Tags.Json.ACTIVITY:
|
||||
case Json.ACTIVITY:
|
||||
Activity childActivity = new Activity();
|
||||
fillElement(elementJsonObject, childActivity);
|
||||
activity.addElement(childActivity);
|
||||
break;
|
||||
|
||||
case Tags.Json.ACTION:
|
||||
case Json.ACTION:
|
||||
Action childAction = new Action();
|
||||
fillElement(elementJsonObject, childAction);
|
||||
activity.addElement(childAction);
|
||||
|
@ -208,9 +197,9 @@ public class StrolchElementFromJsonVisitor {
|
|||
}
|
||||
|
||||
protected void fillElement(JsonObject jsonObject, AbstractStrolchElement strolchElement) {
|
||||
if (jsonObject.has(Tags.Json.ID) && jsonObject.has(Tags.Json.NAME)) {
|
||||
strolchElement.setId(jsonObject.get(Tags.Json.ID).getAsString());
|
||||
strolchElement.setName(jsonObject.get(Tags.Json.NAME).getAsString());
|
||||
if (jsonObject.has(Json.ID) && jsonObject.has(Json.NAME)) {
|
||||
strolchElement.setId(jsonObject.get(Json.ID).getAsString());
|
||||
strolchElement.setName(jsonObject.get(Json.NAME).getAsString());
|
||||
} else {
|
||||
String msg = "Check the values of the jsonObject: {0} either id or name attribute is null!"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, jsonObject);
|
||||
|
@ -221,8 +210,8 @@ public class StrolchElementFromJsonVisitor {
|
|||
protected void fillElement(JsonObject jsonObject, GroupedParameterizedElement groupedParameterizedElement) {
|
||||
fillElement(jsonObject, (AbstractStrolchElement) groupedParameterizedElement);
|
||||
|
||||
if (jsonObject.has(Tags.Json.TYPE)) {
|
||||
groupedParameterizedElement.setType(jsonObject.get(Tags.Json.TYPE).getAsString());
|
||||
if (jsonObject.has(Json.TYPE)) {
|
||||
groupedParameterizedElement.setType(jsonObject.get(Json.TYPE).getAsString());
|
||||
} else {
|
||||
String msg = "Check the values of the jsonObject: {0} type attribute is null!"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, jsonObject);
|
||||
|
@ -230,10 +219,10 @@ public class StrolchElementFromJsonVisitor {
|
|||
}
|
||||
|
||||
// add all the parameter bags
|
||||
if (!jsonObject.has(Tags.Json.PARAMETER_BAGS))
|
||||
if (!jsonObject.has(Json.PARAMETER_BAGS))
|
||||
return;
|
||||
|
||||
JsonObject bagsJsonObject = jsonObject.getAsJsonObject(Tags.Json.PARAMETER_BAGS);
|
||||
JsonObject bagsJsonObject = jsonObject.getAsJsonObject(Json.PARAMETER_BAGS);
|
||||
|
||||
Set<Entry<String, JsonElement>> bags = bagsJsonObject.entrySet();
|
||||
for (Entry<String, JsonElement> entry : bags) {
|
||||
|
@ -261,8 +250,8 @@ public class StrolchElementFromJsonVisitor {
|
|||
protected void fillElement(JsonObject jsonObject, ParameterizedElement parameterizedElement) {
|
||||
fillElement(jsonObject, (AbstractStrolchElement) parameterizedElement);
|
||||
|
||||
if (jsonObject.has(Tags.Json.TYPE)) {
|
||||
parameterizedElement.setType(jsonObject.get(Tags.Json.TYPE).getAsString());
|
||||
if (jsonObject.has(Json.TYPE)) {
|
||||
parameterizedElement.setType(jsonObject.get(Json.TYPE).getAsString());
|
||||
} else {
|
||||
String msg = "Check the values of the jsonObject: {0} type attribute is null!"; //$NON-NLS-1$
|
||||
msg = MessageFormat.format(msg, jsonObject);
|
||||
|
@ -270,10 +259,10 @@ public class StrolchElementFromJsonVisitor {
|
|||
}
|
||||
|
||||
// add all the parameters
|
||||
if (!jsonObject.has(Tags.Json.PARAMETERS))
|
||||
if (!jsonObject.has(Json.PARAMETERS))
|
||||
return;
|
||||
|
||||
JsonObject parametersJsonObject = jsonObject.getAsJsonObject(Tags.Json.PARAMETERS);
|
||||
JsonObject parametersJsonObject = jsonObject.getAsJsonObject(Json.PARAMETERS);
|
||||
Set<Entry<String, JsonElement>> parameters = parametersJsonObject.entrySet();
|
||||
for (Entry<String, JsonElement> entry : parameters) {
|
||||
String paramId = entry.getKey();
|
||||
|
@ -285,7 +274,7 @@ public class StrolchElementFromJsonVisitor {
|
|||
}
|
||||
|
||||
JsonObject paramJsonObject = jsonElement.getAsJsonObject();
|
||||
String paramtype = paramJsonObject.get(Tags.Json.TYPE).getAsString();
|
||||
String paramtype = paramJsonObject.get(Json.TYPE).getAsString();
|
||||
|
||||
StrolchValueType paramValueType = StrolchValueType.parse(paramtype);
|
||||
Parameter<?> parameter = paramValueType.parameterInstance();
|
||||
|
@ -303,16 +292,16 @@ public class StrolchElementFromJsonVisitor {
|
|||
protected void fillElement(JsonObject jsonObject, Parameter<?> param) {
|
||||
fillElement(jsonObject, (AbstractStrolchElement) param);
|
||||
|
||||
if (jsonObject.has(Tags.Json.INTERPRETATION))
|
||||
param.setInterpretation(jsonObject.get(Tags.Json.INTERPRETATION).getAsString());
|
||||
if (jsonObject.has(Tags.Json.UOM))
|
||||
param.setUom(jsonObject.get(Tags.Json.UOM).getAsString());
|
||||
if (jsonObject.has(Tags.Json.INDEX))
|
||||
param.setIndex(jsonObject.get(Tags.Json.INDEX).getAsInt());
|
||||
if (jsonObject.has(Tags.Json.HIDDEN))
|
||||
param.setHidden(jsonObject.get(Tags.Json.HIDDEN).getAsBoolean());
|
||||
if (jsonObject.has(Json.INTERPRETATION))
|
||||
param.setInterpretation(jsonObject.get(Json.INTERPRETATION).getAsString());
|
||||
if (jsonObject.has(Json.UOM))
|
||||
param.setUom(jsonObject.get(Json.UOM).getAsString());
|
||||
if (jsonObject.has(Json.INDEX))
|
||||
param.setIndex(jsonObject.get(Json.INDEX).getAsInt());
|
||||
if (jsonObject.has(Json.HIDDEN))
|
||||
param.setHidden(jsonObject.get(Json.HIDDEN).getAsBoolean());
|
||||
|
||||
String value = jsonObject.get(Tags.Json.VALUE).getAsString();
|
||||
String value = jsonObject.get(Json.VALUE).getAsString();
|
||||
param.setValueFromString(value);
|
||||
}
|
||||
|
||||
|
@ -320,12 +309,12 @@ public class StrolchElementFromJsonVisitor {
|
|||
fillElement(jsonObject, (GroupedParameterizedElement) action);
|
||||
|
||||
// attributes
|
||||
if (jsonObject.has(Tags.Json.RESOURCE_ID))
|
||||
action.setResourceId(jsonObject.get(Tags.Json.RESOURCE_ID).getAsString());
|
||||
if (jsonObject.has(Tags.Json.RESOURCE_TYPE))
|
||||
action.setResourceType(jsonObject.get(Tags.Json.RESOURCE_TYPE).getAsString());
|
||||
if (jsonObject.has(Tags.Json.STATE))
|
||||
action.setState(State.parse(jsonObject.get(Tags.Json.STATE).getAsString()));
|
||||
if (jsonObject.has(Json.RESOURCE_ID))
|
||||
action.setResourceId(jsonObject.get(Json.RESOURCE_ID).getAsString());
|
||||
if (jsonObject.has(Json.RESOURCE_TYPE))
|
||||
action.setResourceType(jsonObject.get(Json.RESOURCE_TYPE).getAsString());
|
||||
if (jsonObject.has(Json.STATE))
|
||||
action.setState(State.parse(jsonObject.get(Json.STATE).getAsString()));
|
||||
|
||||
// policies
|
||||
PolicyDefs defs = parsePolicies(jsonObject);
|
||||
|
@ -333,18 +322,18 @@ public class StrolchElementFromJsonVisitor {
|
|||
action.setPolicyDefs(defs);
|
||||
|
||||
// value changes
|
||||
if (!jsonObject.has(Tags.Json.VALUE_CHANGES))
|
||||
if (!jsonObject.has(Json.VALUE_CHANGES))
|
||||
return;
|
||||
|
||||
JsonArray valueChangesJ = jsonObject.getAsJsonArray(Tags.Json.VALUE_CHANGES);
|
||||
JsonArray valueChangesJ = jsonObject.getAsJsonArray(Json.VALUE_CHANGES);
|
||||
valueChangesJ.forEach(e -> {
|
||||
try {
|
||||
JsonObject valueChangeJ = e.getAsJsonObject();
|
||||
|
||||
String stateId = valueChangeJ.get(Tags.Json.STATE_ID).getAsString();
|
||||
String timeS = valueChangeJ.get(Tags.Json.TIME).getAsString();
|
||||
String valueS = valueChangeJ.get(Tags.Json.VALUE).getAsString();
|
||||
String typeS = valueChangeJ.get(Tags.Json.TYPE).getAsString();
|
||||
String stateId = valueChangeJ.has(Json.STATE_ID) ? valueChangeJ.get(Json.STATE_ID).getAsString() : "";
|
||||
String timeS = valueChangeJ.get(Json.TIME).getAsString();
|
||||
String valueS = valueChangeJ.get(Json.VALUE).getAsString();
|
||||
String typeS = valueChangeJ.get(Json.TYPE).getAsString();
|
||||
|
||||
StrolchValueType type = StrolchValueType.parse(typeS);
|
||||
IValue<?> value = type.valueInstance(valueS);
|
||||
|
@ -366,10 +355,10 @@ public class StrolchElementFromJsonVisitor {
|
|||
|
||||
PolicyDefs policyDefs = new PolicyDefs();
|
||||
|
||||
if (!jsonObject.has(Tags.Json.POLICIES))
|
||||
if (!jsonObject.has(Json.POLICIES))
|
||||
return policyDefs;
|
||||
|
||||
JsonObject policiesJsonObject = jsonObject.getAsJsonObject(Tags.Json.POLICIES);
|
||||
JsonObject policiesJsonObject = jsonObject.getAsJsonObject(Json.POLICIES);
|
||||
|
||||
Set<Entry<String, JsonElement>> entrySet = policiesJsonObject.entrySet();
|
||||
for (Entry<String, JsonElement> entry : entrySet) {
|
||||
|
@ -385,16 +374,16 @@ public class StrolchElementFromJsonVisitor {
|
|||
|
||||
protected void parseVersion(StrolchRootElement rootElement, JsonObject jsonObject) {
|
||||
|
||||
if (!jsonObject.has(Tags.Json.VERSION))
|
||||
if (!jsonObject.has(Json.VERSION))
|
||||
return;
|
||||
|
||||
JsonObject versionJ = jsonObject.getAsJsonObject(Tags.Json.VERSION);
|
||||
JsonObject versionJ = jsonObject.getAsJsonObject(Json.VERSION);
|
||||
|
||||
int v = versionJ.get(Tags.Json.VERSION).getAsInt();
|
||||
String createdBy = versionJ.get(Tags.Json.CREATED_BY).getAsString();
|
||||
String createdAtS = versionJ.get(Tags.Json.CREATED_AT).getAsString();
|
||||
int v = versionJ.get(Json.VERSION).getAsInt();
|
||||
String createdBy = versionJ.get(Json.CREATED_BY).getAsString();
|
||||
String createdAtS = versionJ.get(Json.CREATED_AT).getAsString();
|
||||
Date createdAt = ISO8601FormatFactory.getInstance().parseDate(createdAtS);
|
||||
boolean deleted = versionJ.get(Tags.Json.DELETED).getAsBoolean();
|
||||
boolean deleted = versionJ.get(Json.DELETED).getAsBoolean();
|
||||
|
||||
Version version = new Version(rootElement.getLocator(), v, createdBy, createdAt, deleted);
|
||||
rootElement.setVersion(version);
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package li.strolch.model.json;
|
||||
|
||||
import static li.strolch.utils.helper.StringHelper.isNotEmpty;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.function.BiConsumer;
|
||||
|
@ -10,6 +12,7 @@ import com.google.gson.JsonObject;
|
|||
import com.google.gson.JsonPrimitive;
|
||||
import li.strolch.exception.StrolchModelException;
|
||||
import li.strolch.model.*;
|
||||
import li.strolch.model.Tags.Json;
|
||||
import li.strolch.model.activity.Action;
|
||||
import li.strolch.model.activity.Activity;
|
||||
import li.strolch.model.activity.IActivityElement;
|
||||
|
@ -268,7 +271,7 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
|
|||
protected JsonObject toJson(Resource element) {
|
||||
|
||||
JsonObject rootJ = new JsonObject();
|
||||
rootJ.addProperty(Tags.Json.OBJECT_TYPE, Tags.Json.RESOURCE);
|
||||
rootJ.addProperty(Json.OBJECT_TYPE, Json.RESOURCE);
|
||||
|
||||
toJson(element, rootJ);
|
||||
|
||||
|
@ -286,11 +289,11 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
|
|||
protected JsonObject toJson(Order element) {
|
||||
|
||||
JsonObject rootJ = new JsonObject();
|
||||
rootJ.addProperty(Tags.Json.OBJECT_TYPE, Tags.Json.ORDER);
|
||||
rootJ.addProperty(Json.OBJECT_TYPE, Json.ORDER);
|
||||
|
||||
toJson(element, rootJ);
|
||||
rootJ.addProperty(Tags.Json.DATE, formatDate(element.getDate()));
|
||||
rootJ.addProperty(Tags.Json.STATE, element.getState().getName());
|
||||
rootJ.addProperty(Json.DATE, formatDate(element.getDate()));
|
||||
rootJ.addProperty(Json.STATE, element.getState().getName());
|
||||
|
||||
addVersion(element, rootJ);
|
||||
addParameterizedElements(element, rootJ);
|
||||
|
@ -314,13 +317,13 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
|
|||
|
||||
protected JsonObject toJson(Activity element, JsonObject rootJ, int currentDepth) {
|
||||
|
||||
rootJ.addProperty(Tags.Json.OBJECT_TYPE, Tags.Json.ACTIVITY);
|
||||
rootJ.addProperty(Json.OBJECT_TYPE, Json.ACTIVITY);
|
||||
|
||||
toJson((AbstractStrolchElement) element, rootJ);
|
||||
rootJ.addProperty(Tags.Json.TIME_ORDERING, element.getTimeOrdering().getName());
|
||||
rootJ.addProperty(Tags.Json.STATE, element.getState().getName());
|
||||
rootJ.addProperty(Tags.Json.START, formatDate(element.getStart()));
|
||||
rootJ.addProperty(Tags.Json.END, formatDate(element.getEnd()));
|
||||
rootJ.addProperty(Json.TIME_ORDERING, element.getTimeOrdering().getName());
|
||||
rootJ.addProperty(Json.STATE, element.getState().getName());
|
||||
rootJ.addProperty(Json.START, formatDate(element.getStart()));
|
||||
rootJ.addProperty(Json.END, formatDate(element.getEnd()));
|
||||
|
||||
if (element.isRootElement())
|
||||
addVersion(element, rootJ);
|
||||
|
@ -337,7 +340,7 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
|
|||
if (iter.hasNext()) {
|
||||
|
||||
JsonArray elementsJ = new JsonArray();
|
||||
rootJ.add(Tags.Json.ELEMENTS, elementsJ);
|
||||
rootJ.add(Json.ELEMENTS, elementsJ);
|
||||
|
||||
while (iter.hasNext()) {
|
||||
IActivityElement activityElement = iter.next().getValue();
|
||||
|
@ -360,15 +363,15 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
|
|||
|
||||
protected JsonObject toJson(Action element, JsonObject rootJ) {
|
||||
|
||||
rootJ.addProperty(Tags.Json.OBJECT_TYPE, Tags.Json.ACTION);
|
||||
rootJ.addProperty(Json.OBJECT_TYPE, Json.ACTION);
|
||||
|
||||
// attributes
|
||||
toJson((AbstractStrolchElement) element, rootJ);
|
||||
rootJ.addProperty(Tags.Json.RESOURCE_ID, element.getResourceId());
|
||||
rootJ.addProperty(Tags.Json.RESOURCE_TYPE, element.getResourceType());
|
||||
rootJ.addProperty(Tags.Json.STATE, element.getState().getName());
|
||||
rootJ.addProperty(Tags.Json.START, formatDate(element.getStart()));
|
||||
rootJ.addProperty(Tags.Json.END, formatDate(element.getEnd()));
|
||||
rootJ.addProperty(Json.RESOURCE_ID, element.getResourceId());
|
||||
rootJ.addProperty(Json.RESOURCE_TYPE, element.getResourceType());
|
||||
rootJ.addProperty(Json.STATE, element.getState().getName());
|
||||
rootJ.addProperty(Json.START, formatDate(element.getStart()));
|
||||
rootJ.addProperty(Json.END, formatDate(element.getEnd()));
|
||||
|
||||
addParameterizedElements(element, rootJ);
|
||||
addPolicies(element, rootJ);
|
||||
|
@ -381,17 +384,18 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
|
|||
if (iter.hasNext()) {
|
||||
|
||||
JsonArray changesJ = new JsonArray();
|
||||
rootJ.add(Tags.Json.VALUE_CHANGES, changesJ);
|
||||
rootJ.add(Json.VALUE_CHANGES, changesJ);
|
||||
|
||||
while (iter.hasNext()) {
|
||||
IValueChange<? extends IValue<?>> valueChange = iter.next();
|
||||
|
||||
JsonObject changeJ = new JsonObject();
|
||||
|
||||
changeJ.addProperty(Tags.Json.STATE_ID, valueChange.getStateId());
|
||||
changeJ.addProperty(Tags.Json.TIME, formatDate(valueChange.getTime()));
|
||||
changeJ.addProperty(Tags.Json.VALUE, valueChange.getValue().getValueAsString());
|
||||
changeJ.addProperty(Tags.Json.TYPE, valueChange.getValue().getType());
|
||||
if (isNotEmpty(valueChange.getStateId()))
|
||||
changeJ.addProperty(Json.STATE_ID, valueChange.getStateId());
|
||||
changeJ.addProperty(Json.TIME, formatDate(valueChange.getTime()));
|
||||
changeJ.addProperty(Json.VALUE, valueChange.getValue().getValueAsString());
|
||||
changeJ.addProperty(Json.TYPE, valueChange.getValue().getType());
|
||||
|
||||
changesJ.add(changeJ);
|
||||
}
|
||||
|
@ -409,7 +413,7 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
|
|||
PolicyDefs policyDefs = policyContainer.getPolicyDefs();
|
||||
|
||||
JsonObject policyDefsJ = new JsonObject();
|
||||
rootJ.add(Tags.Json.POLICIES, policyDefsJ);
|
||||
rootJ.add(Json.POLICIES, policyDefsJ);
|
||||
|
||||
for (String type : policyDefs.getPolicyTypes()) {
|
||||
PolicyDef policyDef = policyDefs.getPolicyDef(type);
|
||||
|
@ -419,12 +423,12 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
|
|||
|
||||
protected JsonObject toJson(AbstractStrolchElement element, JsonObject rootJ) {
|
||||
|
||||
rootJ.addProperty(Tags.Json.ID, element.getId());
|
||||
rootJ.addProperty(Json.ID, element.getId());
|
||||
|
||||
if (!isWithoutElementName())
|
||||
rootJ.addProperty(Tags.Json.NAME, element.getName());
|
||||
rootJ.addProperty(Json.NAME, element.getName());
|
||||
|
||||
rootJ.addProperty(Tags.Json.TYPE, element.getType());
|
||||
rootJ.addProperty(Json.TYPE, element.getType());
|
||||
|
||||
return rootJ;
|
||||
}
|
||||
|
@ -487,7 +491,7 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
|
|||
return;
|
||||
|
||||
JsonObject parameterBagsJ = new JsonObject();
|
||||
rootJ.add(Tags.Json.PARAMETER_BAGS, parameterBagsJ);
|
||||
rootJ.add(Json.PARAMETER_BAGS, parameterBagsJ);
|
||||
|
||||
for (String bagKey : element.getParameterBagKeySet()) {
|
||||
ParameterBag bag = element.getParameterBag(bagKey);
|
||||
|
@ -505,7 +509,7 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
|
|||
toJson(bag, bagJ);
|
||||
|
||||
JsonObject paramsJ = new JsonObject();
|
||||
bagJ.add(Tags.Json.PARAMETERS, paramsJ);
|
||||
bagJ.add(Json.PARAMETERS, paramsJ);
|
||||
|
||||
for (String paramKey : bag.getParameterKeySet()) {
|
||||
Parameter<?> param = bag.getParameter(paramKey);
|
||||
|
@ -521,18 +525,18 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
|
|||
toJson((AbstractStrolchElement) param, paramJ);
|
||||
|
||||
if (!StrolchModelConstants.INTERPRETATION_NONE.equals(param.getInterpretation()))
|
||||
paramJ.addProperty(Tags.Json.INTERPRETATION, param.getInterpretation());
|
||||
paramJ.addProperty(Json.INTERPRETATION, param.getInterpretation());
|
||||
|
||||
if (param.isHidden())
|
||||
paramJ.addProperty(Tags.Json.HIDDEN, param.isHidden());
|
||||
paramJ.addProperty(Json.HIDDEN, param.isHidden());
|
||||
|
||||
if (!StrolchModelConstants.UOM_NONE.equals(param.getUom()))
|
||||
paramJ.addProperty(Tags.Json.UOM, param.getUom());
|
||||
paramJ.addProperty(Json.UOM, param.getUom());
|
||||
|
||||
if (param.getIndex() != 0)
|
||||
paramJ.addProperty(Tags.Json.INDEX, param.getIndex());
|
||||
paramJ.addProperty(Json.INDEX, param.getIndex());
|
||||
|
||||
paramJ.addProperty(Tags.Json.VALUE, param.getValueAsString());
|
||||
paramJ.addProperty(Json.VALUE, param.getValueAsString());
|
||||
return paramJ;
|
||||
}
|
||||
|
||||
|
@ -549,7 +553,7 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
|
|||
return;
|
||||
|
||||
JsonObject timedStatesJ = new JsonObject();
|
||||
rootJ.add(Tags.Json.TIMED_STATES, timedStatesJ);
|
||||
rootJ.add(Json.TIMED_STATES, timedStatesJ);
|
||||
|
||||
for (String stateKey : element.getTimedStateKeySet()) {
|
||||
|
||||
|
@ -591,7 +595,7 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
|
|||
toJson((AbstractStrolchElement) state, stateJ);
|
||||
|
||||
JsonArray valuesJ = new JsonArray();
|
||||
stateJ.add(Tags.Json.VALUES, valuesJ);
|
||||
stateJ.add(Json.VALUES, valuesJ);
|
||||
|
||||
SortedSet<? extends ITimeValue<? extends IValue<?>>> values = state.getTimeEvolution().getValues();
|
||||
for (ITimeValue<? extends IValue<?>> value : values) {
|
||||
|
@ -602,8 +606,8 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
|
|||
Long time = value.getTime();
|
||||
String valueS = value.getValue().getValueAsString();
|
||||
|
||||
valueJ.addProperty(Tags.Json.TIME, formatDate(time));
|
||||
valueJ.addProperty(Tags.Json.VALUE, valueS);
|
||||
valueJ.addProperty(Json.TIME, formatDate(time));
|
||||
valueJ.addProperty(Json.VALUE, valueS);
|
||||
}
|
||||
return stateJ;
|
||||
}
|
||||
|
@ -615,8 +619,8 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
|
|||
JsonArray arrayJ = new JsonArray();
|
||||
for (ITimeValue<? extends IValue<?>> v : timeEvolution.getValues()) {
|
||||
JsonObject obj = new JsonObject();
|
||||
obj.addProperty(Tags.Json.DATE, ISO8601FormatFactory.getInstance().formatDate(v.getTime()));
|
||||
obj.addProperty(Tags.Json.VALUE, v.getValue().getValueAsString());
|
||||
obj.addProperty(Json.DATE, ISO8601FormatFactory.getInstance().formatDate(v.getTime()));
|
||||
obj.addProperty(Json.VALUE, v.getValue().getValueAsString());
|
||||
|
||||
arrayJ.add(obj);
|
||||
}
|
||||
|
@ -632,11 +636,11 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
|
|||
Version version = element.getVersion();
|
||||
|
||||
JsonObject versionJ = new JsonObject();
|
||||
versionJ.addProperty(Tags.Json.VERSION, version.getVersion());
|
||||
versionJ.addProperty(Tags.Json.CREATED_BY, version.getCreatedBy());
|
||||
versionJ.addProperty(Tags.Json.CREATED_AT, formatDate(version.getCreatedAt()));
|
||||
versionJ.addProperty(Tags.Json.DELETED, version.isDeleted());
|
||||
rootJ.add(Tags.Json.VERSION, versionJ);
|
||||
versionJ.addProperty(Json.VERSION, version.getVersion());
|
||||
versionJ.addProperty(Json.CREATED_BY, version.getCreatedBy());
|
||||
versionJ.addProperty(Json.CREATED_AT, formatDate(version.getCreatedAt()));
|
||||
versionJ.addProperty(Json.DELETED, version.isDeleted());
|
||||
rootJ.add(Json.VERSION, versionJ);
|
||||
}
|
||||
|
||||
private static String formatDate(Date date) {
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
*/
|
||||
package li.strolch.model.timevalue.impl;
|
||||
|
||||
import static li.strolch.utils.helper.StringHelper.trimOrEmpty;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import li.strolch.model.timevalue.IValue;
|
||||
|
@ -55,7 +57,7 @@ public class ValueChange<T extends IValue> implements IValueChange<T>, Serializa
|
|||
public ValueChange(final Long time, final T value, final String stateId) {
|
||||
this.time = time;
|
||||
this.value = value;
|
||||
this.stateId = stateId;
|
||||
this.stateId = trimOrEmpty(stateId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
/*
|
||||
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
@ -15,25 +15,14 @@
|
|||
*/
|
||||
package li.strolch.model.xml;
|
||||
|
||||
import static li.strolch.utils.helper.StringHelper.isNotEmpty;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.SortedSet;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import li.strolch.model.AbstractStrolchElement;
|
||||
import li.strolch.model.GroupedParameterizedElement;
|
||||
import li.strolch.model.Order;
|
||||
import li.strolch.model.ParameterBag;
|
||||
import li.strolch.model.ParameterizedElement;
|
||||
import li.strolch.model.Resource;
|
||||
import li.strolch.model.StrolchModelConstants;
|
||||
import li.strolch.model.StrolchRootElement;
|
||||
import li.strolch.model.Tags;
|
||||
import li.strolch.model.Version;
|
||||
import li.strolch.model.*;
|
||||
import li.strolch.model.activity.Action;
|
||||
import li.strolch.model.activity.Activity;
|
||||
import li.strolch.model.activity.IActivityElement;
|
||||
|
@ -46,8 +35,9 @@ import li.strolch.model.timevalue.IValue;
|
|||
import li.strolch.model.timevalue.IValueChange;
|
||||
import li.strolch.model.visitor.StrolchRootElementVisitor;
|
||||
import li.strolch.utils.helper.DomUtil;
|
||||
import li.strolch.utils.helper.StringHelper;
|
||||
import li.strolch.utils.iso8601.ISO8601FormatFactory;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||
|
@ -144,9 +134,9 @@ public class StrolchElementToDomVisitor implements StrolchRootElementVisitor<Doc
|
|||
Element element = document.createElement(Tags.ACTION);
|
||||
fillElement(element, action);
|
||||
|
||||
if (StringHelper.isNotEmpty(action.getResourceId()))
|
||||
if (isNotEmpty(action.getResourceId()))
|
||||
element.setAttribute(Tags.RESOURCE_ID, action.getResourceId());
|
||||
if (StringHelper.isNotEmpty(action.getResourceType()))
|
||||
if (isNotEmpty(action.getResourceType()))
|
||||
element.setAttribute(Tags.RESOURCE_TYPE, action.getResourceType());
|
||||
|
||||
element.setAttribute(Tags.STATE, action.getState().getName());
|
||||
|
@ -168,7 +158,8 @@ public class StrolchElementToDomVisitor implements StrolchRootElementVisitor<Doc
|
|||
|
||||
protected Element toDom(IValueChange<? extends IValue<?>> value) {
|
||||
Element element = document.createElement(Tags.VALUE_CHANGE);
|
||||
element.setAttribute(Tags.STATE_ID, value.getStateId());
|
||||
if (isNotEmpty(value.getStateId()))
|
||||
element.setAttribute(Tags.STATE_ID, value.getStateId());
|
||||
element.setAttribute(Tags.TIME, ISO8601FormatFactory.getInstance().formatDate(value.getTime()));
|
||||
element.setAttribute(Tags.VALUE, value.getValue().getValueAsString());
|
||||
element.setAttribute(Tags.TYPE, value.getValue().getType());
|
||||
|
|
|
@ -17,6 +17,7 @@ package li.strolch.model.xml;
|
|||
|
||||
import static li.strolch.model.StrolchModelConstants.INTERPRETATION_NONE;
|
||||
import static li.strolch.model.StrolchModelConstants.UOM_NONE;
|
||||
import static li.strolch.utils.helper.StringHelper.isNotEmpty;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Iterator;
|
||||
|
@ -24,19 +25,7 @@ import java.util.Map.Entry;
|
|||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.AttributesImpl;
|
||||
|
||||
import li.strolch.model.GroupedParameterizedElement;
|
||||
import li.strolch.model.Order;
|
||||
import li.strolch.model.ParameterBag;
|
||||
import li.strolch.model.Resource;
|
||||
import li.strolch.model.StrolchElement;
|
||||
import li.strolch.model.StrolchRootElement;
|
||||
import li.strolch.model.Tags;
|
||||
import li.strolch.model.Version;
|
||||
import li.strolch.model.*;
|
||||
import li.strolch.model.activity.Action;
|
||||
import li.strolch.model.activity.Activity;
|
||||
import li.strolch.model.activity.IActivityElement;
|
||||
|
@ -48,8 +37,11 @@ import li.strolch.model.timevalue.ITimeValue;
|
|||
import li.strolch.model.timevalue.IValue;
|
||||
import li.strolch.model.timevalue.IValueChange;
|
||||
import li.strolch.model.visitor.StrolchRootElementVisitor;
|
||||
import li.strolch.utils.helper.StringHelper;
|
||||
import li.strolch.utils.iso8601.ISO8601FormatFactory;
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.AttributesImpl;
|
||||
|
||||
/**
|
||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||
|
@ -328,10 +320,10 @@ public class StrolchElementToSaxVisitor implements StrolchRootElementVisitor<Voi
|
|||
protected AttributesImpl attributesFor(Action action) {
|
||||
AttributesImpl attributes = attributesFor((StrolchElement) action);
|
||||
|
||||
if (StringHelper.isNotEmpty(action.getResourceId()))
|
||||
if (isNotEmpty(action.getResourceId()))
|
||||
attributes.addAttribute(null, null, Tags.RESOURCE_ID, Tags.CDATA, action.getResourceId());
|
||||
|
||||
if (StringHelper.isNotEmpty(action.getResourceType()))
|
||||
if (isNotEmpty(action.getResourceType()))
|
||||
attributes.addAttribute(null, null, Tags.RESOURCE_TYPE, Tags.CDATA, action.getResourceType());
|
||||
|
||||
if (!action.getState().isCreated())
|
||||
|
@ -343,7 +335,7 @@ public class StrolchElementToSaxVisitor implements StrolchRootElementVisitor<Voi
|
|||
protected AttributesImpl attributesFor(IValueChange<? extends IValue<?>> valueChange) {
|
||||
AttributesImpl attributes = new AttributesImpl();
|
||||
|
||||
if (StringHelper.isNotEmpty(valueChange.getStateId()))
|
||||
if (isNotEmpty(valueChange.getStateId()))
|
||||
attributes.addAttribute(null, null, Tags.STATE_ID, Tags.CDATA, valueChange.getStateId());
|
||||
|
||||
attributes.addAttribute(null, null, Tags.TIME, Tags.CDATA,
|
||||
|
|
|
@ -17,6 +17,7 @@ package li.strolch.model.xml;
|
|||
|
||||
import static li.strolch.model.StrolchModelConstants.INTERPRETATION_NONE;
|
||||
import static li.strolch.model.StrolchModelConstants.UOM_NONE;
|
||||
import static li.strolch.utils.helper.StringHelper.isNotEmpty;
|
||||
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
|
@ -38,7 +39,6 @@ import li.strolch.model.timevalue.ITimeVariable;
|
|||
import li.strolch.model.timevalue.IValue;
|
||||
import li.strolch.model.timevalue.IValueChange;
|
||||
import li.strolch.model.visitor.StrolchRootElementVisitor;
|
||||
import li.strolch.utils.helper.StringHelper;
|
||||
import li.strolch.utils.iso8601.ISO8601FormatFactory;
|
||||
|
||||
/**
|
||||
|
@ -186,9 +186,9 @@ public class StrolchElementToSaxWriterVisitor implements StrolchRootElementVisit
|
|||
|
||||
writeStartStrolchElement(Tags.ACTION, empty, action);
|
||||
this.writer.writeAttribute(Tags.STATE, action.getState().getName());
|
||||
if (StringHelper.isNotEmpty(action.getResourceId()))
|
||||
if (isNotEmpty(action.getResourceId()))
|
||||
this.writer.writeAttribute(Tags.RESOURCE_ID, action.getResourceId());
|
||||
if (StringHelper.isNotEmpty(action.getResourceType()))
|
||||
if (isNotEmpty(action.getResourceType()))
|
||||
this.writer.writeAttribute(Tags.RESOURCE_TYPE, action.getResourceType());
|
||||
|
||||
if (action.hasParameterBags())
|
||||
|
@ -197,7 +197,8 @@ public class StrolchElementToSaxWriterVisitor implements StrolchRootElementVisit
|
|||
if (action.hasChanges()) {
|
||||
for (IValueChange<? extends IValue<?>> change : action.getChanges()) {
|
||||
this.writer.writeEmptyElement(Tags.VALUE_CHANGE);
|
||||
this.writer.writeAttribute(Tags.STATE_ID, change.getStateId());
|
||||
if (isNotEmpty(change.getStateId()))
|
||||
this.writer.writeAttribute(Tags.STATE_ID, change.getStateId());
|
||||
this.writer.writeAttribute(Tags.TIME, ISO8601FormatFactory.getInstance().formatDate(change.getTime()));
|
||||
this.writer.writeAttribute(Tags.VALUE, change.getValue().getValueAsString());
|
||||
this.writer.writeAttribute(Tags.TYPE, change.getValue().getType());
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
/*
|
||||
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
|
Loading…
Reference in New Issue