Skip to content
Permalink
Browse files

Cleanup of descriptors and added deserialisation implementations

  • Loading branch information...
richturner committed Apr 15, 2019
1 parent ea31e05 commit 756e3c2d4303e6aa5c1fd785eb805210c344f9a1
@@ -243,7 +243,9 @@ public boolean addAttribute(String name, String type) {

attribute = new AssetAttribute();
attribute.setType(attributeValueType);
attributeValueType.getMetaItemDescriptors().ifPresent(attribute::addMeta);
if (attributeValueType.getMetaItemDescriptors() != null) {
attribute.addMeta(attributeValueType.getMetaItemDescriptors());
}
}

attribute.setName(name);
@@ -465,27 +465,30 @@ public Asset create(RequestParams requestParams, Asset asset) {

// Add meta items to well known attributes if not present
newAsset.getAttributesStream().forEach(assetAttribute -> {
Optional<AttributeDescriptor> attributeDescriptor = assetDescriptor.getAttributeDescriptors()
.flatMap(attributeDescriptors ->
Arrays.stream(attributeDescriptors)
.filter(attrDescriptor -> attrDescriptor.getAttributeName().equals(assetAttribute.getNameOrThrow()))
.findFirst()
);

attributeDescriptor.ifPresent(defaultAttribute ->
defaultAttribute.getMetaItemDescriptors().ifPresent(metaItemDescriptors ->
assetAttribute.addMeta(
Arrays.stream(metaItemDescriptors).filter(metaItemDescriptor -> !assetAttribute.hasMetaItem(metaItemDescriptor)).map(MetaItem::new).toArray(MetaItem[]::new)
))
);
if (assetDescriptor.getAttributeDescriptors() != null) {
Arrays.stream(assetDescriptor.getAttributeDescriptors())
.filter(attrDescriptor -> attrDescriptor.getAttributeName().equals(assetAttribute.getNameOrThrow()))
.findFirst()
.ifPresent(defaultAttribute -> {
if (defaultAttribute.getMetaItemDescriptors() != null) {
assetAttribute.addMeta(
Arrays.stream(defaultAttribute.getMetaItemDescriptors())
.filter(metaItemDescriptor -> !assetAttribute.hasMetaItem(metaItemDescriptor))
.map(MetaItem::new)
.toArray(MetaItem[]::new)
);
}
});
}
});

// Add attributes for this well known asset if not present
assetDescriptor.getAttributeDescriptors().ifPresent(attributeDescriptors ->
newAsset.addAttributes(
Arrays.stream(attributeDescriptors).filter(attributeDescriptor ->
!newAsset.hasAttribute(attributeDescriptor.getAttributeName())).map(AssetAttribute::new).toArray(AssetAttribute[]::new)
));
if (assetDescriptor.getAttributeDescriptors() != null) {
newAsset.addAttributes(
Arrays.stream(assetDescriptor.getAttributeDescriptors()).filter(attributeDescriptor ->
!newAsset.hasAttribute(attributeDescriptor.getAttributeName())).map(AssetAttribute::new).toArray(AssetAttribute[]::new)
);
}
});

//Check if a well known attribute is added
@@ -300,8 +300,9 @@ public Asset(@NotNull String name, @NotNull String type, Asset parent) {

public Asset(@NotNull String name, @NotNull AssetDescriptor type, Asset parent, String realm) {
this(name, type.getType(), type.getAccessPublicRead(), parent, realm);
type.getAttributeDescriptors().ifPresent(attributeDescriptors ->
addAttributes(Arrays.stream(attributeDescriptors).map(AssetAttribute::new).toArray(AssetAttribute[]::new)));
if (type.getAttributeDescriptors() != null) {
addAttributes(Arrays.stream(type.getAttributeDescriptors()).map(AssetAttribute::new).toArray(AssetAttribute[]::new));
}
}

public Asset(@NotNull String name, @NotNull String type, boolean accessPublicRead, Asset parent, String realm) {
@@ -86,9 +86,9 @@ public AssetAttribute(String name, AttributeDescriptor attributeDescriptor, Valu
}
}
setValue(value, timestamp);
attributeDescriptor.getMetaItemDescriptors().ifPresent(metaItemDescriptors ->
addMeta(Arrays.stream(metaItemDescriptors).map(MetaItem::new).toArray(MetaItem[]::new))
);
if (attributeDescriptor.getMetaItemDescriptors() != null) {
addMeta(Arrays.stream(attributeDescriptor.getMetaItemDescriptors()).map(MetaItem::new).toArray(MetaItem[]::new));
}
}

public AssetAttribute(String name, AttributeValueType type, Value value, long timestamp) {
@@ -21,11 +21,11 @@

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.openremote.model.attribute.AttributeDescriptor;

import java.util.Optional;

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
@JsonDeserialize(as = AssetDescriptorImpl.class)
public interface AssetDescriptor {

@JsonProperty
@@ -37,5 +37,5 @@

boolean getAccessPublicRead();

Optional<AttributeDescriptor[]> getAttributeDescriptors();
AttributeDescriptor[] getAttributeDescriptors();
}
@@ -0,0 +1,70 @@
/*
* Copyright 2019, 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.model.asset;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.openremote.model.attribute.AttributeDescriptor;

public class AssetDescriptorImpl implements AssetDescriptor {

protected String name;
protected String type;
protected String icon;
protected boolean accessPublicRead;
AttributeDescriptor[] attributeDescriptors;

@JsonCreator
public AssetDescriptorImpl(@JsonProperty("name") String name,
@JsonProperty("type") String type,
@JsonProperty("icon") String icon,
@JsonProperty("accessPublicRead") boolean accessPublicRead,
@JsonProperty("attributeDescriptors") AttributeDescriptor[] attributeDescriptors) {
this.name = name;
this.type = type;
this.icon = icon;
this.accessPublicRead = accessPublicRead;
this.attributeDescriptors = attributeDescriptors;
}

@Override
public String getName() {
return name;
}

@Override
public String getType() {
return type;
}

@Override
public String getIcon() {
return icon;
}

public boolean getAccessPublicRead() {
return accessPublicRead;
}

@Override
public AttributeDescriptor[] getAttributeDescriptors() {
return attributeDescriptors;
}
}
@@ -103,7 +103,7 @@ public boolean getAccessPublicRead() {
}

@Override
public Optional<AttributeDescriptor[]> getAttributeDescriptors() {
return Optional.ofNullable(attributeDescriptors);
public AttributeDescriptor[] getAttributeDescriptors() {
return attributeDescriptors;
}
}
@@ -20,20 +20,28 @@
package org.openremote.model.attribute;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.openremote.model.value.Value;

import java.util.Optional;

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
@JsonDeserialize(as = AttributeDescriptorImpl.class)
public interface AttributeDescriptor {

@JsonProperty
String getName();

@JsonProperty
String getAttributeName();

@JsonProperty
AttributeValueDescriptor getValueDescriptor();

Optional<MetaItemDescriptor[]> getMetaItemDescriptors();
@JsonProperty
MetaItemDescriptor[] getMetaItemDescriptors();

@JsonProperty
Value getInitialValue();
}
@@ -0,0 +1,71 @@
/*
* Copyright 2019, 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.model.attribute;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.openremote.model.value.Value;

public class AttributeDescriptorImpl implements AttributeDescriptor {

protected String name;
protected String attributeName;
protected AttributeValueDescriptor valueDescriptor;
protected MetaItemDescriptor[] metaItemDescriptors;
protected Value initialValue;

@JsonCreator
public AttributeDescriptorImpl(@JsonProperty("name") String name,
@JsonProperty("attributeName") String attributeName,
@JsonProperty("valueDescriptor") AttributeValueDescriptor valueDescriptor,
@JsonProperty("metaItemDescriptors") MetaItemDescriptor[] metaItemDescriptors,
@JsonProperty("initialValue") Value initialValue) {
this.name = name;
this.attributeName = attributeName;
this.valueDescriptor = valueDescriptor;
this.metaItemDescriptors = metaItemDescriptors;
this.initialValue = initialValue;
}

@Override
public String getName() {
return name;
}

@Override
public String getAttributeName() {
return attributeName;
}

@Override
public AttributeValueDescriptor getValueDescriptor() {
return valueDescriptor;
}

@Override
public MetaItemDescriptor[] getMetaItemDescriptors() {
return metaItemDescriptors;
}

@Override
public Value getInitialValue() {
return initialValue;
}
}
@@ -19,7 +19,6 @@
*/
package org.openremote.model.attribute;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.openremote.model.value.Value;
import org.openremote.model.value.Values;

@@ -70,17 +69,17 @@


final protected String attributeName;
final protected AttributeValueDescriptor attributeValueDescriptor;
final protected AttributeValueDescriptor valueDescriptor;
final protected Value initialValue;
final protected MetaItemDescriptor[] metaItemDescriptors;

AttributeType(String attributeName, AttributeValueDescriptor attributeValueDescriptor, MetaItemDescriptor... metaItemDescriptors) {
this(attributeName, attributeValueDescriptor, null, metaItemDescriptors);
AttributeType(String attributeName, AttributeValueDescriptor valueDescriptor, MetaItemDescriptor... metaItemDescriptors) {
this(attributeName, valueDescriptor, null, metaItemDescriptors);
}

AttributeType(String attributeName, AttributeValueDescriptor attributeValueDescriptor, Value initialValue, MetaItemDescriptor... metaItemDescriptors) {
AttributeType(String attributeName, AttributeValueDescriptor valueDescriptor, Value initialValue, MetaItemDescriptor... metaItemDescriptors) {
this.attributeName = attributeName;
this.attributeValueDescriptor = attributeValueDescriptor;
this.valueDescriptor = valueDescriptor;
this.metaItemDescriptors = metaItemDescriptors;
this.initialValue = initialValue;
}
@@ -109,12 +108,12 @@ public String getAttributeName() {

@Override
public AttributeValueDescriptor getValueDescriptor() {
return attributeValueDescriptor;
return valueDescriptor;
}

@Override
public Optional<MetaItemDescriptor[]> getMetaItemDescriptors() {
return Optional.ofNullable(metaItemDescriptors);
public MetaItemDescriptor[] getMetaItemDescriptors() {
return metaItemDescriptors;
}

@Override
@@ -123,40 +122,11 @@ public Value getInitialValue() {
}

public AttributeDescriptor withName(String name, MetaItemDescriptor... metaItemDescriptors) {
return withName(name, null, metaItemDescriptors);
return withName(name, initialValue, metaItemDescriptors);
}

public AttributeDescriptor withName(String name, Value initialValue, MetaItemDescriptor... metaItemDescriptors) {

return new AttributeDescriptor() {
@Override
public String getName() {
return name();
}

@Override
@JsonProperty
public String getAttributeName() {
return name;
}

@Override
@JsonProperty
public AttributeValueDescriptor getValueDescriptor() {
return attributeValueDescriptor;
}

@Override
@JsonProperty
public Optional<MetaItemDescriptor[]> getMetaItemDescriptors() {
return Optional.ofNullable(metaItemDescriptors);
}

@Override
@JsonProperty
public Value getInitialValue() {
return initialValue;
}
};
return new AttributeDescriptorImpl(name, attributeName, valueDescriptor, metaItemDescriptors, initialValue);
}
}
Oops, something went wrong.

0 comments on commit 756e3c2

Please sign in to comment.
You can’t perform that action at this time.