|
| 1 | +package com.exalttech.trex.ui.controllers.daemon; |
| 2 | + |
| 3 | +import com.google.common.net.InetAddresses; |
| 4 | +import javafx.beans.property.SimpleBooleanProperty; |
| 5 | +import javafx.beans.value.ObservableBooleanValue; |
| 6 | +import javafx.collections.FXCollections; |
| 7 | +import javafx.collections.ObservableList; |
| 8 | + |
| 9 | +import java.text.MessageFormat; |
| 10 | +import java.util.*; |
| 11 | +import java.util.regex.Pattern; |
| 12 | +import java.util.stream.Collectors; |
| 13 | + |
| 14 | + |
| 15 | +public class ConfigNode { |
| 16 | + @FunctionalInterface |
| 17 | + public interface Validator { |
| 18 | + /** |
| 19 | + * Validates given value. If doesn't throw then it's value is valid |
| 20 | + * @param value |
| 21 | + * @throws Exception |
| 22 | + */ |
| 23 | + void validate(Object value) throws Exception; |
| 24 | + } |
| 25 | + |
| 26 | + private MetaField meta; |
| 27 | + |
| 28 | + private ObservableList<ConfigNode> children = FXCollections.observableArrayList(); |
| 29 | + private ConfigNode parent; |
| 30 | + |
| 31 | + private boolean removable; |
| 32 | + private SimpleBooleanProperty mandatoryProperty = new SimpleBooleanProperty(); |
| 33 | + |
| 34 | + private Object value; |
| 35 | + |
| 36 | + private Map<MetaField.Type, Validator> validationMap = new HashMap<>(); |
| 37 | + |
| 38 | + |
| 39 | + public ConfigNode(MetaField meta, ConfigNode parent, boolean removable) { |
| 40 | + this.removable = removable; |
| 41 | + this.parent = parent; |
| 42 | + this.meta = meta; |
| 43 | + |
| 44 | + initValidator(); |
| 45 | + |
| 46 | + if (getType() == MetaField.Type.OBJECT) { |
| 47 | + this.meta.attributes.forEach(metaField -> { |
| 48 | + ConfigNode child = new ConfigNode(metaField, this); |
| 49 | + this.children.add(child); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + mandatoryProperty.setValue(isMandatory()); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + public ConfigNode(MetaField meta, ConfigNode parent) { |
| 58 | + this(meta, parent, false); |
| 59 | + } |
| 60 | + |
| 61 | + public ConfigNode(MetaField meta) { |
| 62 | + this(meta, null, false); |
| 63 | + } |
| 64 | + |
| 65 | + private void initValidator() { |
| 66 | + validationMap.put(MetaField.Type.STRING, val -> { String casted = (String) value; }); |
| 67 | + validationMap.put(MetaField.Type.NUMBER, val -> Integer.parseInt((String) value)); |
| 68 | + validationMap.put(MetaField.Type.FLOAT, val -> Float.parseFloat((String) value)); |
| 69 | + validationMap.put(MetaField.Type.BOOLEAN, val -> { boolean casted = (boolean) value; }); |
| 70 | + validationMap.put(MetaField.Type.IP, val -> InetAddresses.forString((String) value)); |
| 71 | + validationMap.put(MetaField.Type.MAC, val -> { |
| 72 | + if (!Pattern.matches("^([0-9a-fA-F]{2}:){5}([0-9a-fA-F]{2})$", (String) value)) { |
| 73 | + throw new Exception(); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + validationMap.put(MetaField.Type.LIST, val -> { |
| 78 | + for (ConfigNode node : children) { |
| 79 | + if (!node.validateValue()) { |
| 80 | + throw new Exception(); |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + validationMap.put(MetaField.Type.OBJECT, val -> { |
| 86 | + for (ConfigNode node : children) { |
| 87 | + if (!node.validateValue()) { |
| 88 | + throw new Exception(); |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + public MetaField.Type getType() { |
| 95 | + return meta.type; |
| 96 | + } |
| 97 | + |
| 98 | + public boolean isMandatory() { |
| 99 | + if (meta.mandatory_if_not_set == null) { |
| 100 | + return meta.mandatory; |
| 101 | + } else { |
| 102 | + return !isDependencySpecified(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private boolean isDependencySpecified() { |
| 107 | + return parent.getChildren() |
| 108 | + .stream() |
| 109 | + .filter(sibling -> sibling.getId().equals(meta.mandatory_if_not_set)) |
| 110 | + .anyMatch(sibling -> sibling.getValue() != null); |
| 111 | + } |
| 112 | + |
| 113 | + public ObservableBooleanValue mandatoryProperty() { |
| 114 | + return mandatoryProperty; |
| 115 | + } |
| 116 | + |
| 117 | + public String getDefaultValue() { |
| 118 | + return meta.default_value; |
| 119 | + } |
| 120 | + |
| 121 | + public String getName() { |
| 122 | + return meta.name; |
| 123 | + } |
| 124 | + |
| 125 | + public String getDescription() { |
| 126 | + return meta.description; |
| 127 | + } |
| 128 | + |
| 129 | + public String getId() { |
| 130 | + return meta.id; |
| 131 | + } |
| 132 | + |
| 133 | + public void setValue(Object newValue) { |
| 134 | + if (getType() == MetaField.Type.LIST) { |
| 135 | + List<Object> newChildren = ((List<Object>) newValue); |
| 136 | + newChildren.forEach(x -> { |
| 137 | + ConfigNode configNode = addListItem(); |
| 138 | + configNode.setValue(x); |
| 139 | + }); |
| 140 | + } else if (getType() == MetaField.Type.OBJECT){ |
| 141 | + Map<String, Object> newMap = (Map<String, Object>) newValue; |
| 142 | + for (Map.Entry<String, Object> entry : newMap.entrySet()) { |
| 143 | + children.stream() |
| 144 | + .filter(x -> x.getId().equals(entry.getKey())) |
| 145 | + .findFirst() |
| 146 | + .ifPresent(x -> x.setValue(entry.getValue())); |
| 147 | + } |
| 148 | + } else { |
| 149 | + value = newValue; |
| 150 | + } |
| 151 | + |
| 152 | + if (getParent() != null) { |
| 153 | + getParent().updateDependencies(); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public Object getValue() { |
| 158 | + if (getType() == MetaField.Type.LIST) { |
| 159 | + List<Object> childrenValues = getChildren().stream() |
| 160 | + .map(ConfigNode::getValue) |
| 161 | + .filter(Objects::nonNull) |
| 162 | + .collect(Collectors.toList()); |
| 163 | + |
| 164 | + if (childrenValues.isEmpty()) { |
| 165 | + return null; |
| 166 | + } |
| 167 | + return childrenValues; |
| 168 | + |
| 169 | + } else if (getType() == MetaField.Type.OBJECT) { |
| 170 | + Map<String, Object> childrenMap = new LinkedHashMap<>(); |
| 171 | + getChildren().stream() |
| 172 | + .filter(field -> field.getValue() != null) |
| 173 | + .forEach(field -> childrenMap.put(field.meta.id, field.getValue())); |
| 174 | + |
| 175 | + if (childrenMap.isEmpty()) { |
| 176 | + return null; |
| 177 | + } |
| 178 | + return childrenMap; |
| 179 | + |
| 180 | + } else { |
| 181 | + try { |
| 182 | + if (getType() == MetaField.Type.NUMBER) { |
| 183 | + return Integer.parseInt((String) value); |
| 184 | + } else if (getType() == MetaField.Type.FLOAT) { |
| 185 | + return Float.parseFloat((String) value); |
| 186 | + } |
| 187 | + } catch (Exception ignored) { |
| 188 | + |
| 189 | + } |
| 190 | + return value; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + private void updateDependencies() { |
| 195 | + getChildren().forEach(x -> x.mandatoryProperty.setValue(x.isMandatory())); |
| 196 | + } |
| 197 | + |
| 198 | + public boolean validateValue() { |
| 199 | + if (getValue() == null) { |
| 200 | + return !isMandatory(); |
| 201 | + } |
| 202 | + |
| 203 | + try { |
| 204 | + validationMap.get(getType()).validate(value); |
| 205 | + return true; |
| 206 | + } catch (Exception ex) { |
| 207 | + return false; |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + public List<String> getValidationErrors() { |
| 212 | + List<String> errors = new ArrayList<>(); |
| 213 | + for (ConfigNode node : this.children) { |
| 214 | + errors.addAll(node.getValidationErrors()); |
| 215 | + } |
| 216 | + |
| 217 | + if (errors.isEmpty()) { |
| 218 | + String idDescription = getIdDescription(); |
| 219 | + |
| 220 | + if (getValue() == null && isMandatory()) { |
| 221 | + errors.add(MessageFormat.format("Field {0} ({1}) is mandatory, it must be specified", |
| 222 | + getName(), |
| 223 | + idDescription |
| 224 | + )); |
| 225 | + } else if (!validateValue()) { |
| 226 | + errors.add(MessageFormat.format("Field {0} ({1}) equals \"{2}\", and cannot be parsed as {3}", |
| 227 | + getName(), |
| 228 | + idDescription, |
| 229 | + value, |
| 230 | + getType().toString() |
| 231 | + )); |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + return errors; |
| 236 | + } |
| 237 | + |
| 238 | + private String getIdDescription() { |
| 239 | + if (getParent() != null) { |
| 240 | + if (getParent().getType() == MetaField.Type.LIST) { |
| 241 | + return getParent().getIdDescription() + "[" + getParent().getChildren().indexOf(this) + "]"; |
| 242 | + } else { |
| 243 | + return getParent().getIdDescription() + "." + getId(); |
| 244 | + } |
| 245 | + } else { |
| 246 | + return getId(); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + public ConfigNode getParent() { |
| 251 | + return parent; |
| 252 | + } |
| 253 | + |
| 254 | + public ObservableList<ConfigNode> getChildren() { |
| 255 | + return children; |
| 256 | + } |
| 257 | + |
| 258 | + public ConfigNode addListItem() { |
| 259 | + ConfigNode newChild = new ConfigNode(meta.item, this, true); |
| 260 | + children.add(newChild); |
| 261 | + return newChild; |
| 262 | + } |
| 263 | + |
| 264 | + public boolean isRemovable() { |
| 265 | + return removable; |
| 266 | + } |
| 267 | +} |
0 commit comments