From fb22ab92cc21ad35dfebcf0a58c8f9eabf897b00 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Tue, 8 Dec 2020 09:20:51 +0100 Subject: [PATCH] implemented custom fields --- pom.xml | 2 +- .../translations/Application.de.translation | 2 + .../de/srsoftware/web4rail/BaseClass.java | 64 +++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a4d1f5d..f4b0696 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 de.srsoftware web4rail - 1.2.32 + 1.2.33 Web4Rail jar Java Model Railway Control diff --git a/resources/translations/Application.de.translation b/resources/translations/Application.de.translation index 8568b0f..416c133 100644 --- a/resources/translations/Application.de.translation +++ b/resources/translations/Application.de.translation @@ -11,6 +11,7 @@ add car\: : Waggon hinzufügen: Add condition : Bedingung hinzufügen add locomotive\: : Lok hinzufügen: add new car : neuen Waggon anlegen +Add new custom field : neues benutzerdefinierted Feld hinzufügen add new locomotive : neue Lok anlegen add new train : neuen Zug anlegen Add tile : Kachel hinzufügen @@ -64,6 +65,7 @@ Current location\: : Aktueller Ort: Current location : Aufenthaltsort Current location\: {} : Aufenthaltsort: {} Current velocity\: {} {} : Aktuelle Geschwindigkeit: {} {} +custom fields : benutzerdefinierte Felder Decoder address : Decoder-Adresse Delay : Verzögerung DelayedAction : verzögerte Aktion diff --git a/src/main/java/de/srsoftware/web4rail/BaseClass.java b/src/main/java/de/srsoftware/web4rail/BaseClass.java index 9fb6505..5280a55 100644 --- a/src/main/java/de/srsoftware/web4rail/BaseClass.java +++ b/src/main/java/de/srsoftware/web4rail/BaseClass.java @@ -6,9 +6,11 @@ import java.util.AbstractMap; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Random; +import java.util.Set; import org.json.JSONObject; import org.slf4j.Logger; @@ -41,8 +43,12 @@ public abstract class BaseClass implements Constants{ protected Id id = null; protected String notes; private static HashMap registry = new HashMap(); + private static HashMap,Set> customFieldNames = new HashMap, Set>(); public static final Logger LOG = LoggerFactory.getLogger(BaseClass.class); + private static final String CUSTOM_FIELDS = "custom_Fields"; + private static final String NEW_CUSTOM_FIELD_NAME = "new_custom_field_name"; + private HashMap customFieldValues = new HashMap(); private BaseClass parent; public static class Context { @@ -315,6 +321,17 @@ public abstract class BaseClass implements Constants{ JSONObject json = new JSONObject(); if (isSet(id)) json.put(ID, id().toString()); if (isSet(notes) && !notes.isEmpty()) json.put(NOTES, notes); + Set customFieldNames = BaseClass.customFieldNames.get(getClass()); + JSONObject customFields = null; + if (isSet(customFieldNames)) for (String fieldName : customFieldNames){ + String val = customFieldValues.get(fieldName); + if (isSet(val) && !val.trim().isEmpty()) { + if (isNull(customFields)) customFields = new JSONObject(); + customFields.put(fieldName, val); + } + } + if (isSet(customFields)) json.put(CUSTOM_FIELDS, customFields); + return json; } @@ -344,6 +361,19 @@ public abstract class BaseClass implements Constants{ register(); } if (json.has(NOTES)) notes = json.getString(NOTES); + if (json.has(CUSTOM_FIELDS)) { + JSONObject customFields = json.getJSONObject(CUSTOM_FIELDS); + for (String fieldName : customFields.keySet()) { + String val = customFields.getString(fieldName); + Set customFieldNames = BaseClass.customFieldNames.get(getClass()); + if (isNull(customFieldNames)) { + customFieldNames = new HashSet(); + BaseClass.customFieldNames.put(getClass(),customFieldNames); + } + customFieldNames.add(fieldName); + customFieldValues.put(fieldName, val); + } + } return this; } @@ -399,6 +429,26 @@ public abstract class BaseClass implements Constants{ postForm.forEach(fieldset -> fieldset.addTo(win)); + Fieldset customFields = new Fieldset(t("custom fields")); + + Form customForm = new Form(CUSTOM_FIELDS); + new Input(ACTION, ACTION_UPDATE).hideIn(customForm); + new Input(REALM,realm()).hideIn(customForm); + new Input(ID,id()).hideIn(customForm); + + Table table = new Table(); + + Set fieldNames = customFieldNames.get(getClass()); + if (isSet(fieldNames)) for (String fieldName : fieldNames) { + String val = customFieldValues.get(fieldName); + table.addRow(fieldName,new Input(fieldName,isNull(val) ? "" : val)); + } + + table.addRow(t("Add new custom field"),new Input(NEW_CUSTOM_FIELD_NAME)); + table.addTo(customForm); + new Button(t("Apply"),customForm).addTo(customForm).addTo(customFields); + customFields.addTo(win); + return win; } @@ -450,6 +500,20 @@ public abstract class BaseClass implements Constants{ protected Object update(HashMap params) { LOG.debug("update: {}",params); if (params.containsKey(NOTES)) notes = params.get(NOTES).trim(); + String newCustomFieldName = params.get(NEW_CUSTOM_FIELD_NAME); + Set fieldNames = customFieldNames.get(getClass()); + if (isSet(fieldNames)) for (String fieldName : fieldNames) { + String fieldValue = params.get(fieldName); + if (isSet(fieldValue)) customFieldValues.put(fieldName, fieldValue); + } + if (isSet(newCustomFieldName) && !newCustomFieldName.trim().isEmpty()) { + if (isNull(fieldNames)) { + fieldNames = new HashSet(); + customFieldNames.put(getClass(), fieldNames); + } + fieldNames.add(newCustomFieldName); + } + return this; } }