Example usage for org.hibernate.criterion DetachedCriteria add

List of usage examples for org.hibernate.criterion DetachedCriteria add

Introduction

In this page you can find the example usage for org.hibernate.criterion DetachedCriteria add.

Prototype

public DetachedCriteria add(Criterion criterion) 

Source Link

Document

Add a restriction

Usage

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.AbstractTypeDAO.java

License:Apache License

final T tryFindTypeByCode(final String code, final boolean appendDatabaseInstance) throws DataAccessException {
    assert code != null : "Unspecified code";

    final DetachedCriteria criteria = DetachedCriteria.forClass(getEntityClass());
    criteria.add(Restrictions.eq("code", CodeConverter.tryToDatabase(code)));
    if (appendDatabaseInstance) {
        criteria.add(Restrictions.eq("databaseInstance", getDatabaseInstance()));
    }//from   www.  j ava2 s  .c om
    final List<T> list = cast(getHibernateTemplate().findByCriteria(criteria));
    final T entity = tryFindEntity(list, "type");
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format("%s(%s,%s): Entity type '%s' has been found.",
                MethodUtils.getCurrentMethod().getName(), code, appendDatabaseInstance, entity));
    }
    return entity;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.AbstractTypeDAO.java

License:Apache License

final List<T> listTypes(final boolean appendDatabaseInstance) throws DataAccessException {
    final DetachedCriteria criteria = DetachedCriteria.forClass(getEntityClass());
    if (appendDatabaseInstance) {
        criteria.add(Restrictions.eq("databaseInstance", getDatabaseInstance()));
    }/*from   w  w w.j av a2s  .  co m*/
    final List<T> list = cast(getHibernateTemplate().findByCriteria(criteria));
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format("%s(%s): %d entity type(s) have been found.",
                MethodUtils.getCurrentMethod().getName(), appendDatabaseInstance, list.size()));
    }
    return list;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.EntityPropertyTypeDAO.java

License:Apache License

public List<IEntityPropertiesHolder> listEntities(final EntityTypePE entityType) throws DataAccessException {
    assert entityType != null : "Unspecified entity type.";

    final DetachedCriteria criteria = DetachedCriteria.forClass(entityKind.getEntityClass());
    criteria.add(Restrictions.eq(entityKind.getEntityTypeFieldName(), entityType));
    final List<IEntityPropertiesHolder> list = cast(getHibernateTemplate().findByCriteria(criteria));
    return list;/*from   w  ww.  ja  v  a2s . c om*/
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.EntityPropertyTypeDAO.java

License:Apache License

public List<IEntityPropertiesHolder> listEntitiesWithoutPropertyValue(final EntityTypePE entityType,
        final PropertyTypePE propertyType) throws DataAccessException {
    assert entityType != null : "Unspecified entity type.";
    assert propertyType != null : "Unspecified property type.";

    final DetachedCriteria criteria = DetachedCriteria.forClass(entityKind.getEntityClass());
    criteria.add(Restrictions.eq(entityKind.getEntityTypeFieldName(), entityType));
    ////from  w  w w.j av a  2  s  . com
    final List<IEntityPropertiesHolder> list = cast(getHibernateTemplate().findByCriteria(criteria));

    // TODO 2009-07-01, Piotr Buczek: filter results with criteria
    // final DetachedCriteria propertyTypesCriteria =
    // DetachedCriteria.forClass(entityKind.getEntityPropertyClass());
    // propertyTypesCriteria.add(Restrictions.eq("entityTypePropertyType", propertyType));
    // criteria.add(Subqueries.notExists(propertyTypesCriteria.setProjection(Projections
    // .property(...))));
    final List<IEntityPropertiesHolder> result = new ArrayList<IEntityPropertiesHolder>(list.size());
    for (IEntityPropertiesHolder entity : list) {
        if (isEntityWithoutPropertyValue(entity, propertyType)) {
            result.add(entity);
        }
    }
    return result;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.EntityPropertyTypeDAO.java

License:Apache License

public void fillTermUsageStatistics(List<VocabularyTermWithStats> termsWithStats, VocabularyPE vocabulary) {
    assert termsWithStats != null : "Unspecified terms.";
    assert vocabulary != null : "Unspecified vocabulary.";
    assert termsWithStats.size() == vocabulary.getTerms()
            .size() : "Sizes of terms to be filled and vocabulary terms don't match.";

    Map<Long, VocabularyTermWithStats> termsById = new HashMap<Long, VocabularyTermWithStats>(
            termsWithStats.size());//w w  w. java2  s . c o m
    for (VocabularyTermWithStats termWithStats : termsWithStats) {
        Long id = termWithStats.getTerm().getId();
        termsById.put(id, termWithStats);
    }

    final DetachedCriteria criteria = DetachedCriteria.forClass(entityKind.getEntityPropertyClass());
    // alias is the easiest way to restrict on association using criteria
    criteria.createAlias("vocabularyTerm", "term");
    criteria.add(Restrictions.eq("term.vocabularyInternal", vocabulary));
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.rowCount());
    projectionList.add(Projections.groupProperty("term.id"));
    criteria.setProjection(projectionList);

    final List<Object[]> results = cast(getHibernateTemplate().findByCriteria(criteria));

    for (Object[] result : results) {
        Integer numberOfUsages = (Integer) result[0];
        Long termId = (Long) result[1];
        termsById.get(termId).registerUsage(entityKind, numberOfUsages);
    }
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.EntityTypeDAO.java

License:Apache License

public final <T extends EntityTypePE> List<T> listEntityTypes() throws DataAccessException {
    final DetachedCriteria criteria = DetachedCriteria.forClass(getEntityClass());
    criteria.add(Restrictions.eq("databaseInstance", getDatabaseInstance()));
    final String entityKindName = entityKind.name().toLowerCase();
    criteria.setFetchMode(entityKindName + "TypePropertyTypesInternal", FetchMode.JOIN);
    criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);
    final List<T> list = cast(getHibernateTemplate().findByCriteria(criteria));
    return list;// ww w . j  av  a  2 s  .  com
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.EventDAO.java

License:Apache License

public List<DeletedDataSet> listDeletedDataSets(Long lastSeenDeletionEventIdOrNull) {
    final DetachedCriteria criteria = DetachedCriteria.forClass(EventPE.class);
    if (lastSeenDeletionEventIdOrNull != null) {
        criteria.add(Restrictions.gt("id", lastSeenDeletionEventIdOrNull));
    }//from   w ww. j a va 2  s.com
    criteria.add(Restrictions.eq("eventType", EventType.DELETION));
    criteria.add(Restrictions.eq("entityType", EntityType.DATASET));
    final List<EventPE> list = cast(getHibernateTemplate().findByCriteria(criteria));
    if (operationLog.isDebugEnabled()) {
        String lastDesc = lastSeenDeletionEventIdOrNull == null ? "all"
                : "id > " + lastSeenDeletionEventIdOrNull;
        operationLog.debug(String.format("%s(%d): data set deletion events(s) have been found.",
                MethodUtils.getCurrentMethod().getName(), lastDesc, list.size()));
    }
    ArrayList<DeletedDataSet> result = new ArrayList<DeletedDataSet>();
    for (EventPE event : list) {
        result.add(new DeletedDataSet(event.getIdentifier(), event.getId()));
    }
    return result;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.ExperimentDAO.java

License:Apache License

public List<ExperimentPE> listExperimentsWithProperties(final ExperimentTypePE experimentTypeOrNull,
        final ProjectPE project) throws DataAccessException {
    assert project != null : "Unspecified project.";

    final DetachedCriteria criteria = DetachedCriteria.forClass(getEntityClass());
    if (experimentTypeOrNull != null) {
        criteria.add(Restrictions.eq("experimentType", experimentTypeOrNull));
    }/* w  ww.ja  v  a2  s  .  c o  m*/
    criteria.add(Restrictions.eq("projectInternal", project));
    criteria.setFetchMode("experimentProperties", FetchMode.JOIN);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    final List<ExperimentPE> list = cast(getHibernateTemplate().findByCriteria(criteria));
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format("%d experiments have been found for project '%s'%s.", list.size(),
                project,
                (experimentTypeOrNull == null) ? "" : " and experiment type '" + experimentTypeOrNull + "'"));
    }
    return list;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.ExternalDataDAO.java

License:Apache License

public boolean hasExternalData(SamplePE sample) throws DataAccessException {
    final DetachedCriteria criteria = DetachedCriteria.forClass(ExternalDataPE.class);
    criteria.add(Restrictions.eq("sampleInternal", sample));
    criteria.setProjection(Projections.rowCount());
    Integer count = (Integer) getHibernateTemplate().findByCriteria(criteria).get(0);
    return count > 0;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.ExternalDataDAO.java

License:Apache License

public final List<ExternalDataPE> listExternalData(final DataStorePE dataStore) throws DataAccessException {
    assert dataStore != null : "Unspecified data store.";
    final DetachedCriteria criteria = DetachedCriteria.forClass(ExternalDataPE.class);
    criteria.add(Restrictions.eq("dataStore", dataStore));
    final List<ExternalDataPE> list = cast(getHibernateTemplate().findByCriteria(criteria));
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format("%s(%s): %d data set(s) have been found.",
                MethodUtils.getCurrentMethod().getName(), dataStore, list.size()));
    }//from w ww  .  j ava 2 s  .c  o  m
    return list;
}