refactoring of load/save processes
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>de.srsoftware</groupId>
|
<groupId>de.srsoftware</groupId>
|
||||||
<artifactId>web4rail</artifactId>
|
<artifactId>web4rail</artifactId>
|
||||||
<version>1.2.10</version>
|
<version>1.2.11</version>
|
||||||
<name>Web4Rail</name>
|
<name>Web4Rail</name>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<description>Java Model Railway Control</description>
|
<description>Java Model Railway Control</description>
|
||||||
|
|||||||
@@ -352,7 +352,7 @@ public abstract class BaseClass implements Constants{
|
|||||||
|
|
||||||
|
|
||||||
protected Window properties(List<Fieldset> preForm,FormInput formInputs,List<Fieldset> postForm) {
|
protected Window properties(List<Fieldset> preForm,FormInput formInputs,List<Fieldset> postForm) {
|
||||||
Window win = new Window(getClass().getSimpleName()+"-properties", t("Properties of {} ({})",this.title(),id));
|
Window win = new Window(getClass().getSimpleName()+"-properties", t("Properties of {}",this.title()));
|
||||||
|
|
||||||
preForm.forEach(fieldset -> fieldset.addTo(win));
|
preForm.forEach(fieldset -> fieldset.addTo(win));
|
||||||
|
|
||||||
@@ -372,7 +372,7 @@ public abstract class BaseClass implements Constants{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String title() {
|
private String title() {
|
||||||
return getClass().getSimpleName();
|
return toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String,String> props(Map<String,String> additionalProps){
|
private Map<String,String> props(Map<String,String> additionalProps){
|
||||||
|
|||||||
@@ -53,20 +53,22 @@ public class Route extends BaseClass implements Comparable<Route>{
|
|||||||
private static final Logger LOG = LoggerFactory.getLogger(Route.class);
|
private static final Logger LOG = LoggerFactory.getLogger(Route.class);
|
||||||
|
|
||||||
private static final String ACTIONS = "actions";
|
private static final String ACTIONS = "actions";
|
||||||
private static final String ACTION_LISTS = "action_lists";
|
|
||||||
private static final String BRAKE_TIMES = "brake_times";
|
private static final String BRAKE_TIMES = "brake_times";
|
||||||
private static final String CONDITIONS = "conditions";
|
private static final String CONDITION_LIST = "condition_list";
|
||||||
private static final String END_DIRECTION = "direction_end";
|
private static final String END_DIRECTION = "direction_end";
|
||||||
private static final String ROUTES = "routes";
|
private static final String ROUTES = "routes";
|
||||||
private static final String SETUP_ACTIONS = "setup_actions";
|
private static final String SETUP_ACTIONS = "setup_actions";
|
||||||
private static final String START_ACTIONS = "start_actions";
|
private static final String START_ACTIONS = "start_actions";
|
||||||
private static final String START_DIRECTION = "direction_start";
|
private static final String START_DIRECTION = "direction_start";
|
||||||
private static final String TRIGGER = "trigger";
|
|
||||||
static final String NAME = "name";
|
static final String NAME = "name";
|
||||||
static final String PATH = "path";
|
static final String PATH = "path";
|
||||||
static final String SIGNALS = "signals";
|
static final String SIGNALS = "signals";
|
||||||
static final String TURNOUTS = "turnouts";
|
static final String TURNOUTS = "turnouts";
|
||||||
|
|
||||||
|
private static final String ROUTE_START = "route_start";
|
||||||
|
|
||||||
|
private static final String ROUTE_SETUP = "route_setup";
|
||||||
|
|
||||||
private static HashMap<Id, String> names = new HashMap<Id, String>(); // maps id to name. needed to keep names during plan.analyze()
|
private static HashMap<Id, String> names = new HashMap<Id, String>(); // maps id to name. needed to keep names during plan.analyze()
|
||||||
|
|
||||||
private class BrakeProcessor extends Thread {
|
private class BrakeProcessor extends Thread {
|
||||||
@@ -144,17 +146,10 @@ public class Route extends BaseClass implements Comparable<Route>{
|
|||||||
public Train train;
|
public Train train;
|
||||||
private HashMap<String,ActionList> triggers = new HashMap<String, ActionList>();
|
private HashMap<String,ActionList> triggers = new HashMap<String, ActionList>();
|
||||||
private HashMap<Turnout,Turnout.State> turnouts;
|
private HashMap<Turnout,Turnout.State> turnouts;
|
||||||
private ActionList setupActions;
|
|
||||||
private ActionList startActions;
|
|
||||||
private Block startBlock = null;
|
private Block startBlock = null;
|
||||||
public Direction startDirection;
|
public Direction startDirection;
|
||||||
private HashSet<Contact> triggeredContacts = new HashSet<>();
|
private HashSet<Contact> triggeredContacts = new HashSet<>();
|
||||||
|
|
||||||
public Route() {
|
|
||||||
setupActions = new ActionList(this);
|
|
||||||
startActions = new ActionList(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* process commands from the client
|
* process commands from the client
|
||||||
* @param params
|
* @param params
|
||||||
@@ -253,10 +248,10 @@ public class Route extends BaseClass implements Comparable<Route>{
|
|||||||
Tag list = new Tag("ol");
|
Tag list = new Tag("ol");
|
||||||
|
|
||||||
Tag setup = new Tag("li").content(t("Setup actions")+NBSP);
|
Tag setup = new Tag("li").content(t("Setup actions")+NBSP);
|
||||||
setupActions.list().addTo(setup).addTo(list);
|
triggers.get(ROUTE_SETUP).list().addTo(setup).addTo(list);
|
||||||
|
|
||||||
Tag start = new Tag("li").content(t("Start actions")+NBSP);
|
Tag start = new Tag("li").content(t("Start actions")+NBSP);
|
||||||
startActions.list().addTo(start).addTo(list);
|
triggers.get(ROUTE_START).list().addTo(start).addTo(list);
|
||||||
|
|
||||||
for (Contact c : contacts) {
|
for (Contact c : contacts) {
|
||||||
Tag item = c.link("span", c).addTo(new Tag("li")).content(NBSP);
|
Tag item = c.link("span", c).addTo(new Tag("li")).content(NBSP);
|
||||||
@@ -277,7 +272,6 @@ public class Route extends BaseClass implements Comparable<Route>{
|
|||||||
|
|
||||||
conditions.addAll(existingRoute.conditions);
|
conditions.addAll(existingRoute.conditions);
|
||||||
|
|
||||||
setupActions.addActionsFrom(existingRoute.setupActions);
|
|
||||||
for (Entry<String, ActionList> entry : triggers.entrySet()) {
|
for (Entry<String, ActionList> entry : triggers.entrySet()) {
|
||||||
String trigger = entry.getKey();
|
String trigger = entry.getKey();
|
||||||
ActionList existingActionList = existingRoute.triggers.get(trigger);
|
ActionList existingActionList = existingRoute.triggers.get(trigger);
|
||||||
@@ -376,8 +370,8 @@ public class Route extends BaseClass implements Comparable<Route>{
|
|||||||
add(lastContact.trigger(), new BrakeStop(this));
|
add(lastContact.trigger(), new BrakeStop(this));
|
||||||
add(lastContact.trigger(), new FinishRoute(this));
|
add(lastContact.trigger(), new FinishRoute(this));
|
||||||
}
|
}
|
||||||
for (Signal signal : signals) setupActions.add(new SetSignal(this).set(signal).to(Signal.GO));
|
for (Signal signal : signals) add(ROUTE_SETUP,new SetSignal(this).set(signal).to(Signal.GO));
|
||||||
startActions.add(new SetSpeed(this).to(999));
|
add(ROUTE_START,new SetSpeed(this).to(999));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -437,7 +431,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean fireSetupActions(Context context) {
|
public boolean fireSetupActions(Context context) {
|
||||||
return setupActions.fire(context);
|
return triggers.get(ROUTE_SETUP).fire(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String generateName() {
|
private String generateName() {
|
||||||
@@ -492,21 +486,17 @@ public class Route extends BaseClass implements Comparable<Route>{
|
|||||||
|
|
||||||
json.put(BRAKE_TIMES, brakeTimes);
|
json.put(BRAKE_TIMES, brakeTimes);
|
||||||
|
|
||||||
if (!conditions.isEmpty()) json.put(CONDITIONS, conditions.jsonArray());
|
if (!conditions.isEmpty()) {
|
||||||
|
json.put(CONDITION_LIST, conditions.json());
|
||||||
JSONArray jTriggers = new JSONArray();
|
|
||||||
for (Entry<String, ActionList> entry : triggers.entrySet()) {
|
|
||||||
JSONObject trigger = new JSONObject();
|
|
||||||
trigger.put(TRIGGER, entry.getKey());
|
|
||||||
ActionList actionList = entry.getValue();
|
|
||||||
trigger.put(ACTIONS, actionList.jsonArray());
|
|
||||||
|
|
||||||
jTriggers.put(trigger);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
if (!jTriggers.isEmpty()) json.put(ACTION_LISTS, jTriggers);
|
|
||||||
if (!setupActions.isEmpty()) json.put(SETUP_ACTIONS, setupActions.json());
|
JSONObject jActions = new JSONObject();
|
||||||
if (!startActions.isEmpty()) json.put(START_ACTIONS, startActions.json());
|
for (Entry<String, ActionList> entry : triggers.entrySet()) {
|
||||||
|
String trigger = entry.getKey();
|
||||||
|
ActionList lst = entry.getValue();
|
||||||
|
jActions.put(trigger,lst.json());
|
||||||
|
}
|
||||||
|
json.put(ACTIONS, jActions);
|
||||||
|
|
||||||
String name = name();
|
String name = name();
|
||||||
if (isSet(name)) json.put(NAME, name);
|
if (isSet(name)) json.put(NAME, name);
|
||||||
@@ -550,10 +540,96 @@ public class Route extends BaseClass implements Comparable<Route>{
|
|||||||
if (json.has(SIGNALS)) {
|
if (json.has(SIGNALS)) {
|
||||||
for (Object signalId : json.getJSONArray(SIGNALS)) addSignal((Signal) plan.get(new Id((String) signalId), false));
|
for (Object signalId : json.getJSONArray(SIGNALS)) addSignal((Signal) plan.get(new Id((String) signalId), false));
|
||||||
}
|
}
|
||||||
if (json.has(ACTION_LISTS)) loadActions(json.getJSONArray(ACTION_LISTS));
|
if (json.has(ACTIONS)) {
|
||||||
if (json.has(CONDITIONS)) conditions.load(json.getJSONArray(CONDITIONS)).parent(this);
|
loadActions(json.getJSONObject(ACTIONS));
|
||||||
if (json.has(SETUP_ACTIONS)) setupActions.load(json.getJSONArray(SETUP_ACTIONS)).parent(this);
|
}
|
||||||
if (json.has(START_ACTIONS)) startActions.load(json.getJSONArray(START_ACTIONS)).parent(this);
|
if (json.has("action_lists")) { // TODO: this is legacy!
|
||||||
|
JSONArray jarr = json.getJSONArray("action_lists");
|
||||||
|
for (Object o : jarr) {
|
||||||
|
if (o instanceof JSONObject) {
|
||||||
|
JSONObject jo = (JSONObject) o;
|
||||||
|
ActionList aList = new ActionList(this);
|
||||||
|
String trigger = jo.getString("trigger");
|
||||||
|
JSONArray jActions = jo.getJSONArray(ACTIONS);
|
||||||
|
for (Object ja : jActions) {
|
||||||
|
JSONObject jao = (JSONObject) ja;
|
||||||
|
String type = jao.getString(TYPE);
|
||||||
|
Action action = Action.create(type, aList);
|
||||||
|
if (isSet(action)) {
|
||||||
|
action.load(jao);
|
||||||
|
aList.add(action);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
triggers.put(trigger, aList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (json.has("conditions")) { // TODO: this is legacy!
|
||||||
|
JSONArray jConditions = json.getJSONArray("conditions");
|
||||||
|
for (Object o : jConditions) {
|
||||||
|
if (o instanceof JSONObject) {
|
||||||
|
JSONObject jo = (JSONObject) o;
|
||||||
|
String type = jo.getString(TYPE);
|
||||||
|
Condition condition = Condition.create(type);
|
||||||
|
if (isSet(condition)) {
|
||||||
|
condition.load(jo);
|
||||||
|
conditions.add(condition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (json.has(CONDITION_LIST)) conditions.load(json.getJSONObject(CONDITION_LIST)).parent(this);
|
||||||
|
if (json.has(SETUP_ACTIONS)) { // TODO: this is legacy!
|
||||||
|
Object so = json.get(SETUP_ACTIONS);
|
||||||
|
if (so instanceof JSONObject) {
|
||||||
|
JSONObject jo = (JSONObject) so;
|
||||||
|
ActionList setupActions = new ActionList(this);
|
||||||
|
setupActions.load(jo).parent(this);
|
||||||
|
triggers.put(ROUTE_SETUP, setupActions);
|
||||||
|
}
|
||||||
|
if (so instanceof JSONArray) {
|
||||||
|
JSONArray ja = (JSONArray) so;
|
||||||
|
ActionList setupActions = new ActionList(this);
|
||||||
|
for (Object o : ja) {
|
||||||
|
if (o instanceof JSONObject) {
|
||||||
|
JSONObject jo = (JSONObject) o;
|
||||||
|
String type = jo.getString(TYPE);
|
||||||
|
Action action = Action.create(type, setupActions);
|
||||||
|
if (isSet(action)) {
|
||||||
|
action.load(jo);
|
||||||
|
setupActions.add(action);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
triggers.put(ROUTE_SETUP, setupActions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (json.has(START_ACTIONS)) { // TODO: this is legacy!
|
||||||
|
Object so = json.get(START_ACTIONS);
|
||||||
|
if (so instanceof JSONObject) {
|
||||||
|
JSONObject jo = (JSONObject) so;
|
||||||
|
ActionList startActions = new ActionList(this);
|
||||||
|
startActions.load(jo).parent(this);
|
||||||
|
triggers.put(ROUTE_START, startActions);
|
||||||
|
}
|
||||||
|
if (so instanceof JSONArray) {
|
||||||
|
JSONArray ja = (JSONArray) so;
|
||||||
|
ActionList startActions = new ActionList(this);
|
||||||
|
for (Object o : ja) {
|
||||||
|
if (o instanceof JSONObject) {
|
||||||
|
JSONObject jo = (JSONObject) o;
|
||||||
|
String type = jo.getString(TYPE);
|
||||||
|
Action action = Action.create(type, startActions);
|
||||||
|
if (isSet(action)) {
|
||||||
|
action.load(jo);
|
||||||
|
startActions.add(action);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
triggers.put(ROUTE_START, startActions);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
if (json.has(DISABLED)) disabled = json.getBoolean(DISABLED);
|
if (json.has(DISABLED)) disabled = json.getBoolean(DISABLED);
|
||||||
if (json.has(BRAKE_TIMES)) {
|
if (json.has(BRAKE_TIMES)) {
|
||||||
JSONObject dummy = json.getJSONObject(BRAKE_TIMES);
|
JSONObject dummy = json.getJSONObject(BRAKE_TIMES);
|
||||||
@@ -562,12 +638,13 @@ public class Route extends BaseClass implements Comparable<Route>{
|
|||||||
return plan.registerRoute(this);
|
return plan.registerRoute(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadActions(JSONArray arr) {
|
private void loadActions(JSONObject jsonObject) {
|
||||||
for (int i=0; i<arr.length(); i++) {
|
for (String trigger : jsonObject.keySet()) {
|
||||||
JSONObject json = arr.getJSONObject(i);
|
JSONObject json = jsonObject.getJSONObject(trigger);
|
||||||
String trigger = json.getString(TRIGGER);
|
String type = json.getString(TYPE);
|
||||||
ActionList actionList = new ActionList(this).load(json.getJSONArray(ACTIONS));
|
ActionList actionList = Action.create(type, this);
|
||||||
actionList.parent(this);
|
if (isNull(actionList)) continue;
|
||||||
|
actionList.load(json);
|
||||||
triggers.put(trigger, actionList);
|
triggers.put(trigger, actionList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -578,7 +655,9 @@ public class Route extends BaseClass implements Comparable<Route>{
|
|||||||
JSONObject json = new JSONObject(tokener);
|
JSONObject json = new JSONObject(tokener);
|
||||||
JSONArray routes = json.getJSONArray(ROUTES);
|
JSONArray routes = json.getJSONArray(ROUTES);
|
||||||
for (Object o : routes) {
|
for (Object o : routes) {
|
||||||
if (o instanceof JSONObject) new Route().load((JSONObject)o, plan);
|
if (o instanceof JSONObject) {
|
||||||
|
new Route().load((JSONObject)o, plan);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fis.close();
|
fis.close();
|
||||||
}
|
}
|
||||||
@@ -659,8 +738,6 @@ public class Route extends BaseClass implements Comparable<Route>{
|
|||||||
ActionList actionList = triggers.remove(key);
|
ActionList actionList = triggers.remove(key);
|
||||||
if (isSet(actionList)) actionList.remove();
|
if (isSet(actionList)) actionList.remove();
|
||||||
};
|
};
|
||||||
setupActions.remove();
|
|
||||||
startActions.remove();
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -674,8 +751,6 @@ public class Route extends BaseClass implements Comparable<Route>{
|
|||||||
if (child == train) train = null;
|
if (child == train) train = null;
|
||||||
for (ActionList list : triggers.values()) list.removeChild(child);
|
for (ActionList list : triggers.values()) list.removeChild(child);
|
||||||
turnouts.remove(child);
|
turnouts.remove(child);
|
||||||
setupActions.removeChild(child);
|
|
||||||
startActions.removeChild(child);
|
|
||||||
if (child == startBlock) startBlock = null;
|
if (child == startBlock) startBlock = null;
|
||||||
triggeredContacts.remove(child);
|
triggeredContacts.remove(child);
|
||||||
}
|
}
|
||||||
@@ -771,7 +846,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
|||||||
public boolean train(Train newTrain) {
|
public boolean train(Train newTrain) {
|
||||||
if (isSet(train) && newTrain != train) return false;
|
if (isSet(train) && newTrain != train) return false;
|
||||||
train = newTrain;
|
train = newTrain;
|
||||||
return isSet(train) ? startActions.fire(new Context(this).train(train)) : true;
|
return isSet(train) ? triggers.get(ROUTE_START).fire(new Context(this).train(train)) : true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Route unlock() throws IOException {
|
public Route unlock() throws IOException {
|
||||||
|
|||||||
@@ -37,9 +37,10 @@ public abstract class Action extends BaseClass {
|
|||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Action create(String type,BaseClass parent) {
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T extends Action> T create(String type,BaseClass parent) {
|
||||||
try {
|
try {
|
||||||
return (Action) Class.forName(PREFIX+"."+type).getDeclaredConstructor(BaseClass.class).newInstance(parent);
|
return (T) Class.forName(PREFIX+"."+type).getDeclaredConstructor(BaseClass.class).newInstance(parent);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import de.srsoftware.web4rail.tags.Input;
|
|||||||
|
|
||||||
public class ActionList extends Action implements Iterable<Action>{
|
public class ActionList extends Action implements Iterable<Action>{
|
||||||
static final Logger LOG = LoggerFactory.getLogger(ActionList.class);
|
static final Logger LOG = LoggerFactory.getLogger(ActionList.class);
|
||||||
|
private static final String ACTIONS = "actions";
|
||||||
|
|
||||||
protected Vector<Action> actions;
|
protected Vector<Action> actions;
|
||||||
|
|
||||||
@@ -104,14 +105,11 @@ public class ActionList extends Action implements Iterable<Action>{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JSONObject json() {
|
public JSONObject json() {
|
||||||
String cls = getClass().getSimpleName();
|
JSONObject json = super.json();
|
||||||
throw new UnsupportedOperationException(cls+".json() not supported, use "+cls+".jsonArray instead!");
|
JSONArray jActions = new JSONArray();
|
||||||
}
|
actions.forEach(action -> jActions.put(action.json()));
|
||||||
|
json.put(ACTIONS,jActions);
|
||||||
public JSONArray jsonArray() {
|
return json;
|
||||||
JSONArray result = new JSONArray();
|
|
||||||
for (Action action : actions) result.put(action.json());
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tag list() {
|
public Tag list() {
|
||||||
@@ -136,12 +134,16 @@ public class ActionList extends Action implements Iterable<Action>{
|
|||||||
return span;
|
return span;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ActionList load(JSONArray list) {
|
public Action load(JSONObject json) {
|
||||||
|
super.load(json);
|
||||||
|
if (json.has(ACTIONS)) {
|
||||||
|
JSONArray list = json.getJSONArray(ACTIONS);
|
||||||
for (Object o : list) {
|
for (Object o : list) {
|
||||||
if (o instanceof JSONObject) {
|
if (o instanceof JSONObject) {
|
||||||
JSONObject json = (JSONObject) o;
|
JSONObject jsonObject = (JSONObject) o;
|
||||||
Action action = Action.create(json.getString(TYPE),this);
|
Action action = Action.create(jsonObject.getString(TYPE),this);
|
||||||
if (action != null) add(action.load(json));
|
if (action != null) add(action.load(jsonObject));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ import de.srsoftware.web4rail.tags.Fieldset;
|
|||||||
public class ConditionalAction extends ActionList {
|
public class ConditionalAction extends ActionList {
|
||||||
|
|
||||||
private static final String CONDITIONS = "conditions";
|
private static final String CONDITIONS = "conditions";
|
||||||
private static final String ACTIONS = "actions";
|
|
||||||
private ConditionList conditions = new ConditionList();
|
private ConditionList conditions = new ConditionList();
|
||||||
|
|
||||||
public ConditionalAction(BaseClass parent) {
|
public ConditionalAction(BaseClass parent) {
|
||||||
@@ -41,13 +40,13 @@ public class ConditionalAction extends ActionList {
|
|||||||
JSONArray conditions = new JSONArray();
|
JSONArray conditions = new JSONArray();
|
||||||
for (Condition condition : this.conditions) conditions.put(condition.json());
|
for (Condition condition : this.conditions) conditions.put(condition.json());
|
||||||
json.put(CONDITIONS, conditions);
|
json.put(CONDITIONS, conditions);
|
||||||
json.put(ACTIONS, super.jsonArray());
|
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Action load(JSONObject json) {
|
public Action load(JSONObject json) {
|
||||||
super.load(json);
|
super.load(json);
|
||||||
|
if (json.has(CONDITIONS)) {
|
||||||
for (Object o : json.getJSONArray(CONDITIONS)) {
|
for (Object o : json.getJSONArray(CONDITIONS)) {
|
||||||
if (o instanceof JSONObject) {
|
if (o instanceof JSONObject) {
|
||||||
JSONObject j = (JSONObject) o;
|
JSONObject j = (JSONObject) o;
|
||||||
@@ -58,7 +57,7 @@ public class ConditionalAction extends ActionList {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
super.load(json.getJSONArray(ACTIONS));
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,8 +13,6 @@ import de.srsoftware.web4rail.tags.Input;
|
|||||||
|
|
||||||
public class DelayedAction extends ActionList {
|
public class DelayedAction extends ActionList {
|
||||||
|
|
||||||
|
|
||||||
private static final String ACTIONS = "actions";
|
|
||||||
public static final String DELAY = "delay";
|
public static final String DELAY = "delay";
|
||||||
private static final int DEFAULT_DELAY = 1000;
|
private static final int DEFAULT_DELAY = 1000;
|
||||||
private int delay = DEFAULT_DELAY;
|
private int delay = DEFAULT_DELAY;
|
||||||
@@ -45,16 +43,12 @@ public class DelayedAction extends ActionList {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JSONObject json() {
|
public JSONObject json() {
|
||||||
JSONObject json = super.json();
|
return super.json().put(DELAY, delay);
|
||||||
json.put(DELAY, delay);
|
|
||||||
json.put(ACTIONS, jsonArray());
|
|
||||||
return json;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DelayedAction load(JSONObject json) {
|
public DelayedAction load(JSONObject json) {
|
||||||
super.load(json);
|
super.load(json);
|
||||||
delay = json.getInt(DELAY);
|
delay = json.getInt(DELAY);
|
||||||
if (json.has(ACTIONS)) super.load(json.getJSONArray(ACTIONS));
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import de.srsoftware.web4rail.tags.Input;
|
|||||||
|
|
||||||
public class ConditionList extends Condition implements Iterable<Condition>{
|
public class ConditionList extends Condition implements Iterable<Condition>{
|
||||||
|
|
||||||
|
private static final String CONDITIONS = "conditions";
|
||||||
private Vector<Condition> conditions = new Vector<Condition>();
|
private Vector<Condition> conditions = new Vector<Condition>();
|
||||||
|
|
||||||
public ConditionList add(Condition condition) {
|
public ConditionList add(Condition condition) {
|
||||||
@@ -53,13 +54,10 @@ public class ConditionList extends Condition implements Iterable<Condition>{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JSONObject json() {
|
public JSONObject json() {
|
||||||
String cls = getClass().getSimpleName();
|
JSONObject json = super.json();
|
||||||
throw new UnsupportedOperationException(cls+".json() not supported, use "+cls+".jsonArray instead!");
|
JSONArray jConditions = new JSONArray();
|
||||||
}
|
conditions.stream().map(Condition::json).forEach(js -> jConditions.put(js));
|
||||||
|
json.put(CONDITIONS, jConditions);
|
||||||
public JSONArray jsonArray() {
|
|
||||||
JSONArray json = new JSONArray();
|
|
||||||
for (Condition condition : conditions) json.put(condition.json());
|
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,13 +87,17 @@ public class ConditionList extends Condition implements Iterable<Condition>{
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConditionList load(JSONArray arr) {
|
@Override
|
||||||
for (int i=0; i<arr.length(); i++) {
|
public Condition load(JSONObject json) {
|
||||||
JSONObject json = arr.getJSONObject(i);
|
super.load(json);
|
||||||
Condition condition = Condition.create(json.getString(TYPE));
|
if (json.has(CONDITIONS)) {
|
||||||
if (condition != null) {
|
JSONArray jConditions = json.getJSONArray(CONDITIONS);
|
||||||
condition.parent(this);
|
for (Object o : jConditions) {
|
||||||
conditions.add(condition.load(json));
|
if (o instanceof JSONObject) {
|
||||||
|
JSONObject jo = (JSONObject) o;
|
||||||
|
Condition condition = Condition.create(jo.getString(TYPE));
|
||||||
|
if (isSet(condition)) add(condition.load(jo));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
@@ -8,11 +8,13 @@ import java.util.Map;
|
|||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import de.srsoftware.tools.Tag;
|
import de.srsoftware.tools.Tag;
|
||||||
import de.srsoftware.web4rail.BaseClass;
|
import de.srsoftware.web4rail.BaseClass;
|
||||||
import de.srsoftware.web4rail.Window;
|
import de.srsoftware.web4rail.Window;
|
||||||
|
import de.srsoftware.web4rail.actions.Action;
|
||||||
import de.srsoftware.web4rail.actions.ActionList;
|
import de.srsoftware.web4rail.actions.ActionList;
|
||||||
import de.srsoftware.web4rail.tags.Fieldset;
|
import de.srsoftware.web4rail.tags.Fieldset;
|
||||||
import de.srsoftware.web4rail.tags.Input;
|
import de.srsoftware.web4rail.tags.Input;
|
||||||
@@ -124,7 +126,26 @@ public class Contact extends Tile{
|
|||||||
@Override
|
@Override
|
||||||
public Tile load(JSONObject json) {
|
public Tile load(JSONObject json) {
|
||||||
if (json.has(ADDRESS)) addr(json.getInt(ADDRESS));
|
if (json.has(ADDRESS)) addr(json.getInt(ADDRESS));
|
||||||
if (json.has(REALM_ACTIONS)) actions.load(json.getJSONArray(REALM_ACTIONS));
|
if (json.has(REALM_ACTIONS)) {
|
||||||
|
Object dummy = json.get(REALM_ACTIONS);
|
||||||
|
if (dummy instanceof JSONArray) {
|
||||||
|
JSONArray jarr = (JSONArray) dummy;
|
||||||
|
for (Object o : jarr) {
|
||||||
|
if (o instanceof JSONObject) {
|
||||||
|
JSONObject jo = (JSONObject) o;
|
||||||
|
String type = jo.getString("type");
|
||||||
|
Action action = Action.create(type, actions);
|
||||||
|
if (isSet(action)) {
|
||||||
|
action.load(jo);
|
||||||
|
actions.add(action);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (dummy instanceof JSONObject) {
|
||||||
|
actions.load((JSONObject) dummy);
|
||||||
|
}
|
||||||
|
}
|
||||||
return super.load(json);
|
return super.load(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user