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;
}
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();
}
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);
});
}
}
Aggregations