Example usage for org.hibernate Query setParameterList

List of usage examples for org.hibernate Query setParameterList

Introduction

In this page you can find the example usage for org.hibernate Query setParameterList.

Prototype

Query<R> setParameterList(int position, Object[] values);

Source Link

Usage

From source file:com.abiquo.server.core.enterprise.UserDAO.java

License:Open Source License

@SuppressWarnings("rawtypes")
public boolean isUserAllowedToEnterprise(final String username, final String authtype,
        final String[] privileges, final Integer ident) {

    Query query = getSession().createSQLQuery(USER_ALLOWED_ENTERPRSE_SQL);
    query.setParameter("username", username);
    query.setParameter("authtype", authtype);
    query.setParameterList("privileges", privileges);
    query.setParameter("identerprise", ident);
    List result = query.list();/*ww w.ja  va 2 s . c o m*/

    if (result == null || result.isEmpty()) {
        return false;
    } else {
        return true;
    }
}

From source file:com.abiquo.server.core.infrastructure.MachineDAO.java

License:Open Source License

public List<Machine> findCandidateMachines(final Integer idRack, final Integer idVirtualDatacenter,
        final Long hdRequiredOnDatastore, final Enterprise enterprise) {

    List<Machine> machines;

    if (enterprise.getIsReservationRestricted()) {
        machines = findFirstCandidateMachinesReservedRestricted(idRack, idVirtualDatacenter,
                hdRequiredOnDatastore, enterprise);
    } else {/*from   www  . jav a 2 s  .c  om*/
        machines = findFirstCandidateMachines(idRack, idVirtualDatacenter, hdRequiredOnDatastore, enterprise);
    }

    // StringBuilder sbcandidates = new StringBuilder();
    List<Integer> candidatesids = new LinkedList<Integer>();
    for (Machine m : machines) {
        candidatesids.add(m.getId());
    }

    // with datastore
    Query datastoreQuery = getSession().createQuery(QUERY_CANDIDATE_DATASTORE);
    datastoreQuery.setLong("hdRequiredOnRepository", hdRequiredOnDatastore);
    datastoreQuery.setParameterList("candidates", candidatesids);

    List<Integer> includedIds = datastoreQuery.list();

    if (includedIds.size() == 0) {
        throw new PersistenceException(String.format(
                "There isn't any machine with the required datastore capacity [%d]", hdRequiredOnDatastore));
    }

    // execute the enterprise exclusion rule
    Query excludedQuery = getSession().createQuery(QUERY_CANDIDATE_NO_ENTERPRISE_EXCLUDED);
    excludedQuery.setParameter("enterpriseId", enterprise.getId());
    List<Integer> excludedMachineIds = excludedQuery.list();

    List<Machine> notExcludedMachines = new LinkedList<Machine>();

    for (Machine m : machines) {
        Integer machineId = m.getId();

        if (!excludedMachineIds.contains(machineId) && includedIds.contains(machineId)) {
            notExcludedMachines.add(m);
        }
    }

    if (notExcludedMachines.size() == 0) {
        throw new PersistenceException("All the candiate machines are excluded by other enterprsies "
                + "with virtual machines deployed on it. Please check the enterprise affinity rules.");
    }

    return notExcludedMachines;
}

From source file:com.abiquo.server.core.infrastructure.MachineDAO.java

License:Open Source License

private List<Machine> findFirstCandidateMachinesReservedRestricted(final Integer idRack,
        final Integer idVirtualDatacenter, final Long hdRequiredOnDatastore, final Enterprise enterprise) {

    List<Machine> machines = null;

    List<Machine> reservMachines = findReservedMachines(enterprise);

    if (reservMachines != null && reservMachines.size() != 0) {
        List<Integer> reserveds = new LinkedList<Integer>();
        for (Machine m : reservMachines) {
            reserveds.add(m.getId());/*from w w  w  . j a va  2 s.  co m*/
        }

        Query query = getSession().createQuery(QUERY_CANDIDATE_MACHINES_RESERVED);
        query.setInteger("idVirtualDataCenter", idVirtualDatacenter);
        query.setInteger("idRack", idRack);
        query.setParameter("state", MachineState.MANAGED);
        query.setParameterList("reserveds", reserveds);

        machines = query.list();

        if (machines == null || machines.size() == 0) {
            whyNotCandidateMachines(idRack, idVirtualDatacenter, hdRequiredOnDatastore, enterprise, reserveds);
        }

        return machines;
    } else {
        final String msg = String.format(
                "Enterprise works in restricted reserved machines mode but no machine is reserved. Current enterprise: %s",
                enterprise.getName());

        throw new PersistenceException(msg);
    }
}

From source file:com.abiquo.server.core.infrastructure.MachineDAO.java

License:Open Source License

protected List<Machine> findFirstCandidateMachinesReservedRestrictedHAExclude(final Integer idRack,
        final Integer idVirtualDatacenter, final Enterprise enterprise, final Integer originalHypervisorId) {

    List<Machine> machines = null;

    List<Machine> reservMachines = findReservedMachines(enterprise);

    if (reservMachines != null && reservMachines.size() != 0) {
        List<Integer> reserveds = new LinkedList<Integer>();
        for (Machine m : reservMachines) {
            reserveds.add(m.getId());//from w w w.  j av  a  2 s.co  m
        }

        Query query = getSession().createQuery(QUERY_CANDIDATE_MACHINES_RESERVED_HA_EXCLUDE_ORIGINAL);
        query.setInteger("idVirtualDataCenter", idVirtualDatacenter);
        query.setInteger("idRack", idRack);
        query.setParameter("state", MachineState.MANAGED);
        query.setParameterList("reserveds", reserveds);
        query.setInteger("enterpriseId", enterprise.getId());
        query.setInteger("originalHypervisorId", originalHypervisorId);

        machines = query.list();

        if (machines == null || machines.size() == 0) {
            whyNotCandidateMachines(idRack, idVirtualDatacenter, 0l, enterprise, reserveds);
        }

        return machines;
    } else {
        final String msg = String.format(
                "Enterprise works in restricted reserved machines mode but no machine is reserved. Current enterprise: %s",
                enterprise.getName());

        throw new PersistenceException(msg);
    }

}

From source file:com.abiquo.server.core.infrastructure.MachineDAO.java

License:Open Source License

/**
 * Do not require additional space on the datastore. Used during HA, selects a machine different
 * of the ''originalHypervisorId'' with the same ''datastoreUuid'' enabled.
 *///from   w ww  . j a va  2  s.  c  o  m
public List<Machine> findCandidateMachines(final Integer idRack, final Integer idVirtualDatacenter,
        final Enterprise enterprise, final String datastoreUuid, final Integer originalHypervisorId) {

    List<Machine> machines = null;
    if (enterprise.getIsReservationRestricted()) {
        machines = findFirstCandidateMachinesReservedRestrictedHAExclude(idRack, idVirtualDatacenter,
                enterprise, originalHypervisorId);
    }

    else {
        Query query = getSession().createQuery(QUERY_CANDIDATE_MACHINES_HA_EXCLUDE_ORIGINAL);
        query.setInteger("idVirtualDataCenter", idVirtualDatacenter);
        query.setInteger("idRack", idRack);
        query.setParameter("state", MachineState.MANAGED);
        query.setParameter("enterpriseId", enterprise.getId());
        query.setParameter("originalHypervisorId", originalHypervisorId);

        machines = query.list();
    }

    if (machines == null || machines.size() == 0) {
        Query query = getSession().createQuery(QUERY_CANDIDATE_MACHINES);
        query.setInteger("idVirtualDataCenter", idVirtualDatacenter);
        query.setInteger("idRack", idRack);
        query.setParameter("state", MachineState.MANAGED);
        query.setParameter("enterpriseId", enterprise.getId());

        machines = query.list();

        if (machines == null || machines.size() == 0) {
            throw new PersistenceException(String.format(
                    "There isn't any MANAGED machine on the required rack [%d] and virtual datacenter [%d] available for the current enterpirse [%s]. "
                            + "Pleas check the machine reservation policies.",
                    idRack, idVirtualDatacenter, enterprise.getName()));
        } else {
            throw new PersistenceException(String.format(
                    "The only MANAGED machine on the required rack [%d] and virtual datacenter [%d] available for the current enterpirse [%s]"
                            + "is the target of the high availability (so can't be used) ",
                    idRack, idVirtualDatacenter, enterprise.getName()));
        }
    }

    // StringBuilder sbcandidates = new StringBuilder();
    List<Integer> candidatesids = new LinkedList<Integer>();
    for (Machine m : machines) {
        candidatesids.add(m.getId());
    }

    // with datastore
    Query datastoreQuery = getSession().createQuery(QUERY_CANDIDATE_DATASTORE_HA_DATASTOREUUID);
    datastoreQuery.setParameterList("candidates", candidatesids);
    datastoreQuery.setParameter("datastoreUuid", datastoreUuid);

    List<Integer> includedIds = datastoreQuery.list();

    if (includedIds.size() == 0) {
        throw new PersistenceException(String
                .format("There isn't any machine with the required shared datastore [%s]", datastoreUuid));
    }

    // execute the enterprise exclusion rule
    Query excludedQuery = getSession().createQuery(QUERY_CANDIDATE_NO_ENTERPRISE_EXCLUDED);
    excludedQuery.setParameter("enterpriseId", enterprise.getId());
    List<Integer> excludedMachineIds = excludedQuery.list();

    List<Machine> notExcludedMachines = new LinkedList<Machine>();

    for (Machine m : machines) {
        Integer machineId = m.getId();

        if (!excludedMachineIds.contains(machineId) && includedIds.contains(machineId)) {
            notExcludedMachines.add(m);
        }
    }

    if (notExcludedMachines.size() == 0) {
        throw new PersistenceException("All the candiate machines are excluded by other enterprsies "
                + "with virtual machines deployed on it. Please check the enterprise affinity rules.");
    }

    return notExcludedMachines;
}

From source file:com.abiquo.server.core.infrastructure.MachineDAO.java

License:Open Source License

private void whyNotCandidateMachines(final Integer idRack, final Integer idVirtualDatacenter,
        final Long hdRequiredOnDatastore, final Enterprise enterprise, final List<Integer> reserveds)
        throws PersistenceException {

    if (reserveds != null) {

        StringBuilder reservedMachinesB = new StringBuilder(
                String.format("Enterprise %s has the following machine reservations : ", enterprise.getName()));
        for (Integer mid : reserveds) {
            reservedMachinesB.append(mid + ' ');
        }//from w w  w  . j  a  v a 2s  .  com

        /**
         * rack and hypervisor type
         */
        Query query1 = getSession().createQuery(WHY_QUERY_CANDIDATE_SAME_VDC_RACK_AND_TYPE_AND_RESERVED);
        query1.setInteger("idVirtualDataCenter", idVirtualDatacenter);
        query1.setInteger("idRack", idRack);
        query1.setParameterList("reserveds", reserveds);

        List<Integer> query1res = query1.list();

        if (query1res.size() == 0) {
            throw new PersistenceException(String.format(
                    "%s\nThere isn't any machine on the required rack [%d] and virtual datacenter [%d]. "
                            + "Please check the racks and hypervisor technology on the infrastructure.",
                    reservedMachinesB.toString(), idRack, idVirtualDatacenter));
        }

        /**
         * rack, hypervisor type and managed state
         */
        Query query2 = getSession().createQuery(QUERY_CANDIDATE_MACHINES_RESERVED);
        query2.setInteger("idVirtualDataCenter", idVirtualDatacenter);
        query2.setInteger("idRack", idRack);
        query2.setParameter("state", MachineState.MANAGED);
        query2.setParameterList("reserveds", reserveds);

        List<Integer> query2res = query2.list();

        if (query2res.size() == 0) {
            throw new PersistenceException(String.format(
                    "%s\nThere isn't any MANAGED machine on the required rack [%d] and virtual datacenter [%d]. "
                            + "Please check the machine health on the infrastructure.",
                    reservedMachinesB.toString(), idRack, idVirtualDatacenter));
        }

        /**
         * rack, hypervisor type, managed state, enterprise reservation and datastore capacity.
         */
        throw new PersistenceException(
                String.format("%s\nThere isn't any machine with the required datastore capacity [%d]",
                        reservedMachinesB.toString(), hdRequiredOnDatastore));
    } // reserved machines
    else {
        /**
         * rack and hypervisor type
         */
        Query query1 = getSession().createQuery(WHY_QUERY_CANDIDATE_SAME_VDC_RACK_AND_TYPE);
        query1.setInteger("idVirtualDataCenter", idVirtualDatacenter);
        query1.setInteger("idRack", idRack);

        List<Integer> query1res = query1.list();

        if (query1res.size() == 0) {
            throw new PersistenceException(String.format(
                    "There isn't any machine on the required rack [%d] and virtual datacenter [%d]. "
                            + "Please check the racks and hypervisor technology on the infrastructure.",
                    idRack, idVirtualDatacenter));
        }

        /**
         * rack, hypervisor type and managed state
         */
        Query query2 = getSession().createQuery(WHT_QUERY_CANDIDATE_SAME_VDC_RACK_AND_TYPE_AND_STATE);
        query2.setInteger("idVirtualDataCenter", idVirtualDatacenter);
        query2.setInteger("idRack", idRack);
        query2.setParameter("state", MachineState.MANAGED);

        List<Integer> query2res = query2.list();

        if (query2res.size() == 0) {
            throw new PersistenceException(String.format(
                    "There isn't any MANAGED machine on the required rack [%d] and virtual datacenter [%d]. "
                            + "Please check the machine health on the infrastructure.",
                    idRack, idVirtualDatacenter));
        }

        /**
         * rack, hypervisor type, managed state and enterprise reservation
         */
        Query query3 = getSession().createQuery(QUERY_CANDIDATE_MACHINES);
        query3.setInteger("idVirtualDataCenter", idVirtualDatacenter);
        query3.setInteger("idRack", idRack);
        query3.setParameter("state", MachineState.MANAGED);
        query3.setParameter("enterpriseId", enterprise.getId());

        List<Integer> query3res = query3.list();

        if (query3res.size() == 0) {
            throw new PersistenceException(String.format(
                    "There isn't any MANAGED machine on the required rack [%d] and virtual datacenter [%d] available for the current enterpirse [%s]. "
                            + "Pleas check the machine reservation policies.",
                    idRack, idVirtualDatacenter, enterprise.getName()));
        }

        /**
         * rack, hypervisor type, managed state, enterprise reservation and datastore capacity.
         */
        throw new PersistenceException(String.format(
                "There isn't any machine with the required datastore capacity [%d]", hdRequiredOnDatastore));
    }
}

From source file:com.abiquo.server.core.infrastructure.MachineDAO.java

License:Open Source License

/**
 * returns machien ids form selected datacenters
 *//*  www  .j ava  2  s.c  o m*/
public List<Integer> findAllIdsInDatacenters(final Datacenter... datacenters) {
    Query query = getSession().createQuery(QUERY_ALL_IDS_IN);
    query.setParameterList("datacenters", datacenters);
    return query.list();
}

From source file:com.abiquo.server.core.infrastructure.network.IpPoolManagementDAO.java

License:Open Source License

/**
 * Find next IpPoolManagement created and available by a vLAN with filter options
 * // w  w  w .j a  v a 2  s.c om
 * @param vlanId identifier of the vlan.
 * @param excludedIps ips excluded from result if exists
 * @return next available IP address.
 */
public IpPoolManagement findNextIpAvailable(final Integer vlanId, final String... excludedIps) {
    // Get the query that counts the total results.
    Query query = getSession()
            .createQuery(BY_VLAN_NEXT_AVAILABLE + defineOrderBy(IpPoolManagement.OrderByEnum.IP, Boolean.TRUE));
    query.setMaxResults(1);
    query.setParameter("vlan_id", vlanId);
    // query.setLockMode("next_ip", LockMode.PESSIMISTIC_WRITE);
    if (excludedIps != null && excludedIps.length != 0) {
        query.setParameterList("excludedIp", Arrays.asList(excludedIps));
    } else {
        query.setString("excludedIp", null);
    }
    List<IpPoolManagement> result = query.list();

    return result.isEmpty() ? null : result.get(0);
}

From source file:com.abiquo.server.core.infrastructure.network.IpPoolManagementDAO.java

License:Open Source License

/**
 * Find next IpPoolManagement created and available by a vLAN with filter options
 * /*w  w  w  . j av  a2s  . com*/
 * @param vlanId identifier of the vlan.
 * @param excludedIps ips excluded from result if exists
 * @return next available IP address.
 */
public IpPoolManagement findNextExternalIpAvailable(final Integer vlanId, final String... excludedIps) {
    // Get the query that counts the total results.
    Query query = getSession().createQuery(
            BY_VLAN_NEXT_EXTERNAL_IP_AVAILABLE + defineOrderBy(IpPoolManagement.OrderByEnum.IP, Boolean.TRUE));
    query.setMaxResults(1);
    query.setParameter("vlan_id", vlanId);
    // query.setLockMode("next_ip", LockMode.PESSIMISTIC_WRITE);
    if (excludedIps != null && excludedIps.length != 0) {
        query.setParameterList("excludedIp", Arrays.asList(excludedIps));
    } else {
        query.setString("excludedIp", null);
    }
    List<IpPoolManagement> result = query.list();

    return result.isEmpty() ? null : result.get(0);
}

From source file:com.abiquo.server.core.scheduler.MachineLoadRuleDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<MachineLoadRule> findCandidateMachineLoadRules(
        final Collection<Machine> firstPassCandidateMachines) {
    Query query = getSession().createQuery(CANDIDATE_MACHINE_RULES);
    query.setParameterList("machines", firstPassCandidateMachines);

    return query.list();
}