use of eu.bcvsolutions.idm.core.api.dto.IdmRequestAttributeValueDto in project CzechIdMng by bcvsolutions.
the class DefaultRequestManager method getChanges.
@Override
public List<IdmRequestItemAttributeDto> getChanges(AbstractDto currentDto, AbstractDto changedDto, RequestOperationType itemOperation) {
List<IdmRequestItemAttributeDto> resultAttributes = new ArrayList<>();
Map<String, Object> currentFieldsValues = this.dtoToMap(currentDto);
Map<String, Object> changedFieldsValues = this.dtoToMap(changedDto);
// First add all new attributes
changedFieldsValues.keySet().stream().forEach(changedAttribute -> {
if (!currentFieldsValues.containsKey(changedAttribute)) {
Object value = changedFieldsValues.get(changedAttribute);
IdmRequestItemAttributeDto attribute = new IdmRequestItemAttributeDto(changedAttribute, value instanceof List, true);
if (attribute.isMultivalue()) {
if (value instanceof List) {
((List<?>) value).forEach(v -> {
attribute.getValues().add(new IdmRequestAttributeValueDto(v, null, RequestOperationType.ADD));
});
}
} else {
attribute.setValue(new IdmRequestAttributeValueDto(value, null, RequestOperationType.ADD));
}
resultAttributes.add(attribute);
}
});
// Second add all already exists attributes
currentFieldsValues.keySet().forEach(currentAttribute -> {
Object changedValue = changedFieldsValues.get(currentAttribute);
IdmRequestItemAttributeDto attribute;
Object currentValue = currentFieldsValues.get(currentAttribute);
attribute = new IdmRequestItemAttributeDto(currentAttribute, changedValue instanceof List, false);
if (attribute.isMultivalue()) {
if (changedValue instanceof List) {
((List<?>) changedValue).forEach(value -> {
if (currentValue instanceof List && ((List<?>) currentValue).contains(value)) {
attribute.getValues().add(new IdmRequestAttributeValueDto(value, value, null));
} else {
attribute.setChanged(true);
attribute.getValues().add(new IdmRequestAttributeValueDto(value, null, RequestOperationType.ADD));
}
});
}
if (currentValue instanceof List) {
((List<?>) currentValue).forEach(value -> {
if (changedValue == null || !((List<?>) changedValue).contains(value)) {
attribute.setChanged(true);
attribute.getValues().add(new IdmRequestAttributeValueDto(value, value, RequestOperationType.REMOVE));
}
});
}
} else {
if ((changedValue == null && currentValue == null) || (changedValue != null && changedValue.equals(currentValue)) || (currentValue != null && currentValue.equals(changedValue))) {
attribute.setChanged(RequestOperationType.UPDATE == itemOperation ? false : true);
attribute.setValue(new IdmRequestAttributeValueDto(changedValue, currentValue, RequestOperationType.UPDATE == itemOperation ? null : itemOperation));
} else {
attribute.setChanged(true);
attribute.setValue(new IdmRequestAttributeValueDto(changedValue, currentValue, itemOperation));
}
}
resultAttributes.add(attribute);
});
// Make all values nicer
//
resultAttributes.stream().filter(//
attribute -> attribute.getValue() != null).forEach(attribute -> {
//
attribute.getValue().setValue(this.makeNiceValue(attribute.getValue().getValue()));
attribute.getValue().setOldValue(this.makeNiceValue(attribute.getValue().getOldValue()));
List<IdmRequestAttributeValueDto> attributeValues = attribute.getValues();
attributeValues.forEach(attributeValue -> {
attributeValue.setValue(this.makeNiceValue(attributeValue.getValue()));
attributeValue.setOldValue(this.makeNiceValue(attributeValue.getOldValue()));
});
});
return resultAttributes;
}
Aggregations