Example usage for org.hibernate.criterion Restrictions disjunction

List of usage examples for org.hibernate.criterion Restrictions disjunction

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions disjunction.

Prototype

public static Disjunction disjunction() 

Source Link

Document

Group expressions together in a single disjunction (A or B or C...).

Usage

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

License:BSD License

/**
 * Set the disjunction used in point of contact queries
 * /*w w w. j av  a2s  . c om*/
 *
 */
private Disjunction getPointOfContactDisjunction(AdvancedSampleSearchBean searchBean, String pocAlias,
        String otherPOCAlias) {
    Disjunction disjunction = Restrictions.disjunction();
    for (SampleQueryBean query : searchBean.getSampleQueries()) {
        if (query.getNameType().equals("point of contact name")) {
            Disjunction pocDisjunction = getPointOfContactDisjunctionPerQuery(query, pocAlias, otherPOCAlias);
            if (pocDisjunction != null)
                disjunction.add(pocDisjunction);
        }
    }
    return disjunction;
}

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

License:BSD License

private Disjunction getPointOfContactDisjunctionPerQuery(SampleQueryBean query, String pocAlias,
        String otherPOCAlias) {//from  ww  w . ja  va2s  .  c o m
    String pocCritStrs[] = null;
    if (StringUtils.isEmpty(otherPOCAlias)) {
        pocCritStrs = new String[] { pocAlias + "lastName", pocAlias + "firstName", "organization.name" };
    } else {
        pocCritStrs = new String[] { pocAlias + "lastName", pocAlias + "firstName", "organization.name",
                otherPOCAlias + "lastName", otherPOCAlias + "firstName", "otherOrg.name" };
    }
    TextMatchMode nameMatchMode = null;
    if (query.getOperand().equals("equals")) {
        nameMatchMode = new TextMatchMode(query.getName());
    } else if (query.getOperand().equals(Constants.STRING_OPERAND_CONTAINS)) {
        nameMatchMode = new TextMatchMode("*" + query.getName() + "*");
    }
    Disjunction pocDisjunction = Restrictions.disjunction();
    for (String critStr : pocCritStrs) {
        Criterion pocCrit = Restrictions.ilike(critStr, nameMatchMode.getUpdatedText(),
                nameMatchMode.getMatchMode());
        pocDisjunction.add(pocCrit);
    }
    return pocDisjunction;
}

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

License:BSD License

/**
 * Get the sample name junction used in sample queries
 * // www .  j  a va  2  s .  c o  m
 *
 */
private Junction getSampleNameJunction(AdvancedSampleSearchBean searchBean) throws Exception {
    Disjunction sampleDisjunction = Restrictions.disjunction();
    Conjunction sampleConjunction = Restrictions.conjunction();
    for (SampleQueryBean query : searchBean.getSampleQueries()) {
        if (query.getNameType().equals("sample name")) {
            Criterion sampleNameCrit = getSampleNameCriterion(query);
            if (sampleNameCrit != null) {
                sampleDisjunction.add(sampleNameCrit);
                sampleConjunction.add(sampleNameCrit);
            }
        }
    }
    Junction junction = (searchBean.getSampleLogicalOperator().equals("or")
            || searchBean.getSampleQueries().size() == 1) ? sampleDisjunction : sampleConjunction;
    return junction;
}

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

License:BSD License

/**
 * Get the sample disjunction used in sample queries
 * /*from  w  ww .  ja va  2 s  .c om*/
 *
 */
private Junction getSampleJunction(AdvancedSampleSearchBean searchBean) throws Exception {
    // if there are more than one POC query in AND, don't use junction.
    if (searchBean.getSampleLogicalOperator().equals("and") && searchBean.getPocCount() > 1) {
        return null;
    }
    Disjunction sampleDisjunction = Restrictions.disjunction();
    Conjunction sampleConjunction = Restrictions.conjunction();
    for (SampleQueryBean query : searchBean.getSampleQueries()) {
        if (query.getNameType().equals("sample name")) {
            Criterion sampleNameCrit = getSampleNameCriterion(query);
            if (sampleNameCrit != null) {
                sampleDisjunction.add(sampleNameCrit);
                sampleConjunction.add(sampleNameCrit);
            }
        }
        if (query.getNameType().equals("point of contact name")) {
            Disjunction pocDisjunction = getPointOfContactDisjunction(searchBean, "poc.", "otherPOC.");
            if (pocDisjunction != null) {
                sampleDisjunction.add(pocDisjunction);
                if (searchBean.getPocCount() == 1) {
                    sampleConjunction.add(pocDisjunction);
                }
            }
        }
    }
    Junction junction = (searchBean.getSampleLogicalOperator().equals("or")
            || searchBean.getSampleQueries().size() == 1) ? sampleDisjunction : sampleConjunction;
    return junction;
}

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()));
    }// w  w w  .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

/**
 * search sampleNames based on sample name str. The str can contain just a
 * partial sample name or multiple partial names one per line
 * /*from   w ww .  j  a v a  2  s . c  o m*/
 * @param nameStr
 * @return
 * @throws Exception
 */
public List<String> findSampleNamesBy(String nameStr) throws Exception {
    CaNanoLabApplicationService appService = (CaNanoLabApplicationService) ApplicationServiceProvider
            .getApplicationService();

    DetachedCriteria crit = DetachedCriteria.forClass(Sample.class);
    if (!StringUtils.isEmpty(nameStr)) {
        // split nameStr to multiple words if needed
        List<String> nameStrs = StringUtils.parseToWords(nameStr, "\n");
        if (nameStrs.size() == 1) {
            crit.add(Restrictions.ilike("name", nameStr, MatchMode.ANYWHERE));
        } else {
            Disjunction disjunction = Restrictions.disjunction();
            for (String str : nameStrs) {
                Criterion strCrit = Restrictions.ilike("name", str, MatchMode.ANYWHERE);
                disjunction.add(strCrit);
            }
            crit.add(disjunction);
        }
    }
    List results = appService.query(crit);
    List<String> sampleNames = new ArrayList<String>();
    for (int i = 0; i < results.size(); i++) {
        Sample sample = (Sample) results.get(i);
        if (springSecurityAclService.currentUserHasReadPermission(sample.getId(),
                SecureClassesEnum.SAMPLE.getClazz())
                || springSecurityAclService.currentUserHasWritePermission(sample.getId(),
                        SecureClassesEnum.SAMPLE.getClazz())) {
            sampleNames.add(sample.getName());
        } else {
            logger.debug("User doesn't have access to sample of name: " + sample.getName());
        }
    }
    return sampleNames;
}

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

License:BSD License

public List<Sample> findSamplesBy(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   w ww.jav a  2  s.c  om
    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);
    List<Sample> samples = new ArrayList<Sample>();
    //      List<String> accessibleData = getAccessibleData();
    for (int i = 0; i < results.size(); i++) {

        try {
            Sample sample = (Sample) results.get(i);
            System.out.println("sample ID test === " + sample.getId());
            samples.add(sample);
        } 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 samples;
}

From source file:gov.nih.nci.eagle.service.handlers.EpidemiologicalQueryHandler.java

License:BSD License

private void populateFamilyHistoryCrit(FamilyHistoryCriterion familyHistcrit, Criteria targetCrit) {
    Integer lungCancerrelativeCrit = familyHistcrit.getFamilyLungCancer();
    if (lungCancerrelativeCrit != null) {

        targetCrit.add(Restrictions.eq("finding.relativeWithLungCancer", lungCancerrelativeCrit.toString()));
    }/*from  w  w  w . j ava2 s . c  om*/
    Collection<Relative> smokingRelativeCrit = familyHistcrit.getSmokingRelativeCollection();
    if (smokingRelativeCrit != null) {
        targetCrit.createAlias("finding.relativeCollection", "relatives");
        Disjunction dis = Restrictions.disjunction();
        for (Relative r : smokingRelativeCrit) {
            Conjunction con = Restrictions.conjunction();
            con.add(Restrictions.eq("relatives.relationshipType", r.getName().toUpperCase()));
            con.add(Restrictions.eq("relatives.smokingStatus", "1"));
            dis.add(con);
        }
        targetCrit.add(dis);
    }
}

From source file:gr.abiss.calipso.domain.ItemSearch.java

License:Open Source License

/**
 * Get item visibility criteria for the spaces included in the search
 * @param spaces/*  w w w  .  jav  a 2 s  .  co  m*/
 * @return
 */
private Disjunction getVisibilityCriteriaForItem(Collection<Space> spaces) {
    Disjunction itemRestrictions = Restrictions.disjunction();
    List<Space> includedSpaces = new LinkedList<Space>();
    for (Space space : spaces) {
        Short visibility = space.getItemVisibility();
        boolean isLoggedIn = user.getId() != 0;
        boolean hasRegularRoleForSpace = user.hasRegularRoleForSpace(space);
        // for spaces allowing item view for anonymous users
        if (visibility.equals(Space.ITEMS_VISIBLE_TO_ANONYMOUS_USERS)) {
            itemRestrictions.add(Restrictions.eq("space", space));
            includedSpaces.add(space);
            //logger.debug("Included space '" + space.getName() + "' since it allows anonymous");

        }
        // for spaces allowing item view for loggedin users
        else if (isLoggedIn && visibility.equals(Space.ITEMS_VISIBLE_TO_ANY_LOGGEDIN_USER)) {
            itemRestrictions.add(Restrictions.eq("space", space));
            //logger.debug("Included space '" + space.getName() + "' since the user is logged in");
        }
        // for spaces allowing item view for item reporters
        else if (isLoggedIn && (visibility.equals(Space.ITEMS_VISIBLE_TO_LOGGEDIN_REPORTERS)
                || visibility.equals(Space.ITEMS_VISIBLE_TO_LOGGEDIN_REPORTERS_NO_COMMENTS))) {
            if (hasRegularRoleForSpace) {
                itemRestrictions.add(Restrictions.eq("space", space));
                //logger.debug("Included space '" + space.getName() + "' since the user is a regular user");
            } else {
                itemRestrictions.add(Restrictions.conjunction().add(Restrictions.eq("space", space))
                        .add(Restrictions.eq("reportedBy", user)));
            }
            //logger.debug("Included space '" + space.getName() + "' but only for items reported by user");
        }
        // for spaces allowing item view users with explicit roles only
        else if (isLoggedIn /* && visibility.equals(Space.ITEMS_VISIBLE_TO_REGULAR_ROLES) */
                && hasRegularRoleForSpace) {
            itemRestrictions.add(Restrictions.eq("space", space));
            //logger.debug("Included space '" + space.getName() + "' since the user is a regular user");
        } else {
            //itemRestrictions.add(Restrictions.ne("space", space));
            //logger.debug("Did NOT include space '" + space.getName()+" user has regular roles for space: "+hasRegularRoleForSpace+", space visibility: "+space.getItemVisibility());
        }
    }
    return itemRestrictions;
}

From source file:id.web.faisalabdillah.dao.impl.GroupDaoImpl.java

License:Open Source License

public PaginationResult<Group> searchPaginated(Group group, int firstResult, int maxResult) {
    DetachedCriteria dc = DetachedCriteria.forClass(Group.class);
    Disjunction disjuncion = Restrictions.disjunction();
    if (group.getCode() != null)
        disjuncion.add(Restrictions.like("code", "%" + group.getCode() + "%"));
    if (group.getDescription() != null)
        disjuncion.add(Restrictions.like("description", group.getDescription()));
    dc.add(disjuncion);//  w  w w  .j  a va 2s  .  c om
    return new PaginationResult<Group>(getHibernateTemplate().findByCriteria(dc, firstResult, maxResult),
            getResultSize(dc), firstResult, maxResult);
}