Search in sources :

Example 1 with Environment

use of org.openremote.app.client.Environment 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 Environment

use of org.openremote.app.client.Environment 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)2 IsWidget (com.google.gwt.user.client.ui.IsWidget)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 Environment (org.openremote.app.client.Environment)2 org.openremote.app.client.widget (org.openremote.app.client.widget)2 ValidationFailure (org.openremote.model.ValidationFailure)2 AssetAttribute (org.openremote.model.asset.AssetAttribute)2 AssetMeta (org.openremote.model.asset.AssetMeta)2 AgentLink (org.openremote.model.asset.agent.AgentLink)2 ProtocolDescriptor (org.openremote.model.asset.agent.ProtocolDescriptor)2 AttributeValidationResult (org.openremote.model.attribute.AttributeValidationResult)2 MetaItem (org.openremote.model.attribute.MetaItem)2 MetaItemDescriptor (org.openremote.model.attribute.MetaItemDescriptor)2 Supplier (org.openremote.model.interop.Supplier)2 EnumUtil (org.openremote.model.util.EnumUtil)2 Pair (org.openremote.model.util.Pair)2 TextUtil.isNullOrEmpty (org.openremote.model.util.TextUtil.isNullOrEmpty)2 Value (org.openremote.model.value.Value)2