15 changed files with 295 additions and 44 deletions
@ -0,0 +1,93 @@
@@ -0,0 +1,93 @@
|
||||
package de.srsoftware.web4rail.actions; |
||||
|
||||
import static de.srsoftware.web4rail.Constants.TYPE; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Vector; |
||||
|
||||
import de.srsoftware.tools.Tag; |
||||
import de.srsoftware.web4rail.Route; |
||||
import de.srsoftware.web4rail.Window; |
||||
import de.srsoftware.web4rail.conditions.Condition; |
||||
import de.srsoftware.web4rail.conditions.TrainSelect; |
||||
import de.srsoftware.web4rail.tags.Button; |
||||
import de.srsoftware.web4rail.tags.Form; |
||||
import de.srsoftware.web4rail.tags.Input; |
||||
import de.srsoftware.web4rail.tags.Select; |
||||
import de.srsoftware.web4rail.tiles.Contact; |
||||
|
||||
public class ConditionalAction extends Action { |
||||
|
||||
private Vector<Condition> conditions = new Vector<Condition>(); |
||||
private Vector<Action> actions = new Vector<Action>(); |
||||
|
||||
private ConditionalAction addCondition(Condition condition) { |
||||
conditions.add(new TrainSelect()); |
||||
return this; |
||||
} |
||||
|
||||
private static void addToContact(Route route, Contact contact, String conditionType) { |
||||
Condition condition = null; |
||||
switch (conditionType) { |
||||
case "TrainSelect": |
||||
condition = new TrainSelect(); |
||||
break; |
||||
default: return; |
||||
} |
||||
route.addAction(contact.trigger(), new ConditionalAction().addCondition(condition)); |
||||
} |
||||
|
||||
@Override |
||||
public boolean fire(Context context) throws IOException { |
||||
for (Condition condition : conditions) { |
||||
if (condition.fulfilledBy(context)) return fireActions(context); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
private boolean fireActions(Context context) { |
||||
for (Action action : actions) try { |
||||
action.fire(context); |
||||
} catch (IOException e) { |
||||
LOG.warn("Was not able to fire {}",action); |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
public static Window propForm(HashMap<String, String> params, Route route, Contact contact) { |
||||
String condition = params.get(REALM_CONDITION); |
||||
if (condition != null) { |
||||
addToContact(route,contact,condition); |
||||
return route.properties(); |
||||
} |
||||
Window win = Action.propForm(params); |
||||
String formId = "add-action-to-contact-"+contact.id(); |
||||
Tag form = new Form(formId); |
||||
new Tag("div").content(t("Add Action {} to contact {} on route {}:",ConditionalAction.class.getSimpleName(),contact,route)).addTo(win); |
||||
new Input(REALM, REALM_ROUTE).hideIn(form); |
||||
new Input(ID,route.id()).hideIn(form); |
||||
new Input(ACTION,ACTION_ADD_ACTION).hideIn(form); |
||||
new Input(CONTACT,contact.id()).hideIn(form); |
||||
new Input(TYPE,ConditionalAction.class.getSimpleName()).hideIn(form); |
||||
Select select = new Select(REALM_CONDITION); |
||||
List<Class<? extends Condition>> conditionTypes = List.of(TrainSelect.class); |
||||
for (Class<? extends Condition> clazz : conditionTypes) select.addOption(clazz.getSimpleName()); |
||||
select.addTo(form); |
||||
new Button(t("Create action"),"return submitForm('"+formId+"');").addTo(form).addTo(win); |
||||
return win; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
if (conditions.isEmpty()) return t("Invalid condition"); |
||||
StringBuffer sb = new StringBuffer(); |
||||
for (int i = 0; i<conditions.size(); i++) { |
||||
Condition condition = conditions.get(i); |
||||
Tag link = condition.link("span"); |
||||
sb.append(link); |
||||
} |
||||
return t("if ({}):",sb); |
||||
} |
||||
} |
@ -0,0 +1,69 @@
@@ -0,0 +1,69 @@
|
||||
package de.srsoftware.web4rail.conditions; |
||||
|
||||
import java.util.Date; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
import org.json.JSONObject; |
||||
|
||||
import de.keawe.tools.translations.Translation; |
||||
import de.srsoftware.tools.Tag; |
||||
import de.srsoftware.web4rail.Application; |
||||
import de.srsoftware.web4rail.Constants; |
||||
import de.srsoftware.web4rail.Window; |
||||
import de.srsoftware.web4rail.actions.Action.Context; |
||||
|
||||
public abstract class Condition implements Constants { |
||||
|
||||
private static HashMap<Integer, Condition> conditions = new HashMap<Integer, Condition>(); |
||||
|
||||
public abstract boolean fulfilledBy(Context context); |
||||
protected int id; |
||||
|
||||
public static Object action(HashMap<String, String> params) { |
||||
if (!params.containsKey(ID)) return t("No id passed to Condition.action!"); |
||||
int cid = Integer.parseInt(params.get(ID)); |
||||
Condition condition = conditions.get(cid); |
||||
if (condition == null) return t("No condition with id {}!",cid); |
||||
|
||||
String action = params.get(ACTION); |
||||
if (action == null) return t("No action passed to Condition.action!"); |
||||
|
||||
switch (action) { |
||||
case ACTION_PROPS: |
||||
return condition.properties(); |
||||
case ACTION_UPDATE: |
||||
return condition.update(params); |
||||
} |
||||
return t("Unknown action: {}",action); |
||||
} |
||||
|
||||
protected abstract Window properties(); |
||||
|
||||
|
||||
public Condition() { |
||||
this(new Date().hashCode()); |
||||
} |
||||
|
||||
public Condition(int id) { |
||||
this.id = id; |
||||
conditions.put(id, this); |
||||
} |
||||
|
||||
public Tag link(String tagClass) { |
||||
String json = new JSONObject(Map.of(REALM,REALM_CONDITION,ID,id,ACTION,ACTION_PROPS)).toString().replace("\"", "'"); |
||||
return new Tag(tagClass).clazz("link").attr("onclick","request("+json+")").content(toString()); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public String toString() { |
||||
return t("invalid condition"); |
||||
} |
||||
|
||||
public static String t(String text, Object...fills) { |
||||
return Translation.get(Application.class, text, fills); |
||||
} |
||||
|
||||
protected abstract Object update(HashMap<String, String> params); |
||||
} |
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
package de.srsoftware.web4rail.conditions; |
||||
|
||||
import java.util.HashMap; |
||||
|
||||
import de.srsoftware.web4rail.Window; |
||||
import de.srsoftware.web4rail.actions.Action.Context; |
||||
import de.srsoftware.web4rail.moving.Train; |
||||
import de.srsoftware.web4rail.tags.Button; |
||||
import de.srsoftware.web4rail.tags.Form; |
||||
import de.srsoftware.web4rail.tags.Input; |
||||
import de.srsoftware.web4rail.tags.Label; |
||||
|
||||
public class TrainSelect extends Condition { |
||||
|
||||
private static final Object TRAIN = Train.class.getSimpleName(); |
||||
private Train train; |
||||
|
||||
@Override |
||||
public boolean fulfilledBy(Context context) { |
||||
// TODO Auto-generated method stub
|
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
protected Window properties() { |
||||
Window win = new Window("condition-props", t("Properties of {}",getClass().getSimpleName())); |
||||
String formId = "conditional-props-"+id; |
||||
Form form = new Form(formId); |
||||
new Input(REALM,REALM_CONDITION).hideIn(form); |
||||
new Input(ACTION,ACTION_UPDATE).hideIn(form); |
||||
new Input(ID,id).hideIn(form); |
||||
Train.selector(train, null).addTo(new Label(t("Select train:")+NBSP)).addTo(form); |
||||
new Button(t("Save")).addTo(form).addTo(win); |
||||
return win; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
if (train == null) return super.toString(); |
||||
return t("Train = {}",train); |
||||
} |
||||
|
||||
@Override |
||||
protected Object update(HashMap<String, String> params) { |
||||
if (!params.containsKey(TRAIN)) return t("No train id passed to TrainSelect.update()!"); |
||||
int tid = Integer.parseInt(params.get(TRAIN)); |
||||
Train train = Train.get(tid); |
||||
if (train == null) return t("No train with id {} found!",tid); |
||||
this.train = train; |
||||
return t("Updated condition"); |
||||
} |
||||
} |
Loading…
Reference in new issue