@Override
  public void remove(Partition partition) throws IdentityManagementException {
    checkPartitionManagementSupported();
    checkIfPartitionExists(partition);

    try {
      IdentityContext context = createIdentityContext();

      AttributeStore<?> attributeStore = getStoreForAttributeOperation(context);

      if (attributeStore != null) {
        Partition storedType = lookupById(partition.getClass(), partition.getId());

        IdentityManager identityManager = createIdentityManager(storedType);
        IdentityQuery<IdentityType> query = identityManager.createIdentityQuery(IdentityType.class);

        for (IdentityType identityType : query.getResultList()) {
          identityManager.remove(identityType);
        }

        for (Attribute<? extends Serializable> attribute : storedType.getAttributes()) {
          attributeStore.removeAttribute(context, storedType, attribute.getName());
        }
      }

      getStoreForPartitionOperation(context).remove(context, partition);
    } catch (Exception e) {
      throw MESSAGES.partitionRemoveFailed(partition, e);
    }
  }
 @Override
 public RoleModel getRoleById(String id) {
   IdentityQuery<Role> query = getIdm().createIdentityQuery(Role.class);
   query.setParameter(IdentityType.ID, id);
   List<Role> roles = query.getResultList();
   if (roles.size() == 0) return null;
   return new RoleAdapter(roles.get(0), getIdm());
 }
 @Override
 public List<AeroGearUser> findAllByRole(String role) {
   List aerogearUsers = new ArrayList();
   IdentityQuery<User> query = identityManager.createQuery(User.class);
   query.setParameter(User.HAS_ROLE, new String[] {role});
   List<User> result = query.getResultList();
   for (User user : result) {
     aerogearUsers.add(Converter.convertToAerogearUser(user));
   }
   return aerogearUsers;
 }
 @Override
 public List<RoleModel> getRoles() {
   IdentityQuery<Role> query = getIdm().createIdentityQuery(Role.class);
   query.setParameter(Role.PARTITION, applicationData);
   List<Role> roles = query.getResultList();
   List<RoleModel> roleModels = new ArrayList<RoleModel>();
   for (Role role : roles) {
     roleModels.add(new RoleAdapter(role, idm));
   }
   return roleModels;
 }
  /**
   * Finds all groups for a specific user.
   *
   * @throws Exception
   */
  @Test
  public void testFindAgentGroups() throws Exception {
    Group someGroup = createGroup("someGroup", null);
    Group someAnotherGroup = createGroup("someAnotherGroup", null);
    Group someImportantGroup = createGroup("someImportantGroup", null);

    T user = createIdentityType("someAgent", null);

    IdentityManager identityManager = getIdentityManager();

    IdentityQuery<Group> query = identityManager.createIdentityQuery(Group.class);

    query.setParameter(Group.HAS_MEMBER, new Object[] {user});

    List<Group> result = query.getResultList();

    assertFalse(contains(result, "someGroup"));
    assertFalse(contains(result, "someAnotherGroup"));
    assertFalse(contains(result, "someImportantGroup"));

    identityManager.addToGroup(user, someGroup);

    query = identityManager.createIdentityQuery(Group.class);

    query.setParameter(Group.HAS_MEMBER, new Object[] {user});

    result = query.getResultList();

    assertFalse(result.isEmpty());
    assertTrue(contains(result, "someGroup"));
    assertFalse(contains(result, "someAnotherGroup"));
    assertFalse(contains(result, "someImportantGroup"));

    identityManager.addToGroup(user, someAnotherGroup);

    query = identityManager.createIdentityQuery(Group.class);

    query.setParameter(Group.HAS_MEMBER, new Object[] {user});

    result = query.getResultList();

    assertFalse(result.isEmpty());
    assertTrue(contains(result, "someGroup"));
    assertTrue(contains(result, "someAnotherGroup"));
    assertFalse(contains(result, "someImportantGroup"));

    identityManager.addToGroup(user, someImportantGroup);

    query = identityManager.createIdentityQuery(Group.class);

    query.setParameter(Group.HAS_MEMBER, new Object[] {user});

    result = query.getResultList();

    assertFalse(result.isEmpty());
    assertTrue(contains(result, "someGroup"));
    assertTrue(contains(result, "someAnotherGroup"));
    assertTrue(contains(result, "someImportantGroup"));
  }
 @GET
 @Produces("application/json")
 public List<User> listAll() {
   IdentityQuery<User> identityQuery = identityManager.createIdentityQuery(User.class);
   return identityQuery.getResultList();
 }