use of com.cloud.legacymodel.utils.Ternary in project cosmic by MissionCriticalCloud.
the class QueryManagerImpl method searchForAffinityGroupsInternal.
public Pair<List<AffinityGroupJoinVO>, Integer> searchForAffinityGroupsInternal(final ListAffinityGroupsCmd cmd) {
final Long affinityGroupId = cmd.getId();
final String affinityGroupName = cmd.getAffinityGroupName();
final String affinityGroupType = cmd.getAffinityGroupType();
final Long vmId = cmd.getVirtualMachineId();
final String accountName = cmd.getAccountName();
Long domainId = cmd.getDomainId();
final Long projectId = cmd.getProjectId();
Boolean isRecursive = cmd.isRecursive();
final Boolean listAll = cmd.listAll();
final Long startIndex = cmd.getStartIndex();
final Long pageSize = cmd.getPageSizeVal();
final String keyword = cmd.getKeyword();
final Account caller = CallContext.current().getCallingAccount();
if (vmId != null) {
final UserVmVO userVM = _userVmDao.findById(vmId);
if (userVM == null) {
throw new InvalidParameterValueException("Unable to list affinity groups for virtual machine instance " + vmId + "; instance not found.");
}
_accountMgr.checkAccess(caller, null, true, userVM);
return listAffinityGroupsByVM(vmId.longValue(), startIndex, pageSize);
}
final List<Long> permittedAccounts = new ArrayList<>();
final Ternary<Long, Boolean, ListProjectResourcesCriteria> ternary = new Ternary<>(domainId, isRecursive, null);
_accountMgr.buildACLSearchParameters(caller, affinityGroupId, accountName, projectId, permittedAccounts, ternary, listAll, false);
domainId = ternary.first();
isRecursive = ternary.second();
final ListProjectResourcesCriteria listProjectResourcesCriteria = ternary.third();
final Filter searchFilter = new Filter(AffinityGroupJoinVO.class, ID_FIELD, true, startIndex, pageSize);
final SearchCriteria<AffinityGroupJoinVO> sc = buildAffinityGroupSearchCriteria(domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria, affinityGroupId, affinityGroupName, affinityGroupType, keyword);
final Pair<List<AffinityGroupJoinVO>, Integer> uniqueGroupsPair = _affinityGroupJoinDao.searchAndCount(sc, searchFilter);
// search group details by ids
List<AffinityGroupJoinVO> affinityGroups = new ArrayList<>();
final Integer count = uniqueGroupsPair.second();
if (count.intValue() != 0) {
final List<AffinityGroupJoinVO> uniqueGroups = uniqueGroupsPair.first();
final Long[] vrIds = new Long[uniqueGroups.size()];
int i = 0;
for (final AffinityGroupJoinVO v : uniqueGroups) {
vrIds[i++] = v.getId();
}
affinityGroups = _affinityGroupJoinDao.searchByIds(vrIds);
}
if (!permittedAccounts.isEmpty()) {
// add domain level affinity groups
if (domainId != null) {
final SearchCriteria<AffinityGroupJoinVO> scDomain = buildAffinityGroupSearchCriteria(null, isRecursive, new ArrayList<>(), listProjectResourcesCriteria, affinityGroupId, affinityGroupName, affinityGroupType, keyword);
affinityGroups.addAll(listDomainLevelAffinityGroups(scDomain, searchFilter, domainId));
} else {
for (final Long permAcctId : permittedAccounts) {
final Account permittedAcct = _accountDao.findById(permAcctId);
final SearchCriteria<AffinityGroupJoinVO> scDomain = buildAffinityGroupSearchCriteria(null, isRecursive, new ArrayList<>(), listProjectResourcesCriteria, affinityGroupId, affinityGroupName, affinityGroupType, keyword);
affinityGroups.addAll(listDomainLevelAffinityGroups(scDomain, searchFilter, permittedAcct.getDomainId()));
}
}
} else if (permittedAccounts.isEmpty() && domainId != null && isRecursive) {
// list all domain level affinity groups for the domain admin case
final SearchCriteria<AffinityGroupJoinVO> scDomain = buildAffinityGroupSearchCriteria(null, isRecursive, new ArrayList<>(), listProjectResourcesCriteria, affinityGroupId, affinityGroupName, affinityGroupType, keyword);
affinityGroups.addAll(listDomainLevelAffinityGroups(scDomain, searchFilter, domainId));
}
return new Pair<>(affinityGroups, affinityGroups.size());
}
use of com.cloud.legacymodel.utils.Ternary in project cosmic by MissionCriticalCloud.
the class QueryManagerImpl method listProjectInvitationsInternal.
public Pair<List<ProjectInvitationJoinVO>, Integer> listProjectInvitationsInternal(final ListProjectInvitationsCmd cmd) {
final Long id = cmd.getId();
final Long projectId = cmd.getProjectId();
final String accountName = cmd.getAccountName();
Long domainId = cmd.getDomainId();
final String state = cmd.getState();
final boolean activeOnly = cmd.isActiveOnly();
final Long startIndex = cmd.getStartIndex();
final Long pageSizeVal = cmd.getPageSizeVal();
boolean isRecursive = cmd.isRecursive();
final boolean listAll = cmd.listAll();
final Account caller = CallContext.current().getCallingAccount();
final List<Long> permittedAccounts = new ArrayList<>();
final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(domainId, isRecursive, null);
_accountMgr.buildACLSearchParameters(caller, id, accountName, projectId, permittedAccounts, domainIdRecursiveListProject, listAll, true);
domainId = domainIdRecursiveListProject.first();
isRecursive = domainIdRecursiveListProject.second();
final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
final Filter searchFilter = new Filter(ProjectInvitationJoinVO.class, "id", true, startIndex, pageSizeVal);
final SearchBuilder<ProjectInvitationJoinVO> sb = _projectInvitationJoinDao.createSearchBuilder();
_accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
sb.and("projectId", sb.entity().getProjectId(), SearchCriteria.Op.EQ);
sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
sb.and("created", sb.entity().getCreated(), SearchCriteria.Op.GT);
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
final SearchCriteria<ProjectInvitationJoinVO> sc = sb.create();
_accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
if (projectId != null) {
sc.setParameters("projectId", projectId);
}
if (state != null) {
sc.setParameters("state", state);
}
if (id != null) {
sc.setParameters("id", id);
}
if (activeOnly) {
sc.setParameters("state", ProjectInvitation.State.Pending);
sc.setParameters("created", new Date(DateUtil.currentGMTTime().getTime() - _projectMgr.getInvitationTimeout()));
}
return _projectInvitationJoinDao.searchAndCount(sc, searchFilter);
}
use of com.cloud.legacymodel.utils.Ternary in project cosmic by MissionCriticalCloud.
the class QueryManagerImpl method searchForTemplatesInternal.
private Pair<List<TemplateJoinVO>, Integer> searchForTemplatesInternal(final ListTemplatesCmd cmd) {
final TemplateFilter templateFilter = TemplateFilter.valueOf(cmd.getTemplateFilter());
final Long id = cmd.getId();
final Map<String, String> tags = cmd.getTags();
final boolean showRemovedTmpl = cmd.getShowRemoved();
final Account caller = CallContext.current().getCallingAccount();
boolean listAll = false;
if (templateFilter != null && templateFilter == TemplateFilter.all) {
if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL) {
throw new InvalidParameterValueException("Filter " + TemplateFilter.all + " can be specified by admin only");
}
listAll = true;
}
final List<Long> permittedAccountIds = new ArrayList<>();
final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
_accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccountIds, domainIdRecursiveListProject, listAll, false);
final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
final List<Account> permittedAccounts = new ArrayList<>();
for (final Long accountId : permittedAccountIds) {
permittedAccounts.add(_accountMgr.getAccount(accountId));
}
final boolean showDomr = templateFilter != TemplateFilter.selfexecutable && templateFilter != TemplateFilter.featured;
final HypervisorType hypervisorType = HypervisorType.getType(cmd.getHypervisor());
return searchForTemplatesInternal(id, cmd.getTemplateName(), cmd.getKeyword(), templateFilter, false, null, cmd.getPageSizeVal(), cmd.getStartIndex(), cmd.getZoneId(), hypervisorType, showDomr, cmd.listInReadyState(), permittedAccounts, caller, listProjectResourcesCriteria, tags, showRemovedTmpl);
}
use of com.cloud.legacymodel.utils.Ternary in project cosmic by MissionCriticalCloud.
the class QueryManagerImpl method listTagsInternal.
private Pair<List<ResourceTagJoinVO>, Integer> listTagsInternal(final ListTagsCmd cmd) {
final Account caller = CallContext.current().getCallingAccount();
final List<Long> permittedAccounts = new ArrayList<>();
final String key = cmd.getKey();
final String value = cmd.getValue();
final String resourceId = cmd.getResourceId();
final String resourceType = cmd.getResourceType();
final String customerName = cmd.getCustomer();
final boolean listAll = cmd.listAll();
final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
_accountMgr.buildACLSearchParameters(caller, null, 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(ResourceTagJoinVO.class, "resourceType", false, cmd.getStartIndex(), cmd.getPageSizeVal());
final SearchBuilder<ResourceTagJoinVO> sb = _resourceTagJoinDao.createSearchBuilder();
_accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
sb.and("key", sb.entity().getKey(), SearchCriteria.Op.EQ);
sb.and("value", sb.entity().getValue(), SearchCriteria.Op.EQ);
if (resourceId != null) {
sb.and("resourceId", sb.entity().getResourceId(), SearchCriteria.Op.EQ);
sb.and("resourceUuid", sb.entity().getResourceUuid(), SearchCriteria.Op.EQ);
}
sb.and("resourceType", sb.entity().getResourceType(), SearchCriteria.Op.EQ);
sb.and("customer", sb.entity().getCustomer(), SearchCriteria.Op.EQ);
// now set the SC criteria...
final SearchCriteria<ResourceTagJoinVO> sc = sb.create();
_accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
if (key != null) {
sc.setParameters("key", key);
}
if (value != null) {
sc.setParameters("value", value);
}
if (resourceId != null) {
try {
final long rid = Long.parseLong(resourceId);
sc.setParameters("resourceId", rid);
} catch (final NumberFormatException ex) {
// internal id instead of resource id is passed
sc.setParameters("resourceUuid", resourceId);
}
}
if (resourceType != null) {
sc.setParameters("resourceType", resourceType);
}
if (customerName != null) {
sc.setParameters("customer", customerName);
}
final Pair<List<ResourceTagJoinVO>, Integer> result = _resourceTagJoinDao.searchAndCount(sc, searchFilter);
return result;
}
use of com.cloud.legacymodel.utils.Ternary in project cosmic by MissionCriticalCloud.
the class QueryManagerImpl method searchForRoutersInternal.
private Pair<List<DomainRouterJoinVO>, Integer> searchForRoutersInternal(final BaseListProjectAndAccountResourcesCmd cmd, final Long id, final String name, final String state, final Long zoneId, final Long podId, final Long clusterId, final Long hostId, final String keyword, final Long networkId, final Long vpcId, final Boolean forVpc, final String role, final String version) {
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, 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(DomainRouterJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
final SearchBuilder<DomainRouterJoinVO> sb = _routerJoinDao.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().getInstanceName(), SearchCriteria.Op.LIKE);
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.IN);
sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
sb.and("clusterId", sb.entity().getClusterId(), SearchCriteria.Op.EQ);
sb.and("hostId", sb.entity().getHostId(), SearchCriteria.Op.EQ);
sb.and("vpcId", sb.entity().getVpcId(), SearchCriteria.Op.EQ);
sb.and("role", sb.entity().getRole(), SearchCriteria.Op.EQ);
sb.and("version", sb.entity().getTemplateVersion(), SearchCriteria.Op.LIKE);
if (forVpc != null) {
if (forVpc) {
sb.and("forVpc", sb.entity().getVpcId(), SearchCriteria.Op.NNULL);
} else {
sb.and("forVpc", sb.entity().getVpcId(), SearchCriteria.Op.NULL);
}
}
if (networkId != null) {
sb.and("networkId", sb.entity().getNetworkId(), SearchCriteria.Op.EQ);
}
final SearchCriteria<DomainRouterJoinVO> sc = sb.create();
_accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
if (keyword != null) {
final SearchCriteria<DomainRouterJoinVO> ssc = _routerJoinDao.createSearchCriteria();
ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
ssc.addOr("instanceName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
ssc.addOr("state", SearchCriteria.Op.LIKE, "%" + keyword + "%");
ssc.addOr("networkName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
ssc.addOr("vpcName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
sc.addAnd("instanceName", SearchCriteria.Op.SC, ssc);
}
if (name != null) {
sc.setParameters("name", "%" + name + "%");
}
if (id != null) {
sc.setParameters("id", id);
}
if (state != null) {
sc.setParameters("state", state);
}
if (zoneId != null) {
sc.setParameters("dataCenterId", zoneId);
}
if (podId != null) {
sc.setParameters("podId", podId);
}
if (clusterId != null) {
sc.setParameters("clusterId", clusterId);
}
if (hostId != null) {
sc.setParameters("hostId", hostId);
}
if (networkId != null) {
sc.setParameters("networkId", networkId);
}
if (vpcId != null) {
sc.setParameters("vpcId", vpcId);
}
if (role != null) {
sc.setParameters("role", role);
}
if (version != null) {
sc.setParameters("version", "Cloudstack Release " + version + "%");
}
// search VR details by ids
final Pair<List<DomainRouterJoinVO>, Integer> uniqueVrPair = _routerJoinDao.searchAndCount(sc, searchFilter);
final Integer count = uniqueVrPair.second();
if (count.intValue() == 0) {
// empty result
return uniqueVrPair;
}
final List<DomainRouterJoinVO> uniqueVrs = uniqueVrPair.first();
final Long[] vrIds = new Long[uniqueVrs.size()];
int i = 0;
for (final DomainRouterJoinVO v : uniqueVrs) {
vrIds[i++] = v.getId();
}
final List<DomainRouterJoinVO> vrs = _routerJoinDao.searchByIds(vrIds);
return new Pair<>(vrs, count);
}
Aggregations