Example usage for org.hibernate.criterion Projections property

List of usage examples for org.hibernate.criterion Projections property

Introduction

In this page you can find the example usage for org.hibernate.criterion Projections property.

Prototype

public static PropertyProjection property(String propertyName) 

Source Link

Document

A property value projection

Usage

From source file:gov.nih.nci.caintegrator.data.CaIntegrator2DaoImpl.java

License:BSD License

/**
 * {@inheritDoc}/*from  w w  w .j  a va2 s .c o m*/
 */
@Override
@SuppressWarnings(UNCHECKED)
public List<Platform> retrievePlatformsForGenomicSource(GenomicDataSourceConfiguration genomicSource) {
    Criteria arrayCriteria = getCurrentSession().createCriteria(Array.class);
    arrayCriteria.setProjection(Projections.distinct(Projections.property(PLATFORM_ASSOCIATION)))
            .add(Restrictions.isNotNull(PLATFORM_ASSOCIATION)).createCriteria("sampleCollection")
            .add(Restrictions.eq("genomicDataSource", genomicSource));

    return arrayCriteria.list();
}

From source file:gov.nih.nci.cananolab.service.protocol.helper.ProtocolServiceHelper.java

License:BSD License

public List<String> findProtocolIdsByOwner(String currentOwner) throws Exception {
    List<String> protocolIds = new ArrayList<String>();
    DetachedCriteria crit = DetachedCriteria.forClass(Protocol.class)
            .setProjection(Projections.projectionList().add(Projections.property("id")));
    Criterion crit1 = Restrictions.eq("createdBy", currentOwner);
    // in case of copy createdBy is like lijowskim:COPY
    Criterion crit2 = Restrictions.like("createdBy", currentOwner + ":", MatchMode.START);
    crit.add(Expression.or(crit1, crit2));

    CaNanoLabApplicationService appService = (CaNanoLabApplicationService) ApplicationServiceProvider
            .getApplicationService();/*from w w w.j  a  v a2s. com*/
    List results = appService.query(crit);
    for (int i = 0; i < results.size(); i++) {
        String protocolId = results.get(i).toString();
        if (springSecurityAclService.currentUserHasReadPermission(Long.valueOf(protocolId),
                SecureClassesEnum.PROTOCOL.getClazz())
                || springSecurityAclService.currentUserHasWritePermission(Long.valueOf(protocolId),
                        SecureClassesEnum.PROTOCOL.getClazz())) {
            protocolIds.add(protocolId);
        } else {
            logger.debug("User doesn't have access to protocol of ID: " + protocolId);
        }
    }
    return protocolIds;
}

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

License:BSD License

public List<String> findSampleIdsBy(String sampleName, String samplePointOfContact,
        String[] nanomaterialEntityClassNames, String[] otherNanomaterialEntityTypes,
        String[] functionalizingEntityClassNames, String[] otherFunctionalizingEntityTypes,
        String[] functionClassNames, String[] otherFunctionTypes, String[] characterizationClassNames,
        String[] otherCharacterizationTypes, String[] wordList) throws Exception {
    List<String> sampleIds = new ArrayList<String>();

    //logger.error("Processing: " + sampleName);

    // can't query for the entire Sample object due to
    // limitations in pagination in SDK

    // added createdDate and sample name in the results so data can be
    // sorted by date and name
    DetachedCriteria crit = DetachedCriteria.forClass(Sample.class)
            .setProjection(Projections.projectionList().add(Projections.property("id"))
                    .add(Projections.property("name")).add(Projections.property("createdDate")));
    if (!StringUtils.isEmpty(sampleName)) {
        TextMatchMode nameMatchMode = new TextMatchMode(sampleName);
        crit.add(Restrictions.ilike("name", nameMatchMode.getUpdatedText(), nameMatchMode.getMatchMode()));
    }/*from  www.ja  va2s.  c o  m*/
    if (!StringUtils.isEmpty(samplePointOfContact)) {
        TextMatchMode pocMatchMode = new TextMatchMode(samplePointOfContact);
        Disjunction disjunction = Restrictions.disjunction();
        crit.createAlias("primaryPointOfContact", "pointOfContact");
        crit.createAlias("pointOfContact.organization", "organization");
        crit.createAlias("otherPointOfContactCollection", "otherPoc", CriteriaSpecification.LEFT_JOIN);
        crit.createAlias("otherPoc.organization", "otherOrg", CriteriaSpecification.LEFT_JOIN);
        String[] critStrs = { "pointOfContact.lastName", "pointOfContact.firstName", "pointOfContact.role",
                "organization.name", "otherPoc.lastName", "otherPoc.firstName", "otherOrg.name" };
        for (String critStr : critStrs) {
            Criterion pocCrit = Restrictions.ilike(critStr, pocMatchMode.getUpdatedText(),
                    pocMatchMode.getMatchMode());
            disjunction.add(pocCrit);
        }
        crit.add(disjunction);
    }

    // join composition
    if (nanomaterialEntityClassNames != null && nanomaterialEntityClassNames.length > 0
            || otherNanomaterialEntityTypes != null && otherNanomaterialEntityTypes.length > 0
            || functionClassNames != null && functionClassNames.length > 0
            || otherFunctionTypes != null && otherFunctionTypes.length > 0
            || functionalizingEntityClassNames != null && functionalizingEntityClassNames.length > 0
            || otherFunctionalizingEntityTypes != null && otherFunctionalizingEntityTypes.length > 0) {
        crit.createAlias("sampleComposition", "comp", CriteriaSpecification.LEFT_JOIN);
    }
    // join nanomaterial entity
    if (nanomaterialEntityClassNames != null && nanomaterialEntityClassNames.length > 0
            || otherNanomaterialEntityTypes != null && otherNanomaterialEntityTypes.length > 0
            || functionClassNames != null && functionClassNames.length > 0
            || otherFunctionTypes != null && otherFunctionTypes.length > 0) {
        crit.createAlias("comp.nanomaterialEntityCollection", "nanoEntity", CriteriaSpecification.LEFT_JOIN);
    }

    // join functionalizing entity
    if (functionalizingEntityClassNames != null && functionalizingEntityClassNames.length > 0
            || otherFunctionalizingEntityTypes != null && otherFunctionalizingEntityTypes.length > 0
            || functionClassNames != null && functionClassNames.length > 0
            || otherFunctionTypes != null && otherFunctionTypes.length > 0) {
        crit.createAlias("comp.functionalizingEntityCollection", "funcEntity", CriteriaSpecification.LEFT_JOIN);
    }

    // nanomaterial entity
    if (nanomaterialEntityClassNames != null && nanomaterialEntityClassNames.length > 0
            || otherNanomaterialEntityTypes != null && otherNanomaterialEntityTypes.length > 0
            || functionClassNames != null && functionClassNames.length > 0
            || otherFunctionTypes != null && otherFunctionTypes.length > 0) {
        Disjunction disjunction = Restrictions.disjunction();
        if (nanomaterialEntityClassNames != null && nanomaterialEntityClassNames.length > 0) {
            Criterion nanoEntityCrit = Restrictions.in("nanoEntity.class", nanomaterialEntityClassNames);
            disjunction.add(nanoEntityCrit);
        }
        if (otherNanomaterialEntityTypes != null && otherNanomaterialEntityTypes.length > 0) {
            Criterion otherNanoCrit1 = Restrictions.eq("nanoEntity.class", "OtherNanomaterialEntity");
            Criterion otherNanoCrit2 = Restrictions.in("nanoEntity.type", otherNanomaterialEntityTypes);
            Criterion otherNanoCrit = Restrictions.and(otherNanoCrit1, otherNanoCrit2);
            disjunction.add(otherNanoCrit);
        }
        crit.add(disjunction);
    }

    // functionalizing entity
    // need to turn class names into integers in order for the .class
    // clause to work
    if (functionalizingEntityClassNames != null && functionalizingEntityClassNames.length > 0
            || otherFunctionalizingEntityTypes != null && otherFunctionalizingEntityTypes.length > 0
            || functionClassNames != null && functionClassNames.length > 0
            || otherFunctionTypes != null && otherFunctionTypes.length > 0) {
        Disjunction disjunction = Restrictions.disjunction();
        if (functionalizingEntityClassNames != null && functionalizingEntityClassNames.length > 0) {
            Integer[] functionalizingEntityClassNameIntegers = this
                    .convertToFunctionalizingEntityClassOrderNumber(functionalizingEntityClassNames);
            Criterion funcEntityCrit = Restrictions.in("funcEntity.class",
                    functionalizingEntityClassNameIntegers);
            disjunction.add(funcEntityCrit);
        }
        if (otherFunctionalizingEntityTypes != null && otherFunctionalizingEntityTypes.length > 0) {
            Integer classOrderNumber = Constants.FUNCTIONALIZING_ENTITY_SUBCLASS_ORDER_MAP
                    .get("OtherFunctionalizingEntity");
            Criterion otherFuncCrit1 = Restrictions.eq("funcEntity.class", classOrderNumber);
            Criterion otherFuncCrit2 = Restrictions.in("funcEntity.type", otherFunctionalizingEntityTypes);
            Criterion otherFuncCrit = Restrictions.and(otherFuncCrit1, otherFuncCrit2);
            disjunction.add(otherFuncCrit);
        }
        crit.add(disjunction);
    }

    // function
    if (functionClassNames != null && functionClassNames.length > 0
            || otherFunctionTypes != null && otherFunctionTypes.length > 0) {
        Disjunction disjunction = Restrictions.disjunction();
        crit.createAlias("nanoEntity.composingElementCollection", "compElement",
                CriteriaSpecification.LEFT_JOIN).createAlias("compElement.inherentFunctionCollection", "inFunc",
                        CriteriaSpecification.LEFT_JOIN);
        crit.createAlias("funcEntity.functionCollection", "func", CriteriaSpecification.LEFT_JOIN);
        if (functionClassNames != null && functionClassNames.length > 0) {
            Criterion funcCrit1 = Restrictions.in("inFunc.class", functionClassNames);
            Criterion funcCrit2 = Restrictions.in("func.class", functionClassNames);
            disjunction.add(funcCrit1).add(funcCrit2);
        }
        if (otherFunctionTypes != null && otherFunctionTypes.length > 0) {
            Criterion otherFuncCrit1 = Restrictions.and(Restrictions.eq("inFunc.class", "OtherFunction"),
                    Restrictions.in("inFunc.type", otherFunctionTypes));
            Criterion otherFuncCrit2 = Restrictions.and(Restrictions.eq("func.class", "OtherFunction"),
                    Restrictions.in("func.type", otherFunctionTypes));
            disjunction.add(otherFuncCrit1).add(otherFuncCrit2);
        }
        crit.add(disjunction);
    }

    // join characterization
    if (characterizationClassNames != null && characterizationClassNames.length > 0
            || otherCharacterizationTypes != null && otherCharacterizationTypes.length > 0
            || wordList != null && wordList.length > 0) {
        crit.createAlias("characterizationCollection", "chara", CriteriaSpecification.LEFT_JOIN);
    }
    // characterization
    if (characterizationClassNames != null && characterizationClassNames.length > 0
            || otherCharacterizationTypes != null && otherCharacterizationTypes.length > 0) {
        Disjunction disjunction = Restrictions.disjunction();
        if (characterizationClassNames != null && characterizationClassNames.length > 0) {
            Criterion charCrit = Restrictions.in("chara.class", characterizationClassNames);
            disjunction.add(charCrit);
        }
        if (otherCharacterizationTypes != null && otherCharacterizationTypes.length > 0) {
            Criterion otherCharCrit1 = Restrictions.eq("chara.class", "OtherCharacterization");
            Criterion otherCharCrit2 = Restrictions.in("chara.name", otherCharacterizationTypes);
            Criterion otherCharCrit = Restrictions.and(otherCharCrit1, otherCharCrit2);
            disjunction.add(otherCharCrit);
        }
        crit.add(disjunction);
    }
    // join keyword, finding, publication
    if (wordList != null && wordList.length > 0) {
        crit.createAlias("keywordCollection", "keyword1", CriteriaSpecification.LEFT_JOIN);
        crit.createAlias("chara.findingCollection", "finding", CriteriaSpecification.LEFT_JOIN)
                .createAlias("finding.fileCollection", "charFile", CriteriaSpecification.LEFT_JOIN)
                .createAlias("charFile.keywordCollection", "keyword2", CriteriaSpecification.LEFT_JOIN);
        // publication keywords
        crit.createAlias("publicationCollection", "pub1", CriteriaSpecification.LEFT_JOIN);
        crit.createAlias("pub1.keywordCollection", "keyword3", CriteriaSpecification.LEFT_JOIN);
    }

    // keyword
    if (wordList != null && wordList.length > 0) {
        Disjunction disjunction = Restrictions.disjunction();
        for (String keyword : wordList) {
            // strip wildcards from either ends of keyword
            keyword = StringUtils.stripWildcards(keyword);
            Criterion keywordCrit1 = Restrictions.ilike("keyword1.name", keyword, MatchMode.ANYWHERE);
            Criterion keywordCrit2 = Restrictions.ilike("keyword2.name", keyword, MatchMode.ANYWHERE);
            Criterion keywordCrit3 = Restrictions.ilike("keyword3.name", keyword, MatchMode.ANYWHERE);
            disjunction.add(keywordCrit1);
            disjunction.add(keywordCrit2);
            disjunction.add(keywordCrit3);
        }
        for (String word : wordList) {
            Criterion summaryCrit1 = Restrictions.ilike("chara.designMethodsDescription", word,
                    MatchMode.ANYWHERE);
            Criterion summaryCrit2 = Restrictions.ilike("charFile.description", word, MatchMode.ANYWHERE);
            Criterion summaryCrit = Restrictions.or(summaryCrit1, summaryCrit2);
            disjunction.add(summaryCrit);
        }
        crit.add(disjunction);
    }

    CaNanoLabApplicationService appService = (CaNanoLabApplicationService) ApplicationServiceProvider
            .getApplicationService();

    List results = appService.query(crit);

    int resSize = results.size();
    /*
     * We will look only though the maximum amount of allowed records to avoid Exceptions
     */
    int maxCount = appService.getMaxRecordsCount();
    Set<Sample> samples = new HashSet<Sample>();
    for (int i = 0; (i < resSize) && (i < maxCount); i++) {
        try {
            /*
             * There is a bug when searching with keyword "tes", where the following line
             * whould trigger a ClassCastException. Reason unknow but suspected to be reaching
             * the last row of a dataset. 
             */
            //            Object[] row = (Object[]) obj;
            Object[] row = (Object[]) results.get(i);

            Long sampleId = (Long) row[0];
            if (springSecurityAclService.currentUserHasReadPermission(sampleId,
                    SecureClassesEnum.SAMPLE.getClazz())
                    || springSecurityAclService.currentUserHasWritePermission(sampleId,
                            SecureClassesEnum.SAMPLE.getClazz())) {
                Sample sample = new Sample();
                sample.setId(sampleId);
                sample.setName((String) row[1]);
                sample.setCreatedDate((Date) row[2]);
                samples.add(sample);
            } else {
                logger.debug("User doesn't have access to sample of ID: " + sampleId);
            }

        } catch (ClassCastException e) {
            logger.error("Got ClassCastException: " + e.getMessage());
            break;
        }
    }

    List<Sample> orderedSamples = new ArrayList<Sample>(samples);
    // Collections.sort(orderedSamples,
    // Collections.reverseOrder(new Comparators.SampleDateComparator()));

    Collections.sort(orderedSamples, new Comparators.SampleDateComparator());

    for (Sample sample : orderedSamples) {
        sampleIds.add(sample.getId().toString());
    }

    return sampleIds;
}

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

License:BSD License

public List<String> findSampleIdsByOwner(String currentOwner) throws Exception {
    List<String> sampleIds = new ArrayList<String>();

    // can't query for the entire Sample object due to
    // limitations in pagination in SDK
    // Sample sample = new Sample();
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("id"));
    DetachedCriteria crit = DetachedCriteria.forClass(Sample.class);
    crit.setProjection(Projections.distinct(projectionList));
    Criterion crit1 = Restrictions.eq("createdBy", currentOwner);
    // in case of copy createdBy is like lijowskim:COPY
    Criterion crit2 = Restrictions.like("createdBy", currentOwner + ":", MatchMode.START);
    crit.add(Expression.or(crit1, crit2));
    CaNanoLabApplicationService appService = (CaNanoLabApplicationService) ApplicationServiceProvider
            .getApplicationService();/*from w ww.  j  av  a  2  s  . c  o  m*/

    List results = appService.query(crit);
    for (int i = 0; i < results.size(); i++) {
        String id = results.get(i).toString();
        if (springSecurityAclService.currentUserHasReadPermission(Long.valueOf(id),
                SecureClassesEnum.SAMPLE.getClazz())
                || springSecurityAclService.currentUserHasWritePermission(Long.valueOf(id),
                        SecureClassesEnum.SAMPLE.getClazz())) {
            sampleIds.add(id);
        } else {
            logger.debug("User doesn't have access to sample of ID: " + id);
        }
    }
    return sampleIds;
}

From source file:gov.nih.nci.eagle.util.UILookupLoader.java

License:BSD License

private void init() {
    valueMap = new HashMap<String, List<String>>();
    hibernateTemplate.execute(new HibernateCallback() {
        public Object doInHibernate(Session sess) throws HibernateException, SQLException {

            Criteria criteria = sess.createCriteria(Lifestyle.class);
            criteria.setProjection(Projections.distinct(Projections.property(RESIDENTIAL_AREA)));
            List list = criteria.list();
            list.remove(null);//from   w w w .j ava2  s.  c o  m
            valueMap.put(RESIDENTIAL_AREA, list);
            return null;
        }
    });
}

From source file:grails.orm.HibernateCriteriaBuilder.java

License:Apache License

/**
 * A projection that selects a property name
 * @param propertyName The name of the property
 * @param alias The alias to use// w  w w  . j  a  va 2  s.c  o  m
 */
public org.grails.datastore.mapping.query.api.Projections property(String propertyName, String alias) {
    final PropertyProjection propertyProjection = Projections.property(calculatePropertyName(propertyName));
    addProjectionToList(propertyProjection, alias);
    return this;
}

From source file:grails.orm.HibernateCriteriaBuilder.java

License:Apache License

/**
 * A projection that selects a distince property name
 * @param propertyName The property name
 * @param alias The alias to use//w w w  .  ja va  2s  .  c  o m
 */
public org.grails.datastore.mapping.query.api.Projections distinct(String propertyName, String alias) {
    final Projection proj = Projections.distinct(Projections.property(calculatePropertyName(propertyName)));
    addProjectionToList(proj, alias);
    return this;
}

From source file:grails.orm.HibernateCriteriaBuilder.java

License:Apache License

/**
 * A distinct projection that takes a list
 *
 * @param propertyNames The list of distince property names
 * @param alias The alias to use// w  w  w .ja v a  2  s  . c o m
 */
@SuppressWarnings("rawtypes")
public org.grails.datastore.mapping.query.api.Projections distinct(Collection propertyNames, String alias) {
    ProjectionList list = Projections.projectionList();
    for (Object o : propertyNames) {
        list.add(Projections.property(calculatePropertyName(o.toString())));
    }
    final Projection proj = Projections.distinct(list);
    addProjectionToList(proj, alias);
    return this;
}

From source file:herudi.implement.implCustomer.java

@Override
public List<DiscountCode> selectCode() {
    Criteria cr = csf.getSf().openSession().createCriteria(DiscountCode.class);
    cr.setProjection(Projections.property("discountCode"));
    return cr.list();
}