Commit c2f7c07f authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

fix #254: Migration assistant will add default values for new members.

parent 25bcc7b9
......@@ -24,7 +24,8 @@ public enum Version {
V0_8(0, 8),
V0_9(0,9),
V0_10(0,10),
V1_0(1,0)
V1_0(1,0),
V1_1(1,1)
;
......
......@@ -4,10 +4,13 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.projects.Scenario;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.simulator.projects.migration.MigrationException;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.logging.Logger;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.LinkedList;
......@@ -162,4 +165,22 @@ public abstract class AbstractJsonTransformation implements JsonTransformation,
return StateJsonConverter.deserializeToNode(sortedRoot);
}
/**
* This hook will ensure that new attributes will be added with their default values.
* Important: must be used as PostHook only. The JsonNode representation of the scenario
* must already have the new Version Number!
* @param node
* @return
* @throws MigrationException
*/
public static JsonNode addNewMembersWithDefaultValues(JsonNode node) throws MigrationException{
try {
Scenario scenario = JsonConverter.deserializeScenarioRunManagerFromNode(node);
node = JsonConverter.serializeScenarioRunManagerToNode(scenario, false);
} catch (IOException e) {
throw new MigrationException("Error while serializing and deserializing scenario",e);
}
return node;
}
}
......@@ -114,6 +114,9 @@ public class JsonMigrationAssistant extends MigrationAssistant {
logger.info(migrationLogger.last());
transformedNode = transform(transformedNode, v);
}
if (targetVersion.equals(Version.latest())){
transformedNode = AbstractJsonTransformation.addNewMembersWithDefaultValues(transformedNode);
}
try {
restLog();
......@@ -275,6 +278,8 @@ public class JsonMigrationAssistant extends MigrationAssistant {
logger.info(migrationLogger.last());
transformedNode = transform(transformedNode, v);
}
// will always be Version.latest()
transformedNode = AbstractJsonTransformation.addNewMembersWithDefaultValues(transformedNode);
if (legacyDir != null) {
migrationLogger.info("Scenario Migrated - OK. Move copy of old version to legacllyDir");
logger.info(migrationLogger.last());
......
package org.vadere.simulator.projects.migration.jsontranformation.json;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.vadere.annotation.factories.migrationassistant.MigrationTransformation;
import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.projects.migration.MigrationException;
import org.vadere.simulator.projects.migration.jsontranformation.SimpleJsonTransformation;
@MigrationTransformation(targetVersionLabel = "1.1")
public class JsonTransformV1_0ToV01_1 extends SimpleJsonTransformation {
public JsonTransformV1_0ToV01_1() {
super(Version.V1_1);
}
@Override
protected void initDefaultHooks() {
addPostHookLast(this::removeCarAttributes);
addPostHookLast(this::addCommitHashWarningIfMissing);
addPostHookLast(this::sort);
}
@Override
public JsonNode applyTransformation(JsonNode node) throws MigrationException {
return super.applyTransformation(node);
}
public JsonNode removeCarAttributes(JsonNode scenarioFile) throws MigrationException {
JsonNode attributesCar = pathMustExist(scenarioFile, "scenario/topography/attributesCar");
if (!attributesCar.isNull()){
ObjectNode topography = (ObjectNode) pathMustExist(scenarioFile, "scenario/topography");
topography.replace("attributesCar", NullNode.getInstance());
}
return scenarioFile;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment