refactoring of load/save processes
This commit is contained in:
@@ -37,9 +37,10 @@ public abstract class Action extends BaseClass {
|
||||
return context;
|
||||
}
|
||||
|
||||
public static Action create(String type,BaseClass parent) {
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T extends Action> T create(String type,BaseClass parent) {
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -22,7 +22,8 @@ import de.srsoftware.web4rail.tags.Input;
|
||||
|
||||
public class ActionList extends Action implements Iterable<Action>{
|
||||
static final Logger LOG = LoggerFactory.getLogger(ActionList.class);
|
||||
|
||||
private static final String ACTIONS = "actions";
|
||||
|
||||
protected Vector<Action> actions;
|
||||
|
||||
public ActionList(BaseClass parent) {
|
||||
@@ -104,14 +105,11 @@ public class ActionList extends Action implements Iterable<Action>{
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
String cls = getClass().getSimpleName();
|
||||
throw new UnsupportedOperationException(cls+".json() not supported, use "+cls+".jsonArray instead!");
|
||||
}
|
||||
|
||||
public JSONArray jsonArray() {
|
||||
JSONArray result = new JSONArray();
|
||||
for (Action action : actions) result.put(action.json());
|
||||
return result;
|
||||
JSONObject json = super.json();
|
||||
JSONArray jActions = new JSONArray();
|
||||
actions.forEach(action -> jActions.put(action.json()));
|
||||
json.put(ACTIONS,jActions);
|
||||
return json;
|
||||
}
|
||||
|
||||
public Tag list() {
|
||||
@@ -136,12 +134,16 @@ public class ActionList extends Action implements Iterable<Action>{
|
||||
return span;
|
||||
}
|
||||
|
||||
public ActionList load(JSONArray list) {
|
||||
for (Object o : list) {
|
||||
if (o instanceof JSONObject) {
|
||||
JSONObject json = (JSONObject) o;
|
||||
Action action = Action.create(json.getString(TYPE),this);
|
||||
if (action != null) add(action.load(json));
|
||||
public Action load(JSONObject json) {
|
||||
super.load(json);
|
||||
if (json.has(ACTIONS)) {
|
||||
JSONArray list = json.getJSONArray(ACTIONS);
|
||||
for (Object o : list) {
|
||||
if (o instanceof JSONObject) {
|
||||
JSONObject jsonObject = (JSONObject) o;
|
||||
Action action = Action.create(jsonObject.getString(TYPE),this);
|
||||
if (action != null) add(action.load(jsonObject));
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
|
||||
@@ -15,7 +15,6 @@ import de.srsoftware.web4rail.tags.Fieldset;
|
||||
public class ConditionalAction extends ActionList {
|
||||
|
||||
private static final String CONDITIONS = "conditions";
|
||||
private static final String ACTIONS = "actions";
|
||||
private ConditionList conditions = new ConditionList();
|
||||
|
||||
public ConditionalAction(BaseClass parent) {
|
||||
@@ -41,24 +40,24 @@ public class ConditionalAction extends ActionList {
|
||||
JSONArray conditions = new JSONArray();
|
||||
for (Condition condition : this.conditions) conditions.put(condition.json());
|
||||
json.put(CONDITIONS, conditions);
|
||||
json.put(ACTIONS, super.jsonArray());
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action load(JSONObject json) {
|
||||
super.load(json);
|
||||
for (Object o : json.getJSONArray(CONDITIONS)) {
|
||||
if (o instanceof JSONObject) {
|
||||
JSONObject j = (JSONObject) o;
|
||||
Condition condition = Condition.create(j.getString(TYPE));
|
||||
if (isSet(condition)) {
|
||||
condition.parent(this);
|
||||
conditions.add(condition.load(j));
|
||||
if (json.has(CONDITIONS)) {
|
||||
for (Object o : json.getJSONArray(CONDITIONS)) {
|
||||
if (o instanceof JSONObject) {
|
||||
JSONObject j = (JSONObject) o;
|
||||
Condition condition = Condition.create(j.getString(TYPE));
|
||||
if (isSet(condition)) {
|
||||
condition.parent(this);
|
||||
conditions.add(condition.load(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
super.load(json.getJSONArray(ACTIONS));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,6 @@ import de.srsoftware.web4rail.tags.Input;
|
||||
|
||||
public class DelayedAction extends ActionList {
|
||||
|
||||
|
||||
private static final String ACTIONS = "actions";
|
||||
public static final String DELAY = "delay";
|
||||
private static final int DEFAULT_DELAY = 1000;
|
||||
private int delay = DEFAULT_DELAY;
|
||||
@@ -45,16 +43,12 @@ public class DelayedAction extends ActionList {
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
json.put(DELAY, delay);
|
||||
json.put(ACTIONS, jsonArray());
|
||||
return json;
|
||||
return super.json().put(DELAY, delay);
|
||||
}
|
||||
|
||||
public DelayedAction load(JSONObject json) {
|
||||
super.load(json);
|
||||
delay = json.getInt(DELAY);
|
||||
if (json.has(ACTIONS)) super.load(json.getJSONArray(ACTIONS));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user