diff --git a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineClusterDaoImpl.java b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineClusterDaoImpl.java index c02bed0ff70..e7eea6c42c3 100644 --- a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineClusterDaoImpl.java +++ b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineClusterDaoImpl.java @@ -202,7 +202,7 @@ public class EngineClusterDaoImpl extends GenericDaoBase @Override public List listDisabledClusters(long zoneId, Long podId) { GenericSearchBuilder clusterIdSearch = createSearchBuilder(Long.class); - clusterIdSearch.selectField(clusterIdSearch.entity().getId()); + clusterIdSearch.selectFields(clusterIdSearch.entity().getId()); clusterIdSearch.and("dataCenterId", clusterIdSearch.entity().getDataCenterId(), Op.EQ); if(podId != null){ clusterIdSearch.and("podId", clusterIdSearch.entity().getPodId(), Op.EQ); @@ -224,12 +224,12 @@ public class EngineClusterDaoImpl extends GenericDaoBase public List listClustersWithDisabledPods(long zoneId) { GenericSearchBuilder disabledPodIdSearch = _hostPodDao.createSearchBuilder(Long.class); - disabledPodIdSearch.selectField(disabledPodIdSearch.entity().getId()); + disabledPodIdSearch.selectFields(disabledPodIdSearch.entity().getId()); disabledPodIdSearch.and("dataCenterId", disabledPodIdSearch.entity().getDataCenterId(), Op.EQ); disabledPodIdSearch.and("allocationState", disabledPodIdSearch.entity().getAllocationState(), Op.EQ); GenericSearchBuilder clusterIdSearch = createSearchBuilder(Long.class); - clusterIdSearch.selectField(clusterIdSearch.entity().getId()); + clusterIdSearch.selectFields(clusterIdSearch.entity().getId()); clusterIdSearch.join("disabledPodIdSearch", disabledPodIdSearch, clusterIdSearch.entity().getPodId(), disabledPodIdSearch.entity().getId(), JoinBuilder.JoinType.INNER); clusterIdSearch.done(); diff --git a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineHostDaoImpl.java b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineHostDaoImpl.java index 5ba65cce29d..4bbc601c8b9 100644 --- a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineHostDaoImpl.java +++ b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineHostDaoImpl.java @@ -269,7 +269,7 @@ public class EngineHostDaoImpl extends GenericDaoBase implem AvailHypevisorInZone.done(); HostsInStatusSearch = createSearchBuilder(Long.class); - HostsInStatusSearch.selectField(HostsInStatusSearch.entity().getId()); + HostsInStatusSearch.selectFields(HostsInStatusSearch.entity().getId()); HostsInStatusSearch.and("dc", HostsInStatusSearch.entity().getDataCenterId(), Op.EQ); HostsInStatusSearch.and("pod", HostsInStatusSearch.entity().getPodId(), Op.EQ); HostsInStatusSearch.and("cluster", HostsInStatusSearch.entity().getClusterId(), Op.EQ); diff --git a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineHostPodDaoImpl.java b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineHostPodDaoImpl.java index 638d0bded05..7bf85d530e5 100644 --- a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineHostPodDaoImpl.java +++ b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineHostPodDaoImpl.java @@ -138,7 +138,7 @@ public class EngineHostPodDaoImpl extends GenericDaoBase @Override public List listDisabledPods(long zoneId) { GenericSearchBuilder podIdSearch = createSearchBuilder(Long.class); - podIdSearch.selectField(podIdSearch.entity().getId()); + podIdSearch.selectFields(podIdSearch.entity().getId()); podIdSearch.and("dataCenterId", podIdSearch.entity().getDataCenterId(), Op.EQ); podIdSearch.and("allocationState", podIdSearch.entity().getAllocationState(), Op.EQ); podIdSearch.done(); diff --git a/engine/schema/src/com/cloud/dc/dao/ClusterDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/ClusterDaoImpl.java index 64bf1fe6103..d115ce9e973 100644 --- a/engine/schema/src/com/cloud/dc/dao/ClusterDaoImpl.java +++ b/engine/schema/src/com/cloud/dc/dao/ClusterDaoImpl.java @@ -94,7 +94,7 @@ public class ClusterDaoImpl extends GenericDaoBase implements C ZoneClusterSearch.done(); ClusterIdSearch = createSearchBuilder(Long.class); - ClusterIdSearch.selectField(ClusterIdSearch.entity().getId()); + ClusterIdSearch.selectFields(ClusterIdSearch.entity().getId()); ClusterIdSearch.and("dataCenterId", ClusterIdSearch.entity().getDataCenterId(), Op.EQ); ClusterIdSearch.done(); } @@ -196,7 +196,7 @@ public class ClusterDaoImpl extends GenericDaoBase implements C @Override public List listDisabledClusters(long zoneId, Long podId) { GenericSearchBuilder clusterIdSearch = createSearchBuilder(Long.class); - clusterIdSearch.selectField(clusterIdSearch.entity().getId()); + clusterIdSearch.selectFields(clusterIdSearch.entity().getId()); clusterIdSearch.and("dataCenterId", clusterIdSearch.entity().getDataCenterId(), Op.EQ); if (podId != null) { clusterIdSearch.and("podId", clusterIdSearch.entity().getPodId(), Op.EQ); @@ -217,12 +217,12 @@ public class ClusterDaoImpl extends GenericDaoBase implements C public List listClustersWithDisabledPods(long zoneId) { GenericSearchBuilder disabledPodIdSearch = _hostPodDao.createSearchBuilder(Long.class); - disabledPodIdSearch.selectField(disabledPodIdSearch.entity().getId()); + disabledPodIdSearch.selectFields(disabledPodIdSearch.entity().getId()); disabledPodIdSearch.and("dataCenterId", disabledPodIdSearch.entity().getDataCenterId(), Op.EQ); disabledPodIdSearch.and("allocationState", disabledPodIdSearch.entity().getAllocationState(), Op.EQ); GenericSearchBuilder clusterIdSearch = createSearchBuilder(Long.class); - clusterIdSearch.selectField(clusterIdSearch.entity().getId()); + clusterIdSearch.selectFields(clusterIdSearch.entity().getId()); clusterIdSearch.join("disabledPodIdSearch", disabledPodIdSearch, clusterIdSearch.entity().getPodId(), disabledPodIdSearch.entity().getId(), JoinBuilder.JoinType.INNER); clusterIdSearch.done(); diff --git a/engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java index 14b2931dcc5..f77dedae1b7 100644 --- a/engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java +++ b/engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java @@ -57,7 +57,7 @@ public class HostPodDaoImpl extends GenericDaoBase implements H DataCenterIdSearch.done(); PodIdSearch = createSearchBuilder(Long.class); - PodIdSearch.selectField(PodIdSearch.entity().getId()); + PodIdSearch.selectFields(PodIdSearch.entity().getId()); PodIdSearch.and("dataCenterId", PodIdSearch.entity().getDataCenterId(), Op.EQ); PodIdSearch.and("allocationState", PodIdSearch.entity().getAllocationState(), Op.EQ); PodIdSearch.done(); diff --git a/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDaoImpl.java index 782ee0d9727..ee1c3db8641 100755 --- a/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDaoImpl.java +++ b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDaoImpl.java @@ -56,7 +56,7 @@ public class StorageNetworkIpAddressDaoImpl extends GenericDaoBase implements Dom FindAllChildrenSearch.done(); FindIdsOfAllChildrenSearch = createSearchBuilder(Long.class); - FindIdsOfAllChildrenSearch.selectField(FindIdsOfAllChildrenSearch.entity().getId()); + FindIdsOfAllChildrenSearch.selectFields(FindIdsOfAllChildrenSearch.entity().getId()); FindIdsOfAllChildrenSearch.and("path", FindIdsOfAllChildrenSearch.entity().getPath(), SearchCriteria.Op.LIKE); FindIdsOfAllChildrenSearch.done(); diff --git a/engine/schema/src/com/cloud/host/dao/HostDaoImpl.java b/engine/schema/src/com/cloud/host/dao/HostDaoImpl.java index b373737ee2b..d0d3a5c394b 100755 --- a/engine/schema/src/com/cloud/host/dao/HostDaoImpl.java +++ b/engine/schema/src/com/cloud/host/dao/HostDaoImpl.java @@ -306,7 +306,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao AvailHypevisorInZone.done(); HostsInStatusSearch = createSearchBuilder(Long.class); - HostsInStatusSearch.selectField(HostsInStatusSearch.entity().getId()); + HostsInStatusSearch.selectFields(HostsInStatusSearch.entity().getId()); HostsInStatusSearch.and("dc", HostsInStatusSearch.entity().getDataCenterId(), Op.EQ); HostsInStatusSearch.and("pod", HostsInStatusSearch.entity().getPodId(), Op.EQ); HostsInStatusSearch.and("cluster", HostsInStatusSearch.entity().getClusterId(), Op.EQ); @@ -370,7 +370,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao HostsInClusterSearch.done(); HostIdSearch = createSearchBuilder(Long.class); - HostIdSearch.selectField(HostIdSearch.entity().getId()); + HostIdSearch.selectFields(HostIdSearch.entity().getId()); HostIdSearch.and("dataCenterId", HostIdSearch.entity().getDataCenterId(), Op.EQ); HostIdSearch.done(); diff --git a/engine/schema/src/com/cloud/network/dao/NetworkDaoImpl.java b/engine/schema/src/com/cloud/network/dao/NetworkDaoImpl.java index 1b2c2bf862f..23ccba9b03a 100644 --- a/engine/schema/src/com/cloud/network/dao/NetworkDaoImpl.java +++ b/engine/schema/src/com/cloud/network/dao/NetworkDaoImpl.java @@ -226,7 +226,7 @@ public class NetworkDaoImpl extends GenericDaoBase implements N OfferingAccountNetworkSearch.done(); GarbageCollectedSearch = createSearchBuilder(Long.class); - GarbageCollectedSearch.selectField(GarbageCollectedSearch.entity().getId()); + GarbageCollectedSearch.selectFields(GarbageCollectedSearch.entity().getId()); SearchBuilder join7 = _ntwkOpDao.createSearchBuilder(); join7.and("activenics", join7.entity().getActiveNicsCount(), Op.EQ); join7.and("gc", join7.entity().isGarbageCollected(), Op.EQ); diff --git a/engine/schema/src/com/cloud/network/dao/NetworkOpDaoImpl.java b/engine/schema/src/com/cloud/network/dao/NetworkOpDaoImpl.java index a3f54b78850..18e2c806910 100644 --- a/engine/schema/src/com/cloud/network/dao/NetworkOpDaoImpl.java +++ b/engine/schema/src/com/cloud/network/dao/NetworkOpDaoImpl.java @@ -39,7 +39,7 @@ public class NetworkOpDaoImpl extends GenericDaoBase implemen super(); ActiveNicsSearch = createSearchBuilder(Integer.class); - ActiveNicsSearch.selectField(ActiveNicsSearch.entity().getActiveNicsCount()); + ActiveNicsSearch.selectFields(ActiveNicsSearch.entity().getActiveNicsCount()); ActiveNicsSearch.and("network", ActiveNicsSearch.entity().getId(), Op.EQ); ActiveNicsSearch.done(); diff --git a/engine/schema/src/com/cloud/network/dao/NetworkServiceMapDaoImpl.java b/engine/schema/src/com/cloud/network/dao/NetworkServiceMapDaoImpl.java index 3cdd73885c8..a4ebd8c1b45 100644 --- a/engine/schema/src/com/cloud/network/dao/NetworkServiceMapDaoImpl.java +++ b/engine/schema/src/com/cloud/network/dao/NetworkServiceMapDaoImpl.java @@ -57,7 +57,7 @@ public class NetworkServiceMapDaoImpl extends GenericDaoBase super(); RouterNetworksSearch = createSearchBuilder(Long.class); - RouterNetworksSearch.selectField(RouterNetworksSearch.entity().getNetworkId()); + RouterNetworksSearch.selectFields(RouterNetworksSearch.entity().getNetworkId()); RouterNetworksSearch.and("routerId", RouterNetworksSearch.entity().getRouterId(), Op.EQ); RouterNetworksSearch.done(); diff --git a/engine/schema/src/com/cloud/network/security/dao/SecurityGroupVMMapDaoImpl.java b/engine/schema/src/com/cloud/network/security/dao/SecurityGroupVMMapDaoImpl.java index 46135d18029..26d5a42bc7b 100644 --- a/engine/schema/src/com/cloud/network/security/dao/SecurityGroupVMMapDaoImpl.java +++ b/engine/schema/src/com/cloud/network/security/dao/SecurityGroupVMMapDaoImpl.java @@ -59,7 +59,7 @@ public class SecurityGroupVMMapDaoImpl extends GenericDaoBase DistinctProvidersSearch = createSearchBuilder(String.class); DistinctProvidersSearch.and("vpcId", DistinctProvidersSearch.entity().getVpcId(), SearchCriteria.Op.EQ); DistinctProvidersSearch.and("provider", DistinctProvidersSearch.entity().getProvider(), SearchCriteria.Op.EQ); - DistinctProvidersSearch.selectField(DistinctProvidersSearch.entity().getProvider()); + DistinctProvidersSearch.selectFields(DistinctProvidersSearch.entity().getProvider()); DistinctProvidersSearch.done(); } diff --git a/engine/schema/src/com/cloud/offerings/dao/NetworkOfferingDaoImpl.java b/engine/schema/src/com/cloud/offerings/dao/NetworkOfferingDaoImpl.java index ef8237a48f5..84324734072 100644 --- a/engine/schema/src/com/cloud/offerings/dao/NetworkOfferingDaoImpl.java +++ b/engine/schema/src/com/cloud/offerings/dao/NetworkOfferingDaoImpl.java @@ -76,7 +76,7 @@ public class NetworkOfferingDaoImpl extends GenericDaoBase implements V super(); CurrentVersionSearch = createSearchBuilder(String.class); - CurrentVersionSearch.selectField(CurrentVersionSearch.entity().getVersion()); + CurrentVersionSearch.selectFields(CurrentVersionSearch.entity().getVersion()); CurrentVersionSearch.and("step", CurrentVersionSearch.entity().getStep(), Op.EQ); CurrentVersionSearch.done(); diff --git a/engine/schema/src/com/cloud/user/dao/AccountDaoImpl.java b/engine/schema/src/com/cloud/user/dao/AccountDaoImpl.java index aa67e86bf70..41f1f734732 100755 --- a/engine/schema/src/com/cloud/user/dao/AccountDaoImpl.java +++ b/engine/schema/src/com/cloud/user/dao/AccountDaoImpl.java @@ -96,7 +96,7 @@ public class AccountDaoImpl extends GenericDaoBase implements A NonProjectAccountSearch.done(); AccountIdsSearch = createSearchBuilder(Long.class); - AccountIdsSearch.selectField(AccountIdsSearch.entity().getId()); + AccountIdsSearch.selectFields(AccountIdsSearch.entity().getId()); AccountIdsSearch.and("ids", AccountIdsSearch.entity().getDomainId(), Op.IN); AccountIdsSearch.done(); } diff --git a/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java index 3a7dde78a6d..cf8e0a89c40 100644 --- a/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java +++ b/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java @@ -198,7 +198,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase implem HostNameSearch.done(); FindIdsOfVirtualRoutersByAccount = createSearchBuilder(Long.class); - FindIdsOfVirtualRoutersByAccount.selectField(FindIdsOfVirtualRoutersByAccount.entity().getId()); + FindIdsOfVirtualRoutersByAccount.selectFields(FindIdsOfVirtualRoutersByAccount.entity().getId()); FindIdsOfVirtualRoutersByAccount.and("account", FindIdsOfVirtualRoutersByAccount.entity().getAccountId(), SearchCriteria.Op.EQ); FindIdsOfVirtualRoutersByAccount.and("type", FindIdsOfVirtualRoutersByAccount.entity().getType(), SearchCriteria.Op.EQ); FindIdsOfVirtualRoutersByAccount.and("state", FindIdsOfVirtualRoutersByAccount.entity().getState(), SearchCriteria.Op.NIN); @@ -233,7 +233,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase implem nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ); DistinctHostNameSearch = createSearchBuilder(String.class); - DistinctHostNameSearch.selectField(DistinctHostNameSearch.entity().getHostName()); + DistinctHostNameSearch.selectFields(DistinctHostNameSearch.entity().getHostName()); DistinctHostNameSearch.and("types", DistinctHostNameSearch.entity().getType(), SearchCriteria.Op.IN); DistinctHostNameSearch.and("removed", DistinctHostNameSearch.entity().getRemoved(), SearchCriteria.Op.NULL); diff --git a/engine/schema/src/org/apache/cloudstack/affinity/dao/AffinityGroupVMMapDaoImpl.java b/engine/schema/src/org/apache/cloudstack/affinity/dao/AffinityGroupVMMapDaoImpl.java index 89dfa5aff45..ad8f4f0a4ea 100644 --- a/engine/schema/src/org/apache/cloudstack/affinity/dao/AffinityGroupVMMapDaoImpl.java +++ b/engine/schema/src/org/apache/cloudstack/affinity/dao/AffinityGroupVMMapDaoImpl.java @@ -56,7 +56,7 @@ public class AffinityGroupVMMapDaoImpl extends GenericDaoBase sc = SearchCriteria2.create(HostVO.class); sc.addAnd(sc.getEntity().getClusterId(), Op.EQ, store.getScope().getScopeId()); sc.addAnd(sc.getEntity().getStatus(), Op.EQ, Status.Up); - List hosts = sc.find(); + List hosts = sc.list(); for (HostVO host : hosts) { endPoints.add(RemoteHostEndPoint.getHypervisorHostEndPoint(host.getId(), host.getPrivateIpAddress(), host.getPublicIpAddress())); diff --git a/framework/db/src/com/cloud/utils/db/GenericDaoBase.java b/framework/db/src/com/cloud/utils/db/GenericDaoBase.java index fd561713feb..ad8c770f904 100755 --- a/framework/db/src/com/cloud/utils/db/GenericDaoBase.java +++ b/framework/db/src/com/cloud/utils/db/GenericDaoBase.java @@ -928,7 +928,6 @@ public abstract class GenericDaoBase extends Compone } @Override @DB(txn=false) - @SuppressWarnings("unchecked") public T findByUuid(final String uuid) { SearchCriteria sc = createSearchCriteria(); sc.addAnd("uuid", SearchCriteria.Op.EQ, uuid); @@ -936,7 +935,6 @@ public abstract class GenericDaoBase extends Compone } @Override @DB(txn=false) - @SuppressWarnings("unchecked") public T findByUuidIncludingRemoved(final String uuid) { SearchCriteria sc = createSearchCriteria(); sc.addAnd("uuid", SearchCriteria.Op.EQ, uuid); @@ -1044,7 +1042,7 @@ public abstract class GenericDaoBase extends Compone @DB(txn=false) protected List addGroupBy(final StringBuilder sql, SearchCriteria sc) { - Pair, List> groupBys = sc.getGroupBy(); + Pair, List> groupBys = sc.getGroupBy(); if (groupBys != null) { groupBys.first().toSql(sql); return groupBys.second(); @@ -1368,7 +1366,7 @@ public abstract class GenericDaoBase extends Compone Object obj = entry.getValue(); EcInfo ec = (EcInfo)attr.attache; - Enumeration en = null; + Enumeration en = null; if (ec.rawClass == null) { en = Collections.enumeration(Arrays.asList((Object[])obj)); } else { diff --git a/framework/db/src/com/cloud/utils/db/GenericSearchBuilder.java b/framework/db/src/com/cloud/utils/db/GenericSearchBuilder.java index 732527aa30a..16c77f1205e 100755 --- a/framework/db/src/com/cloud/utils/db/GenericSearchBuilder.java +++ b/framework/db/src/com/cloud/utils/db/GenericSearchBuilder.java @@ -19,14 +19,9 @@ package com.cloud.utils.db; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; -import java.util.List; -import java.util.UUID; - -import net.sf.cglib.proxy.Factory; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.SearchCriteria.SelectType; /** * GenericSearchBuilder is used to build a search based on a VO object @@ -37,34 +32,22 @@ import com.cloud.utils.db.SearchCriteria.SelectType; * @param Result object that should contain the results. */ public class GenericSearchBuilder extends SearchBase { - @SuppressWarnings("unchecked") protected GenericSearchBuilder(Class entityType, Class resultType) { super(entityType, resultType); } - public T entity() { - return _entity; - } - - protected Attribute getSpecifiedAttribute() { - if (_entity == null || _specifiedAttrs == null || _specifiedAttrs.size() != 1) { - throw new RuntimeException("Now now, better specify an attribute or else we can't help you"); - } - return _specifiedAttrs.get(0); - } - - public GenericSearchBuilder selectField(Object... useless) { + public GenericSearchBuilder selectFields(Object... useless) { if (_entity == null) { throw new RuntimeException("SearchBuilder cannot be modified once it has been setup"); } if (_specifiedAttrs.size() <= 0) { throw new RuntimeException("You didn't specify any attributes"); } - + if (_selects == null) { _selects = new ArrayList _selects; + protected GroupBy, T, K> _groupBy = null; + protected SelectType _selectType; + T _entity; + + SearchBase(Class entityType, Class resultType) { + _dao = (GenericDaoBase)GenericDaoBase.getDao(entityType); + if (_dao == null) { + throw new CloudRuntimeException("Unable to find DAO for " + entityType); + } + + _entityBeanType = entityType; + _resultType = resultType; + _attrs = _dao.getAllAttributes(); + + _entity = _dao.createSearchEntity(new Interceptor()); + _conditions = new ArrayList(); + _joins = null; + _specifiedAttrs = new ArrayList(); + } + + public SelectType getSelectType() { + return _selectType; + } + + protected void set(String name) { + Attribute attr = _attrs.get(name); + assert (attr != null) : "Searching for a field that's not there: " + name; + _specifiedAttrs.add(attr); + } + + public T entity() { + return _entity; + } + + protected Attribute getSpecifiedAttribute() { + if (_entity == null || _specifiedAttrs == null || _specifiedAttrs.size() != 1) { + throw new RuntimeException("Now now, better specify an attribute or else we can't help you"); + } + return _specifiedAttrs.get(0); + } + + protected List getSpecifiedAttributes() { + return _specifiedAttrs; + } + + protected Condition constructCondition(String conditionName, String cond, Attribute attr, Op op) { + assert _entity != null : "SearchBuilder cannot be modified once it has been setup"; + assert op == null || _specifiedAttrs.size() == 1 : "You didn't select the attribute."; + assert op != Op.SC : "Call join"; + + Condition condition = new Condition(conditionName, cond, attr, op); + _conditions.add(condition); + _specifiedAttrs.clear(); + return condition; + } + + /** + * creates the SearchCriteria so the actual values can be filled in. + * + * @return SearchCriteria + */ + public SearchCriteria create() { + if (_entity != null) { + finalize(); + } + return new SearchCriteria(this); + } + + /** + * Marks the SearchBuilder as completed in building the search conditions. + */ + @Override + public synchronized void finalize() { + if (_entity != null) { + Factory factory = (Factory)_entity; + factory.setCallback(0, null); + _entity = null; + } + + if (_joins != null) { + for (JoinBuilder> join : _joins.values()) { + join.getT().finalize(); + } + } + + if (_selects == null || _selects.size() == 0) { + _selectType = SelectType.Entity; + assert _entityBeanType.equals(_resultType) : "Expecting " + _entityBeanType + " because you didn't specify any selects but instead got " + _resultType; + return; + } + + for (Select select : _selects) { + if (select.field == null) { + assert (_selects.size() == 1) : "You didn't specify any fields to put the result in but you're specifying more than one select so where should I put the selects?"; + _selectType = SelectType.Single; + return; + } + if (select.func != null) { + _selectType = SelectType.Result; + return; + } + } + + _selectType = SelectType.Fields; + } + + protected static class Condition { + protected final String name; + protected final String cond; + protected final Op op; + protected final Attribute attr; + protected Object[] presets; + + protected Condition(String name) { + this(name, null, null, null); + } + + public Condition(String name, String cond, Attribute attr, Op op) { + this.name = name; + this.attr = attr; + this.cond = cond; + this.op = op; + this.presets = null; + } + + public boolean isPreset() { + return presets != null; + } + + public void setPresets(Object... presets) { + this.presets = presets; + } + + public Object[] getPresets() { + return presets; + } + + public void toSql(StringBuilder sql, Object[] params, int count) { + if (count > 0) { + sql.append(cond); + } + + if (op == null) { + return; + } + + if (op == Op.SC) { + sql.append(" (").append(((SearchCriteria)params[0]).getWhereClause()).append(") "); + return; + } + + if (attr == null) { + return; + } + + sql.append(attr.table).append(".").append(attr.columnName).append(op.toString()); + if (op == Op.IN && params.length == 1) { + sql.delete(sql.length() - op.toString().length(), sql.length()); + sql.append("=?"); + } else if (op == Op.NIN && params.length == 1) { + sql.delete(sql.length() - op.toString().length(), sql.length()); + sql.append("!=?"); + } else if (op.getParams() == -1) { + for (int i = 0; i < params.length; i++) { + sql.insert(sql.length() - 2, "?,"); + } + sql.delete(sql.length() - 3, sql.length() - 2); // remove the last , + } else if (op == Op.EQ && (params == null || params.length == 0 || params[0] == null)) { + sql.delete(sql.length() - 4, sql.length()); + sql.append(" IS NULL "); + } else if (op == Op.NEQ && (params == null || params.length == 0 || params[0] == null)) { + sql.delete(sql.length() - 5, sql.length()); + sql.append(" IS NOT NULL "); + } else { + if ((op.getParams() != 0 || params != null) && (params.length != op.getParams())) { + throw new RuntimeException("Problem with condition: " + name); + } + } + } + + @Override + public int hashCode() { + return name.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Condition)) { + return false; + } + + Condition condition = (Condition)obj; + return name.equals(condition.name); + } + } + + protected static class Select { + public Func func; + public Attribute attr; + public Object[] params; + public Field field; + + protected Select() { + } + + public Select(Func func, Attribute attr, Field field, Object[] params) { + this.func = func; + this.attr = attr; + this.params = params; + this.field = field; + } + } + + protected class Interceptor implements MethodInterceptor { + @Override + public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { + String name = method.getName(); + if (method.getAnnotation(Transient.class) == null) { + if (name.startsWith("get")) { + String fieldName = Character.toLowerCase(name.charAt(3)) + name.substring(4); + set(fieldName); + return null; + } else if (name.startsWith("is")) { + String fieldName = Character.toLowerCase(name.charAt(2)) + name.substring(3); + set(fieldName); + return null; + } else { + Column ann = method.getAnnotation(Column.class); + if (ann != null) { + String colName = ann.name(); + for (Map.Entry attr : _attrs.entrySet()) { + if (colName.equals(attr.getValue().columnName)) { + set(attr.getKey()); + return null; + } + } + } + throw new RuntimeException("Perhaps you need to make the method start with get or is: " + method); + } + } + return methodProxy.invokeSuper(object, args); + } + + } +} \ No newline at end of file diff --git a/framework/db/src/com/cloud/utils/db/SearchBuilder.java b/framework/db/src/com/cloud/utils/db/SearchBuilder.java index 8410b01e5d9..a56ec1bd74b 100755 --- a/framework/db/src/com/cloud/utils/db/SearchBuilder.java +++ b/framework/db/src/com/cloud/utils/db/SearchBuilder.java @@ -56,7 +56,6 @@ package com.cloud.utils.db; */ public class SearchBuilder extends GenericSearchBuilder { - @SuppressWarnings("unchecked") public SearchBuilder(Class entityType) { super(entityType, entityType); } diff --git a/framework/db/src/com/cloud/utils/db/SearchCriteria.java b/framework/db/src/com/cloud/utils/db/SearchCriteria.java index ba4a0fd49ad..a1f6408744c 100755 --- a/framework/db/src/com/cloud/utils/db/SearchCriteria.java +++ b/framework/db/src/com/cloud/utils/db/SearchCriteria.java @@ -113,12 +113,13 @@ public class SearchCriteria { private int _counter; private HashMap>> _joins; private final ArrayList selects, SelectType selectType, Class resultType, HashMap params) { + protected SearchCriteria(Map attrs, ArrayList conditions, ArrayList