Example usage for javax.persistence.criteria MapJoin key

List of usage examples for javax.persistence.criteria MapJoin key

Introduction

In this page you can find the example usage for javax.persistence.criteria MapJoin key.

Prototype

Path<K> key();

Source Link

Document

Create a path expression that corresponds to the map key.

Usage

From source file:org.artificer.repository.hibernate.query.ArtificerToHibernateQueryVisitor.java

/**
 * @see org.artificer.common.query.xpath.visitors.XPathVisitor#visit(org.artificer.common.query.xpath.ast.FunctionCall)
 *///from  w  ww  .  j  a v  a  2 s.  c om
@Override
public void visit(FunctionCall node) {
    if (ArtificerConstants.SRAMP_NS.equals(node.getFunctionName().getNamespaceURI())) {
        if (node.getFunctionName().equals(CLASSIFIED_BY_ALL_OF)) {
            visitClassifications(node, false, true);
        } else if (node.getFunctionName().equals(CLASSIFIED_BY_ANY_OF)) {
            visitClassifications(node, true, true);
        } else if (node.getFunctionName().equals(EXACTLY_CLASSIFIED_BY_ALL_OF)) {
            visitClassifications(node, false, false);
        } else if (node.getFunctionName().equals(EXACTLY_CLASSIFIED_BY_ANY_OF)) {
            visitClassifications(node, true, false);
        } else if (node.getFunctionName().equals(GET_RELATIONSHIP_ATTRIBUTE)) {
            String otherAttributeKey = reduceStringLiteralArgument(node.getArguments().get(1));
            // Ex. query: /s-ramp/wsdl/WsdlDocument[someRelationship[s-ramp:getRelationshipAttribute(., 'someAttribute') = 'true']]
            // Note that the predicate function needs to add a condition on the relationship selector itself, *not*
            // the artifact targeted by the relationship.
            customPropertySubquery = query.subquery(ArtificerRelationship.class);
            From customPropertyFrom = customPropertySubquery.from(ArtificerRelationship.class);
            MapJoin customPropertyJoin = customPropertyFrom.joinMap("otherAttributes");
            customPropertySubquery.select(customPropertyFrom.get("id"));
            customPropertyPredicates = new ArrayList<>();
            customPropertyPredicates
                    .add(criteriaBuilder.equal(customPropertyFrom.get("id"), relationshipFrom.get("id")));
            customPropertyPredicates.add(criteriaBuilder.equal(customPropertyJoin.key(), otherAttributeKey));
            customPropertyValuePath = customPropertyJoin.value();
            predicates.add(criteriaBuilder.exists(customPropertySubquery));
            propertyContext = null;
        } else if (node.getFunctionName().equals(GET_TARGET_ATTRIBUTE)) {
            String otherAttributeKey = reduceStringLiteralArgument(node.getArguments().get(1));
            // Ex. query: /s-ramp/wsdl/WsdlDocument[someRelationship[s-ramp:getTargetAttribute(., 'someAttribute') = 'true']]
            // Note that the predicate function needs to add a condition on the relationship target selector itself, *not*
            // the artifact targeted by the relationship.
            customPropertySubquery = query.subquery(ArtificerTarget.class);
            From customPropertyFrom = customPropertySubquery.from(ArtificerTarget.class);
            MapJoin customPropertyJoin = customPropertyFrom.joinMap("otherAttributes");
            customPropertySubquery.select(customPropertyFrom.get("id"));
            customPropertyPredicates = new ArrayList<>();
            customPropertyPredicates
                    .add(criteriaBuilder.equal(customPropertyFrom.get("id"), targetFrom.get("id")));
            customPropertyPredicates.add(criteriaBuilder.equal(customPropertyJoin.key(), otherAttributeKey));
            customPropertyValuePath = customPropertyJoin.value();
            predicates.add(criteriaBuilder.exists(customPropertySubquery));
            propertyContext = null;
        } else {
            if (node.getFunctionName().getLocalPart().equals("matches")
                    || node.getFunctionName().getLocalPart().equals("not")) {
                throw new RuntimeException(
                        Messages.i18n.format("XP_BAD_FUNC_NS", node.getFunctionName().getLocalPart()));
            }
            throw new RuntimeException(
                    Messages.i18n.format("XP_FUNC_NOT_SUPPORTED", node.getFunctionName().toString()));
        }
    } else if (MATCHES.equals(node.getFunctionName())) {
        if (node.getArguments().size() != 2) {
            throw new RuntimeException(
                    Messages.i18n.format("XP_MATCHES_FUNC_NUM_ARGS_ERROR", node.getArguments().size()));
        }
        Argument attributeArg = node.getArguments().get(0);
        Argument patternArg = node.getArguments().get(1);

        String pattern = reduceStringLiteralArgument(patternArg);

        if (isFullTextSearch(attributeArg)) {
            fullTextSearch(pattern);
        } else {
            pattern = pattern.replace(".*", "%"); // the only valid wildcard
            ForwardPropertyStep attribute = reducePropertyArgument(attributeArg);
            attribute.accept(this);
            like(propertyContext, pattern);
        }
    } else if (NOT.equals(node.getFunctionName())) {
        if (node.getArguments().size() != 1) {
            throw new RuntimeException(
                    Messages.i18n.format("XP_NOT_FUNC_NUM_ARGS_ERROR", node.getArguments().size()));
        }

        Argument argument = node.getArguments().get(0);
        if (argument.getExpr() != null) {
            argument.getExpr().accept(this);
            // Should have resulted in only 1 constraint -- negate it and re-add
            Predicate predicate = predicates.remove(predicates.size() - 1);
            predicates.add(criteriaBuilder.not(predicate));
        } else {
            // TODO: When would not() be given a literal?  That's what this implies.  As-is, it won't be negated...
            argument.accept(this);
        }
    } else {
        throw new RuntimeException(
                Messages.i18n.format("XP_FUNCTION_NOT_SUPPORTED", node.getFunctionName().toString()));
    }
}