Skip to content

Commit

Permalink
Renamed AssetModel to AssetModelUtil and moved into model module and …
Browse files Browse the repository at this point in the history
…descriptors are injected at start up by the AssetModelService
  • Loading branch information
richturner committed Apr 8, 2019
1 parent d1754bd commit 9486aa8
Show file tree
Hide file tree
Showing 20 changed files with 299 additions and 179 deletions.
Expand Up @@ -382,7 +382,7 @@ final protected void updateLinkedAttribute(final AttributeState finalState, long
}

// Do basic value conversion
Optional<ValueType> attributeValueType = attribute.getType().map(AttributeValueType::getValueType);
Optional<ValueType> attributeValueType = attribute.getType().map(AttributeValueDescriptor::getValueType);

if (value != null && attributeValueType.isPresent()) {
if (attributeValueType.get() != value.getType()) {
Expand Down
Expand Up @@ -517,7 +517,7 @@ private void onPollingResponse(PollingKey pollingKey, List<String> sensorNameLis
*/
private void updateAttributeValue(AttributeRef attributeRef, String value) {
LOG.fine("### Updating attribute " + attributeRef + " with value " + value);
AttributeValueType attributeType = this.linkedAttributes.get(attributeRef).getTypeOrThrow();
AttributeValueDescriptor attributeType = this.linkedAttributes.get(attributeRef).getTypeOrThrow();

ValueType valueType = attributeType.getValueType();
try {
Expand Down
Expand Up @@ -243,7 +243,7 @@ protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocol
// Verify the type of the attribute matches the action value
if (attribute
.getType()
.map(AttributeValueType::getValueType)
.map(AttributeValueDescriptor::getValueType)
.orElse(null) != actionValue.getType()) {
// Use a value of null so it is clear that the attribute isn't linked correctly
actionValue = null;
Expand Down
Expand Up @@ -317,7 +317,7 @@ public Asset[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguratio

getLinkedAttributeDescriptors(deviceType.get(), baseAddress)
.forEach(descriptor -> {
AssetAttribute attribute = new AssetAttribute(descriptor.getName(), descriptor.getAttributeValueType())
AssetAttribute attribute = new AssetAttribute(descriptor.getName(), descriptor.getAttributeValueDescriptor())
.setMeta(
agentLink,
new MetaItem(MetaItemType.LABEL, Values.create(descriptor.getDisplayName()))
Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.openremote.app.client.Environment;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.attribute.AttributeValueDescriptor;
import org.openremote.model.attribute.AttributeValueType;
import org.openremote.model.attribute.AttributeValidationResult;
import org.openremote.model.interop.Consumer;
Expand Down Expand Up @@ -195,7 +196,7 @@ protected void notifyAttributeModified(Value newValue) {

protected IsWidget createAttributeValueEditor() {
return valueEditorSupplier.createValueEditor(attribute,
attribute.getType().map(AttributeValueType::getValueType).orElse(null),
attribute.getType().map(AttributeValueDescriptor::getValueType).orElse(null),
style,
null,
this::notifyAttributeModified);
Expand Down Expand Up @@ -250,7 +251,7 @@ protected void refresh() {
} else if (attribute.isProtocolConfiguration()) {
formLabel.setIcon("cogs");
} else {
formLabel.setIcon(attribute.getType().map(AttributeValueType::getIcon).orElse(AttributeValueType.DEFAULT_ICON));
formLabel.setIcon(attribute.getType().map(AttributeValueDescriptor::getIcon).orElse(AttributeValueType.DEFAULT_ICON));
}

getFormLabel().setText(getAttributeLabel());
Expand All @@ -265,7 +266,7 @@ protected void refresh() {
infoText.append(connectionStatus != null ? connectionStatus.toString() : environment.getMessages().waitingForStatus());
}
} else if (attribute.getType().isPresent()) {
infoText.append(environment.getMessages().attributeValueType(attribute.getType().get().name()));
infoText.append(environment.getMessages().attributeValueType(attribute.getTypeOrThrow().getName()));
}
getAttributeDescription()
.ifPresent(description -> {
Expand Down
Expand Up @@ -35,6 +35,7 @@
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.AssetResource;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.attribute.AttributeValueDescriptor;
import org.openremote.model.attribute.AttributeValueType;
import org.openremote.model.event.bus.EventBus;
import org.openremote.model.event.bus.EventRegistration;
Expand Down Expand Up @@ -205,7 +206,7 @@ protected void showAssetInfoItems() {
List<MapInfoItem> infoItems = dashboardAttributes.stream()
.filter(attribute -> attribute.getLabel().isPresent())
.map(attribute -> new MapInfoItem(
attribute.getType().map(AttributeValueType::getIcon).orElse(AttributeValueType.DEFAULT_ICON),
attribute.getType().map(AttributeValueDescriptor::getIcon).orElse(AttributeValueType.DEFAULT_ICON),
attribute.getLabel().get(),
attribute.getFormat().orElse(null),
attribute.getValue().orElse(null)
Expand Down
147 changes: 0 additions & 147 deletions manager/src/main/java/org/openremote/manager/asset/AssetModel.java

This file was deleted.

@@ -0,0 +1,80 @@
/*
* 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.manager.asset;

import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.model.asset.AssetDescriptor;
import org.openremote.model.asset.AssetModelProvider;
import org.openremote.model.attribute.AttributeDescriptor;
import org.openremote.model.attribute.AttributeValueDescriptor;
import org.openremote.model.attribute.MetaItemDescriptor;
import org.openremote.model.util.AssetModelUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ServiceLoader;
import java.util.logging.Logger;

/**
* This service populates the descriptors in {@link org.openremote.model.util.AssetModelUtil} by looking for
* {@link AssetModelProvider}s through {@link ServiceLoader}.
*/
public class AssetModelService implements ContainerService {

private static final Logger LOG = Logger.getLogger(AssetModelService.class.getName());

@Override
public int getPriority() {
return Integer.MIN_VALUE + 10;
}

@Override
public void init(Container container) throws Exception {

List<AssetDescriptor> assetDescriptors = 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()));
attributeDescriptors.addAll(Arrays.asList(assetModelProvider.getAttributeDescriptors()));
attributeValueDescriptors.addAll(Arrays.asList(assetModelProvider.getAttributeValueDescriptors()));
metaItemDescriptors.addAll(Arrays.asList(assetModelProvider.getMetaItemDescriptors()));
});

AssetModelUtil.setAssetDescriptors(assetDescriptors.toArray(new AssetDescriptor[0]));
AssetModelUtil.setAttributeDescriptors(attributeDescriptors.toArray(new AttributeDescriptor[0]));
AssetModelUtil.setAttributeValueDescriptors(attributeValueDescriptors.toArray(new AttributeValueDescriptor[0]));
AssetModelUtil.setMetaItemDescriptors(metaItemDescriptors.toArray(new MetaItemDescriptor[0]));
}

@Override
public void start(Container container) throws Exception {
}

@Override
public void stop(Container container) throws Exception {
}
}
Expand Up @@ -42,6 +42,7 @@
import org.openremote.model.attribute.AttributeEvent.Source;
import org.openremote.model.attribute.AttributeExecuteStatus;
import org.openremote.model.security.ClientRole;
import org.openremote.model.util.AssetModelUtil;
import org.openremote.model.value.Value;
import org.openremote.model.value.Values;

Expand Down Expand Up @@ -349,7 +350,7 @@ public void configure() throws Exception {
}

//Check if attribute is well known and the value is valid
AssetModel.getAttributeDescriptor(oldAttribute.name).ifPresent(wellKnownAttribute -> {
AssetModelUtil.getAttributeDescriptor(oldAttribute.name).ifPresent(wellKnownAttribute -> {
// Check if the value is valid
wellKnownAttribute.getValueDescriptor()
.getValidator().flatMap(v -> v.apply(event.getValue().orElse(null)))
Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.openremote.model.query.filter.ParentPredicate;
import org.openremote.model.query.filter.TenantPredicate;
import org.openremote.model.security.Tenant;
import org.openremote.model.util.AssetModelUtil;
import org.openremote.model.util.TextUtil;
import org.openremote.model.value.Value;
import org.openremote.model.value.ValueException;
Expand All @@ -43,7 +44,6 @@
import java.io.IOException;
import java.util.*;
import java.util.logging.Logger;
import java.util.stream.Stream;

import static javax.ws.rs.core.Response.Status.*;
import static org.openremote.container.Container.JSON;
Expand Down Expand Up @@ -285,10 +285,10 @@ public void update(RequestParams requestParams, String assetId, Asset asset) {
Meta existingMetaItems = existingAttribute.getMeta().copy();

// Remove any writable existing meta items
existingMetaItems.removeIf(AssetModel::isMetaItemRestrictedWrite);
existingMetaItems.removeIf(AssetModelUtil::isMetaItemRestrictedWrite);

// Add any writable updated meta items
updatedMetaItems.stream().filter(AssetModel::isMetaItemRestrictedWrite).forEach(existingMetaItems::add);
updatedMetaItems.stream().filter(AssetModelUtil::isMetaItemRestrictedWrite).forEach(existingMetaItems::add);

// Replace existing with updated attribute
updatedAttribute.setMeta(existingMetaItems);
Expand All @@ -298,7 +298,7 @@ public void update(RequestParams requestParams, String assetId, Asset asset) {

// An attribute added by a restricted user can only have meta items which are writable
updatedAttribute.getMetaStream().forEach(metaItem -> {
if (!AssetModel.isMetaItemRestrictedWrite(metaItem)) {
if (!AssetModelUtil.isMetaItemRestrictedWrite(metaItem)) {
LOG.fine("Attribute has " + metaItem + " not writable by restricted client: " + updatedAttributeName);
throw new WebApplicationException(
"Attribute has meta item not writable by restricted client: " + updatedAttributeName,
Expand Down Expand Up @@ -351,7 +351,7 @@ public void update(RequestParams requestParams, String assetId, Asset asset) {

private void checkForWellKnownAttributes(Asset asset) {
asset.getAttributesStream().forEach(assetAttribute -> {
AssetModel.getAttributeDescriptor(assetAttribute.name).ifPresent(wellKnownAttribute -> {
AssetModelUtil.getAttributeDescriptor(assetAttribute.name).ifPresent(wellKnownAttribute -> {
//Check if the type matches
if (!wellKnownAttribute.getValueDescriptor().equals(assetAttribute.getTypeOrThrow())) {
throw new IllegalStateException(
Expand Down Expand Up @@ -459,7 +459,7 @@ public Asset create(RequestParams requestParams, Asset asset) {
newAsset.setId(asset.getId());
}

AssetModel.getAssetDescriptor(asset.getType()).ifPresent(assetDescriptor -> {
AssetModelUtil.getAssetDescriptor(asset.getType()).ifPresent(assetDescriptor -> {

newAsset.setAccessPublicRead(assetDescriptor.getAccessPublicRead());

Expand Down

0 comments on commit 9486aa8

Please sign in to comment.