}
protected JetspeedPrincipal synchronizeEntity(Entity entity, Set<JetspeedPrincipalAssociationReference> toAssociations) throws SecurityException
{
JetspeedPrincipal principal = getJetspeedPrincipal(entity.getType(), entity.getId());
JetspeedPrincipalManager principalManager = principalManagerProvider.getManager(principalManagerProvider.getPrincipalType(entity.getType()));
boolean syncAll = false;
if (principal == null)
{
// principal does not exist yet, create a new one using the principal manager
principal = principalManager.newPrincipal(entity.getId(), true);
principalManager.addPrincipal(principal, toAssociations);
syncAll = true;
}
else if (!principal.isMapped())
{
feedbackLogger.warn("Found {} principal: {} is not mapped therefore not synchronized!", principal.getType().getName(),principal.getName());
return null;
}
else
{
// sync relations
for (final SecurityEntityRelationType relationType : securityEntityManager.getSupportedEntityRelationTypes(entity.getType()))
{
if (relationType.getFromEntityType().equals(entity.getType()))
{
List<? extends JetspeedPrincipal> associatedFrom = principalManager.getAssociatedFrom(principal.getName(), principal.getType(), relationType.getRelationType());
for (JetspeedPrincipal p : associatedFrom)
{
if (toAssociations.isEmpty() ||
!toAssociations.remove(new JetspeedPrincipalAssociationReference(JetspeedPrincipalAssociationReference.Type.TO, p, relationType.getRelationType())))
{
principalManager.removeAssociation(principal, p, relationType.getRelationType());
}
}
}
}
for (JetspeedPrincipalAssociationReference ref : toAssociations)
{
principalManager.addAssociation(principal, ref.ref, ref.associationName);
}
}
boolean updated = false;
SecurityAttributes principalAttrs = principal.getSecurityAttributes();
for (AttributeDef attrDef : entity.getAttributeDefinitions())
{
if (attrDef.isMapped() && !attrDef.isMultiValue())
{
Attribute attr = entity.getAttribute(attrDef.getName());
if (attr == null)
{
if (!syncAll)
{
// if principal has attr: remove it
SecurityAttribute principalAttr = principalAttrs.getAttribute(attrDef.getMappedName());
if (principalAttr != null)
{
feedbackLogger.debug("Removing attribute {} for principal {}", principalAttr.getName(), principal.getName());
principalAttrs.removeAttribute(principalAttr.getName());
updated = true;
}
}
}
else if (syncAll)
{
SecurityAttribute principalAttr = principalAttrs.getAttribute(attrDef.getMappedName(), true);
if (feedbackLogger.isDebugEnabled())
{
feedbackLogger.debug("Adding attribute {} for principal {}. Value: {}",
new String[] {principalAttr.getName(), principal.getName(), attr.getValue()});
}
principalAttr.setStringValue(attr.getValue());
updated = true;
}
else
{
SecurityAttribute principalAttr = principalAttrs.getAttribute(attrDef.getMappedName(), true);
if (!StringUtils.equals(principalAttr.getStringValue(), attr.getValue()))
{
if (feedbackLogger.isDebugEnabled())
{
feedbackLogger.debug("Attribute attribute {} for principal {}. Old value: {}, new value: {}",
new String[] {principalAttr.getName(), principal.getName(), (principalAttr.getStringValue()), attr.getValue()});
}
principalAttr.setStringValue(attr.getValue());
updated = true;
}
}
}
}
if (updated)
{
feedbackLogger.debug("Storing attribute changes for principal {}", principal.getName());
principalManager.updatePrincipal(principal);
}
feedbackLogger.debug("Synchronized entity {} id: {} mapped attributes", entity.getType(), entity.getId());
return principal;
}