Search in sources :

Example 1 with MetaItemDescriptor

use of org.openremote.model.attribute.MetaItemDescriptor in project openremote by openremote.

the class MetaEditor method addItem.

protected boolean addItem(MetaItem item, boolean viewOnly) {
    if (!viewOnly) {
        MetaItemDescriptor[] descriptor = new MetaItemDescriptor[1];
        // Check item can be added
        boolean canAdd = Arrays.stream(metaItemDescriptors).filter(metaItemDescriptor -> metaItemDescriptor.getUrn().equals(item.getName().orElse(null))).findFirst().map(metaItemDescriptor -> {
            descriptor[0] = metaItemDescriptor;
            return metaItemDescriptor.getMaxPerAttribute();
        }).map(maxCount -> attribute.getMeta().stream().filter(metaItem -> metaItem.getName().map(name -> name.equals(item.getName().orElse(null))).orElse(false)).count() < maxCount).orElse(true);
        if (!canAdd) {
            showValidationError(attribute.getName().orElse(""), null, new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_DUPLICATION, getMetaItemDisplayName(environment, descriptor[0].name())));
            return false;
        }
        attribute.getMeta().add(item);
        // Notify the presenter that the attribute has changed
        notifyAttributeModified();
    }
    int index = itemListPanel.getWidgetCount();
    MetaItemEditor metaItemEditor = createMetaItemEditor(item, false);
    itemListPanel.add(metaItemEditor);
    setLabelVisible(itemListPanel.getWidgetCount() > 0);
    // Check if we have a validation failure for this editor
    if (lastValidationResult != null && lastValidationResult.getMetaFailures() != null) {
        List<ValidationFailure> failures = lastValidationResult.getMetaFailures().get(index);
        if (failures != null && !failures.isEmpty()) {
            metaItemEditor.setError(true);
        }
    }
    return true;
}
Also used : IntStream(java.util.stream.IntStream) Supplier(org.openremote.model.interop.Supplier) java.util(java.util) ValueType(org.openremote.model.value.ValueType) Environment(org.openremote.app.client.Environment) MetaItemDescriptor(org.openremote.model.attribute.MetaItemDescriptor) Pair(org.openremote.model.util.Pair) EnumUtil(org.openremote.model.util.EnumUtil) ValidationFailure(org.openremote.model.ValidationFailure) org.openremote.app.client.widget(org.openremote.app.client.widget) Collectors(java.util.stream.Collectors) AssetMeta(org.openremote.model.asset.AssetMeta) AttributeValidationResult(org.openremote.model.attribute.AttributeValidationResult) MetaItem(org.openremote.model.attribute.MetaItem) AgentLink(org.openremote.model.asset.agent.AgentLink) Value(org.openremote.model.value.Value) FlowPanel(com.google.gwt.user.client.ui.FlowPanel) ProtocolDescriptor(org.openremote.model.asset.agent.ProtocolDescriptor) IsWidget(com.google.gwt.user.client.ui.IsWidget) Values(org.openremote.model.value.Values) TextUtil.isNullOrEmpty(org.openremote.model.util.TextUtil.isNullOrEmpty) AssetAttribute(org.openremote.model.asset.AssetAttribute) MetaItemDescriptor(org.openremote.model.attribute.MetaItemDescriptor) ValidationFailure(org.openremote.model.ValidationFailure)

Example 2 with MetaItemDescriptor

use of org.openremote.model.attribute.MetaItemDescriptor in project openremote by openremote.

the class MetaEditor method onMetaItemTypeChanged.

protected void onMetaItemTypeChanged(MetaItemEditor itemEditor, boolean updateItem) {
    if (updateItem) {
        itemEditor.item.clearValue();
        // TODO Should use meta item descriptors from server
        Value initialValue = Arrays.stream(AssetMeta.values()).filter(assetMeta -> assetMeta.getUrn().equals(itemEditor.nameList.getSelectedValue())).map(MetaItemDescriptor::getInitialValue).findFirst().orElse(null);
        ValueType valueType = EnumUtil.enumFromString(ValueType.class, itemEditor.typeList.getSelectedValue()).orElse(null);
        if (valueType == ValueType.BOOLEAN && initialValue == null) {
            initialValue = Values.create(false);
        }
        itemEditor.onModified(initialValue);
    }
    itemEditor.updateValueEditor();
}
Also used : IntStream(java.util.stream.IntStream) Supplier(org.openremote.model.interop.Supplier) java.util(java.util) ValueType(org.openremote.model.value.ValueType) Environment(org.openremote.app.client.Environment) MetaItemDescriptor(org.openremote.model.attribute.MetaItemDescriptor) Pair(org.openremote.model.util.Pair) EnumUtil(org.openremote.model.util.EnumUtil) ValidationFailure(org.openremote.model.ValidationFailure) org.openremote.app.client.widget(org.openremote.app.client.widget) Collectors(java.util.stream.Collectors) AssetMeta(org.openremote.model.asset.AssetMeta) AttributeValidationResult(org.openremote.model.attribute.AttributeValidationResult) MetaItem(org.openremote.model.attribute.MetaItem) AgentLink(org.openremote.model.asset.agent.AgentLink) Value(org.openremote.model.value.Value) FlowPanel(com.google.gwt.user.client.ui.FlowPanel) ProtocolDescriptor(org.openremote.model.asset.agent.ProtocolDescriptor) IsWidget(com.google.gwt.user.client.ui.IsWidget) Values(org.openremote.model.value.Values) TextUtil.isNullOrEmpty(org.openremote.model.util.TextUtil.isNullOrEmpty) AssetAttribute(org.openremote.model.asset.AssetAttribute) ValueType(org.openremote.model.value.ValueType) Value(org.openremote.model.value.Value) MetaItemDescriptor(org.openremote.model.attribute.MetaItemDescriptor)

Example 3 with MetaItemDescriptor

use of org.openremote.model.attribute.MetaItemDescriptor in project openremote by openremote.

the class MetaEditor method showMetaItemFailure.

protected void showMetaItemFailure(MetaItemEditor metaItemEditor, List<ValidationFailure> failures) {
    if (failures != null) {
        Optional<MetaItemDescriptor> optionalMetaItemDescriptor = metaItemEditor.getCurrentDescriptor();
        String displayName = optionalMetaItemDescriptor.map(metaItemDescriptor -> getMetaItemDisplayName(environment, metaItemDescriptor.name())).orElse(metaItemEditor.getItem().getName().orElse(""));
        failures.forEach(failure -> {
            if (failure.getReason() == MetaItem.MetaItemFailureReason.META_ITEM_VALUE_IS_REQUIRED) {
                // Substitute in value type info
                String parameter = EnumUtil.enumFromString(ValueType.class, metaItemEditor.getTypeList().getSelectedValue()).map(Enum::name).orElse("Value");
                failure = new ValidationFailure(failure.getReason(), parameter);
            }
            showValidationError(attribute.getName().orElse(""), displayName, failure);
        });
    }
}
Also used : IntStream(java.util.stream.IntStream) Supplier(org.openremote.model.interop.Supplier) java.util(java.util) ValueType(org.openremote.model.value.ValueType) Environment(org.openremote.app.client.Environment) MetaItemDescriptor(org.openremote.model.attribute.MetaItemDescriptor) Pair(org.openremote.model.util.Pair) EnumUtil(org.openremote.model.util.EnumUtil) ValidationFailure(org.openremote.model.ValidationFailure) org.openremote.app.client.widget(org.openremote.app.client.widget) Collectors(java.util.stream.Collectors) AssetMeta(org.openremote.model.asset.AssetMeta) AttributeValidationResult(org.openremote.model.attribute.AttributeValidationResult) MetaItem(org.openremote.model.attribute.MetaItem) AgentLink(org.openremote.model.asset.agent.AgentLink) Value(org.openremote.model.value.Value) FlowPanel(com.google.gwt.user.client.ui.FlowPanel) ProtocolDescriptor(org.openremote.model.asset.agent.ProtocolDescriptor) IsWidget(com.google.gwt.user.client.ui.IsWidget) Values(org.openremote.model.value.Values) TextUtil.isNullOrEmpty(org.openremote.model.util.TextUtil.isNullOrEmpty) AssetAttribute(org.openremote.model.asset.AssetAttribute) MetaItemDescriptor(org.openremote.model.attribute.MetaItemDescriptor) ValidationFailure(org.openremote.model.ValidationFailure)

Aggregations

FlowPanel (com.google.gwt.user.client.ui.FlowPanel)3 IsWidget (com.google.gwt.user.client.ui.IsWidget)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 IntStream (java.util.stream.IntStream)3 Environment (org.openremote.app.client.Environment)3 org.openremote.app.client.widget (org.openremote.app.client.widget)3 ValidationFailure (org.openremote.model.ValidationFailure)3 AssetAttribute (org.openremote.model.asset.AssetAttribute)3 AssetMeta (org.openremote.model.asset.AssetMeta)3 AgentLink (org.openremote.model.asset.agent.AgentLink)3 ProtocolDescriptor (org.openremote.model.asset.agent.ProtocolDescriptor)3 AttributeValidationResult (org.openremote.model.attribute.AttributeValidationResult)3 MetaItem (org.openremote.model.attribute.MetaItem)3 MetaItemDescriptor (org.openremote.model.attribute.MetaItemDescriptor)3 Supplier (org.openremote.model.interop.Supplier)3 EnumUtil (org.openremote.model.util.EnumUtil)3 Pair (org.openremote.model.util.Pair)3 TextUtil.isNullOrEmpty (org.openremote.model.util.TextUtil.isNullOrEmpty)3 Value (org.openremote.model.value.Value)3