Example usage for org.hibernate.criterion DetachedCriteria createAlias

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

Introduction

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

Prototype

public DetachedCriteria createAlias(String associationPath, String alias) 

Source Link

Document

Creates an association path alias within this DetachedCriteria.

Usage

From source file:de.iteratec.iteraplan.businesslogic.reports.query.node.PropertyLeafNode.java

License:Open Source License

/** {@inheritDoc} */
@Override//from ww  w. jav a2s . co  m
DetachedCriteria getWhereCriteria(DetachedCriteria criteria) {
    String effectivePropertyName = null;

    if (getExtensionSize() == 0) {
        effectivePropertyName = String.format("%s.%s", getResultTypeDBNameShortWithSuffix(), getPropertyName());
    } else {
        effectivePropertyName = String.format("%s.%s", getLeafTypeDBNameShortWithSuffix(), getPropertyName());
    }

    final String[] properties = StringUtils.split(getPropertyName(), '.');
    if (!getResultType().isSpecialProperty(getPropertyName()) && properties != null && properties.length > 1) {
        final String associationPath = String.format("%s.%s", getResultTypeDBNameShortWithSuffix(),
                properties[0]);
        final String alias = String.format("%sAlias", properties[0]);
        criteria.createAlias(associationPath, alias);
        effectivePropertyName = String.format("%s.%s", alias, properties[1]);
    }

    if ("runtimePeriod.start".equals(getPropertyName()) || "runtimePeriod.end".equals(getPropertyName())) {
        criteria.add(Restrictions.or(
                getCriterionForComparator(effectivePropertyName, getComparator(), attributeType),
                Restrictions.isNull(effectivePropertyName)));
    } else if ("technicalComponent.availableForInterfaces".equals(getPropertyName())) {
        if (Comparator.LIKE.equals(getComparator())) {
            criteria.add(getCriterionForComparator(effectivePropertyName, Comparator.EQ, attributeType));
        } else if (Comparator.NOT_LIKE.equals(getComparator())) {
            criteria.add(getCriterionForComparator(effectivePropertyName, Comparator.NEQ, attributeType));
        }
    } else {
        criteria.add(getCriterionForComparator(effectivePropertyName, getComparator(), attributeType));
    }

    return criteria;
}

From source file:de.iteratec.iteraplan.businesslogic.reports.query.node.SealLeafNode.java

License:Open Source License

/** {@inheritDoc} */
@Override//from   w  w w  .  j a  v  a 2 s. co m
DetachedCriteria getWhereCriteria(DetachedCriteria criteria) {
    String effectivePropertyName = null;

    if (getExtensionSize() == 0) {
        effectivePropertyName = String.format("%s.%s", getResultTypeDBNameShortWithSuffix(), getPropertyName());
    } else {
        effectivePropertyName = String.format("%s.%s", getLeafTypeDBNameShortWithSuffix(), getPropertyName());
    }

    final String[] properties = StringUtils.split(getPropertyName(), '.');
    if (!getResultType().isSpecialProperty(getPropertyName()) && properties != null && properties.length > 1) {
        final String associationPath = String.format("%s.%s", getResultTypeDBNameShortWithSuffix(),
                properties[0]);
        final String alias = String.format("%sAlias", properties[0]);
        criteria.createAlias(associationPath, alias);
        effectivePropertyName = String.format("%s.%s", alias, properties[1]);
    }

    SealState sealState = (SealState) getPattern();
    switch (sealState) {
    case VALID:
        Criterion validCriterion = getCriterionForComparator(effectivePropertyName, Comparator.EQ, null);
        Criterion notOutdatedCriterion = Restrictions.not(getOutdatedCriterion(effectivePropertyName));
        criteria.add(Restrictions.and(validCriterion, notOutdatedCriterion));
        break;
    case INVALID:
        criteria.add(getCriterionForComparator(effectivePropertyName, Comparator.EQ, null));
        break;
    case NOT_AVAILABLE:
        Criterion isNull = Restrictions.isNull(effectivePropertyName);
        Criterion eqCriterion = getCriterionForComparator(effectivePropertyName, Comparator.EQ, null);
        criteria.add(Restrictions.or(isNull, eqCriterion));
        break;
    case OUTDATED:
        criteria.add(getOutdatedCriterion(effectivePropertyName));
        break;
    default:
        throw new IllegalStateException("The seal state " + sealState + " is not supported!");
    }

    return criteria;
}

From source file:de.iteratec.iteraplan.businesslogic.service.InformationSystemReleaseServiceImpl.java

License:Open Source License

/** {@inheritDoc} */
@Override//from  www .j  a v  a2  s  . c  o m
public List<InformationSystemRelease> findByNames(Set<String> names) {
    if (names.isEmpty()) {
        return Collections.emptyList();
    }

    DetachedCriteria criteria = DetachedCriteria.forClass(InformationSystemRelease.class);
    criteria.createAlias("informationSystem", "informationSystem");

    Disjunction disjunction = Restrictions.disjunction();
    for (String name : names) {
        String[] partsOfReleaseName = GeneralHelper.getPartsOfReleaseName(name);

        //FIXME will eq do the trick here too or do we need like?
        //if like is needed we should use the IteraplanLikeExpression
        //      SimpleExpression nameExpression = Restrictions.like("informationSystem.name", partsOfReleaseName[0], MatchMode.EXACT).ignoreCase();
        Criterion nameExpression = new IteraplanLikeExpression("informationSystem.name", partsOfReleaseName[0],
                true);
        String version = "version";
        if (partsOfReleaseName[1] != null) {
            //FIXME will eq do the trick here too or do we need like?
            //if like is needed we should use the IteraplanLikeExpression
            //        SimpleExpression versionExpression = Restrictions.like(version, partsOfReleaseName[1], MatchMode.EXACT).ignoreCase();
            Criterion versionExpression = new IteraplanLikeExpression(version, partsOfReleaseName[1], true);
            disjunction.add(Restrictions.and(nameExpression, versionExpression));
        } else {
            Criterion versionExpression = Restrictions.or(Restrictions.isNull(version),
                    Restrictions.eq(version, Constants.DB_NU1L));
            disjunction.add(Restrictions.and(nameExpression, versionExpression));
        }
    }
    criteria.add(disjunction);

    return informationSystemReleaseDAO.findByCriteria(criteria);
}

From source file:de.iteratec.iteraplan.businesslogic.service.TechnicalComponentReleaseServiceImpl.java

License:Open Source License

/** {@inheritDoc} */
@Override//from ww  w .  j  av a2 s  . c om
public List<TechnicalComponentRelease> findByNames(Set<String> names) {
    if (names.isEmpty()) {
        return Collections.emptyList();
    }

    DetachedCriteria criteria = DetachedCriteria.forClass(TechnicalComponentRelease.class);
    criteria.createAlias("technicalComponent", "technicalComponent");

    Disjunction disjunction = Restrictions.disjunction();
    for (String name : names) {
        String[] partsOfReleaseName = GeneralHelper.getPartsOfReleaseName(name);

        //FIXME will eq do the trick here too or do we need like?
        //if like is needed we should use the IteraplanLikeExpression
        //      SimpleExpression nameExpression = Restrictions.like("technicalComponent.name", partsOfReleaseName[0], MatchMode.EXACT).ignoreCase();
        Criterion nameExpression = new IteraplanLikeExpression("technicalComponent.name", partsOfReleaseName[0],
                true);
        if (partsOfReleaseName[1] != null) {
            //FIXME will eq do the trick here too or do we need like?
            //if like is needed we should use the IteraplanLikeExpression
            //        SimpleExpression versionExpression = Restrictions.like(VERSION, partsOfReleaseName[1], MatchMode.EXACT).ignoreCase();
            Criterion versionExpression = new IteraplanLikeExpression(VERSION, partsOfReleaseName[1], true);
            disjunction.add(Restrictions.and(nameExpression, versionExpression));
        } else {
            Criterion versionExpression = Restrictions.or(Restrictions.isNull(VERSION),
                    Restrictions.eq(VERSION, Constants.DB_NU1L));
            disjunction.add(Restrictions.and(nameExpression, versionExpression));
        }
    }
    criteria.add(disjunction);

    return technicalComponentReleaseDAO.findByCriteria(criteria);
}

From source file:edu.uoc.dao.impl.MeetingRoomDaoImpl.java

@Override
public List<MeetingRoom> findbyForm(SearchMeeting searchMeeting, List<Room> ids_room) {

    String topic = searchMeeting.getTopic();
    Room room = searchMeeting.getRoom();

    //Convert TimeStamp to Date
    Timestamp tsStart = Util.converToTimestamp(searchMeeting.getStart_meeting(), logger);
    Timestamp tsEnd = Util.converToTimestamp(searchMeeting.getEnd_meeting(), logger);

    Criteria criteria;// w w w  .j  a v a2 s  . c  om
    criteria = this.getSession().createCriteria(MeetingRoom.class, "meeting");
    criteria.add(Restrictions.eq("meeting.finished", (byte) 1));
    criteria.add(Restrictions.eq("meeting.recorded", (byte) 1));
    if (tsStart != null) {
        criteria.add(Restrictions.ge("meeting.start_meeting", tsStart));
    }
    if (tsEnd != null) {
        criteria.add(Restrictions.le("meeting.end_meeting", tsEnd));
    }
    if (topic != null && topic.length() > 0) {
        criteria.add(Restrictions.like("meeting.topic", "%" + topic + "%"));
    }
    if (room != null && room.getId() > 0) {
        criteria.add(Restrictions.eq("meeting.id_room", room));
    } else {
        criteria.add(Restrictions.in("meeting.id_room", ids_room));
    }
    if (searchMeeting.getParticipants() != null && searchMeeting.getParticipants().length() > 0) {
        DetachedCriteria subCriteria = DetachedCriteria.forClass(UserMeeting.class, "userMeeting");
        subCriteria.createAlias("userMeeting.pk.meeting", "userMeeting.id");
        subCriteria.setProjection(Projections.projectionList().add(Projections.property("userMeeting.id")));
        subCriteria.add(Restrictions.eqProperty("meeting.id", "userMeeting.id"));

        DetachedCriteria subCriteriaUser = DetachedCriteria.forClass(User.class, "user");
        subCriteriaUser.setProjection(Projections.projectionList().add(Projections.property("user.id")));
        subCriteriaUser.add(Restrictions.like("user.fullname", "%" + searchMeeting.getParticipants() + "%"));
        subCriteriaUser.add(Restrictions.eqProperty("user.id", "userMeeting.pk.user.id"));
        subCriteria.add(Subqueries.exists(subCriteriaUser));
        criteria.add(Subqueries.exists(subCriteria));
    }
    logger.info("Criteria " + criteria.toString());

    return criteria.list();
}

From source file:gov.nih.nci.cananolab.service.admin.impl.OwnershipTransferServiceImpl.java

License:BSD License

private SampleComposition loadComposition(String sampleId) throws Exception {
    SampleComposition composition = null;

    CaNanoLabApplicationService appService = (CaNanoLabApplicationService) ApplicationServiceProvider
            .getApplicationService();/*from  w ww. j a  va  2  s .com*/
    DetachedCriteria crit = DetachedCriteria.forClass(SampleComposition.class);
    crit.createAlias("sample", "sample");
    crit.add(Property.forName("sample.id").eq(new Long(sampleId)));
    crit.setFetchMode("nanomaterialEntityCollection", FetchMode.JOIN);
    crit.setFetchMode("nanomaterialEntityCollection.fileCollection", FetchMode.JOIN);
    crit.setFetchMode("nanomaterialEntityCollection.fileCollection.keywordCollection", FetchMode.JOIN);
    crit.setFetchMode("nanomaterialEntityCollection.composingElementCollection", FetchMode.JOIN);
    crit.setFetchMode("nanomaterialEntityCollection.composingElementCollection.inherentFunctionCollection",
            FetchMode.JOIN);
    crit.setFetchMode(
            "nanomaterialEntityCollection.composingElementCollection.inherentFunctionCollection.targetCollection",
            FetchMode.JOIN);
    crit.setFetchMode("functionalizingEntityCollection", FetchMode.JOIN);
    crit.setFetchMode("functionalizingEntityCollection.fileCollection", FetchMode.JOIN);
    crit.setFetchMode("functionalizingEntityCollection.fileCollection.keywordCollection", FetchMode.JOIN);
    crit.setFetchMode("functionalizingEntityCollection.functionCollection", FetchMode.JOIN);
    crit.setFetchMode("functionalizingEntityCollection.functionCollection.targetCollection", FetchMode.JOIN);
    crit.setFetchMode("functionalizingEntityCollection.activationMethod", FetchMode.JOIN);
    crit.setFetchMode("chemicalAssociationCollection", FetchMode.JOIN);
    crit.setFetchMode("chemicalAssociationCollection.fileCollection", FetchMode.JOIN);
    crit.setFetchMode("chemicalAssociationCollection.fileCollection.keywordCollection", FetchMode.JOIN);
    crit.setFetchMode("chemicalAssociationCollection.associatedElementA", FetchMode.JOIN);
    crit.setFetchMode("chemicalAssociationCollection.associatedElementB", FetchMode.JOIN);
    crit.setFetchMode("fileCollection", FetchMode.JOIN);
    crit.setFetchMode("fileCollection.keywordCollection", FetchMode.JOIN);
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    List result = appService.query(crit);

    if (!result.isEmpty()) {
        composition = (SampleComposition) result.get(0);
    }
    return composition;
}

From source file:gov.nih.nci.cananolab.service.admin.impl.OwnershipTransferServiceImpl.java

License:BSD License

private List<Characterization> loadCharacterizations(String sampleId) throws Exception {
    List<Characterization> chars = new ArrayList<Characterization>();

    CaNanoLabApplicationService appService = (CaNanoLabApplicationService) ApplicationServiceProvider
            .getApplicationService();//from  w  w w.j a v a 2s.  c  o  m
    DetachedCriteria crit = DetachedCriteria.forClass(Characterization.class);
    crit.createAlias("sample", "sample");
    crit.add(Property.forName("sample.id").eq(new Long(sampleId)));
    // fully load characterization
    crit.setFetchMode("pointOfContact", FetchMode.JOIN);
    crit.setFetchMode("pointOfContact.organization", FetchMode.JOIN);
    crit.setFetchMode("protocol", FetchMode.JOIN);
    crit.setFetchMode("protocol.file", FetchMode.JOIN);
    crit.setFetchMode("protocol.file.keywordCollection", FetchMode.JOIN);
    crit.setFetchMode("experimentConfigCollection", FetchMode.JOIN);
    crit.setFetchMode("experimentConfigCollection.technique", FetchMode.JOIN);
    crit.setFetchMode("experimentConfigCollection.instrumentCollection", FetchMode.JOIN);
    crit.setFetchMode("findingCollection", FetchMode.JOIN);
    crit.setFetchMode("findingCollection.datumCollection", FetchMode.JOIN);
    crit.setFetchMode("findingCollection.datumCollection.conditionCollection", FetchMode.JOIN);
    crit.setFetchMode("findingCollection.fileCollection", FetchMode.JOIN);
    crit.setFetchMode("findingCollection.fileCollection.keywordCollection", FetchMode.JOIN);
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    List results = appService.query(crit);

    for (int i = 0; i < results.size(); i++) {
        Characterization achar = (Characterization) results.get(i);
        chars.add(achar);
    }
    return chars;
}

From source file:gov.nih.nci.cananolab.service.protocol.impl.ProtocolServiceLocalImpl.java

License:BSD License

private List<Long> findCharacterizationIdsByProtocolId(String protocolId) throws Exception {
    CaNanoLabApplicationService appService = (CaNanoLabApplicationService) ApplicationServiceProvider
            .getApplicationService();/*  w  w w.java  2 s  .  c o  m*/
    DetachedCriteria crit = DetachedCriteria.forClass(Characterization.class)
            .setProjection(Projections.distinct(Property.forName("id")));
    crit.createAlias("protocol", "protocol");
    crit.add(Property.forName("protocol.id").eq(new Long(protocolId)));
    List results = appService.query(crit);
    List<Long> ids = new ArrayList<Long>();
    for (int i = 0; i < results.size(); i++) {
        Long charId = (Long) results.get(i);
        ids.add(charId);
    }
    return ids;
}

From source file:gov.nih.nci.cananolab.service.protocol.impl.ProtocolServiceLocalImpl.java

License:BSD License

private List<Characterization> findCharacterizationsByProtocolId(String protocolId) throws Exception {
    CaNanoLabApplicationService appService = (CaNanoLabApplicationService) ApplicationServiceProvider
            .getApplicationService();/*from  w w w  .  j av  a  2s  .c  om*/
    DetachedCriteria crit = DetachedCriteria.forClass(Characterization.class);
    crit.createAlias("protocol", "protocol");
    crit.add(Property.forName("protocol.id").eq(new Long(protocolId)));
    List results = appService.query(crit);
    List<Characterization> chars = new ArrayList<Characterization>();
    for (int i = 0; i < results.size(); i++) {
        Characterization achar = (Characterization) results.get(i);
        chars.add(achar);
    }
    return chars;
}

From source file:gov.nih.nci.cananolab.service.sample.helper.AdvancedSampleServiceHelper.java

License:BSD License

private List<Characterization> findCharacterizationsBy(String sampleId, AdvancedSampleSearchBean searchBean)
        throws Exception {
    List<Characterization> chars = new ArrayList<Characterization>();
    if (searchBean.getCharacterizationQueries().isEmpty()) {
        return chars;
    }/*from   w  ww  .j  a va 2s.c  om*/
    Long id = new Long(sampleId);
    CaNanoLabApplicationService appService = (CaNanoLabApplicationService) ApplicationServiceProvider
            .getApplicationService();
    if (searchBean.getCharacterizationQueries().size() == 1
            || searchBean.getCharacterizationLogicalOperator().equals("or")) {
        DetachedCriteria crit = DetachedCriteria.forClass(Characterization.class, "rootCrit");
        crit.createAlias("sample", "sample");
        // join finding and datum
        if (searchBean.getHasDatum()) {
            crit.createAlias("findingCollection", "finding", CriteriaSpecification.LEFT_JOIN);
            crit.createAlias("finding.datumCollection", "datum", CriteriaSpecification.LEFT_JOIN);
        }
        crit.add(Restrictions.eq("sample.id", id));
        Disjunction charDisjunction = getCharacterizationDisjunction(searchBean, crit, "");
        crit.add(charDisjunction);
        crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        List results = appService.query(crit);
        for (int i = 0; i < results.size(); i++) {
            Characterization achar = (Characterization) results.get(i);
            chars.add(achar);
        }
    } else {
        // hibernate doesn't support union have to execute the query one at
        // a time union the result in Java
        for (CharacterizationQueryBean charQuery : searchBean.getCharacterizationQueries()) {
            DetachedCriteria crit = DetachedCriteria.forClass(Characterization.class, "rootCrit");
            crit.createAlias("sample", "sample");
            crit.add(Restrictions.eq("sample.id", id));
            DetachedCriteria subCrit = getCharacterizationSubquery(charQuery, "id");
            crit.add(Subqueries.exists(subCrit));
            crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            List results = appService.query(crit);
            for (int i = 0; i < results.size(); i++) {
                Characterization achar = (Characterization) results.get(i);
                if (!chars.contains(achar)) {
                    chars.add(achar);
                }
            }
        }
    }
    Collections.sort(chars, new Comparators.CharacterizationNameAssayTypeDateComparator());
    return chars;
}