Search in sources :

Example 1 with ProjectJoinVO

use of com.cloud.api.query.vo.ProjectJoinVO in project cloudstack by apache.

the class ViewResponseHelper method createProjectResponse.

public static List<ProjectResponse> createProjectResponse(ProjectJoinVO... projects) {
    Hashtable<Long, ProjectResponse> prjDataList = new Hashtable<Long, ProjectResponse>();
    // Initialise the prjdatalist with the input data
    for (ProjectJoinVO p : projects) {
        ProjectResponse pData = prjDataList.get(p.getId());
        if (pData == null) {
            // first time encountering this vm
            pData = ApiDBUtils.newProjectResponse(p);
        } else {
            // update those  1 to many mapping fields
            pData = ApiDBUtils.fillProjectDetails(pData, p);
        }
        prjDataList.put(p.getId(), pData);
    }
    return new ArrayList<ProjectResponse>(prjDataList.values());
}
Also used : ProjectResponse(org.apache.cloudstack.api.response.ProjectResponse) Hashtable(java.util.Hashtable) ProjectJoinVO(com.cloud.api.query.vo.ProjectJoinVO) ArrayList(java.util.ArrayList)

Example 2 with ProjectJoinVO

use of com.cloud.api.query.vo.ProjectJoinVO in project cosmic by MissionCriticalCloud.

the class QueryManagerImpl method listProjectsInternal.

private Pair<List<ProjectJoinVO>, Integer> listProjectsInternal(final ListProjectsCmd cmd) {
    final Long id = cmd.getId();
    final String name = cmd.getName();
    final String displayText = cmd.getDisplayText();
    final String state = cmd.getState();
    final String accountName = cmd.getAccountName();
    final Long domainId = cmd.getDomainId();
    final String keyword = cmd.getKeyword();
    final Long startIndex = cmd.getStartIndex();
    final Long pageSize = cmd.getPageSizeVal();
    final boolean listAll = cmd.listAll();
    final boolean isRecursive = cmd.isRecursive();
    cmd.getTags();
    final Account caller = CallContext.current().getCallingAccount();
    Long accountId = null;
    String path = null;
    final Filter searchFilter = new Filter(ProjectJoinVO.class, "id", false, startIndex, pageSize);
    final SearchBuilder<ProjectJoinVO> sb = _projectJoinDao.createSearchBuilder();
    // select distinct
    sb.select(null, Func.DISTINCT, sb.entity().getId());
    if (_accountMgr.isAdmin(caller.getId())) {
        if (domainId != null) {
            final DomainVO domain = _domainDao.findById(domainId);
            if (domain == null) {
                throw new InvalidParameterValueException("Domain id=" + domainId + " doesn't exist in the system");
            }
            _accountMgr.checkAccess(caller, domain);
            if (accountName != null) {
                final Account owner = _accountMgr.getActiveAccountByName(accountName, domainId);
                if (owner == null) {
                    throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId);
                }
                accountId = owner.getId();
            }
        } else {
            // domainId == null
            if (accountName != null) {
                throw new InvalidParameterValueException("could not find account " + accountName + " because domain is not specified");
            }
        }
    } else {
        if (accountName != null && !accountName.equals(caller.getAccountName())) {
            throw new PermissionDeniedException("Can't list account " + accountName + " projects; unauthorized");
        }
        if (domainId != null && !domainId.equals(caller.getDomainId())) {
            throw new PermissionDeniedException("Can't list domain id= " + domainId + " projects; unauthorized");
        }
        accountId = caller.getId();
    }
    if (domainId == null && accountId == null && (_accountMgr.isNormalUser(caller.getId()) || !listAll)) {
        accountId = caller.getId();
    } else if (_accountMgr.isDomainAdmin(caller.getId()) || isRecursive && !listAll) {
        final DomainVO domain = _domainDao.findById(caller.getDomainId());
        path = domain.getPath();
    }
    if (path != null) {
        sb.and("domainPath", sb.entity().getDomainPath(), SearchCriteria.Op.LIKE);
    }
    if (accountId != null) {
        sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
    }
    final SearchCriteria<ProjectJoinVO> sc = sb.create();
    if (id != null) {
        sc.addAnd("id", Op.EQ, id);
    }
    if (domainId != null && !isRecursive) {
        sc.addAnd("domainId", Op.EQ, domainId);
    }
    if (name != null) {
        sc.addAnd("name", Op.EQ, name);
    }
    if (displayText != null) {
        sc.addAnd("displayText", Op.EQ, displayText);
    }
    if (accountId != null) {
        sc.setParameters("accountId", accountId);
    }
    if (state != null) {
        sc.addAnd("state", Op.EQ, state);
    }
    if (keyword != null) {
        final SearchCriteria<ProjectJoinVO> ssc = _projectJoinDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("displayText", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (path != null) {
        sc.setParameters("domainPath", path);
    }
    // search distinct projects to get count
    final Pair<List<ProjectJoinVO>, Integer> uniquePrjPair = _projectJoinDao.searchAndCount(sc, searchFilter);
    final Integer count = uniquePrjPair.second();
    if (count.intValue() == 0) {
        // handle empty result cases
        return uniquePrjPair;
    }
    final List<ProjectJoinVO> uniquePrjs = uniquePrjPair.first();
    final Long[] prjIds = new Long[uniquePrjs.size()];
    int i = 0;
    for (final ProjectJoinVO v : uniquePrjs) {
        prjIds[i++] = v.getId();
    }
    final List<ProjectJoinVO> prjs = _projectJoinDao.searchByIds(prjIds);
    return new Pair<>(prjs, count);
}
Also used : Account(com.cloud.user.Account) ProjectJoinVO(com.cloud.api.query.vo.ProjectJoinVO) DomainVO(com.cloud.domain.DomainVO) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.utils.Pair)

Example 3 with ProjectJoinVO

use of com.cloud.api.query.vo.ProjectJoinVO in project cosmic by MissionCriticalCloud.

the class ViewResponseHelper method createProjectResponse.

public static List<ProjectResponse> createProjectResponse(final ProjectJoinVO... projects) {
    final Hashtable<Long, ProjectResponse> prjDataList = new Hashtable<>();
    // Initialise the prjdatalist with the input data
    for (final ProjectJoinVO p : projects) {
        ProjectResponse pData = prjDataList.get(p.getId());
        if (pData == null) {
            // first time encountering this vm
            pData = ApiDBUtils.newProjectResponse(p);
        } else {
            // update those  1 to many mapping fields
            pData = ApiDBUtils.fillProjectDetails(pData, p);
        }
        prjDataList.put(p.getId(), pData);
    }
    return new ArrayList<>(prjDataList.values());
}
Also used : ProjectResponse(com.cloud.api.response.ProjectResponse) Hashtable(java.util.Hashtable) ProjectJoinVO(com.cloud.api.query.vo.ProjectJoinVO) ArrayList(java.util.ArrayList)

Example 4 with ProjectJoinVO

use of com.cloud.api.query.vo.ProjectJoinVO in project cloudstack by apache.

the class QueryManagerImpl method listProjectsInternal.

private Pair<List<ProjectJoinVO>, Integer> listProjectsInternal(ListProjectsCmd cmd) {
    Long id = cmd.getId();
    String name = cmd.getName();
    String displayText = cmd.getDisplayText();
    String state = cmd.getState();
    String accountName = cmd.getAccountName();
    String username = cmd.getUsername();
    Long domainId = cmd.getDomainId();
    String keyword = cmd.getKeyword();
    Long startIndex = cmd.getStartIndex();
    Long pageSize = cmd.getPageSizeVal();
    boolean listAll = cmd.listAll();
    boolean isRecursive = cmd.isRecursive();
    cmd.getTags();
    Account caller = CallContext.current().getCallingAccount();
    User user = CallContext.current().getCallingUser();
    Long accountId = null;
    Long userId = null;
    String path = null;
    Filter searchFilter = new Filter(ProjectJoinVO.class, "id", false, startIndex, pageSize);
    SearchBuilder<ProjectJoinVO> sb = _projectJoinDao.createSearchBuilder();
    // select distinct
    sb.select(null, Func.DISTINCT, sb.entity().getId());
    if (_accountMgr.isAdmin(caller.getId())) {
        if (domainId != null) {
            DomainVO domain = _domainDao.findById(domainId);
            if (domain == null) {
                throw new InvalidParameterValueException("Domain id=" + domainId + " doesn't exist in the system");
            }
            _accountMgr.checkAccess(caller, domain);
            if (accountName != null) {
                Account owner = _accountMgr.getActiveAccountByName(accountName, domainId);
                if (owner == null) {
                    throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId);
                }
                accountId = owner.getId();
            }
            if (StringUtils.isNotEmpty(username)) {
                User owner = userDao.getUserByName(username, domainId);
                if (owner == null) {
                    throw new InvalidParameterValueException("Unable to find user " + username + " in domain " + domainId);
                }
                userId = owner.getId();
                if (accountName == null) {
                    accountId = owner.getAccountId();
                }
            }
        } else {
            // domainId == null
            if (accountName != null) {
                throw new InvalidParameterValueException("could not find account " + accountName + " because domain is not specified");
            }
            if (StringUtils.isNotEmpty(username)) {
                throw new InvalidParameterValueException("could not find user " + username + " because domain is not specified");
            }
        }
    } else {
        if (accountName != null && !accountName.equals(caller.getAccountName())) {
            throw new PermissionDeniedException("Can't list account " + accountName + " projects; unauthorized");
        }
        if (domainId != null && !domainId.equals(caller.getDomainId())) {
            throw new PermissionDeniedException("Can't list domain id= " + domainId + " projects; unauthorized");
        }
        if (StringUtils.isNotEmpty(username) && !username.equals(user.getUsername())) {
            throw new PermissionDeniedException("Can't list user " + username + " projects; unauthorized");
        }
        accountId = caller.getId();
        userId = user.getId();
    }
    if (domainId == null && accountId == null && (_accountMgr.isNormalUser(caller.getId()) || !listAll)) {
        accountId = caller.getId();
        userId = user.getId();
    } else if (_accountMgr.isDomainAdmin(caller.getId()) || (isRecursive && !listAll)) {
        DomainVO domain = _domainDao.findById(caller.getDomainId());
        path = domain.getPath();
    }
    if (path != null) {
        sb.and("domainPath", sb.entity().getDomainPath(), SearchCriteria.Op.LIKE);
    }
    if (accountId != null) {
        if (userId == null) {
            sb.and().op("accountId", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
            sb.and("userIdNull", sb.entity().getUserId(), Op.NULL);
            sb.cp();
        } else {
            sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
        }
    }
    if (userId != null) {
        sb.and().op("userId", sb.entity().getUserId(), Op.EQ);
        sb.or("userIdNull", sb.entity().getUserId(), Op.NULL);
        sb.cp();
    }
    SearchCriteria<ProjectJoinVO> sc = sb.create();
    if (id != null) {
        sc.addAnd("id", Op.EQ, id);
    }
    if (domainId != null && !isRecursive) {
        sc.addAnd("domainId", Op.EQ, domainId);
    }
    if (name != null) {
        sc.addAnd("name", Op.EQ, name);
    }
    if (displayText != null) {
        sc.addAnd("displayText", Op.EQ, displayText);
    }
    if (accountId != null) {
        sc.setParameters("accountId", accountId);
    }
    if (userId != null) {
        sc.setParameters("userId", userId);
    }
    if (state != null) {
        sc.addAnd("state", Op.EQ, state);
    }
    if (keyword != null) {
        SearchCriteria<ProjectJoinVO> ssc = _projectJoinDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("displayText", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (path != null) {
        sc.setParameters("domainPath", path);
    }
    // search distinct projects to get count
    Pair<List<ProjectJoinVO>, Integer> uniquePrjPair = _projectJoinDao.searchAndCount(sc, searchFilter);
    Integer count = uniquePrjPair.second();
    if (count.intValue() == 0) {
        // handle empty result cases
        return uniquePrjPair;
    }
    List<ProjectJoinVO> uniquePrjs = uniquePrjPair.first();
    Long[] prjIds = new Long[uniquePrjs.size()];
    int i = 0;
    for (ProjectJoinVO v : uniquePrjs) {
        prjIds[i++] = v.getId();
    }
    List<ProjectJoinVO> prjs = _projectJoinDao.searchByIds(prjIds);
    return new Pair<List<ProjectJoinVO>, Integer>(prjs, count);
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) ProjectJoinVO(com.cloud.api.query.vo.ProjectJoinVO) DomainVO(com.cloud.domain.DomainVO) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.utils.Pair)

Example 5 with ProjectJoinVO

use of com.cloud.api.query.vo.ProjectJoinVO in project cloudstack by apache.

the class ViewResponseHelper method createProjectResponse.

public static List<ProjectResponse> createProjectResponse(EnumSet<DomainDetails> details, ProjectJoinVO... projects) {
    Hashtable<Long, ProjectResponse> prjDataList = new Hashtable<Long, ProjectResponse>();
    // Initialise the prjdatalist with the input data
    for (ProjectJoinVO p : projects) {
        ProjectResponse pData = prjDataList.get(p.getId());
        if (pData == null) {
            // first time encountering this vm
            pData = ApiDBUtils.newProjectResponse(details, p);
            prjDataList.put(p.getId(), pData);
        }
    }
    return new ArrayList<ProjectResponse>(prjDataList.values());
}
Also used : ProjectResponse(org.apache.cloudstack.api.response.ProjectResponse) Hashtable(java.util.Hashtable) ProjectJoinVO(com.cloud.api.query.vo.ProjectJoinVO) ArrayList(java.util.ArrayList)

Aggregations

ProjectJoinVO (com.cloud.api.query.vo.ProjectJoinVO)5 ArrayList (java.util.ArrayList)5 Hashtable (java.util.Hashtable)3 DomainVO (com.cloud.domain.DomainVO)2 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)2 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)2 Account (com.cloud.user.Account)2 Pair (com.cloud.utils.Pair)2 Filter (com.cloud.utils.db.Filter)2 List (java.util.List)2 ProjectResponse (org.apache.cloudstack.api.response.ProjectResponse)2 ProjectResponse (com.cloud.api.response.ProjectResponse)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 User (com.cloud.user.User)1 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)1