Example usage for org.hibernate.criterion Projections distinct

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

Introduction

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

Prototype

public static Projection distinct(Projection projection) 

Source Link

Document

Create a distinct projection from a projection.

Usage

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.  j  a v  a  2 s  . co  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.sample.helper.AdvancedSampleServiceHelper.java

License:BSD License

/**
 * Find sample names based on advanced search parameters
 * //  w  ww .ja  v a  2  s  . c  om
 * @param searchBean
 * @return
 * @throws Exception
 */
public List<String> findSampleIdsByAdvancedSearch(AdvancedSampleSearchBean searchBean) throws Exception {
    List<String> sampleIds = new ArrayList<String>();
    CaNanoLabApplicationService appService = (CaNanoLabApplicationService) ApplicationServiceProvider
            .getApplicationService();
    // AND or all empty
    if (searchBean.getLogicalOperator().equals("and")
            || searchBean.getSampleQueries().isEmpty() && searchBean.getCharacterizationQueries().isEmpty()
                    && searchBean.getCompositionQueries().isEmpty()) {
        DetachedCriteria crit = DetachedCriteria.forClass(Sample.class, "rootCrit")
                .setProjection(Projections.distinct(Property.forName("id")));
        setSampleCriteria(searchBean, crit);
        setCompositionCriteria(searchBean, crit);
        setCharacterizationCriteria(searchBean, crit);
        List results = appService.query(crit);
        for (int i = 0; i < results.size(); i++) {
            String sampleId = results.get(i).toString();
            sampleIds.add(sampleId);
        }
    }
    // OR union the results
    else {
        Set<String> sampleIdSet = new HashSet<String>();
        // sample
        if (!searchBean.getSampleQueries().isEmpty()) {
            DetachedCriteria crit = DetachedCriteria.forClass(Sample.class, "rootCrit")
                    .setProjection(Projections.distinct(Property.forName("id")));
            setSampleCriteria(searchBean, crit);
            List results = appService.query(crit);
            for (int i = 0; i < results.size(); i++) {
                String sampleId = results.get(i).toString();
                sampleIds.add(sampleId);
            }
        }
        // composition
        if (!searchBean.getCompositionQueries().isEmpty()) {
            if (searchBean.getCompositionLogicalOperator().equals("and")) {
                for (CompositionQueryBean query : searchBean.getCompositionQueries()) {
                    List<String> subSampleIds = new ArrayList<String>();
                    DetachedCriteria crit = DetachedCriteria.forClass(Sample.class, "rootCrit")
                            .setProjection(Projections.distinct(Property.forName("id")));
                    setSampleCriteria(searchBean, crit);
                    setCharacterizationCriteria(searchBean, crit);
                    setCompositionCriteriaBase(searchBean, crit);

                    if (query.getCompositionType().equals("function")) {
                        DetachedCriteria subCrit = getFunctionSubquery(query, "inherentFunction.", "function.",
                                "id");
                        crit.add(Subqueries.exists(subCrit));
                    } else if (query.getCompositionType().equals("nanomaterial entity")) {
                        DetachedCriteria subCrit = getNanomaterialEntitySubquery(query, "nanoEntity.", "id");
                        crit.add(Subqueries.exists(subCrit));
                    } else if (query.getCompositionType().equals("functionalizing entity")) {
                        DetachedCriteria subCrit = getFunctionalizingEntitySubquery(query, "funcEntity.", "id");
                        crit.add(Subqueries.exists(subCrit));
                    }

                    List results = appService.query(crit);
                    for (int i = 0; i < results.size(); i++) {
                        String sampleId = results.get(i).toString();
                        subSampleIds.add(sampleId);
                    }

                    if (sampleIds.size() > 0)
                        sampleIds.retainAll(subSampleIds);
                    else
                        sampleIds.addAll(subSampleIds);
                }
            } else {
                DetachedCriteria crit = DetachedCriteria.forClass(Sample.class, "rootCrit")
                        .setProjection(Projections.distinct(Property.forName("id")));
                setCompositionCriteria(searchBean, crit);
                List results = appService.query(crit);
                for (int i = 0; i < results.size(); i++) {
                    String sampleId = results.get(i).toString();
                    sampleIds.add(sampleId);
                }
            }
        }
        if (!searchBean.getCharacterizationQueries().isEmpty()) {
            // characterization
            DetachedCriteria crit = DetachedCriteria.forClass(Sample.class, "rootCrit")
                    .setProjection(Projections.distinct(Property.forName("id")));
            setCharacterizationCriteria(searchBean, crit);
            List results = appService.query(crit);
            for (int i = 0; i < results.size(); i++) {
                String sampleId = results.get(i).toString();
                sampleIds.add(sampleId);
            }
        }
    }

    //filter out redundant ones and non-accessible ones
    List<String> filteredSampleIds = new ArrayList<String>();
    for (String sampleId : sampleIds) {
        if (!filteredSampleIds.contains(sampleId) && (springSecurityAclService
                .currentUserHasReadPermission(Long.valueOf(sampleId), SecureClassesEnum.SAMPLE.getClazz())
                || springSecurityAclService.currentUserHasWritePermission(Long.valueOf(sampleId),
                        SecureClassesEnum.SAMPLE.getClazz()))) {
            filteredSampleIds.add(sampleId);
        } else { // ignore no access exception
            logger.debug("User doesn't have access to sample with id " + sampleId);
        }
    }
    return filteredSampleIds;
}

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

License:BSD License

/**
 * Find sample names based on advanced search parameters
 * //  w  w w. j  a  va  2s  . c  o  m
 * @param searchBean
 * @return
 * @throws Exception
 */
public Map<String, String> findSampleIdNamesByAdvancedSearch(AdvancedSampleSearchBean searchBean)
        throws Exception {
    List<String> sampleIds = new ArrayList<String>();

    Map<String, String> sampleIdNameMap = new HashMap<String, String>();

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

    ProjectionList projList = Projections.projectionList();
    projList.add(Property.forName("id"));
    projList.add(Property.forName("name"));

    // AND or all empty
    if (searchBean.getLogicalOperator().equals("and")
            || searchBean.getSampleQueries().isEmpty() && searchBean.getCharacterizationQueries().isEmpty()
                    && searchBean.getCompositionQueries().isEmpty()) {
        DetachedCriteria crit = DetachedCriteria.forClass(Sample.class, "rootCrit")
                .setProjection(Projections.distinct(projList));
        //Projections.distinct(Property.forName("id")));
        setSampleCriteria(searchBean, crit);
        setCompositionCriteria(searchBean, crit);
        setCharacterizationCriteria(searchBean, crit);
        List results = appService.query(crit);
        for (int i = 0; i < results.size(); i++) {

            Object[] row = (Object[]) results.get(i);
            String id = row[0].toString();
            String name = row[1].toString();

            logger.debug("id is: " + id);
            logger.debug("name is: " + name);
            String sampleId = id;

            sampleIds.add(sampleId);

            sampleIdNameMap.put(id, name);

        }
    }
    // OR union the results
    else {
        Set<String> sampleIdSet = new HashSet<String>();
        // sample
        if (!searchBean.getSampleQueries().isEmpty()) {

            //            ProjectionList projList = Projections.projectionList();
            //            projList.add(Property.forName("id"));
            //            projList.add(Property.forName("name"));

            DetachedCriteria crit = DetachedCriteria.forClass(Sample.class, "rootCrit")
                    .setProjection(Projections.distinct(projList));
            //Projections.distinct(Property.forName("id")));
            setSampleCriteria(searchBean, crit);
            List results = appService.query(crit);
            for (int i = 0; i < results.size(); i++) {

                Object[] row = (Object[]) results.get(i);
                String id = row[0].toString();
                String name = row[1].toString();
                logger.debug("id is: " + id);
                logger.debug("name is: " + name);
                String sampleId = id;
                sampleIds.add(sampleId);

                sampleIdNameMap.put(id, name);
            }
        }

        //         // composition
        if (!searchBean.getCompositionQueries().isEmpty()) {
            if (searchBean.getCompositionLogicalOperator().equals("and")) {
                for (CompositionQueryBean query : searchBean.getCompositionQueries()) {
                    List<String> subSampleIds = new ArrayList<String>();
                    DetachedCriteria crit = DetachedCriteria.forClass(Sample.class, "rootCrit")
                            .setProjection(Projections.distinct(projList));
                    //Projections.distinct(Property.forName("id")));
                    setSampleCriteria(searchBean, crit);
                    setCharacterizationCriteria(searchBean, crit);
                    setCompositionCriteriaBase(searchBean, crit);

                    if (query.getCompositionType().equals("function")) {
                        DetachedCriteria subCrit = getFunctionSubquery(query, "inherentFunction.", "function.",
                                "id");
                        crit.add(Subqueries.exists(subCrit));
                    } else if (query.getCompositionType().equals("nanomaterial entity")) {
                        DetachedCriteria subCrit = getNanomaterialEntitySubquery(query, "nanoEntity.", "id");
                        crit.add(Subqueries.exists(subCrit));
                    } else if (query.getCompositionType().equals("functionalizing entity")) {
                        DetachedCriteria subCrit = getFunctionalizingEntitySubquery(query, "funcEntity.", "id");
                        crit.add(Subqueries.exists(subCrit));
                    }

                    List results = appService.query(crit);
                    for (int i = 0; i < results.size(); i++) {
                        Object[] row = (Object[]) results.get(i);
                        String id = row[0].toString();
                        String name = row[1].toString();
                        logger.debug("id is: " + id);
                        logger.debug("name is: " + name);
                        String sampleId = id;
                        //String sampleId = obj.toString();
                        subSampleIds.add(sampleId);

                        sampleIdNameMap.put(id, name);
                    }

                    if (sampleIds.size() > 0)
                        sampleIds.retainAll(subSampleIds);
                    else
                        sampleIds.addAll(subSampleIds);
                }
            } else {
                DetachedCriteria crit = DetachedCriteria.forClass(Sample.class, "rootCrit")
                        .setProjection(Projections.distinct(projList));
                //Projections.distinct(Property.forName("id")));
                setCompositionCriteria(searchBean, crit);
                List results = appService.query(crit);
                for (int i = 0; i < results.size(); i++) {

                    Object[] row = (Object[]) results.get(i);
                    String id = row[0].toString();
                    String name = row[1].toString();
                    logger.debug("id is: " + id);
                    logger.debug("name is: " + name);
                    String sampleId = id;
                    //String sampleId = obj.toString();
                    sampleIds.add(sampleId);

                    sampleIdNameMap.put(id, name);
                }
            }
        }
        if (!searchBean.getCharacterizationQueries().isEmpty()) {
            // characterization
            DetachedCriteria crit = DetachedCriteria.forClass(Sample.class, "rootCrit")
                    .setProjection(Projections.distinct(projList));
            //Projections.distinct(Property.forName("id")));
            setCharacterizationCriteria(searchBean, crit);
            List results = appService.query(crit);
            for (int i = 0; i < results.size(); i++) {
                Object[] row = (Object[]) results.get(i);
                String id = row[0].toString();
                String name = row[1].toString();
                logger.debug("id is: " + id);
                logger.debug("name is: " + name);
                String sampleId = id;
                //String sampleId = obj.toString();
                sampleIds.add(sampleId);
                sampleIdNameMap.put(id, name);
            }
        }
    }

    Iterator<String> ite = sampleIdNameMap.keySet().iterator();
    while (ite.hasNext()) {
        Long sampleId = Long.valueOf(ite.next());
        if (!springSecurityAclService.currentUserHasReadPermission(sampleId,
                SecureClassesEnum.SAMPLE.getClazz())
                && !springSecurityAclService.currentUserHasWritePermission(sampleId,
                        SecureClassesEnum.SAMPLE.getClazz())) {
            logger.debug("User doesn't have access to sample with id " + sampleId);
            ite.remove();
        }
    }

    //filter out redundant ones and non-accessible ones
    //      List<String>filteredSampleIds=new ArrayList<String>();
    //      for (String sampleId: sampleIds) {
    //         if (!filteredSampleIds.contains(sampleId)
    //               && StringUtils.containsIgnoreCase(getAccessibleData(),
    //                     sampleId)) {
    //            filteredSampleIds.add(sampleId);
    //         } else { // ignore no access exception
    //            logger.debug("User doesn't have access to sample with id "
    //                  + sampleId);
    //         }
    //      }

    return sampleIdNameMap;
}

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

License:BSD License

private DetachedCriteria getDatumSubquery(CharacterizationQueryBean charQuery, String projectionProperty) {
    DetachedCriteria subCrit = DetachedCriteria.forClass(Datum.class, "subCrit");
    subCrit.setProjection(Projections.distinct(Property.forName(projectionProperty)));
    Criterion datumCrit = getDatumCriterion(charQuery);
    subCrit.add(datumCrit);//from   w  w w . j av a2s.  c o  m
    subCrit.add(Restrictions.eqProperty("subCrit." + projectionProperty, "rootCrit.id"));
    return subCrit;
}

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

License:BSD License

private DetachedCriteria getCharacterizationSubquery(CharacterizationQueryBean charQuery,
        String projectionProperty) {
    DetachedCriteria subCrit = DetachedCriteria.forClass(Characterization.class, "subCrit");
    subCrit.setProjection(Projections.distinct(Property.forName(projectionProperty)));
    // join finding and datum
    if (!StringUtils.isEmpty(charQuery.getDatumName())) {
        subCrit.createAlias("subCrit.findingCollection", "finding", CriteriaSpecification.LEFT_JOIN);
        subCrit.createAlias("finding.datumCollection", "datum", CriteriaSpecification.LEFT_JOIN);
    }//from www  .  j  ava2 s  . co  m
    Criterion charCrit = getCharacterizationCriterion(charQuery, "");
    subCrit.add(charCrit);
    subCrit.add(Restrictions.eqProperty("subCrit." + projectionProperty, "rootCrit.id"));
    return subCrit;
}

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

License:BSD License

private DetachedCriteria getFunctionalizingEntitySubquery(CompositionQueryBean query, String funcEntityAlias,
        String projectionProperty) throws Exception {
    DetachedCriteria subCrit = DetachedCriteria.forClass(FunctionalizingEntity.class, "subCrit");
    subCrit.setProjection(Projections.distinct(Property.forName(projectionProperty)));
    Criterion funcCrit = getFunctionalizingEntityCriterion(query, "");
    subCrit.add(funcCrit);//from  ww w.  j  a  va2s.com
    subCrit.add(Restrictions.eqProperty("subCrit." + projectionProperty, funcEntityAlias + "id"));
    return subCrit;
}

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

License:BSD License

private DetachedCriteria getFunctionSubquery(CompositionQueryBean query, String funcAlias1, String funcAlias2,
        String projectionProperty) throws Exception {
    DetachedCriteria subCrit = DetachedCriteria.forClass(Function.class, "subCrit");
    subCrit.setProjection(Projections.distinct(Property.forName(projectionProperty)));
    Criterion funcCrit = getFunctionCriterion(query, "", "");
    subCrit.add(funcCrit);//from  www. j a v  a  2  s  .co  m
    if (funcAlias1.equals(funcAlias2)) {
        subCrit.add(Restrictions.eqProperty("subCrit." + projectionProperty, funcAlias1 + "id"));
    } else {
        subCrit.add(Restrictions.or(Restrictions.eqProperty("subCrit." + projectionProperty, funcAlias1 + "id"),
                Restrictions.eqProperty("subCrit." + projectionProperty, funcAlias2 + "id")));
    }
    return subCrit;
}

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

License:BSD License

private DetachedCriteria getNanomaterialEntitySubquery(CompositionQueryBean query, String nanoEntityAlias,
        String projectionProperty) throws Exception {
    DetachedCriteria subCrit = DetachedCriteria.forClass(NanomaterialEntity.class, "subCrit");
    subCrit.setProjection(Projections.distinct(Property.forName(projectionProperty)));
    // join composing element
    if (!StringUtils.isEmpty(query.getChemicalName())) {
        subCrit.createAlias("subCrit.composingElementCollection", "compElement",
                CriteriaSpecification.LEFT_JOIN);
    }/*from   w  w  w.j a va2  s.c o  m*/
    Criterion nanoCrit = getNanomaterialEntityCriterion(query, "");
    subCrit.add(nanoCrit);
    subCrit.add(Restrictions.eqProperty("subCrit." + projectionProperty, nanoEntityAlias + "id"));
    return subCrit;
}

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

License:BSD License

private DetachedCriteria getPointOfContactSubquery(SampleQueryBean query, String pocAlias1, String pocAlias2,
        String projectionProperty) {
    DetachedCriteria subCrit = DetachedCriteria.forClass(PointOfContact.class, "subCrit");
    subCrit.createAlias("subCrit.organization", "organization", CriteriaSpecification.LEFT_JOIN);
    subCrit.setProjection(Projections.distinct(Property.forName("id")));
    Disjunction pocDisjunction = getPointOfContactDisjunctionPerQuery(query, "", "");
    subCrit.add(pocDisjunction);// w w  w.  j a  v  a 2s  . com
    if (pocAlias1.equals(pocAlias2)) {
        subCrit.add(Restrictions.eqProperty("subCrit." + projectionProperty, pocAlias1 + "id"));
    } else {
        subCrit.add(Restrictions.or(Restrictions.eqProperty("subCrit." + projectionProperty, pocAlias1 + "id"),
                Restrictions.eqProperty("subCrit." + projectionProperty, pocAlias2 + "id")));
    }
    return subCrit;
}

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

License:BSD License

public int getNumberOfPublicCharacterizations(String characterizationClassName) throws Exception {
    CaNanoLabApplicationService appService = (CaNanoLabApplicationService) ApplicationServiceProvider
            .getApplicationService();//from w w  w.j ava2 s. c o m
    DetachedCriteria crit = DetachedCriteria.forClass(ClassUtils.getFullClass(characterizationClassName))
            .setProjection(Projections.distinct(Property.forName("id")));
    List results = appService.query(crit);
    int count = 0;
    for (int i = 0; i < results.size(); i++) {
        Long id = Long.valueOf(results.get(i).toString());
        if (springSecurityAclService.checkObjectPublic(id, SecureClassesEnum.CHAR.getClazz()))
            count++;
    }
    return count;
}