Example usage for org.hibernate Criteria createAlias

List of usage examples for org.hibernate Criteria createAlias

Introduction

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

Prototype

public Criteria createAlias(String associationPath, String alias) throws HibernateException;

Source Link

Document

Join an association, assigning an alias to the joined association.

Usage

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

License:Mozilla Public License

@Override
public List<Map> getScanComparison(VulnerabilitySearchParameters parameters, boolean isFalsePositive) {
    assert parameters != null;
    List<Integer> idList = getVulnIdList(parameters);

    if (idList.isEmpty())
        return list();

    Session session = sessionFactory.getCurrentSession();

    List<Map> fullList = list();

    // TODO refactor this to reduce duplication or remove the need for it
    int current = 0;
    while (current < idList.size()) {

        int start = current, end = current + 500;
        if (end > idList.size()) {
            end = idList.size();//w  w w.j  ava2 s. com
        }

        List<Integer> thisPage = idList.subList(start, end);

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

        criteria.createAlias("findings", "finding");
        criteria.createAlias("finding.scan", "scan");
        criteria.createAlias("scan.applicationChannel", "applicationChannel");
        criteria.createAlias("applicationChannel.channelType", "channelType");
        criteria.add(Restrictions.in("id", thisPage));

        ProjectionList projectionList = Projections.projectionList()
                .add(Projections.groupProperty("channelType.name"), "channelName")
                .add(Projections.alias(Projections.countDistinct("id"), "foundCount"));

        if (!isFalsePositive) {
            projectionList.add(Projections.groupProperty("foundHAMEndpoint"), "foundHAMEndpoint");
        }
        criteria.setProjection(projectionList);
        criteria.addOrder(Order.desc("foundCount"));
        criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

        List results = (List<Map>) criteria.list();
        fullList.addAll(results);

        current += 500;
    }

    return fullList;

}

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

License:Mozilla Public License

private void addAgeRestrictions() {
    // Limit scanner if present
    if (parameters.getDaysOldModifier() != null
            && (parameters.getDaysOldModifier().equals("Less")
                    || parameters.getDaysOldModifier().equals("More"))
            && parameters.getDaysOld() != null && parameters.getDaysOld() > 0) {

        Criteria subCriteria = session.createCriteria(Finding.class);

        Calendar targetDate = Calendar.getInstance();
        targetDate.add(Calendar.DAY_OF_YEAR, -parameters.getDaysOld());

        subCriteria.createAlias("scan", "scanAlias");
        if (parameters.getDaysOldModifier().equals("More")) {
            //subCriteria.add(Restrictions.lt("scanAlias.openTime", targetDate));
            criteria.add(Restrictions.lt("openTime", targetDate));
            LOG.debug("Set age restriction to after " + parameters.getDaysOld() + " days ago.");
        } else if (parameters.getDaysOldModifier().equals("Less")) {
            //subCriteria.add(Restrictions.gt("scanAlias.openTime", targetDate));
            criteria.add(Restrictions.gt("openTime", targetDate));
            LOG.debug("Set age restriction to before " + parameters.getDaysOld() + " days ago.");
        }/*from  w  w  w .  ja  v a  2s  . c o m*/
    }
}

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

License:Mozilla Public License

private void addAppTags() {
    List<Integer> appIds;
    List<Integer> tagIds = list();

    if (parameters.getTags() != null) {
        for (Tag tag : parameters.getTags()) {
            if (tag.getId() != null) {
                tagIds.add(tag.getId());
            }// w w w  . j av a  2  s  .  c o  m
        }
    }

    if (tagIds.isEmpty()) {
        LOG.debug("No tag IDs found in parameters.");
    } else {
        Criteria subCriteria = session.createCriteria(Tag.class);
        subCriteria.createAlias("applications", "applicationsAlias");
        subCriteria.add(Restrictions.in("id", tagIds));
        subCriteria.setProjection(Projections.property("applicationsAlias.id"));
        appIds = (List<Integer>) subCriteria.list();

        if (appIds.isEmpty())
            appIds.add(0);
        criteria.add(Restrictions.in("application.id", appIds));
        LOG.debug("Added applications with IDs " + appIds);
    }
}

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

License:Mozilla Public License

private void addChannelTypeRestrictions() {

    // Limit scanner if present
    if (parameters.getChannelTypes() != null && !parameters.getChannelTypes().isEmpty()) {
        List<Integer> channelTypeIds = list();
        for (ChannelType channelType : parameters.getChannelTypes()) {
            if (channelType.getId() != null) {
                channelTypeIds.add(channelType.getId());
            }/*from  www . ja v a 2 s  .c  o m*/
        }

        if (!channelTypeIds.isEmpty()) {
            Criteria subCriteria = session.createCriteria(Finding.class);

            subCriteria.createAlias("scan", "myScan");
            subCriteria.createAlias("myScan.applicationChannel", "myApplicationChannel");
            subCriteria.createAlias("myApplicationChannel.channelType", "myChannelType");

            subCriteria.add(Restrictions.in("myChannelType.id", channelTypeIds));

            subCriteria.setProjection(Projections.property("vulnerability.id"));

            List<Integer> ids = (List<Integer>) subCriteria.list();

            if (ids.isEmpty()) {
                LOG.debug("Got no valid Scanner type IDs.");
                ids.add(0); // should yield no results
                // throw an exception here?
            } else {
                LOG.debug("Adding scanner restriction: " + channelTypeIds);
            }

            criteria.add(Restrictions.in("id", ids));
        }
    }
}

From source file:com.emergya.persistenceGeo.dao.impl.FolderEntityDaoHibernateImpl.java

License:Open Source License

/**
 * Get all channel folders filtered/*from w  w  w .  ja v a  2s  .co  m*/
 * 
 * @param inZone indicates if obtain channel folders with a zone. If this parameter is null only obtain not zoned channels
 * @param idZone filter by zone. Obtain only channels of the zone identified by <code>idZone</code>
 * 
 * @return folder list
 */
public List<AbstractFolderEntity> getChannelFolders(Boolean inZone, Long idZone) {
    Criteria criteria = getSession().createCriteria(persistentClass);
    //FIXME: remove this fixme when merge
    if (inZone != null) {
        if (inZone) {
            criteria.add(Restrictions.isNotNull(ZONE));
        } else {
            criteria.add(Restrictions.isNull(ZONE));
        }
    }
    if (idZone != null) {
        criteria.createAlias(ZONE, ZONE).add(Restrictions.eq(ZONE + ".id", idZone));
    }
    // only parent folders
    criteria.add(Restrictions.isNull(PARENT));

    return criteria.list();
}

From source file:com.emergya.persistenceGeo.dao.impl.FolderEntityDaoHibernateImpl.java

License:Open Source License

/**
 * Get a folders list by zones with an specific parent. If zoneId is NULL
 * returns all the folder not associated to any zone. If parentId is NULL
 * the returned folders are root folders.
 *
 * @params <code>zoneId</code>
 * @params <code>parentId</code>
 *
 * @return Entities list associated with the zoneId or null if not found
 *///from w  ww . j  ava2s.c  o  m
@Override
public List<AbstractFolderEntity> findByZone(Long zoneId, Long parentId) {
    List<AbstractFolderEntity> folderList = new LinkedList<AbstractFolderEntity>();
    Criteria criteria = getSession().createCriteria(persistentClass).createAlias("zone", "zone")
            .add(Restrictions.eq("zone.id", zoneId));
    if (parentId == null) {
        criteria.add(Restrictions.isNull("parent"));
    } else {
        criteria.createAlias("parent", "parent");
        criteria.add(Restrictions.eq("parent.id", parentId));
    }
    folderList.addAll(criteria.list());
    return folderList;
}

From source file:com.emergya.persistenceGeo.dao.impl.FolderEntityDaoHibernateImpl.java

License:Open Source License

/**
 * Get all channel folders filtered/*  w w  w .  j  a  v  a2  s.co m*/
 * 
 * @param inZone indicates if obtain channel folders with a zone. If this parameter is null only obtain not zoned channels
 * @param idZone filter by zone. Obtain only channels of the zone identified by <code>idZone</code>
  * @param <code>isEnabled</code>
 * 
 * @return folder list
 */
public List<AbstractFolderEntity> getChannelFolders(Boolean inZone, Long idZone, Boolean isEnable) {
    Criteria criteria = getSession().createCriteria(persistentClass);
    //FIXME: remove this fixme when merge
    if (inZone != null) {
        if (inZone) {
            criteria.add(Restrictions.isNotNull(ZONE));
        } else {
            criteria.add(Restrictions.isNull(ZONE));
        }
    }
    if (idZone != null) {
        criteria.createAlias(ZONE, ZONE).add(Restrictions.eq(ZONE + ".id", idZone));
    }
    if (isEnable != null && isEnable) {
        criteria.add(Restrictions.eq("enabled", isEnable));
    } else if (isEnable != null) {
        Disjunction dis = Restrictions.disjunction();
        dis.add(Restrictions.isNull("enabled"));
        dis.add(Restrictions.eq("enabled", Boolean.FALSE));
        criteria.add(dis);
    }
    // only parent folders
    criteria.add(Restrictions.isNull(PARENT));
    criteria.addOrder(Order.asc("name"));

    return criteria.list();

}

From source file:com.emergya.persistenceGeo.dao.impl.FolderEntityDaoHibernateImpl.java

License:Open Source License

/**
 * Get a folders list by zones with an specific parent. If zoneId is NULL
 * returns all the folder not associated to any zone. If parentId is NULL
 * the returned folders are root folders.
 *
 * @param <code>zoneId</code>
 * @param <code>parentId</code>
 * @param <code>isEnabled</code>
 *
 * @return Entities list associated with the zoneId or null if not found
 *//*w w w .  j  a  va2s.com*/
public List<AbstractFolderEntity> findByZone(Long zoneId, Long parentId, Boolean isEnable) {
    List<AbstractFolderEntity> folderList = new LinkedList<AbstractFolderEntity>();
    Criteria criteria = getSession().createCriteria(persistentClass);

    if (zoneId != null) {
        criteria.createAlias("zone", "zone").add(Restrictions.eq("zone.id", zoneId));
    }

    if (parentId == null) {
        criteria.add(Restrictions.isNull("parent"));
    } else {
        criteria.createAlias("parent", "parent");
        criteria.add(Restrictions.eq("parent.id", parentId));
    }
    if (isEnable != null && isEnable) {
        criteria.add(Restrictions.eq("enabled", isEnable));
    } else if (isEnable != null) {
        Disjunction dis = Restrictions.disjunction();
        dis.add(Restrictions.isNull("enabled"));
        dis.add(Restrictions.eq("enabled", Boolean.FALSE));
        criteria.add(dis);
    }
    folderList.addAll(criteria.list());
    return folderList;
}

From source file:com.eucalyptus.entities.Entities.java

License:Open Source License

/**
 * Query items matching the given example restricted by the given criterion.
 *
 * <P>The caller must have an active transaction for the entity.</P>
 *
 * <P>WARNING: This method uses wildcard matching</P>
 * // w w w .  j a va2s  .c o m
 * @param example The example object
 * @param readOnly Use True if the results will not be modified
 * @param criterion Additional restrictions for the query
 * @param aliases Any aliases necessary for the given criterion
 * @param <T> The entity type
 * @return The result list
 */
@SuppressWarnings({ "unchecked", "cast" })
public static <T> List<T> query(final T example, final boolean readOnly, final Criterion criterion,
        final Map<String, String> aliases) {
    final Example qbe = Example.create(example).enableLike(MatchMode.EXACT);
    final Criteria criteria = getTransaction(example).getTxState().getSession()
            .createCriteria(example.getClass()).setReadOnly(readOnly)
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).setCacheable(true).add(qbe).add(criterion);
    for (final Map.Entry<String, String> aliasEntry : aliases.entrySet()) {
        criteria.createAlias(aliasEntry.getKey(), aliasEntry.getValue()); // inner join by default
    }
    final List<T> resultList = (List<T>) criteria.list();
    return Lists.newArrayList(Sets.newHashSet(resultList));
}

From source file:com.eucalyptus.entities.Entities.java

License:Open Source License

/**
 * Count the matching entities for the given example.
 *
 * @param example The example entity/*from  w w  w  . j  a v a  2  s. co  m*/
 * @param criterion Additional restrictions for the query
 * @param aliases Any aliases necessary for the given criterion
 * @return The number of matching entities
 */
public static long count(final Object example, final Criterion criterion, final Map<String, String> aliases) {
    final Example qbe = Example.create(example);
    final Criteria criteria = getTransaction(example).getTxState().getSession()
            .createCriteria(example.getClass()).setReadOnly(true).setCacheable(false).add(qbe).add(criterion)
            .setProjection(Projections.rowCount());
    for (final Map.Entry<String, String> aliasEntry : aliases.entrySet()) {
        criteria.createAlias(aliasEntry.getKey(), aliasEntry.getValue()); // inner join by default
    }
    final Number count = (Number) criteria.uniqueResult();
    return count.longValue();
}