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