// 1. virtual attributes to be removed
for (String vAttrToBeRemoved : vAttrsToBeRemoved) {
AbstractVirSchema virSchema = getVirSchema(vAttrToBeRemoved, attrUtil.virSchemaClass());
if (virSchema != null) {
AbstractVirAttr virAttr = attributable.getVirAttr(virSchema.getName());
if (virAttr == null) {
LOG.debug("No virtual attribute found for schema {}", virSchema.getName());
} else {
attributable.removeVirAttr(virAttr);
virAttrDAO.delete(virAttr);
}
for (ExternalResource resource : resourceDAO.findAll()) {
for (AbstractMappingItem mapItem : attrUtil.getMappingItems(resource, MappingPurpose.PROPAGATION)) {
if (virSchema.getName().equals(mapItem.getIntAttrName())
&& mapItem.getIntMappingType() == attrUtil.virIntMappingType()
&& externalResources.contains(resource)) {
propByRes.add(ResourceOperation.UPDATE, resource.getName());
// Using virtual attribute as AccountId must be avoided
if (mapItem.isAccountid() && virAttr != null && !virAttr.getValues().isEmpty()) {
propByRes.addOldAccountId(resource.getName(), virAttr.getValues().get(0));
}
}
}
}
}
}
LOG.debug("Virtual attributes to be removed:\n{}", propByRes);
// 2. virtual attributes to be updated
for (AttributeMod vAttrToBeUpdated : vAttrsToBeUpdated) {
AbstractVirSchema virSchema = getVirSchema(vAttrToBeUpdated.getSchema(), attrUtil.virSchemaClass());
AbstractVirAttr virAttr = null;
if (virSchema != null) {
virAttr = attributable.getVirAttr(virSchema.getName());
if (virAttr == null) {
virAttr = attrUtil.newVirAttr();
setVirAttrSchema(attributable, virAttr, virSchema);
if (virAttr.getSchema() == null) {
LOG.debug("Ignoring {} because no valid schema or template was found", vAttrToBeUpdated);
} else {
attributable.addVirAttr(virAttr);
}
}
}
if (virSchema != null && virAttr != null && virAttr.getSchema() != null) {
for (ExternalResource resource : resourceDAO.findAll()) {
for (AbstractMappingItem mapItem : attrUtil.getMappingItems(resource, MappingPurpose.PROPAGATION)) {
if (virSchema.getName().equals(mapItem.getIntAttrName())
&& mapItem.getIntMappingType() == attrUtil.virIntMappingType()
&& externalResources.contains(resource)) {
propByRes.add(ResourceOperation.UPDATE, resource.getName());
}
}
}
final List<String> values = new ArrayList<String>(virAttr.getValues());
values.removeAll(vAttrToBeUpdated.getValuesToBeRemoved());
values.addAll(vAttrToBeUpdated.getValuesToBeAdded());
virAttr.setValues(values);
// Owner cannot be specified before otherwise a virtual attribute remove will be invalidated.
virAttr.setOwner(attributable);
}
}
LOG.debug("Virtual attributes to be added:\n{}", propByRes);