@SuppressWarnings("unchecked")
  @Override
  public List<IAMPolicy> listRecursiveIAMPoliciesByGroup(long groupId) {
    List<IAMGroupPolicyMapVO> policyGrpMap = _aclGroupPolicyMapDao.listByGroupId(groupId);
    if (policyGrpMap == null || policyGrpMap.size() == 0) {
      return new ArrayList<IAMPolicy>();
    }

    List<Long> policyIds = new ArrayList<Long>();
    for (IAMGroupPolicyMapVO pg : policyGrpMap) {
      policyIds.add(pg.getAclPolicyId());
    }

    SearchBuilder<IAMPolicyPermissionVO> permSb = _policyPermissionDao.createSearchBuilder();
    permSb.and("isRecursive", permSb.entity().isRecursive(), Op.EQ);

    SearchBuilder<IAMPolicyVO> sb = _aclPolicyDao.createSearchBuilder();
    sb.and("ids", sb.entity().getId(), Op.IN);
    sb.join(
        "recursivePerm",
        permSb,
        sb.entity().getId(),
        permSb.entity().getAclPolicyId(),
        JoinBuilder.JoinType.INNER);

    SearchCriteria<IAMPolicyVO> sc = sb.create();
    sc.setParameters("ids", policyIds.toArray(new Object[policyIds.size()]));
    sc.setJoinParameters("recursivePerm", "isRecursive", true);

    @SuppressWarnings("rawtypes")
    List policies = _aclPolicyDao.customSearch(sc, null);

    return policies;
  }
  @SuppressWarnings("unchecked")
  @Override
  public List<IAMPolicy> listIAMPolicies(long accountId) {

    // static policies of the account
    SearchBuilder<IAMGroupAccountMapVO> groupSB = _aclGroupAccountMapDao.createSearchBuilder();
    groupSB.and("account", groupSB.entity().getAccountId(), Op.EQ);

    GenericSearchBuilder<IAMGroupPolicyMapVO, Long> policySB =
        _aclGroupPolicyMapDao.createSearchBuilder(Long.class);
    policySB.selectFields(policySB.entity().getAclPolicyId());
    policySB.join(
        "accountgroupjoin",
        groupSB,
        groupSB.entity().getAclGroupId(),
        policySB.entity().getAclGroupId(),
        JoinType.INNER);
    policySB.done();
    SearchCriteria<Long> policySc = policySB.create();
    policySc.setJoinParameters("accountgroupjoin", "account", accountId);

    List<Long> policyIds = _aclGroupPolicyMapDao.customSearch(policySc, null);
    // add policies directly attached to the account
    List<IAMAccountPolicyMapVO> acctPolicies = _aclAccountPolicyMapDao.listByAccountId(accountId);
    for (IAMAccountPolicyMapVO p : acctPolicies) {
      policyIds.add(p.getIamPolicyId());
    }
    if (policyIds.size() == 0) {
      return new ArrayList<IAMPolicy>();
    }
    SearchBuilder<IAMPolicyVO> sb = _aclPolicyDao.createSearchBuilder();
    sb.and("ids", sb.entity().getId(), Op.IN);
    SearchCriteria<IAMPolicyVO> sc = sb.create();
    sc.setParameters("ids", policyIds.toArray(new Object[policyIds.size()]));
    @SuppressWarnings("rawtypes")
    List policies = _aclPolicyDao.customSearch(sc, null);

    return policies;
  }
  @SuppressWarnings("unchecked")
  @Override
  public List<IAMPolicy> listIAMPoliciesByGroup(long groupId) {
    List<IAMGroupPolicyMapVO> policyGrpMap = _aclGroupPolicyMapDao.listByGroupId(groupId);
    if (policyGrpMap == null || policyGrpMap.size() == 0) {
      return new ArrayList<IAMPolicy>();
    }

    List<Long> policyIds = new ArrayList<Long>();
    for (IAMGroupPolicyMapVO pg : policyGrpMap) {
      policyIds.add(pg.getAclPolicyId());
    }

    SearchBuilder<IAMPolicyVO> sb = _aclPolicyDao.createSearchBuilder();
    sb.and("ids", sb.entity().getId(), Op.IN);
    SearchCriteria<IAMPolicyVO> sc = sb.create();
    sc.setParameters("ids", policyIds.toArray(new Object[policyIds.size()]));
    @SuppressWarnings("rawtypes")
    List policies = _aclPolicyDao.customSearch(sc, null);

    return policies;
  }