Skip to content
Permalink
Browse files

Added Jackson formatting for optional

Added functions for simple custom AttributeDescriptor
  • Loading branch information...
Michael Rademaker
Michael Rademaker committed Apr 9, 2019
1 parent cc5a5da commit 5eed9206d0052e0e7edffa81ea7275dcb238edac
@@ -17,6 +17,7 @@ dependencies {
compile "com.devskiller.friendly-id:friendly-id:$friendlyIdVersion"

compile "com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:$jacksonVersion"
compile "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:$jacksonVersion"

compile "io.undertow:undertow-servlet:$undertowVersion"
compile "io.undertow:undertow-websockets-jsr:$undertowVersion"
@@ -25,6 +25,7 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import org.openremote.container.concurrent.ContainerThreads;
import org.openremote.container.json.ModelModule;
import org.openremote.container.util.LogUtil;
@@ -70,7 +71,8 @@
.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
.setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY)
.registerModule(new ModelModule());
.registerModule(new ModelModule())
.registerModule(new Jdk8Module());

protected final Map<String, String> config = new HashMap<>();
protected final boolean devMode;
@@ -24,7 +24,7 @@
import org.openremote.manager.web.ManagerWebResource;
import org.openremote.model.asset.AssetDescriptor;
import org.openremote.model.asset.AssetModelResource;
import org.openremote.model.attribute.AttributeTypeDescriptor;
import org.openremote.model.attribute.AttributeDescriptor;
import org.openremote.model.attribute.AttributeValueDescriptor;
import org.openremote.model.attribute.MetaItemDescriptor;
import org.openremote.model.http.RequestParams;
@@ -42,8 +42,8 @@ public AssetModelResourceImpl(TimerService timerService, ManagerIdentityService
}

@Override
public AttributeTypeDescriptor[] getAttributeTypeDescriptors(RequestParams requestParams) {
return AssetModelUtil.getAttributeTypeDescriptors();
public AttributeDescriptor[] getAttributeTypeDescriptors(RequestParams requestParams) {
return AssetModelUtil.getAttributeDescriptors();
}

@Override
@@ -23,7 +23,7 @@
import org.openremote.container.ContainerService;
import org.openremote.model.asset.AssetDescriptor;
import org.openremote.model.asset.AssetModelProvider;
import org.openremote.model.attribute.AttributeTypeDescriptor;
import org.openremote.model.attribute.AttributeDescriptor;
import org.openremote.model.attribute.AttributeValueDescriptor;
import org.openremote.model.attribute.MetaItemDescriptor;
import org.openremote.model.util.AssetModelUtil;
@@ -51,21 +51,21 @@ public int getPriority() {
public void init(Container container) throws Exception {

List<AssetDescriptor> assetDescriptors = new ArrayList<>();
List<AttributeTypeDescriptor> attributeTypeDescriptors = new ArrayList<>();
List<AttributeDescriptor> attributeDescriptors = new ArrayList<>();
List<AttributeValueDescriptor> attributeValueDescriptors = new ArrayList<>();
List<MetaItemDescriptor> metaItemDescriptors = new ArrayList<>();

ServiceLoader.load(AssetModelProvider.class).forEach(assetModelProvider -> {
LOG.fine("Adding asset model descriptors of provider: " + assetModelProvider.getClass().getName());

assetDescriptors.addAll(Arrays.asList(assetModelProvider.getAssetDescriptors()));
attributeTypeDescriptors.addAll(Arrays.asList(assetModelProvider.getAttributeTypeDescriptors()));
attributeDescriptors.addAll(Arrays.asList(assetModelProvider.getAttributeTypeDescriptors()));
attributeValueDescriptors.addAll(Arrays.asList(assetModelProvider.getAttributeValueDescriptors()));
metaItemDescriptors.addAll(Arrays.asList(assetModelProvider.getMetaItemDescriptors()));
});

AssetModelUtil.setAssetDescriptors(assetDescriptors.toArray(new AssetDescriptor[0]));
AssetModelUtil.setAttributeTypeDescriptors(attributeTypeDescriptors.toArray(new AttributeTypeDescriptor[0]));
AssetModelUtil.setAttributeDescriptors(attributeDescriptors.toArray(new AttributeDescriptor[0]));
AssetModelUtil.setAttributeValueDescriptors(attributeValueDescriptors.toArray(new AttributeValueDescriptor[0]));
AssetModelUtil.setMetaItemDescriptors(metaItemDescriptors.toArray(new MetaItemDescriptor[0]));
}
@@ -465,7 +465,7 @@ public Asset create(RequestParams requestParams, Asset asset) {

// Add meta items to well known attributes if not present
newAsset.getAttributesStream().forEach(assetAttribute -> {
Optional<AttributeTypeDescriptor> attributeDescriptor = assetDescriptor.getAttributeTypeDescriptors()
Optional<AttributeDescriptor> attributeDescriptor = assetDescriptor.getAttributeDescriptors()
.flatMap(attributeDescriptors ->
Arrays.stream(attributeDescriptors)
.filter(attrDescriptor -> attrDescriptor.getName().equals(assetAttribute.getNameOrThrow()))
@@ -481,7 +481,7 @@ public Asset create(RequestParams requestParams, Asset asset) {
});

// Add attributes for this well known asset if not present
assetDescriptor.getAttributeTypeDescriptors().ifPresent(attributeDescriptors ->
assetDescriptor.getAttributeDescriptors().ifPresent(attributeDescriptors ->
newAsset.addAttributes(
Arrays.stream(attributeDescriptors).filter(attributeDescriptor ->
!newAsset.hasAttribute(attributeDescriptor.getName())).map(AssetAttribute::new).toArray(AssetAttribute[]::new)
@@ -26,7 +26,7 @@
import org.openremote.model.IdentifiableEntity;
import org.openremote.model.ValidationFailure;
import org.openremote.model.attribute.Attribute;
import org.openremote.model.attribute.AttributeTypeDescriptor;
import org.openremote.model.attribute.AttributeDescriptor;
import org.openremote.model.geo.GeoJSON;
import org.openremote.model.geo.GeoJSONFeature;
import org.openremote.model.geo.GeoJSONFeatureCollection;
@@ -300,7 +300,7 @@ 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.getAttributeTypeDescriptors().ifPresent(attributeDescriptors ->
type.getAttributeDescriptors().ifPresent(attributeDescriptors ->
addAttributes(Arrays.stream(attributeDescriptors).map(AssetAttribute::new).toArray(AssetAttribute[]::new)));
}

@@ -366,8 +366,8 @@ public Asset removeAttribute(String name) {
return this;
}

public Asset removeAttribute(AttributeTypeDescriptor attributeTypeDescriptor) {
return removeAttribute(attributeTypeDescriptor.getName());
public Asset removeAttribute(AttributeDescriptor attributeDescriptor) {
return removeAttribute(attributeDescriptor.getName());
}

public String getId() {
@@ -538,7 +538,7 @@ public boolean hasAttribute(String name) {
return attributes != null && attributes.hasKey(name);
}

public Optional<AssetAttribute> getAttribute(AttributeTypeDescriptor descriptor) {
public Optional<AssetAttribute> getAttribute(AttributeDescriptor descriptor) {
return getAttribute(descriptor.getName());
}

@@ -58,35 +58,35 @@ public AssetAttribute(String name, AttributeValueDescriptor type) {
super(name, type);
}

public AssetAttribute(AttributeTypeDescriptor attributeTypeDescriptor) {
this(attributeTypeDescriptor.getName(), attributeTypeDescriptor);
public AssetAttribute(AttributeDescriptor attributeDescriptor) {
this(attributeDescriptor.getName(), attributeDescriptor);
}

public AssetAttribute(String name, AttributeTypeDescriptor attributeTypeDescriptor) {
this(name, attributeTypeDescriptor, attributeTypeDescriptor.getInitialValue());
public AssetAttribute(String name, AttributeDescriptor attributeDescriptor) {
this(name, attributeDescriptor, attributeDescriptor.getInitialValue());
}

public AssetAttribute(AttributeTypeDescriptor attributeTypeDescriptor, Value value) {
this(attributeTypeDescriptor.getName(), attributeTypeDescriptor, value);
public AssetAttribute(AttributeDescriptor attributeDescriptor, Value value) {
this(attributeDescriptor.getName(), attributeDescriptor, value);
}

public AssetAttribute(String name, AttributeTypeDescriptor attributeTypeDescriptor, Value value) {
this(name, attributeTypeDescriptor, value, 0L);
public AssetAttribute(String name, AttributeDescriptor attributeDescriptor, Value value) {
this(name, attributeDescriptor, value, 0L);
}

public AssetAttribute(AttributeTypeDescriptor attributeTypeDescriptor, Value value, long timestamp) {
this(attributeTypeDescriptor.getName(), attributeTypeDescriptor, value, timestamp);
public AssetAttribute(AttributeDescriptor attributeDescriptor, Value value, long timestamp) {
this(attributeDescriptor.getName(), attributeDescriptor, value, timestamp);
}

public AssetAttribute(String name, AttributeTypeDescriptor attributeTypeDescriptor, Value value, long timestamp) {
super(name, attributeTypeDescriptor.getValueDescriptor(), value);
public AssetAttribute(String name, AttributeDescriptor attributeDescriptor, Value value, long timestamp) {
super(name, attributeDescriptor.getValueDescriptor(), value);
if (value != null) {
if (value.getType() != attributeTypeDescriptor.getValueDescriptor().getValueType()) {
if (value.getType() != attributeDescriptor.getValueDescriptor().getValueType()) {
throw new IllegalArgumentException("Provided value type is not compatible with this attribute type");
}
}
setValue(value, timestamp);
attributeTypeDescriptor.getMetaItemDescriptors().ifPresent(metaItemDescriptors ->
attributeDescriptor.getMetaItemDescriptors().ifPresent(metaItemDescriptors ->
addMeta(Arrays.stream(metaItemDescriptors).map(MetaItem::new).toArray(MetaItem[]::new))
);
}
@@ -21,7 +21,7 @@

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

import java.util.Optional;

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

boolean getAccessPublicRead();

Optional<AttributeTypeDescriptor[]> getAttributeTypeDescriptors();
Optional<AttributeDescriptor[]> getAttributeDescriptors();
}
@@ -19,7 +19,7 @@
*/
package org.openremote.model.asset;

import org.openremote.model.attribute.AttributeTypeDescriptor;
import org.openremote.model.attribute.AttributeDescriptor;
import org.openremote.model.attribute.AttributeValueDescriptor;
import org.openremote.model.attribute.MetaItemDescriptor;

@@ -32,7 +32,7 @@

AssetDescriptor[] getAssetDescriptors();

AttributeTypeDescriptor[] getAttributeTypeDescriptors();
AttributeDescriptor[] getAttributeTypeDescriptors();

AttributeValueDescriptor[] getAttributeValueDescriptors();
}
@@ -21,7 +21,7 @@


import jsinterop.annotations.JsType;
import org.openremote.model.attribute.AttributeTypeDescriptor;
import org.openremote.model.attribute.AttributeDescriptor;
import org.openremote.model.attribute.AttributeValueDescriptor;
import org.openremote.model.attribute.MetaItemDescriptor;
import org.openremote.model.http.RequestParams;
@@ -47,15 +47,15 @@
AssetDescriptor[] getAssetDescriptors(@BeanParam RequestParams requestParams);

/**
* Retrieve attribute type descriptors {@link AttributeTypeDescriptor} present.
* Retrieve attribute type descriptors {@link AttributeDescriptor} present.
* <p>
*/
@GET
@Path("attribute/typeDescriptors")
@Produces(APPLICATION_JSON)
@SuccessStatusCode(200)
@SuppressWarnings("unusable-by-js")
AttributeTypeDescriptor[] getAttributeTypeDescriptors(@BeanParam RequestParams requestParams);
AttributeDescriptor[] getAttributeTypeDescriptors(@BeanParam RequestParams requestParams);

/**
* Retrieve attribute type descriptors {@link AttributeValueDescriptor} present.
@@ -19,7 +19,7 @@
*/
package org.openremote.model.asset;

import org.openremote.model.attribute.AttributeTypeDescriptor;
import org.openremote.model.attribute.AttributeDescriptor;

import java.util.*;

@@ -58,17 +58,17 @@
final protected String type;
final protected String icon;
final protected boolean accessPublicRead;
final protected AttributeTypeDescriptor[] attributeTypeDescriptors;
final protected AttributeDescriptor[] attributeDescriptors;

AssetType(String type, String icon, AttributeTypeDescriptor... attributeTypeDescriptors) {
this(type, icon, false, attributeTypeDescriptors);
AssetType(String type, String icon, AttributeDescriptor... attributeDescriptors) {
this(type, icon, false, attributeDescriptors);
}

AssetType(String type, String icon, boolean accessPublicRead, AttributeTypeDescriptor... attributeTypeDescriptors) {
AssetType(String type, String icon, boolean accessPublicRead, AttributeDescriptor... attributeDescriptors) {
this.type = type;
this.icon = icon;
this.accessPublicRead = accessPublicRead;
this.attributeTypeDescriptors = attributeTypeDescriptors;
this.attributeDescriptors = attributeDescriptors;
}

public static Optional<AssetType> getByValue(String value) {
@@ -103,7 +103,7 @@ public boolean getAccessPublicRead() {
}

@Override
public Optional<AttributeTypeDescriptor[]> getAttributeTypeDescriptors() {
return Optional.ofNullable(attributeTypeDescriptors);
public Optional<AttributeDescriptor[]> getAttributeDescriptors() {
return Optional.ofNullable(attributeDescriptors);
}
}
@@ -37,7 +37,7 @@
}

@Override
public AttributeTypeDescriptor[] getAttributeTypeDescriptors() {
public AttributeDescriptor[] getAttributeTypeDescriptors() {
return AttributeType.values();
}

@@ -25,7 +25,7 @@
import java.util.Optional;

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public interface AttributeTypeDescriptor {
public interface AttributeDescriptor {

String getName();

Oops, something went wrong.

0 comments on commit 5eed920

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