Object serviceOffId = cmd.getServiceOfferingId();
Object pod = null;
Object hostId = null;
Object storageId = null;
if (cmd instanceof ListVMsCmdByAdmin) {
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 (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 (!isRootAdmin) {
sb.and("displayVm", sb.entity().isDisplayVm(), SearchCriteria.Op.EQ);
}
// populate the search criteria with the values passed in
SearchCriteria<UserVmJoinVO> sc = sb.create();
// building ACL condition
_accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts,
listProjectResourcesCriteria);
if (tags != null && !tags.isEmpty()) {
SearchCriteria<UserVmJoinVO> tagSc = _userVmJoinDao.createSearchCriteria();
for (String key : tags.keySet()) {
SearchCriteria<UserVmJoinVO> tsc = _userVmJoinDao.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 (groupId != null && (Long)groupId != -1) {
sc.setParameters("instanceGroupId", groupId);
}
if (keyword != null) {
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("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) {
List<?> idList = (ids instanceof List<?> ? (List<?>)ids : null);
if (idList != null && !idList.isEmpty()) {
sc.setParameters("idIN", idList.toArray());
}
}
if (templateId != null) {
sc.setParameters("templateId", templateId);
}
if (isoId != null) {
sc.setParameters("isoId", isoId);
}
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) {
sc.setParameters("stateEQ", state);
}
if (hypervisor != null) {
sc.setParameters("hypervisorType", hypervisor);
}
// Don't show Destroyed and Expunging vms to the end user
if (!isAdmin) {
sc.setParameters("stateNIN", "Destroyed", "Expunging");
}
if (zoneId != null) {
sc.setParameters("dataCenterId", zoneId);
if (state == null) {
sc.setParameters("stateNEQ", "Destroyed");
}
}
if (affinityGroupId != null) {
sc.setParameters("affinityGroupId", affinityGroupId);
}
if (cmd instanceof ListVMsCmdByAdmin) {
ListVMsCmdByAdmin aCmd = (ListVMsCmdByAdmin)cmd;
if (aCmd.getPodId() != null) {
sc.setParameters("podId", pod);
if (state == null) {
sc.setParameters("stateNEQ", "Destroyed");
}