{
RbacJdoModelStaxReader reader = new RbacJdoModelStaxReader();
FileReader fileReader = new FileReader( new File( backupDirectory, RBAC_XML_NAME ) );
RbacDatabase database;
try
{
database = reader.read( fileReader );
}
finally
{
IOUtil.close( fileReader );
}
Map permissionMap = new HashMap();
Map resources = new HashMap();
Map operations = new HashMap();
for ( Iterator i = database.getRoles().iterator(); i.hasNext(); )
{
Role role = (Role) i.next();
// 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 );
manager.saveRole( role );
}
for ( Iterator i = database.getUserAssignments().iterator(); i.hasNext(); )
{
UserAssignment userAssignment = (UserAssignment) i.next();
manager.saveUserAssignment( userAssignment );
}