}
}
public void removeRelation(String entityId, String relatedEntityInternalId, String attributeName) throws SecurityException
{
AttributeDef attrDef = configuration.getAttributeDef(attributeName);
if (attrDef == null)
{
throw new SecurityException(SecurityException.ENTITY_ATTRIBUTE_UNDEFINED.createScoped(configuration.getEntityType(), attributeName));
}
DirContextOperations dirCtxOps = getEntityContextById(entityId, false);
if (dirCtxOps == null)
{
throw new SecurityException(SecurityException.PRINCIPAL_DOES_NOT_EXIST.createScoped(configuration.getEntityType(), entityId));
}
ModificationItem[] modItems = new ModificationItem[1];
modItems[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute(attributeName));
if (attrDef.isMultiValue())
{
modItems[0].getAttribute().add(relatedEntityInternalId);
}
ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
try
{
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
try
{
ldapTemplate.modifyAttributes(getRelativeDN(dirCtxOps.getNameInNamespace()), modItems);
}
catch (SchemaViolationException e)
{
// required multi-value attribute removal?
if (!(attrDef.isMultiValue() && attrDef.isRequired()))
{
throw e;
}
// replace with required default or dn
modItems[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute(attributeName));
modItems[0].getAttribute().add(attrDef.requiresDnDefaultValue() ? dirCtxOps.getNameInNamespace() : attrDef.getRequiredDefaultValue());
// try again
ldapTemplate.modifyAttributes(getRelativeDN(dirCtxOps.getNameInNamespace()), modItems);
}
}
catch (NamingException e)