Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Added support for latest AttributeLink to GWT client

Rich Turner78e910599317
diff --git a/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java b/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java
index 0acd0f8bc..26fab1e09 100644
--- a/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java
+++ b/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java
@@ -52,6 +52,7 @@ import org.openremote.model.query.filter.BooleanPredicate;
import org.openremote.model.query.filter.TenantPredicate;
import org.openremote.model.util.EnumUtil;
import org.openremote.model.util.Pair;
+import org.openremote.model.value.ObjectValue;
import org.openremote.model.value.Value;
import org.openremote.model.value.ValueType;
import org.openremote.model.value.Values;
@@ -452,8 +453,11 @@ public class AssetEditActivity
parentView,
this::createValueEditor,
this::showValidationError,
- valueHolder.getValueAsString().map(attributeLinkMapper::read).orElse(null),
- attrLink -> onValueModified.accept(attrLink != null ? Values.create(attributeLinkMapper.write(attrLink)) : null),
+ valueHolder.getValue().map(Value::toJson).map(attributeLinkMapper::read).orElse(null),
+ attrLink -> {
+ Value attrLinkObj = attrLink != null ? Values.parse(attributeLinkMapper.write(attrLink)).orElse(null) : null;
+ onValueModified.accept(attrLinkObj);
+ },
isReadOnly,
assetAttributeConsumer -> getLinkableAssetsAndAttributes(valueHolder, assetAttributeConsumer),
assetWatermark,
diff --git a/client/src/main/java/org/openremote/app/client/assets/attributes/ValueFilterArrayMapper.java b/client/src/main/java/org/openremote/app/client/assets/attributes/ValueFilterArrayMapper.java
new file mode 100644
index 000000000..f1f384b8c
--- /dev/null
+++ b/client/src/main/java/org/openremote/app/client/assets/attributes/ValueFilterArrayMapper.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2016, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.app.client.assets.attributes;
+
+import com.github.nmorel.gwtjackson.client.ObjectMapper;
+import com.github.nmorel.gwtjackson.client.annotation.JsonMixIns;
+import org.openremote.app.client.interop.jackson.DefaultJsonMixin;
+import org.openremote.app.client.rest.EntityReader;
+import org.openremote.app.client.rest.EntityWriter;
+import org.openremote.model.attribute.AttributeLink;
+import org.openremote.model.value.ValueFilter;
+
+@JsonMixIns({@JsonMixIns.JsonMixIn(target = ValueFilter.class, mixIn = DefaultJsonMixin.class)})
+public interface ValueFilterArrayMapper
+ extends ObjectMapper<ValueFilter[]> {
+}
diff --git a/client/src/main/java/org/openremote/app/client/widget/AttributeLinkEditor.java b/client/src/main/java/org/openremote/app/client/widget/AttributeLinkEditor.java
index df116b908..be2dc51e8 100644
--- a/client/src/main/java/org/openremote/app/client/widget/AttributeLinkEditor.java
+++ b/client/src/main/java/org/openremote/app/client/widget/AttributeLinkEditor.java
@@ -19,12 +19,14 @@
*/
package org.openremote.app.client.widget;
+import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.IsWidget;
import org.openremote.app.client.Environment;
import org.openremote.app.client.assets.attributes.AttributeView;
import org.openremote.app.client.assets.attributes.MetaEditor;
+import org.openremote.app.client.assets.attributes.ValueFilterArrayMapper;
import org.openremote.model.ValidationFailure;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.attribute.MetaItemType;
@@ -32,15 +34,15 @@ import org.openremote.model.attribute.*;
import org.openremote.model.interop.Consumer;
import org.openremote.model.util.EnumUtil;
import org.openremote.model.util.Pair;
-import org.openremote.model.value.ObjectValue;
-import org.openremote.model.value.Value;
-import org.openremote.model.value.ValueType;
-import org.openremote.model.value.Values;
+import org.openremote.model.value.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
+import static org.openremote.app.client.widget.ValueEditors.createArrayEditor;
+import static org.openremote.app.client.widget.ValueEditors.createObjectEditor;
+
public class AttributeLinkEditor extends FlowPanel {
public enum FailureReason implements ValidationFailure.Reason {
@@ -53,6 +55,7 @@ public class AttributeLinkEditor extends FlowPanel {
protected final Environment environment;
protected final AttributeRefEditor attributeRefEditor;
protected final MetaEditor converterEditor;
+ protected final IsWidget filterEditor;
protected final Consumer<AttributeLink> onValueModified;
protected final AttributeView parentView;
protected final AttributeView.ValidationErrorConsumer validationErrorConsumer;
@@ -60,6 +63,8 @@ public class AttributeLinkEditor extends FlowPanel {
// This is used to map converter values to attribute meta so we can use the meta editor
protected final AssetAttribute converterAttribute;
protected AttributeRef attributeRef;
+ protected ValueFilter[] filters;
+ ValueFilterArrayMapper filterMapper = GWT.create(ValueFilterArrayMapper.class);
public AttributeLinkEditor(Environment environment,
AttributeView.Style viewStyle,
@@ -251,6 +256,35 @@ public class AttributeLinkEditor extends FlowPanel {
converterEditor.setValidationErrorConsumer(this::showConverterEditorValidationError);
add(converterEditor);
+
+ ArrayValue filterObj = null;
+
+ if (currentValue != null && currentValue.getFilters() != null) {
+ filters = currentValue.getFilters();
+ filterObj = Values.parse(filterMapper.write(currentValue.getFilters())).flatMap(Values::getArray).orElse(null);
+ }
+
+ filterEditor = createArrayEditor(
+ filterObj,
+ this::onFiltersModified,
+ Optional.empty(),
+ readOnly,
+ environment.getWidgetStyle(),
+ environment.getMessages()
+ );
+
+ add(filterEditor);
+ }
+
+ private void onFiltersModified(ArrayValue arrayValue) {
+
+ if (arrayValue != null) {
+ filters = filterMapper.read(arrayValue.toJson());
+ } else {
+ filters = null;
+ }
+
+ updateAttributeLink();
}
protected void onAttributeRefModified(AttributeRef attributeRef) {
@@ -318,7 +352,7 @@ public class AttributeLinkEditor extends FlowPanel {
if (!validationResult.isValid()) {
onValueModified.accept(null);
} else {
- onValueModified.accept(new AttributeLink(attributeRef, convertMetaToConverter(converterAttribute.getMeta()), null));
+ onValueModified.accept(new AttributeLink(attributeRef, convertMetaToConverter(converterAttribute.getMeta()), filters));
}
}
diff --git a/model/src/main/java/org/openremote/model/asset/AssetAttribute.java b/model/src/main/java/org/openremote/model/asset/AssetAttribute.java
index 18096f382..a1b0b9e0c 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetAttribute.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetAttribute.java
@@ -453,7 +453,7 @@ public class AssetAttribute extends Attribute {
public static Optional<AssetAttribute> attributeFromJson(ObjectValue objectValue, String assetId, String name) {
if (objectValue == null) {
- return Optional.empty();
+ return Optional.of(new AssetAttribute(name));
}
AssetAttribute attribute = new AssetAttribute(objectValue);
@@ -533,9 +533,8 @@ public class AssetAttribute extends Attribute {
//noinspection ConstantConditions
return Arrays
.stream(objectValue.keys())
- .map(key -> new Pair<>(key, objectValue.getObject(key)))
- .filter(pair -> pair.value.isPresent())
- .map(pair -> attributeFromJson(pair.value.get(), assetId, pair.key))
+ .map(key -> new Pair<>(key, objectValue.getObject(key).orElse(null)))
+ .map(pair -> attributeFromJson(pair.value, assetId, pair.key))
.filter(Optional::isPresent)
.map(Optional::get);
}
diff --git a/model/src/main/java/org/openremote/model/attribute/AttributeLink.java b/model/src/main/java/org/openremote/model/attribute/AttributeLink.java
index 1a70fa8f0..c28ec6f8c 100644
--- a/model/src/main/java/org/openremote/model/attribute/AttributeLink.java
+++ b/model/src/main/java/org/openremote/model/attribute/AttributeLink.java
@@ -23,6 +23,7 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.openremote.model.value.*;
+import java.util.Arrays;
import java.util.Optional;
import static java.util.Objects.requireNonNull;
@@ -126,15 +127,17 @@ public class AttributeLink {
return getClass().getSimpleName() + "{" +
"attributeRef=" + attributeRef +
", converter=" + converter +
+ ", filters=" + (filters != null ? Arrays.toString(filters) : "null") +
'}';
}
public static boolean isAttributeLink(Value value) {
- return Values.getObject(value)
- .map(objectValue -> {
- return AttributeRef.isAttributeRef(objectValue.get("attributeRef").orElse(null)) &&
- objectValue.get("converter").map(v -> v.getType() == ValueType.OBJECT).orElse(true);
- })
+ boolean result = Values.getObject(value)
+ .map(objectValue -> AttributeRef.isAttributeRef(objectValue.get("attributeRef").orElse(null)) &&
+ objectValue.get("converter").map(v -> v.getType() == ValueType.OBJECT).orElse(true) &&
+ objectValue.get("filters").map(v -> v.getType() == ValueType.ARRAY).orElse(true)
+ )
.orElse(false);
+ return result;
}
}
\ No newline at end of file
diff --git a/model/src/main/java/org/openremote/model/attribute/AttributeRef.java b/model/src/main/java/org/openremote/model/attribute/AttributeRef.java
index d2ab78e0b..43bd470fc 100644
--- a/model/src/main/java/org/openremote/model/attribute/AttributeRef.java
+++ b/model/src/main/java/org/openremote/model/attribute/AttributeRef.java
@@ -96,12 +96,20 @@ public class AttributeRef {
}
public static boolean isAttributeRef(Value value) {
- return Values.getArray(value)
- .filter(arrayValue ->
+ boolean result = Values.getArray(value)
+ .map(arrayValue ->
arrayValue.length() == 2
&& arrayValue.getString(0).filter(s -> !s.isEmpty()).isPresent()
&& arrayValue.getString(1).filter(s -> !s.isEmpty()).isPresent()
- ).isPresent();
+ )
+ .orElse(
+ Values.getObject(value)
+ .map(objectValue ->
+ objectValue.getString("entityId").isPresent() && objectValue.getString("attributeName").isPresent()
+ ).orElse(false)
+ );
+
+ return result;
}
@SuppressWarnings("ConstantConditions")