// TODO: this could be generally useful and put into saveRole itself as long as the performance penalty isn't too harsh.
// Currently it always saves everything where it could pull pack the existing permissions, etc if they exist
List permissions = new ArrayList();
for ( Iterator j = role.getPermissions().iterator(); j.hasNext(); )
{
Permission permission = (Permission) j.next();
if ( permissionMap.containsKey( permission.getName() ) )
{
permission = (Permission) permissionMap.get( permission.getName() );
}
else if ( manager.permissionExists( permission ) )
{
permission = manager.getPermission( permission.getName() );
permissionMap.put( permission.getName(), permission );
}
else
{
Operation operation = permission.getOperation();
if ( operations.containsKey( operation.getName() ) )
{
operation = (Operation) operations.get( operation.getName() );
}
else if ( manager.operationExists( operation ) )
{
operation = manager.getOperation( operation.getName() );
operations.put( operation.getName(), operation );
}
else
{
operation = manager.saveOperation( operation );
operations.put( operation.getName(), operation );
}
permission.setOperation( operation );
Resource resource = permission.getResource();
if ( resources.containsKey( resource.getIdentifier() ) )
{
resource = (Resource) resources.get( resource.getIdentifier() );
}
else if ( manager.resourceExists( resource ) )
{
resource = manager.getResource( resource.getIdentifier() );
resources.put( resource.getIdentifier(), resource );
}
else
{
resource = manager.saveResource( resource );
resources.put( resource.getIdentifier(), resource );
}
permission.setResource( resource );
permission = manager.savePermission( permission );
permissionMap.put( permission.getName(), permission );
}
permissions.add( permission );
}
role.setPermissions( permissions );