Example usage for org.hibernate Criteria setProjection

List of usage examples for org.hibernate Criteria setProjection

Introduction

In this page you can find the example usage for org.hibernate Criteria setProjection.

Prototype

public Criteria setProjection(Projection projection);

Source Link

Document

Used to specify that the query results will be a projection (scalar in nature).

Usage

From source file:com.dalamar.model.LateTrainDaoImpl.java

public List executeQuery() {
    Session session = this.sessionFactory.openSession();
    Criteria c = session.createCriteria(LateTrain.class);

    for (Criterion cr : expressions)
        c.add(cr);//from  ww w  .j av a2s. c o  m
    for (Projection p : projections)
        c.setProjection(p);

    List<LateTrain> trainList = c.list();
    session.close();
    return trainList;
}

From source file:com.dell.asm.asmcore.asmmanager.db.DeviceConfigureDAO.java

License:Open Source License

/**
 * Helper method to get total number of records with filter parameters
 * /*from w  w  w. jav  a2s.  co  m*/
 * @param filterInfos
 *            - List for holding filtering information parsed from filter query parameter value
 * @return int - total number of records
 */
public Integer getTotalRecords(List<FilterParamParser.FilterInfo> filterInfos) {
    long totalRecords = 0;
    Session session = null;
    Transaction tx = null;
    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(DeviceConfigureEntity.class);
        BaseDAO.addFilterCriteria(criteria, filterInfos, DeviceConfigureEntity.class);
        totalRecords = (long) criteria.setProjection(Projections.rowCount()).uniqueResult();

        tx.commit();
    } catch (Exception e) {
        if (tx != null)
            try {
                tx.rollback();
            } catch (Exception e2) {
                logger.warn("Error during rollback", e2);
            }
    } finally {
        try {
            if (session != null)
                session.close();
        } catch (Exception e2) {
            logger.warn("Error during session close", e2);
        }
    }

    return (int) totalRecords;
}

From source file:com.dell.asm.asmcore.asmmanager.db.DeviceDiscoverDAO.java

License:Open Source License

/**
 * Helper method to get total number of records with filter parameters
 * //from  ww w. java 2  s . c  o  m
 * @param filterInfos
 *            - List for holding filtering information parsed from filter query parameter value
 * @return int - total number of records
 */
public Integer getTotalRecords(List<FilterParamParser.FilterInfo> filterInfos) {
    long totalRecords = 0;
    Session session = null;
    Transaction tx = null;
    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(DeviceDiscoverEntity.class);
        BaseDAO.addFilterCriteria(criteria, filterInfos, DeviceDiscoverEntity.class);
        totalRecords = (long) criteria.setProjection(Projections.rowCount()).uniqueResult();

        tx.commit();
    } catch (Exception e) {
        if (tx != null)
            try {
                tx.rollback();
            } catch (Exception e2) {
                logger.warn("Error during rollback", e2);
            }
    } finally {
        try {
            if (session != null)
                session.close();
        } catch (Exception e2) {
            logger.warn("Error during session close", e2);
        }
    }

    return (int) totalRecords;
}

From source file:com.dell.asm.asmcore.asmmanager.db.DeviceGroupDAO.java

License:Open Source License

/**
 * Helper method to get total number of records with filter parameters
 * //  ww w  .  j a va  2 s .  com
 * @param filterInfos
 *            - List for holding filtering information parsed from filter query parameter value
 * @return int - total number of records
 */
public Integer getTotalRecords(List<FilterParamParser.FilterInfo> filterInfos) {
    long totalRecords = 0;
    Session session = null;
    Transaction tx = null;
    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(DeviceGroupEntity.class);
        if (filterInfos != null) {
            addFilterCriteria(criteria, filterInfos, DeviceGroupEntity.class);
        }
        totalRecords = (long) criteria.setProjection(Projections.rowCount()).uniqueResult();

        tx.commit();
    } catch (Exception e) {
        if (tx != null)
            try {
                tx.rollback();
            } catch (Exception e2) {
                logger.warn("Error during rollback", e2);
            }
    } finally {
        try {
            if (session != null)
                session.close();
        } catch (Exception e2) {
            logger.warn("Error during session close", e2);
        }
    }

    return (int) totalRecords;
}

From source file:com.dell.asm.asmcore.asmmanager.db.DeviceInventoryDAO.java

License:Open Source License

/**
 * Helper method to get total number of records with filter parameters
 *
 * @param filterInfos - List for holding filtering information parsed from filter query parameter value
 * @return int - total number of records
 *///from  w w  w  .  ja va 2  s  .  co m
public Integer getTotalRecords(List<FilterParamParser.FilterInfo> filterInfos) {
    long totalRecords = 0;
    Session session = null;
    Transaction tx = null;
    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(DeviceInventoryEntity.class);

        final List<FilterParamParser.FilterInfo> notFound = BaseDAO.addFilterCriteria(criteria, filterInfos,
                DeviceInventoryEntity.class);
        if (CollectionUtils.isNotEmpty(notFound)) {

            for (FilterParamParser.FilterInfo filterInfo : notFound) {
                // if this is a filter by service then we need to get the subset of ref ids for devices
                // only in the deployment id subset passed
                if (StringUtils.equals(SERVICE_FILTER_PARAM, filterInfo.getColumnName())) {
                    final List<String> refIds = getRefIdsOfDevicesByDeploymentIds(filterInfo.getColumnValue());
                    criteria.add(Restrictions.in("refId", refIds));
                } else if (StringUtils.equals(SERVERPOOL_FILTER_PARAM, filterInfo.getColumnName())) {
                    if (filterInfo.getColumnValue()
                            .contains(ServiceTemplateSettingIDs.SERVICE_TEMPLATE_SERVER_POOL_GLOBAL_ID)) {
                        final List<String> refIds = getRefIdsOfDevicesByGroupIds(filterInfo.getColumnValue());
                        if (refIds.isEmpty()) {
                            criteria.add(Restrictions.sqlRestriction("(1=0)"));
                        } else {
                            criteria.add(Restrictions.not(Restrictions.in("refId", refIds)));
                        }
                    } else {
                        final List<String> refIds = getRefIdsOfDevicesByGroupIds(filterInfo.getColumnValue());
                        if (refIds.isEmpty()) {
                            criteria.add(Restrictions.sqlRestriction("(1=0)"));
                        } else {
                            criteria.add(Restrictions.in("refId", refIds));
                        }
                    }
                } else {
                    criteria.createAlias("deviceInventoryEntity", "deviceInventoryEntityAlias");
                    criteria.add(Restrictions.eq("deviceInventoryEntityAlias.deviceKey",
                            filterInfo.getColumnName()));
                    if (filterInfo.getColumnValue().size() == 1) {
                        criteria.add(Restrictions.eq("deviceInventoryEntityAlias.deviceValue",
                                filterInfo.getColumnValue().get(0)));
                    } else if (filterInfo.getColumnValue().size() > 1) {
                        criteria.add(Restrictions.in("deviceInventoryEntityAlias.deviceValue",
                                filterInfo.getColumnValue()));
                    }
                }
            }
        }

        totalRecords = (long) criteria.setProjection(Projections.rowCount()).uniqueResult();

        tx.commit();
    } catch (Exception e) {
        logger.error("Exception while getting getTotalRecords", e);
        if (tx != null)
            try {
                tx.rollback();
            } catch (Exception e2) {
                logger.warn("Error during rollback", e2);
            }
    } finally {
        try {
            if (session != null)
                session.close();
        } catch (Exception e2) {
            logger.warn("Error during session close", e2);
        }
    }

    return (int) totalRecords;
}

From source file:com.dell.asm.asmcore.asmmanager.db.DiscoveryResultDAO.java

License:Open Source License

/**
 * Helper method to get total number of records with filter parameters
 * @param filterInfos - List for holding filtering information parsed from filter query parameter value
 * @return int - total number of records
 *//* www  . jav  a 2s . co m*/
public Integer getTotalRecords(List<FilterParamParser.FilterInfo> filterInfos) {
    long totalRecords = 0;
    Session session = null;
    Transaction tx = null;
    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(DiscoveryResultEntity.class);
        BaseDAO.addFilterCriteria(criteria, filterInfos, DiscoveryResultEntity.class);
        totalRecords = (long) criteria.setProjection(Projections.rowCount()).uniqueResult();

        tx.commit();
    } catch (Exception e) {
        if (tx != null)
            try {
                tx.rollback();
            } catch (Exception e2) {
                logger.warn("Error during rollback", e2);
            }
    } finally {
        try {
            if (session != null)
                session.close();
        } catch (Exception e2) {
            logger.warn("Error during session close", e2);
        }
    }

    return (int) totalRecords;
}

From source file:com.dell.asm.asmcore.asmmanager.db.ServiceTemplateDAO.java

License:Open Source License

/**
 * Helper method to get total number of records with filter parameters
 *
 * @param filterInfos - List for holding filtering information parsed from filter query parameter value
 * @return int - total number of records
 *//*from ww w . j  a  v a 2  s.c  om*/
public Integer getTotalRecords(List<FilterParamParser.FilterInfo> filterInfos) {
    long totalRecords = 0;
    Session session = null;
    Transaction tx = null;
    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(ServiceTemplateEntity.class);
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        BaseDAO.addFilterCriteria(criteria, filterInfos, ServiceTemplateEntity.class);
        totalRecords = (long) criteria.setProjection(Projections.rowCount()).uniqueResult();

        tx.commit();
    } catch (Exception e) {
        if (tx != null)
            try {
                tx.rollback();
            } catch (Exception e2) {
                logger.warn("Error during rollback", e2);
            }
    } finally {
        try {
            if (session != null)
                session.close();
        } catch (Exception e2) {
            logger.warn("Error during session close", e2);
        }
    }

    logger.warn("Template DAO getTotalRecords size: " + totalRecords);
    return (int) totalRecords;
}

From source file:com.dell.asm.asmcore.asmmanager.db.TemplateDAO.java

License:Open Source License

/**
 * Helper method to get total number of records with filter parameters
 *
 * @param filterInfos - List for holding filtering information parsed from filter query parameter value
 * @return int - total number of records
 */// w w w.ja va2 s.  c  o  m
public Integer getTotalRecords(List<FilterParamParser.FilterInfo> filterInfos) {
    long totalRecords = 0;
    Session session = null;
    Transaction tx = null;
    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(TemplateEntity.class);
        BaseDAO.addFilterCriteria(criteria, filterInfos, TemplateEntity.class);
        totalRecords = (long) criteria.setProjection(Projections.rowCount()).uniqueResult();

        tx.commit();
    } catch (Exception e) {
        if (tx != null)
            try {
                tx.rollback();
            } catch (Exception e2) {
                logger.warn("Error during rollback", e2);
            }
    } finally {
        try {
            if (session != null)
                session.close();
        } catch (Exception e2) {
            logger.warn("Error during session close", e2);
        }
    }

    logger.warn("Template DAO getTotalRecords size: " + totalRecords);
    return (int) totalRecords;
}

From source file:com.denimgroup.threadfix.data.dao.hibernate.HibernateApplicationDao.java

License:Mozilla Public License

@SuppressWarnings("unchecked")
@Override/*from w w w.j a  v a  2 s  .  c o m*/
public List<Integer> getTopXVulnerableAppsFromList(int numApps, List<Integer> teamIdList,
        List<Integer> applicationIdList, List<Integer> tagIdList, List<Integer> vulnTagIdList) {
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Application.class);
    criteria.createAlias("vulnerabilities", "vulnerability");
    criteria.add(Restrictions.eq("active", true));
    criteria.add(Restrictions.eq("vulnerability.active", true));
    criteria.add(Restrictions.eq("vulnerability.hidden", false));
    criteria.add(Restrictions.eq("vulnerability.isFalsePositive", false));

    if (vulnTagIdList.size() > 0) {
        criteria.createAlias("vulnerability.tags", "tags");
        criteria.add(Restrictions.in("tags.id", vulnTagIdList));
    }

    if (teamIdList.isEmpty() || applicationIdList.isEmpty()) {
        if (!applicationIdList.isEmpty()) {
            criteria.add(Restrictions.in("id", applicationIdList));
        }

        if (!teamIdList.isEmpty()) {
            criteria.add(Restrictions.in("organization.id", teamIdList));
        }
    } else {
        criteria.add(Restrictions.or(Restrictions.in("id", applicationIdList),
                Restrictions.in("organization.id", teamIdList)));
    }
    if (!tagIdList.isEmpty()) {
        criteria.createAlias("tags", "tag");
        criteria.add(Restrictions.in("tag.id", tagIdList));
    }

    criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("id"))
            .add(Projections.alias(Projections.countDistinct("vulnerability.id"), "vulnCount")));
    criteria.addOrder(Order.desc("vulnCount"));

    List<Integer> list = list();
    List results = criteria.list();
    int i = 0;
    for (Object result : results) {
        if (i++ >= numApps) {
            break;
        }
        Object[] resultArray = (Object[]) result;
        list.add((Integer) resultArray[0]);
    }

    if (list.isEmpty())
        list = Arrays.asList(-1);
    return list;
}

From source file:com.denimgroup.threadfix.data.dao.hibernate.HibernateApplicationDao.java

License:Mozilla Public License

@Override
public List<Map<String, Object>> retrieveAppsInfoMap(List<Integer> applicationIdList,
        List<Integer> vulnTagIds) {

    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Application.class);

    criteria.createAlias("vulnerabilities", "vulnerability");
    criteria.createAlias("organization", "team");
    criteria.add(Restrictions.eq("active", true));
    criteria.add(Restrictions.eq("vulnerability.active", true));
    criteria.add(Restrictions.eq("vulnerability.isFalsePositive", false));
    criteria.createAlias("vulnerability.genericSeverity", "severity");

    if (applicationIdList.size() > 0) {
        criteria.add(Restrictions.in("id", applicationIdList));
    }//  www .  j av  a  2s.  c  o m

    if (vulnTagIds.size() > 0) {
        criteria.createAlias("vulnerability.tags", "tags");
        criteria.add(Restrictions.in("tags.id", vulnTagIds));
    }

    criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("id"), "appId")
            .add(Projections.groupProperty("name"), "appName")
            .add(Projections.groupProperty("team.id"), "teamId")
            .add(Projections.groupProperty("team.name"), "teamName")
            .add(Projections.groupProperty("severity.intValue"), "severityIntValue")
            .add(Projections.groupProperty("severity.name"), "severityNameValue")
            .add(Projections.alias(Projections.countDistinct("vulnerability.id"), "vulnCount")));
    criteria.addOrder(Order.desc("vulnCount"));
    criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

    List results = criteria.list();
    return results;
}