Search in sources :

Example 21 with Ternary

use of com.cloud.legacymodel.utils.Ternary in project cosmic by MissionCriticalCloud.

the class QueryManagerImpl method searchForVolumesInternal.

private Pair<List<VolumeJoinVO>, Integer> searchForVolumesInternal(final ListVolumesCmd cmd) {
    final Account caller = CallContext.current().getCallingAccount();
    final List<Long> permittedAccounts = new ArrayList<>();
    final Long id = cmd.getId();
    final Long vmInstanceId = cmd.getVirtualMachineId();
    final String name = cmd.getVolumeName();
    final String keyword = cmd.getKeyword();
    final String type = cmd.getType();
    final Map<String, String> tags = cmd.getTags();
    final Long storageId = cmd.getStorageId();
    final Long diskOffId = cmd.getDiskOfferingId();
    final Boolean display = cmd.getDisplay();
    final Long zoneId = cmd.getZoneId();
    final Long podId = cmd.getPodId();
    final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
    final Long domainId = domainIdRecursiveListProject.first();
    final Boolean isRecursive = domainIdRecursiveListProject.second();
    final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    final Filter searchFilter = new Filter(VolumeJoinVO.class, "created", false, cmd.getStartIndex(), cmd.getPageSizeVal());
    // hack for now, this should be done better but due to needing a join I
    // opted to
    // do this quickly and worry about making it pretty later
    final SearchBuilder<VolumeJoinVO> sb = _volumeJoinDao.createSearchBuilder();
    // select distinct
    sb.select(null, Func.DISTINCT, sb.entity().getId());
    // ids to get
    // number of
    // records with
    // pagination
    _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("volumeType", sb.entity().getVolumeType(), SearchCriteria.Op.LIKE);
    sb.and("instanceId", sb.entity().getVmId(), SearchCriteria.Op.EQ);
    sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
    sb.and("storageId", sb.entity().getPoolId(), SearchCriteria.Op.EQ);
    sb.and("diskOfferingId", sb.entity().getDiskOfferingId(), SearchCriteria.Op.EQ);
    sb.and("display", sb.entity().isDisplayVolume(), SearchCriteria.Op.EQ);
    // Only return volumes that are not destroyed
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.NEQ);
    sb.and("systemUse", sb.entity().isSystemUse(), SearchCriteria.Op.NEQ);
    // now set the SC criteria...
    final SearchCriteria<VolumeJoinVO> sc = sb.create();
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (keyword != null) {
        final SearchCriteria<VolumeJoinVO> ssc = _volumeJoinDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("volumeType", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (name != null) {
        sc.setParameters("name", name);
    }
    if (display != null) {
        sc.setParameters("display", display);
    }
    // normal users and domain admins cannot see volumes of type system
    sc.setParameters("systemUse", 1);
    // root admins can see them
    if (caller.getType() == Account.ACCOUNT_TYPE_ADMIN) {
        sc.setParameters("systemUse", -1);
    }
    if (tags != null && !tags.isEmpty()) {
        final SearchCriteria<VolumeJoinVO> tagSc = _volumeJoinDao.createSearchCriteria();
        for (final String key : tags.keySet()) {
            final SearchCriteria<VolumeJoinVO> tsc = _volumeJoinDao.createSearchCriteria();
            tsc.addAnd("tagKey", SearchCriteria.Op.EQ, key);
            tsc.addAnd("tagValue", SearchCriteria.Op.EQ, tags.get(key));
            tagSc.addOr("tagKey", SearchCriteria.Op.SC, tsc);
        }
        sc.addAnd("tagKey", SearchCriteria.Op.SC, tagSc);
    }
    if (diskOffId != null) {
        sc.setParameters("diskOfferingId", diskOffId);
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (type != null) {
        sc.setParameters("volumeType", "%" + type + "%");
    }
    if (vmInstanceId != null) {
        sc.setParameters("instanceId", vmInstanceId);
    }
    if (zoneId != null) {
        sc.setParameters("dataCenterId", zoneId);
    }
    if (podId != null) {
        sc.setParameters("podId", podId);
    }
    if (storageId != null) {
        sc.setParameters("storageId", storageId);
    }
    // Only return volumes that are not destroyed
    sc.setParameters("state", Volume.State.Destroy);
    // search Volume details by ids
    final Pair<List<VolumeJoinVO>, Integer> uniqueVolPair = _volumeJoinDao.searchAndCount(sc, searchFilter);
    final Integer count = uniqueVolPair.second();
    if (count.intValue() == 0) {
        // empty result
        return uniqueVolPair;
    }
    final List<VolumeJoinVO> uniqueVols = uniqueVolPair.first();
    final Long[] vrIds = new Long[uniqueVols.size()];
    int i = 0;
    for (final VolumeJoinVO v : uniqueVols) {
        vrIds[i++] = v.getId();
    }
    final List<VolumeJoinVO> vrs = _volumeJoinDao.searchByIds(vrIds);
    return new Pair<>(vrs, count);
}
Also used : Account(com.cloud.legacymodel.user.Account) Ternary(com.cloud.legacymodel.utils.Ternary) ArrayList(java.util.ArrayList) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) TemplateFilter(com.cloud.legacymodel.storage.VirtualMachineTemplate.TemplateFilter) ArrayList(java.util.ArrayList) List(java.util.List) VolumeJoinVO(com.cloud.api.query.vo.VolumeJoinVO) Pair(com.cloud.legacymodel.utils.Pair)

Example 22 with Ternary

use of com.cloud.legacymodel.utils.Ternary in project cosmic by MissionCriticalCloud.

the class QueryManagerImpl method searchForUserVMsInternal.

private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsInternal(final ListVMsCmd cmd) {
    final Account caller = CallContext.current().getCallingAccount();
    final List<Long> permittedAccounts = new ArrayList<>();
    final boolean listAll = cmd.listAll();
    final Long id = cmd.getId();
    final Long userId = cmd.getUserId();
    final Map<String, String> tags = cmd.getTags();
    final Boolean display = cmd.getDisplay();
    final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, listAll, false);
    final Long domainId = domainIdRecursiveListProject.first();
    final Boolean isRecursive = domainIdRecursiveListProject.second();
    final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    final Filter searchFilter = new Filter(UserVmJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    final List<Long> ids;
    if (cmd.getId() != null) {
        if (cmd.getIds() != null && !cmd.getIds().isEmpty()) {
            throw new InvalidParameterValueException("Specify either id or ids but not both parameters");
        }
        ids = new ArrayList<>();
        ids.add(cmd.getId());
    } else {
        ids = cmd.getIds();
    }
    // first search distinct vm id by using query criteria and pagination
    final SearchBuilder<UserVmJoinVO> sb = _userVmJoinDao.createSearchBuilder();
    // select distinct ids
    sb.select(null, Func.DISTINCT, sb.entity().getId());
    _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    final String hypervisor = cmd.getHypervisor();
    final Object name = cmd.getName();
    final String state = cmd.getState();
    final Object zoneId = cmd.getZoneId();
    final Object keyword = cmd.getKeyword();
    boolean isAdmin = false;
    boolean isRootAdmin = false;
    if (_accountMgr.isAdmin(caller.getId())) {
        isAdmin = true;
    }
    if (_accountMgr.isRootAdmin(caller.getId())) {
        isRootAdmin = true;
    }
    final Object groupId = cmd.getGroupId();
    final Object networkId = cmd.getNetworkId();
    if (HypervisorType.getType(hypervisor) == HypervisorType.None && hypervisor != null) {
        // invalid hypervisor type input
        throw new InvalidParameterValueException("Invalid HypervisorType " + hypervisor);
    }
    final Object templateId = cmd.getTemplateId();
    final Object isoId = cmd.getIsoId();
    final Object vpcId = cmd.getVpcId();
    final Object affinityGroupId = cmd.getAffinityGroupId();
    final Object keyPairName = cmd.getKeyPairName();
    final Object serviceOffId = cmd.getServiceOfferingId();
    final String complianceStatus = cmd.getComplianceStatus();
    Object pod = null;
    Object hostId = null;
    Object storageId = null;
    if (cmd instanceof ListVMsCmdByAdmin) {
        final ListVMsCmdByAdmin adCmd = (ListVMsCmdByAdmin) cmd;
        pod = adCmd.getPodId();
        hostId = adCmd.getHostId();
        storageId = adCmd.getStorageId();
    }
    sb.and("displayName", sb.entity().getDisplayName(), SearchCriteria.Op.LIKE);
    sb.and("idIN", sb.entity().getId(), SearchCriteria.Op.IN);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
    sb.and("stateEQ", sb.entity().getState(), SearchCriteria.Op.EQ);
    sb.and("stateNEQ", sb.entity().getState(), SearchCriteria.Op.NEQ);
    sb.and("stateNIN", sb.entity().getState(), SearchCriteria.Op.NIN);
    sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
    sb.and("hypervisorType", sb.entity().getHypervisorType(), SearchCriteria.Op.EQ);
    sb.and("hostIdEQ", sb.entity().getHostId(), SearchCriteria.Op.EQ);
    sb.and("templateId", sb.entity().getTemplateId(), SearchCriteria.Op.EQ);
    sb.and("isoId", sb.entity().getIsoId(), SearchCriteria.Op.EQ);
    sb.and("instanceGroupId", sb.entity().getInstanceGroupId(), SearchCriteria.Op.EQ);
    if (serviceOffId != null) {
        sb.and("serviceOfferingId", sb.entity().getServiceOfferingId(), SearchCriteria.Op.EQ);
    }
    if (display != null) {
        sb.and("display", sb.entity().isDisplayVm(), SearchCriteria.Op.EQ);
    }
    if (groupId != null && (Long) groupId != -1) {
        sb.and("instanceGroupId", sb.entity().getInstanceGroupId(), SearchCriteria.Op.EQ);
    }
    if (userId != null) {
        sb.and("userId", sb.entity().getUserId(), SearchCriteria.Op.EQ);
    }
    if (networkId != null) {
        sb.and("networkId", sb.entity().getNetworkId(), SearchCriteria.Op.EQ);
    }
    if (vpcId != null && networkId == null) {
        sb.and("vpcId", sb.entity().getVpcId(), SearchCriteria.Op.EQ);
    }
    if (storageId != null) {
        sb.and("poolId", sb.entity().getPoolId(), SearchCriteria.Op.EQ);
    }
    if (affinityGroupId != null) {
        sb.and("affinityGroupId", sb.entity().getAffinityGroupId(), SearchCriteria.Op.EQ);
    }
    if (keyPairName != null) {
        sb.and("keyPairName", sb.entity().getKeypairName(), SearchCriteria.Op.EQ);
    }
    if (complianceStatus != null) {
        sb.and("complianceStatus", sb.entity().getComplianceStatus(), SearchCriteria.Op.EQ);
    }
    if (!isRootAdmin) {
        sb.and("displayVm", sb.entity().isDisplayVm(), SearchCriteria.Op.EQ);
    }
    // populate the search criteria with the values passed in
    final SearchCriteria<UserVmJoinVO> sc = sb.create();
    // building ACL condition
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (tags != null && !tags.isEmpty()) {
        final SearchCriteria<UserVmJoinVO> tagSc = _userVmJoinDao.createSearchCriteria();
        for (final Map.Entry<String, String> entry : tags.entrySet()) {
            final SearchCriteria<UserVmJoinVO> tsc = _userVmJoinDao.createSearchCriteria();
            tsc.addAnd("tagKey", SearchCriteria.Op.EQ, entry.getKey());
            tsc.addAnd("tagValue", SearchCriteria.Op.EQ, entry.getValue());
            tagSc.addOr("tagKey", SearchCriteria.Op.SC, tsc);
        }
        sc.addAnd("tagKey", SearchCriteria.Op.SC, tagSc);
    }
    if (groupId != null && (Long) groupId != -1) {
        sc.setParameters("instanceGroupId", groupId);
    }
    if (keyword != null) {
        final SearchCriteria<UserVmJoinVO> ssc = _userVmJoinDao.createSearchCriteria();
        ssc.addOr("displayName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("instanceName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("hostName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("state", SearchCriteria.Op.EQ, keyword);
        sc.addAnd("displayName", SearchCriteria.Op.SC, ssc);
    }
    if (serviceOffId != null) {
        sc.setParameters("serviceOfferingId", serviceOffId);
    }
    if (display != null) {
        sc.setParameters("display", display);
    }
    if (ids != null && !ids.isEmpty()) {
        sc.setParameters("idIN", ids.toArray());
    }
    if (templateId != null) {
        sc.setParameters("templateId", templateId);
    }
    if (isoId != null) {
        sc.setParameters("isoId", isoId);
    }
    if (userId != null) {
        sc.setParameters("userId", userId);
    }
    if (networkId != null) {
        sc.setParameters("networkId", networkId);
    }
    if (vpcId != null && networkId == null) {
        sc.setParameters("vpcId", vpcId);
    }
    if (name != null) {
        sc.setParameters("name", "%" + name + "%");
    }
    if (state != null) {
        if (state.equalsIgnoreCase("present")) {
            sc.setParameters("stateNIN", "Destroyed", "Expunging");
        } else {
            sc.setParameters("stateEQ", state);
        }
    }
    if (hypervisor != null) {
        sc.setParameters("hypervisorType", hypervisor);
    }
    // Don't show Destroyed and Expunging vms to the end user if the AllowUserViewDestroyedVM flag is not set.
    if (!isAdmin && !AllowUserViewDestroyedVM.valueIn(caller.getAccountId())) {
        sc.setParameters("stateNIN", "Destroyed", "Expunging");
    }
    if (zoneId != null) {
        sc.setParameters("dataCenterId", zoneId);
    }
    if (affinityGroupId != null) {
        sc.setParameters("affinityGroupId", affinityGroupId);
    }
    if (keyPairName != null) {
        sc.setParameters("keyPairName", keyPairName);
    }
    if (complianceStatus != null) {
        sc.setParameters("complianceStatus", complianceStatus);
    }
    if (cmd instanceof ListVMsCmdByAdmin) {
        final ListVMsCmdByAdmin aCmd = (ListVMsCmdByAdmin) cmd;
        if (aCmd.getPodId() != null) {
            sc.setParameters("podId", pod);
            if (state == null) {
                sc.setParameters("stateNEQ", "Destroyed");
            }
        }
        if (hostId != null) {
            sc.setParameters("hostIdEQ", hostId);
        }
        if (storageId != null) {
            sc.setParameters("poolId", storageId);
        }
    }
    if (!isRootAdmin) {
        sc.setParameters("displayVm", 1);
    }
    // search vm details by ids
    final Pair<List<UserVmJoinVO>, Integer> uniqueVmPair = _userVmJoinDao.searchAndDistinctCount(sc, searchFilter);
    final Integer count = uniqueVmPair.second();
    if (count.intValue() == 0) {
        // handle empty result cases
        return uniqueVmPair;
    }
    final List<UserVmJoinVO> uniqueVms = uniqueVmPair.first();
    final Long[] vmIds = new Long[uniqueVms.size()];
    int i = 0;
    for (final UserVmJoinVO v : uniqueVms) {
        vmIds[i++] = v.getId();
    }
    final List<UserVmJoinVO> vms = _userVmJoinDao.searchByIds(vmIds);
    return new Pair<>(vms, count);
}
Also used : Account(com.cloud.legacymodel.user.Account) ArrayList(java.util.ArrayList) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.legacymodel.utils.Pair) Ternary(com.cloud.legacymodel.utils.Ternary) UserVmJoinVO(com.cloud.api.query.vo.UserVmJoinVO) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) TemplateFilter(com.cloud.legacymodel.storage.VirtualMachineTemplate.TemplateFilter) ListVMsCmdByAdmin(com.cloud.api.command.admin.vm.ListVMsCmdByAdmin) Map(java.util.Map)

Example 23 with Ternary

use of com.cloud.legacymodel.utils.Ternary in project cosmic by MissionCriticalCloud.

the class QueryManagerImpl method searchForAsyncJobsInternal.

private Pair<List<AsyncJobJoinVO>, Integer> searchForAsyncJobsInternal(final ListAsyncJobsCmd cmd) {
    final Account caller = CallContext.current().getCallingAccount();
    final List<Long> permittedAccounts = new ArrayList<>();
    final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, null, cmd.getAccountName(), null, permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
    final Long domainId = domainIdRecursiveListProject.first();
    final Boolean isRecursive = domainIdRecursiveListProject.second();
    final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    final Filter searchFilter = new Filter(AsyncJobJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    final SearchBuilder<AsyncJobJoinVO> sb = _jobJoinDao.createSearchBuilder();
    sb.and("accountIdIN", sb.entity().getAccountId(), SearchCriteria.Op.IN);
    boolean accountJoinIsDone = false;
    if (permittedAccounts.isEmpty() && domainId != null) {
        sb.and("domainId", sb.entity().getDomainId(), SearchCriteria.Op.EQ);
        sb.and("path", sb.entity().getDomainPath(), SearchCriteria.Op.LIKE);
        accountJoinIsDone = true;
    }
    if (listProjectResourcesCriteria != null) {
        if (listProjectResourcesCriteria == Project.ListProjectResourcesCriteria.ListProjectResourcesOnly) {
            sb.and("type", sb.entity().getAccountType(), SearchCriteria.Op.EQ);
        } else if (listProjectResourcesCriteria == Project.ListProjectResourcesCriteria.SkipProjectResources) {
            sb.and("type", sb.entity().getAccountType(), SearchCriteria.Op.NEQ);
        }
        if (!accountJoinIsDone) {
            sb.and("domainId", sb.entity().getDomainId(), SearchCriteria.Op.EQ);
            sb.and("path", sb.entity().getDomainPath(), SearchCriteria.Op.LIKE);
        }
    }
    final Object keyword = cmd.getKeyword();
    final Object startDate = cmd.getStartDate();
    final SearchCriteria<AsyncJobJoinVO> sc = sb.create();
    if (listProjectResourcesCriteria != null) {
        sc.setParameters("type", Account.ACCOUNT_TYPE_PROJECT);
    }
    if (!permittedAccounts.isEmpty()) {
        sc.setParameters("accountIdIN", permittedAccounts.toArray());
    } else if (domainId != null) {
        final DomainVO domain = _domainDao.findById(domainId);
        if (isRecursive) {
            sc.setParameters("path", domain.getPath() + "%");
        } else {
            sc.setParameters("domainId", domainId);
        }
    }
    if (keyword != null) {
        sc.addAnd("cmd", SearchCriteria.Op.LIKE, "%" + keyword + "%");
    }
    if (startDate != null) {
        sc.addAnd("created", SearchCriteria.Op.GTEQ, startDate);
    }
    return _jobJoinDao.searchAndCount(sc, searchFilter);
}
Also used : Account(com.cloud.legacymodel.user.Account) Ternary(com.cloud.legacymodel.utils.Ternary) ArrayList(java.util.ArrayList) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) DomainVO(com.cloud.domain.DomainVO) AsyncJobJoinVO(com.cloud.api.query.vo.AsyncJobJoinVO) Filter(com.cloud.utils.db.Filter) TemplateFilter(com.cloud.legacymodel.storage.VirtualMachineTemplate.TemplateFilter)

Example 24 with Ternary

use of com.cloud.legacymodel.utils.Ternary in project cosmic by MissionCriticalCloud.

the class QueryManagerImpl method searchForEventsInternal.

private Pair<List<EventJoinVO>, Integer> searchForEventsInternal(final ListEventsCmd cmd) {
    final Account caller = CallContext.current().getCallingAccount();
    final List<Long> permittedAccounts = new ArrayList<>();
    final Long id = cmd.getId();
    final String type = cmd.getType();
    final String level = cmd.getLevel();
    final Date startDate = cmd.getStartDate();
    final Date endDate = cmd.getEndDate();
    final String keyword = cmd.getKeyword();
    final Integer entryTime = cmd.getEntryTime();
    final Integer duration = cmd.getDuration();
    final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
    final Long domainId = domainIdRecursiveListProject.first();
    final Boolean isRecursive = domainIdRecursiveListProject.second();
    final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    final Filter searchFilter = new Filter(EventJoinVO.class, "createDate", false, cmd.getStartIndex(), cmd.getPageSizeVal());
    final SearchBuilder<EventJoinVO> sb = _eventJoinDao.createSearchBuilder();
    _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("levelL", sb.entity().getLevel(), SearchCriteria.Op.LIKE);
    sb.and("levelEQ", sb.entity().getLevel(), SearchCriteria.Op.EQ);
    sb.and("type", sb.entity().getType(), SearchCriteria.Op.EQ);
    sb.and("createDateB", sb.entity().getCreateDate(), SearchCriteria.Op.BETWEEN);
    sb.and("createDateG", sb.entity().getCreateDate(), SearchCriteria.Op.GTEQ);
    sb.and("createDateL", sb.entity().getCreateDate(), SearchCriteria.Op.LTEQ);
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.NEQ);
    sb.and("startId", sb.entity().getStartId(), SearchCriteria.Op.EQ);
    sb.and("createDate", sb.entity().getCreateDate(), SearchCriteria.Op.BETWEEN);
    sb.and("displayEvent", sb.entity().getDisplay(), SearchCriteria.Op.EQ);
    sb.and("archived", sb.entity().getArchived(), SearchCriteria.Op.EQ);
    final SearchCriteria<EventJoinVO> sc = sb.create();
    // building ACL condition
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    // For end users display only enabled events
    if (!_accountMgr.isRootAdmin(caller.getId())) {
        sc.setParameters("displayEvent", true);
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (keyword != null) {
        final SearchCriteria<EventJoinVO> ssc = _eventJoinDao.createSearchCriteria();
        ssc.addOr("type", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("level", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("level", SearchCriteria.Op.SC, ssc);
    }
    if (level != null) {
        sc.setParameters("levelEQ", level);
    }
    if (type != null) {
        sc.setParameters("type", type);
    }
    if (startDate != null && endDate != null) {
        sc.setParameters("createDateB", startDate, endDate);
    } else if (startDate != null) {
        sc.setParameters("createDateG", startDate);
    } else if (endDate != null) {
        sc.setParameters("createDateL", endDate);
    }
    sc.setParameters("archived", false);
    Pair<List<EventJoinVO>, Integer> eventPair = null;
    // searchAndCount should be good enough.
    if (entryTime != null && duration != null) {
    // TODO: waiting for response from dev list, logic is mystery to
    // me!!
    /*
             * if (entryTime <= duration) { throw new
             * InvalidParameterValueException
             * ("Entry time must be greater than duration"); } Calendar calMin =
             * Calendar.getInstance(); Calendar calMax = Calendar.getInstance();
             * calMin.add(Calendar.SECOND, -entryTime);
             * calMax.add(Calendar.SECOND, -duration); Date minTime =
             * calMin.getTime(); Date maxTime = calMax.getTime();
             *
             * sc.setParameters("state", com.cloud.event.Event.State.Completed);
             * sc.setParameters("startId", 0); sc.setParameters("createDate",
             * minTime, maxTime); List<EventJoinVO> startedEvents =
             * _eventJoinDao.searchAllEvents(sc, searchFilter);
             * List<EventJoinVO> pendingEvents = new ArrayList<EventJoinVO>();
             * for (EventVO event : startedEvents) { EventVO completedEvent =
             * _eventDao.findCompletedEvent(event.getId()); if (completedEvent
             * == null) { pendingEvents.add(event); } } return pendingEvents;
             */
    } else {
        eventPair = _eventJoinDao.searchAndCount(sc, searchFilter);
    }
    return eventPair;
}
Also used : Account(com.cloud.legacymodel.user.Account) Ternary(com.cloud.legacymodel.utils.Ternary) ArrayList(java.util.ArrayList) Date(java.util.Date) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) TemplateFilter(com.cloud.legacymodel.storage.VirtualMachineTemplate.TemplateFilter) ArrayList(java.util.ArrayList) List(java.util.List) EventJoinVO(com.cloud.api.query.vo.EventJoinVO)

Example 25 with Ternary

use of com.cloud.legacymodel.utils.Ternary in project cosmic by MissionCriticalCloud.

the class QueryManagerImpl method searchForUsersInternal.

private Pair<List<UserAccountJoinVO>, Integer> searchForUsersInternal(final ListUsersCmd cmd) throws PermissionDeniedException {
    final Account caller = CallContext.current().getCallingAccount();
    final List<Long> permittedAccounts = new ArrayList<>();
    final boolean listAll = cmd.listAll();
    Long id = cmd.getId();
    if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL) {
        final long currentId = CallContext.current().getCallingUser().getId();
        if (id != null && currentId != id.longValue()) {
            throw new PermissionDeniedException("Calling user is not authorized to see the user requested by id");
        }
        id = currentId;
    }
    final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), null, permittedAccounts, domainIdRecursiveListProject, listAll, false);
    final Long domainId = domainIdRecursiveListProject.first();
    final Boolean isRecursive = domainIdRecursiveListProject.second();
    final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    final Filter searchFilter = new Filter(UserAccountJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    final Object username = cmd.getUsername();
    final Object type = cmd.getAccountType();
    final Object accountName = cmd.getAccountName();
    final Object state = cmd.getState();
    final Object keyword = cmd.getKeyword();
    final SearchBuilder<UserAccountJoinVO> sb = _userAccountJoinDao.createSearchBuilder();
    _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("username", sb.entity().getUsername(), SearchCriteria.Op.LIKE);
    if (id != null && id == 1) {
        // system user should NOT be searchable
        final List<UserAccountJoinVO> emptyList = new ArrayList<>();
        return new Pair<>(emptyList, 0);
    } else if (id != null) {
        sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    } else {
        // this condition is used to exclude system user from the search
        // results
        sb.and("id", sb.entity().getId(), SearchCriteria.Op.NEQ);
    }
    sb.and("type", sb.entity().getAccountType(), SearchCriteria.Op.EQ);
    sb.and("domainId", sb.entity().getDomainId(), SearchCriteria.Op.EQ);
    sb.and("accountName", sb.entity().getAccountName(), SearchCriteria.Op.EQ);
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
    if (accountName == null && domainId != null) {
        sb.and("domainPath", sb.entity().getDomainPath(), SearchCriteria.Op.LIKE);
    }
    final SearchCriteria<UserAccountJoinVO> sc = sb.create();
    // building ACL condition
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (keyword != null) {
        final SearchCriteria<UserAccountJoinVO> ssc = _userAccountJoinDao.createSearchCriteria();
        ssc.addOr("username", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("firstname", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("lastname", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("email", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("state", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("accountName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("accountType", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("username", SearchCriteria.Op.SC, ssc);
    }
    if (username != null) {
        sc.setParameters("username", username);
    }
    if (id != null) {
        sc.setParameters("id", id);
    } else {
        // Don't return system user, search builder with NEQ
        sc.setParameters("id", 1);
    }
    if (type != null) {
        sc.setParameters("type", type);
    }
    if (accountName != null) {
        sc.setParameters("accountName", accountName);
        if (domainId != null) {
            sc.setParameters("domainId", domainId);
        }
    } else if (domainId != null) {
        final DomainVO domainVO = _domainDao.findById(domainId);
        sc.setParameters("domainPath", domainVO.getPath() + "%");
    }
    if (state != null) {
        sc.setParameters("state", state);
    }
    return _userAccountJoinDao.searchAndCount(sc, searchFilter);
}
Also used : Account(com.cloud.legacymodel.user.Account) Ternary(com.cloud.legacymodel.utils.Ternary) ArrayList(java.util.ArrayList) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) DomainVO(com.cloud.domain.DomainVO) UserAccountJoinVO(com.cloud.api.query.vo.UserAccountJoinVO) Filter(com.cloud.utils.db.Filter) TemplateFilter(com.cloud.legacymodel.storage.VirtualMachineTemplate.TemplateFilter) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) Pair(com.cloud.legacymodel.utils.Pair)

Aggregations

Ternary (com.cloud.legacymodel.utils.Ternary)36 ArrayList (java.util.ArrayList)33 Account (com.cloud.legacymodel.user.Account)31 ListProjectResourcesCriteria (com.cloud.projects.Project.ListProjectResourcesCriteria)30 Filter (com.cloud.utils.db.Filter)28 List (java.util.List)24 Pair (com.cloud.legacymodel.utils.Pair)23 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)12 TemplateFilter (com.cloud.legacymodel.storage.VirtualMachineTemplate.TemplateFilter)12 ResourceTagVO (com.cloud.tags.ResourceTagVO)8 LinkedList (java.util.LinkedList)6 IPAddressVO (com.cloud.network.dao.IPAddressVO)5 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)3 Vpc (com.cloud.legacymodel.network.vpc.Vpc)3 HypervisorType (com.cloud.model.enumeration.HypervisorType)3 DomainVO (com.cloud.domain.DomainVO)2 HostVO (com.cloud.host.HostVO)2 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)2 SSHKeyPair (com.cloud.legacymodel.user.SSHKeyPair)2 LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)2