[New] Added setting flag to set single ParameterBag as flat

This commit is contained in:
Robert von Burg 2019-04-04 18:47:17 +02:00
parent 18cc052a34
commit 33030564e0
2 changed files with 42 additions and 29 deletions

View File

@ -1,5 +1,7 @@
package li.strolch.model.json; package li.strolch.model.json;
import static java.util.Arrays.asList;
import static li.strolch.model.Tags.Json.PARAMETER_BAGS;
import static li.strolch.utils.helper.StringHelper.isNotEmpty; import static li.strolch.utils.helper.StringHelper.isNotEmpty;
import java.util.*; import java.util.*;
@ -41,6 +43,7 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
private BiConsumer<Action, JsonObject> actionHook; private BiConsumer<Action, JsonObject> actionHook;
private boolean flat; private boolean flat;
private Set<String> flatBags;
private boolean withoutElementName; private boolean withoutElementName;
private boolean withLocator; private boolean withLocator;
private boolean withoutVersion; private boolean withoutVersion;
@ -51,12 +54,17 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
this.ignoredKeys = new MapOfSets<>(); this.ignoredKeys = new MapOfSets<>();
this.ignoredTimedStates = new HashSet<>(); this.ignoredTimedStates = new HashSet<>();
this.ignoredBagTypes = new HashSet<>(); this.ignoredBagTypes = new HashSet<>();
this.flatBags = new HashSet<>();
} }
public boolean isFlat() { public boolean isFlat() {
return this.flat; return this.flat;
} }
public boolean isBagFlat(String badId) {
return this.flatBags.contains(badId);
}
public boolean isWithVersion() { public boolean isWithVersion() {
return !this.withoutVersion; return !this.withoutVersion;
} }
@ -122,6 +130,11 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
return this; return this;
} }
public StrolchElementToJsonVisitor flatBags(String... bagIds) {
this.flatBags.addAll(asList(bagIds));
return this;
}
public StrolchElementToJsonVisitor ignoreBag(String bagId) { public StrolchElementToJsonVisitor ignoreBag(String bagId) {
this.ignoredKeys.addSet(bagId, Collections.emptySet()); this.ignoredKeys.addSet(bagId, Collections.emptySet());
return this; return this;
@ -302,7 +315,7 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
@Override @Override
public JsonObject visitParameterBag(ParameterBag bag) { public JsonObject visitParameterBag(ParameterBag bag) {
if (isFlat()) { if (isFlat() || isBagFlat(bag.getId())) {
JsonObject bagJ = new JsonObject(); JsonObject bagJ = new JsonObject();
@ -486,16 +499,14 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
} }
protected void addParameterizedElements(GroupedParameterizedElement element, JsonObject rootJ) { protected void addParameterizedElements(GroupedParameterizedElement element, JsonObject rootJ) {
if (isFlat())
addParameterizedElementsFlat(element, rootJ);
else
addParameterizedElementsFull(element, rootJ);
}
protected void addParameterizedElementsFlat(GroupedParameterizedElement element, JsonObject rootJ) { if (!element.hasParameterBags())
return;
Set<String> bagKeySet = element.getParameterBagKeySet(); for (String bagId : element.getParameterBagKeySet()) {
for (String bagId : bagKeySet) { ParameterBag bag = element.getParameterBag(bagId);
if (!bag.hasParameters())
continue;
// 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
Set<String> ignoredParamIds = this.ignoredKeys.getSet(bagId); Set<String> ignoredParamIds = this.ignoredKeys.getSet(bagId);
@ -503,10 +514,25 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
continue; continue;
ParameterBag parameterBag = element.getParameterBag(bagId); ParameterBag parameterBag = element.getParameterBag(bagId);
if (ignoredBagTypes.contains(parameterBag.getType())) if (this.ignoredBagTypes.contains(parameterBag.getType()))
continue; continue;
addParameterBagFlat(rootJ, ignoredParamIds, parameterBag); if (isFlat() || isBagFlat(bagId)) {
addParameterBagFlat(rootJ, ignoredParamIds, parameterBag);
} else {
JsonObject parameterBagsJ;
if (rootJ.has(PARAMETER_BAGS)) {
parameterBagsJ = rootJ.get(PARAMETER_BAGS).getAsJsonObject();
} else {
parameterBagsJ = new JsonObject();
rootJ.add(PARAMETER_BAGS, parameterBagsJ);
}
parameterBagsJ.add(bagId, parameterBagToJsonFull(bag));
}
} }
} }
@ -537,24 +563,6 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
} }
} }
protected void addParameterizedElementsFull(GroupedParameterizedElement element, JsonObject rootJ) {
if (!element.hasParameterBags())
return;
JsonObject parameterBagsJ = new JsonObject();
rootJ.add(Json.PARAMETER_BAGS, parameterBagsJ);
for (String bagKey : element.getParameterBagKeySet()) {
ParameterBag bag = element.getParameterBag(bagKey);
if (!bag.hasParameters())
continue;
parameterBagsJ.add(bagKey, parameterBagToJsonFull(bag));
}
}
private JsonObject parameterBagToJsonFull(ParameterBag bag) { private JsonObject parameterBagToJsonFull(ParameterBag bag) {
JsonObject bagJ = new JsonObject(); JsonObject bagJ = new JsonObject();

View File

@ -93,6 +93,11 @@ public class StrolchRootElementToJsonVisitor implements StrolchRootElementVisito
return this; return this;
} }
public StrolchRootElementToJsonVisitor flatBags(String... bagIds) {
this.visitor.flatBags(bagIds);
return this;
}
public StrolchRootElementToJsonVisitor ignoreBag(String bagId) { public StrolchRootElementToJsonVisitor ignoreBag(String bagId) {
this.visitor.ignoreBag(bagId); this.visitor.ignoreBag(bagId);
return this; return this;