}
LOG.debug("Resources to be added:\n{}", propByRes);
AbstractSchema schema;
AbstractAttr attribute;
AbstractDerSchema derivedSchema;
AbstractDerAttr derivedAttribute;
// 3. attributes to be removed
for (String attributeToBeRemoved : attributableMod.getAttributesToBeRemoved()) {
schema = getSchema(attributeToBeRemoved, attributableUtil.schemaClass());
if (schema != null) {
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);
attributeDAO.delete(attribute.getId(), attributableUtil.attributeClass());
}
}
for (SchemaMapping mapping : resourceDAO.findAllMappings()) {
if (schema.getName().equals(mapping.getIntAttrName())
&& mapping.getIntMappingType() == attributableUtil.intMappingType()
&& mapping.getResource() != null
&& attributable.getResources().contains(mapping.getResource())) {
propByRes.add(PropagationOperation.UPDATE, mapping.getResource().getName());
if (mapping.isAccountid() && attribute != null && !attribute.getValuesAsStrings().isEmpty()) {
propByRes.addOldAccountId(mapping.getResource().getName(), attribute.getValuesAsStrings()
.iterator().next());
}
}
}
}
}
LOG.debug("Attributes to be removed:\n{}", propByRes);
// 4. attributes to be updated
Set<Long> valuesToBeRemoved;
List<String> valuesToBeAdded;
for (AttributeMod attributeMod : attributableMod.getAttributesToBeUpdated()) {
schema = getSchema(attributeMod.getSchema(), attributableUtil.schemaClass());
if (schema != null) {
for (SchemaMapping mapping : resourceDAO.findAllMappings()) {
if (schema.getName().equals(mapping.getIntAttrName())
&& mapping.getIntMappingType() == attributableUtil.intMappingType()
&& mapping.getResource() != null
&& attributable.getResources().contains(mapping.getResource())) {
propByRes.add(PropagationOperation.UPDATE, mapping.getResource().getName());
}
}
attribute = attributable.getAttribute(schema.getName());
if (attribute == null) {
attribute = attributableUtil.newAttribute();
attribute.setSchema(schema);
attribute.setOwner(attributable);
attributable.addAttribute(attribute);
}
// 1.1 remove values
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, attributableUtil.attributeValueClass());
}
// 1.2 add values
valuesToBeAdded = attributeMod.getValuesToBeAdded();
if (valuesToBeAdded != null
&& !valuesToBeAdded.isEmpty()
&& (!schema.isUniqueConstraint() || attribute.getUniqueValue() == null || !valuesToBeAdded
.iterator().next().equals(attribute.getUniqueValue().getValueAsString()))) {
fillAttribute(attributeMod.getValuesToBeAdded(), attributableUtil, schema, attribute, invalidValues);
}
// if no values are in, the attribute can be safely removed
if (attribute.getValuesAsStrings().isEmpty()) {
attributeDAO.delete(attribute);
}
}
}