LOG.debug("Resources to be added:\n{}", propByRes);
// 3. attributes to be removed
for (String attributeToBeRemoved : attributableMod.getAttributesToBeRemoved()) {
AbstractSchema schema = getSchema(attributeToBeRemoved, attrUtil.schemaClass());
if (schema != null) {
AbstractAttr attribute = attributable.getAttribute(schema.getName());
if (attribute == null) {
LOG.debug("No attribute found for schema {}", schema);
} else {
String newValue = null;
for (AttributeMod mod : attributableMod.getAttributesToBeUpdated()) {
if (schema.getName().equals(mod.getSchema())) {
newValue = mod.getValuesToBeAdded().get(0);
}
}
if (!schema.isUniqueConstraint()
|| (!attribute.getUniqueValue().getStringValue().equals(newValue))) {
attributable.removeAttribute(attribute);
attrDAO.delete(attribute.getId(), attrUtil.attrClass());
}
}
for (ExternalResource resource : resourceDAO.findAll()) {
for (AbstractMappingItem mapItem : attrUtil.getMappingItems(resource, MappingPurpose.PROPAGATION)) {
if (schema.getName().equals(mapItem.getIntAttrName())
&& mapItem.getIntMappingType() == attrUtil.intMappingType()
&& attributable.getResources().contains(resource)) {
propByRes.add(ResourceOperation.UPDATE, resource.getName());
if (mapItem.isAccountid() && attribute != null
&& !attribute.getValuesAsStrings().isEmpty()) {
propByRes.addOldAccountId(resource.getName(),
attribute.getValuesAsStrings().iterator().next());
}
}
}
}
}
}
LOG.debug("Attributes to be removed:\n{}", propByRes);
// 4. attributes to be updated
for (AttributeMod attributeMod : attributableMod.getAttributesToBeUpdated()) {
AbstractSchema schema = getSchema(attributeMod.getSchema(), attrUtil.schemaClass());
if (schema != null) {
for (ExternalResource resource : resourceDAO.findAll()) {
for (AbstractMappingItem mapItem : attrUtil.getMappingItems(resource, MappingPurpose.PROPAGATION)) {
if (schema.getName().equals(mapItem.getIntAttrName())
&& mapItem.getIntMappingType() == attrUtil.intMappingType()
&& attributable.getResources().contains(resource)) {
propByRes.add(ResourceOperation.UPDATE, resource.getName());
}
}
}
AbstractAttr attribute = attributable.getAttribute(schema.getName());
if (attribute == null) {
attribute = attrUtil.newAttr();
attribute.setSchema(schema);
attribute.setOwner(attributable);
attributable.addAttribute(attribute);
}
// 1.1 remove values
Set<Long> valuesToBeRemoved = new HashSet<Long>();
for (String valueToBeRemoved : attributeMod.getValuesToBeRemoved()) {
if (attribute.getSchema().isUniqueConstraint()) {
if (attribute.getUniqueValue() != null
&& valueToBeRemoved.equals(attribute.getUniqueValue().getValueAsString())) {
valuesToBeRemoved.add(attribute.getUniqueValue().getId());
}
} else {
for (AbstractAttrValue mav : attribute.getValues()) {
if (valueToBeRemoved.equals(mav.getValueAsString())) {
valuesToBeRemoved.add(mav.getId());
}
}
}
}
for (Long attributeValueId : valuesToBeRemoved) {
attributeValueDAO.delete(attributeValueId, attrUtil.attrValueClass());
}
// 1.2 add values
List<String> valuesToBeAdded = attributeMod.getValuesToBeAdded();
if (valuesToBeAdded != null && !valuesToBeAdded.isEmpty()
&& (!schema.isUniqueConstraint() || attribute.getUniqueValue() == null
|| !valuesToBeAdded.iterator().next().equals(attribute.getUniqueValue().getValueAsString()))) {
fillAttribute(attributeMod.getValuesToBeAdded(), attrUtil, schema, attribute, invalidValues);
}