LOG.debug("Resources to be added:\n{}", propByRes);
}
// 3. attributes to be removed
for (String attributeToBeRemoved : attributableMod.getAttrsToRemove()) {
AbstractNormalSchema schema = getNormalSchema(attributeToBeRemoved, attrUtil.schemaClass());
if (schema != null) {
AbstractAttr attr = attributable.getAttr(schema.getName());
if (attr == null) {
LOG.debug("No attribute found for schema {}", schema);
} else {
String newValue = null;
for (AttributeMod mod : attributableMod.getAttrsToUpdate()) {
if (schema.getName().equals(mod.getSchema())) {
newValue = mod.getValuesToBeAdded().get(0);
}
}
if (!schema.isUniqueConstraint()
|| (!attr.getUniqueValue().getStringValue().equals(newValue))) {
attributable.removeAttr(attr);
attrDAO.delete(attr.getId(), attrUtil.attrClass());
}
}
if (attributable instanceof AbstractSubject) {
for (ExternalResource resource : resourceDAO.findAll()) {
for (AbstractMappingItem mapItem : attrUtil.
getMappingItems(resource, MappingPurpose.PROPAGATION)) {
if (schema.getName().equals(mapItem.getIntAttrName())
&& mapItem.getIntMappingType() == attrUtil.intMappingType()
&& ((AbstractSubject) attributable).getResources().contains(resource)) {
propByRes.add(ResourceOperation.UPDATE, resource.getName());
if (mapItem.isAccountid() && attr != null
&& !attr.getValuesAsStrings().isEmpty()) {
propByRes.addOldAccountId(resource.getName(),
attr.getValuesAsStrings().iterator().next());
}
}
}
}
}
}
}
LOG.debug("Attributes to be removed:\n{}", propByRes);
// 4. attributes to be updated
for (AttributeMod attributeMod : attributableMod.getAttrsToUpdate()) {
AbstractNormalSchema schema = getNormalSchema(attributeMod.getSchema(), attrUtil.schemaClass());
AbstractAttr attr = null;
if (schema != null) {
attr = attributable.getAttr(schema.getName());
if (attr == null) {
attr = attrUtil.newAttr();
setAttrSchema(attributable, attr, schema);
if (attr.getSchema() == null) {
LOG.debug("Ignoring {} because no valid schema or template was found", attributeMod);
} else {
attr.setOwner(attributable);
attributable.addAttr(attr);
}
}
}
if (schema != null && attr != null && attr.getSchema() != null) {
if (attributable instanceof AbstractSubject) {
for (ExternalResource resource : resourceDAO.findAll()) {
for (AbstractMappingItem mapItem : attrUtil.
getMappingItems(resource, MappingPurpose.PROPAGATION)) {
if (schema.getName().equals(mapItem.getIntAttrName())
&& mapItem.getIntMappingType() == attrUtil.intMappingType()
&& ((AbstractSubject) attributable).getResources().contains(resource)) {
propByRes.add(ResourceOperation.UPDATE, resource.getName());
}
}
}
}
// 1.1 remove values
Set<Long> valuesToBeRemoved = new HashSet<Long>();
for (String valueToBeRemoved : attributeMod.getValuesToBeRemoved()) {
if (attr.getSchema().isUniqueConstraint()) {
if (attr.getUniqueValue() != null
&& valueToBeRemoved.equals(attr.getUniqueValue().getValueAsString())) {
valuesToBeRemoved.add(attr.getUniqueValue().getId());
}
} else {
for (AbstractAttrValue mav : attr.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() || attr.getUniqueValue() == null
|| !valuesToBeAdded.iterator().next().equals(attr.getUniqueValue().getValueAsString()))) {
fillAttribute(attributeMod.getValuesToBeAdded(), attrUtil, schema, attr, invalidValues);
}