// 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<JdoPermission> permissions = new ArrayList<JdoPermission>();
for ( Iterator j = role.getPermissions().iterator(); j.hasNext(); )
{
JdoPermission permission = (JdoPermission) j.next();
if ( permissionMap.containsKey( permission.getName() ) )
{
permission = permissionMap.get( permission.getName() );
}
else if ( objectExists( permission ) )
{
permission = (JdoPermission) PlexusJdoUtils.getObjectById( getPersistenceManager(),
JdoPermission.class,
permission.getName() );
permissionMap.put( permission.getName(), permission );
}
else
{
JdoOperation operation = (JdoOperation) permission.getOperation();
if ( operations.containsKey( operation.getName() ) )
{
operation = operations.get( operation.getName() );
}
else if ( objectExists( operation ) )
{
operation = (JdoOperation) PlexusJdoUtils.getObjectById( getPersistenceManager(),
JdoOperation.class,
operation.getName() );
operations.put( operation.getName(), operation );
}
else
{
RBACObjectAssertions.assertValid( operation );
operation = (JdoOperation) PlexusJdoUtils.saveObject( getPersistenceManager(), operation,
null );
operations.put( operation.getName(), operation );
}
permission.setOperation( operation );
JdoResource resource = (JdoResource) permission.getResource();
if ( resources.containsKey( resource.getIdentifier() ) )
{
resource = resources.get( resource.getIdentifier() );
}
else if ( objectExists( resource ) )
{
resource = (JdoResource) PlexusJdoUtils.getObjectById( getPersistenceManager(),
JdoResource.class,
resource.getIdentifier() );
resources.put( resource.getIdentifier(), resource );
}
else
{
RBACObjectAssertions.assertValid( resource );
resource = (JdoResource) PlexusJdoUtils.saveObject( getPersistenceManager(), resource, null );
resources.put( resource.getIdentifier(), resource );
}
permission.setResource( resource );
RBACObjectAssertions.assertValid( permission );
permission = (JdoPermission) PlexusJdoUtils.saveObject( getPersistenceManager(), permission, null );
permissionMap.put( permission.getName(), permission );
}
permissions.add( permission );
}
role.setPermissions( permissions );