Example usage for org.hibernate.criterion Restrictions and

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

Introduction

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

Prototype

public static LogicalExpression and(Criterion lhs, Criterion rhs) 

Source Link

Document

Return the conjuction of two expressions

Usage

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

License:BSD License

private Criterion getCharacterizationCriterion(CharacterizationQueryBean charQuery, String charAlias) {
    String charClassName = ClassUtils.getShortClassNameFromDisplayName(charQuery.getCharacterizationName());
    Criterion charCrit = null;//from   www  . j  a  v  a  2s.c  o  m
    if (charClassName == null) {
        Criterion otherCharCrit1 = Restrictions.eq(charAlias + "class", "OtherCharacterization");
        Criterion otherCharCrit2 = Restrictions.eq(charAlias + "name", charQuery.getCharacterizationName());
        charCrit = Restrictions.and(otherCharCrit1, otherCharCrit2);
    } else {
        charCrit = Restrictions.eq(charAlias + "class", charClassName);
    }
    // assay type
    if (!StringUtils.isEmpty(charQuery.getAssayType())) {
        charCrit = Restrictions.and(charCrit,
                Restrictions.eq(charAlias + "assayType", charQuery.getAssayType()));
    }
    // datum name
    if (!StringUtils.isEmpty(charQuery.getDatumName())) {
        charCrit = Restrictions.and(charCrit, Restrictions.eq("datum.name", charQuery.getDatumName()));
    }
    // datum value
    if (!StringUtils.isEmpty(charQuery.getDatumValue())) {
        Float datumValue = new Float(charQuery.getDatumValue());
        charCrit = Restrictions.and(charCrit,
                Restrictions.eq("datum.valueUnit", charQuery.getDatumValueUnit()));
        if (charQuery.getOperand().equals("=") || charQuery.getOperand().equals("is")) {
            charCrit = Restrictions.and(charCrit, Expression.eq("datum.value", datumValue));
        } else if (charQuery.getOperand().equals(">")) {
            charCrit = Restrictions.and(charCrit, Expression.gt("datum.value", datumValue));
        } else if (charQuery.getOperand().equals(">=")) {
            charCrit = Restrictions.and(charCrit, Expression.ge("datum.value", datumValue));
        } else if (charQuery.getOperand().equals("<")) {
            charCrit = Restrictions.and(charCrit, Expression.lt("datum.value", datumValue));
        } else if (charQuery.getOperand().equals("<=")) {
            charCrit = Restrictions.and(charCrit, Expression.le("datum.value", datumValue));
        }
        // ignore the bogus place holder entered for emtpy datum/condition
        // cells
        charCrit = Restrictions.and(charCrit, Expression
                .not(Expression.ilike("datum.createdBy", Constants.PLACEHOLDER_DATUM_CONDITION_CREATED_BY)));
    }
    return charCrit;
}

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

License:BSD License

private Criterion getFunctionalizingEntityCriterion(CompositionQueryBean compQuery, String entityAlias)
        throws Exception {
    TextMatchMode chemicalNameMatchMode = null;
    if (compQuery.getOperand().equals("equals")) {
        chemicalNameMatchMode = new TextMatchMode(compQuery.getChemicalName());
    } else if (compQuery.getOperand().equals(Constants.STRING_OPERAND_CONTAINS)) {
        chemicalNameMatchMode = new TextMatchMode("*" + compQuery.getChemicalName() + "*");
    }//from   w  w  w. jav  a 2s . c o  m
    String funcEntityClassName = ClassUtils.getShortClassNameFromDisplayName(compQuery.getEntityType());
    Class clazz = ClassUtils.getFullClass("agentmaterial." + funcEntityClassName);
    Criterion funcEntityCrit = null;
    // other entity type
    if (clazz == null) {
        /*Criterion otherFuncCrit1 = Restrictions.eq(entityAlias + "class",
              "OtherFunctionalizingEntity");
        Criterion otherFuncCrit2 = Restrictions.eq(entityAlias + "type",
              compQuery.getEntityType());
        funcEntityCrit = Restrictions.and(otherFuncCrit1, otherFuncCrit2);*/

        Integer funcClassNameInteger = Constants.FUNCTIONALIZING_ENTITY_SUBCLASS_ORDER_MAP
                .get("OtherFunctionalizingEntity");
        funcEntityCrit = Restrictions.eq(entityAlias + "class", funcClassNameInteger);

    } else {
        Integer funcClassNameInteger = Constants.FUNCTIONALIZING_ENTITY_SUBCLASS_ORDER_MAP
                .get(funcEntityClassName);
        funcEntityCrit = Restrictions.eq(entityAlias + "class", funcClassNameInteger);
    }
    if (!StringUtils.isEmpty(compQuery.getChemicalName()) && !StringUtils.isEmpty(compQuery.getOperand())) {
        Criterion chemicalNameCrit = Restrictions.ilike(entityAlias + "name",
                chemicalNameMatchMode.getUpdatedText(), chemicalNameMatchMode.getMatchMode());
        funcEntityCrit = Restrictions.and(funcEntityCrit, chemicalNameCrit);
    }
    return funcEntityCrit;
}

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

License:BSD License

private Criterion getFunctionCriterion(CompositionQueryBean compQuery, String functionAlias1,
        String functionAlias2) throws Exception {
    String funcClassName = ClassUtils.getShortClassNameFromDisplayName(compQuery.getEntityType());
    Class clazz = ClassUtils.getFullClass("function." + funcClassName);
    Criterion funcCrit, funcCrit1, funcCrit2 = null;
    // other function type
    if (clazz == null) {
        if (!functionAlias1.equals(functionAlias2)) {
            // inherent function
            Criterion otherFuncCrit1 = Restrictions.eq(functionAlias1 + "class", "OtherFunction");
            Criterion otherFuncCrit2 = Restrictions.eq(functionAlias1 + "type", compQuery.getEntityType());
            funcCrit1 = Restrictions.and(otherFuncCrit1, otherFuncCrit2);
            // function
            Criterion otherFuncCrit3 = Restrictions.eq(functionAlias2 + "class", "OtherFunction");
            Criterion otherFuncCrit4 = Restrictions.eq(functionAlias2 + "type", compQuery.getEntityType());
            funcCrit2 = Restrictions.and(otherFuncCrit3, otherFuncCrit4);
            funcCrit = Restrictions.or(funcCrit1, funcCrit2);
        } else {/*from  ww w  .ja v  a  2s .  c  o m*/
            Criterion otherFuncCrit1 = Restrictions.eq(functionAlias1 + "class", "OtherFunction");
            Criterion otherFuncCrit2 = Restrictions.eq(functionAlias1 + "type", compQuery.getEntityType());
            funcCrit = Restrictions.and(otherFuncCrit1, otherFuncCrit2);
        }
    } else {
        if (!functionAlias1.equals(functionAlias2)) {
            funcCrit1 = Restrictions.eq(functionAlias1 + "class", funcClassName);
            funcCrit2 = Restrictions.eq(functionAlias2 + "class", funcClassName);
            funcCrit = Restrictions.and(funcCrit1, funcCrit2);
        } else {
            funcCrit = Restrictions.eq(functionAlias1 + "class", funcClassName);
        }
    }
    return funcCrit;
}

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

License:BSD License

private Criterion getNanomaterialEntityCriterion(CompositionQueryBean compQuery, String entityAlias)
        throws Exception {
    TextMatchMode chemicalNameMatchMode = null;
    if (compQuery.getOperand().equals("equals")) {
        chemicalNameMatchMode = new TextMatchMode(compQuery.getChemicalName());
    } else if (compQuery.getOperand().equals(Constants.STRING_OPERAND_CONTAINS)) {
        chemicalNameMatchMode = new TextMatchMode("*" + compQuery.getChemicalName() + "*");
    }/*from   ww w.ja  va2  s .  c  o  m*/
    String nanoEntityClassName = ClassUtils.getShortClassNameFromDisplayName(compQuery.getEntityType());
    Class clazz = ClassUtils.getFullClass("nanomaterial." + nanoEntityClassName);
    Criterion nanoEntityCrit = null;
    // other entity type
    if (clazz == null) {
        Criterion otherNanoCrit1 = Restrictions.eq(entityAlias + "class", "OtherNanomaterialEntity");
        Criterion otherNanoCrit2 = Restrictions.eq(entityAlias + "type", compQuery.getEntityType());
        nanoEntityCrit = Restrictions.and(otherNanoCrit1, otherNanoCrit2);
    } else {
        nanoEntityCrit = Restrictions.eq(entityAlias + "class", nanoEntityClassName);
    }
    if (!StringUtils.isEmpty(compQuery.getChemicalName()) && !StringUtils.isEmpty(compQuery.getOperand())) {
        Criterion chemicalNameCrit = Restrictions.ilike("compElement.name",
                chemicalNameMatchMode.getUpdatedText(), chemicalNameMatchMode.getMatchMode());
        nanoEntityCrit = Restrictions.and(nanoEntityCrit, chemicalNameCrit);
    }
    return nanoEntityCrit;
}

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()));
    }/*www .  j av a 2s  .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<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  w  w .ja  v a2  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:in.gov.nvli.harvester.daoImpl.HarMetadataTypeRepositoryDaoImpl.java

@Override
public HarMetadataTypeRepository getMetadataTypeRepository(HarMetadataType metadataType, HarRepo repository) {

    HarMetadataTypeRepository harMetadataTypeRepository = null;
    try {//  w  w w  . ja  v  a2s  .  co  m
        harMetadataTypeRepository = (HarMetadataTypeRepository) currentSession()
                .createCriteria(HarMetadataTypeRepository.class).createAlias("metadataTypeId", "metadataType")
                .createAlias("repoId", "repo")
                .add(Restrictions.and(Restrictions.eq("metadataType.metadataId", metadataType.getMetadataId()),
                        Restrictions.eq("repo.repoId", repository.getRepoId())))
                .uniqueResult();

    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return harMetadataTypeRepository;
}

From source file:in.gov.nvli.harvester.daoImpl.HarSetDaoImpl.java

@Override
public HarSet getHarSet(String name, String setSpec) {
    HarSet harSet = null;//  ww  w . j a  v a2 s  .co  m
    try {
        harSet = (HarSet) currentSession().createCriteria(HarSet.class)
                .add(Restrictions.and(Restrictions.eq("setName", name), Restrictions.eq("setSpec", setSpec)))
                .uniqueResult();
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return harSet;
}

From source file:in.gov.nvli.harvester.daoImpl.HarSetRecordDaoImpl.java

@Override
public HarSetRecord getHarSetRecord(HarRecord harRecord, HarSet harSet) {
    try {/*from   w  w  w.j a va  2s.c  om*/
        return (HarSetRecord) currentSession().createCriteria(HarSetRecord.class)
                .createAlias("recordId", "harRecord").createAlias("setId", "harSet")
                .add(Restrictions.and(Restrictions.eq("harRecord.recordId", harRecord.getRecordId()),
                        Restrictions.eq("harSet.setId", harSet.getSetId())))
                .uniqueResult();
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        return null;
    }

}

From source file:io.milton.vfs.db.OrgType.java

License:Open Source License

public static OrgType find(Organisation org, String name, Session session) {
    Criteria crit = session.createCriteria(OrgType.class);
    crit.add(Restrictions.and(Restrictions.eq("organisation", org), Restrictions.eq("name", name)));
    return DbUtils.unique(crit);
}