Package org.exoplatform.services.jcr.impl.core.query

Examples of org.exoplatform.services.jcr.impl.core.query.RelationQueryNode


      return parent;
   }

   public Object visit(ASTUpperFunction node, Object data)
   {
      RelationQueryNode parent = (RelationQueryNode) data;
      if (parent.getValueType() != QueryConstants.TYPE_STRING)
      {
         String msg = "UPPER() function is only supported for String literal";
         throw new IllegalArgumentException(msg);
      }
      parent.addOperand(factory.createPropertyFunctionQueryNode(parent, PropertyFunctionQueryNode.UPPER_CASE));
      return parent;
   }
View Full Code Here


   private RelationQueryNode createRelationQueryNode(QueryNode parent, InternalQName propertyName, int operationType,
            ASTLiteral literal) throws IllegalArgumentException
   {

      String stringValue = literal.getValue();
      RelationQueryNode node = null;

      try
      {
         QPath relPath = null;
         if (propertyName != null)
         {
            relPath = new QPath(new QPathEntry[]
            {new QPathEntry(propertyName, 0)});
         }
         if (literal.getType() == QueryConstants.TYPE_DATE)
         {
            SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN);
            Date date = format.parse(stringValue);
            node = factory.createRelationQueryNode(parent, operationType);
            node.setRelativePath(relPath);
            node.setDateValue(date);
         }
         else if (literal.getType() == QueryConstants.TYPE_DOUBLE)
         {
            double d = Double.parseDouble(stringValue);
            node = factory.createRelationQueryNode(parent, operationType);
            node.setRelativePath(relPath);
            node.setDoubleValue(d);
         }
         else if (literal.getType() == QueryConstants.TYPE_LONG)
         {
            long l = Long.parseLong(stringValue);
            node = factory.createRelationQueryNode(parent, operationType);
            node.setRelativePath(relPath);
            node.setLongValue(l);
         }
         else if (literal.getType() == QueryConstants.TYPE_STRING)
         {
            node = factory.createRelationQueryNode(parent, operationType);
            node.setRelativePath(relPath);
            node.setStringValue(stringValue);
         }
         else if (literal.getType() == QueryConstants.TYPE_TIMESTAMP)
         {
            Calendar c = ISO8601.parse(stringValue);
            node = factory.createRelationQueryNode(parent, operationType);
            node.setRelativePath(relPath);
            node.setDateValue(c.getTime());
         }
      }
      catch (java.text.ParseException e)
      {
         throw new IllegalArgumentException(e.toString());
View Full Code Here

            // check arguments
            if (node.jjtGetNumChildren() == 2)
            {
               if (queryNode instanceof RelationQueryNode)
               {
                  RelationQueryNode rel = (RelationQueryNode) queryNode;
                  SimpleNode literal = (SimpleNode) node.jjtGetChild(1).jjtGetChild(0);
                  if (literal.getId() == JJTSTRINGLITERAL)
                  {
                     String value = literal.getValue();
                     // strip quotes
                     value = value.substring(1, value.length() - 1);
                     Calendar c = ISO8601.parse(value);
                     if (c == null)
                     {
                        exceptions.add(new InvalidQueryException("Unable to parse string literal for xs:dateTime: "
                                 + value));
                     }
                     else
                     {
                        rel.setDateValue(c.getTime());
                     }
                  }
                  else
                  {
                     exceptions.add(new InvalidQueryException("Wrong argument type for xs:dateTime"));
                  }
               }
               else
               {
                  exceptions.add(new InvalidQueryException("Unsupported location for function xs:dateTime"));
               }
            }
            else
            {
               // wrong number of arguments
               exceptions.add(new InvalidQueryException("Wrong number of arguments for xs:dateTime"));
            }
         }
         else if (JCR_CONTAINS.equals(funName))
         {
            // check number of arguments
            if (node.jjtGetNumChildren() == 3)
            {
               if (queryNode instanceof NAryQueryNode)
               {
                  SimpleNode literal = (SimpleNode) node.jjtGetChild(2).jjtGetChild(0);
                  if (literal.getId() == JJTSTRINGLITERAL)
                  {
                     TextsearchQueryNode contains =
                              factory.createTextsearchQueryNode(queryNode, unescapeQuotes(literal.getValue()));
                     // assign property name
                     SimpleNode path = (SimpleNode) node.jjtGetChild(1);
                     path.jjtAccept(this, contains);
                     ((NAryQueryNode) queryNode).addOperand(contains);
                  }
                  else
                  {
                     exceptions.add(new InvalidQueryException("Wrong argument type for jcr:contains"));
                  }
               }
            }
            else
            {
               // wrong number of arguments
               exceptions.add(new InvalidQueryException("Wrong number of arguments for jcr:contains"));
            }
         }
         else if (JCR_LIKE.equals(funName))
         {
            // check number of arguments
            if (node.jjtGetNumChildren() == 3)
            {
               if (queryNode instanceof NAryQueryNode)
               {
                  RelationQueryNode like = factory.createRelationQueryNode(queryNode, RelationQueryNode.OPERATION_LIKE);
                  ((NAryQueryNode) queryNode).addOperand(like);

                  // assign property name
                  node.jjtGetChild(1).jjtAccept(this, like);
                  // check property name
                  if (like.getRelativePath() == null)
                  {
                     exceptions.add(new InvalidQueryException("Wrong first argument type for jcr:like"));
                  }

                  SimpleNode literal = (SimpleNode) node.jjtGetChild(2).jjtGetChild(0);
                  if (literal.getId() == JJTSTRINGLITERAL)
                  {
                     like.setStringValue(unescapeQuotes(literal.getValue()));
                  }
                  else
                  {
                     exceptions.add(new InvalidQueryException("Wrong second argument type for jcr:like"));
                  }
               }
               else
               {
                  exceptions.add(new InvalidQueryException("Unsupported location for function jcr:like"));
               }
            }
            else
            {
               // wrong number of arguments
               exceptions.add(new InvalidQueryException("Wrong number of arguments for jcr:like"));
            }
         }
         else if (FN_TRUE.equals(funName))
         {
            if (queryNode.getType() == QueryNode.TYPE_RELATION)
            {
               RelationQueryNode rel = (RelationQueryNode) queryNode;
               rel.setStringValue("true");
            }
            else
            {
               exceptions.add(new InvalidQueryException("Unsupported location for true()"));
            }
         }
         else if (FN_FALSE.equals(funName))
         {
            if (queryNode.getType() == QueryNode.TYPE_RELATION)
            {
               RelationQueryNode rel = (RelationQueryNode) queryNode;
               rel.setStringValue("false");
            }
            else
            {
               exceptions.add(new InvalidQueryException("Unsupported location for false()"));
            }
         }
         else if (FN_POSITION.equals(funName))
         {
            if (queryNode.getType() == QueryNode.TYPE_RELATION)
            {
               RelationQueryNode rel = (RelationQueryNode) queryNode;
               if (rel.getOperation() == RelationQueryNode.OPERATION_EQ_GENERAL)
               {
                  // set dummy value to set type of relation query node
                  // will be overwritten when the tree is furhter parsed.
                  rel.setPositionValue(1);
                  rel.addPathElement(new QPathEntry(FN_POSITION_FULL, 0));
               }
               else
               {
                  exceptions.add(new InvalidQueryException(
                           "Unsupported expression with position(). Only = is supported."));
               }
            }
            else
            {
               exceptions.add(new InvalidQueryException("Unsupported location for position()"));
            }
         }
         else if (FN_FIRST.equals(funName))
         {
            if (queryNode.getType() == QueryNode.TYPE_RELATION)
            {
               ((RelationQueryNode) queryNode).setPositionValue(1);
            }
            else if (queryNode.getType() == QueryNode.TYPE_LOCATION)
            {
               ((LocationStepQueryNode) queryNode).setIndex(1);
            }
            else
            {
               exceptions.add(new InvalidQueryException("Unsupported location for first()"));
            }
         }
         else if (FN_LAST.equals(funName))
         {
            if (queryNode.getType() == QueryNode.TYPE_RELATION)
            {
               ((RelationQueryNode) queryNode).setPositionValue(LocationStepQueryNode.LAST);
            }
            else if (queryNode.getType() == QueryNode.TYPE_LOCATION)
            {
               ((LocationStepQueryNode) queryNode).setIndex(LocationStepQueryNode.LAST);
            }
            else
            {
               exceptions.add(new InvalidQueryException("Unsupported location for last()"));
            }
         }
         else if (JCR_DEREF.equals(funName))
         {
            // check number of arguments
            if (node.jjtGetNumChildren() == 3)
            {
               boolean descendant = false;
               if (queryNode.getType() == QueryNode.TYPE_LOCATION)
               {
                  LocationStepQueryNode loc = (LocationStepQueryNode) queryNode;
                  // remember if descendant axis
                  descendant = loc.getIncludeDescendants();
                  queryNode = loc.getParent();
                  ((NAryQueryNode) queryNode).removeOperand(loc);
               }
               if (queryNode.getType() == QueryNode.TYPE_PATH)
               {
                  PathQueryNode pathNode = (PathQueryNode) queryNode;
                  DerefQueryNode derefNode = factory.createDerefQueryNode(pathNode, null, false);

                  // assign property name
                  node.jjtGetChild(1).jjtAccept(this, derefNode);
                  // check property name
                  if (derefNode.getRefProperty() == null)
                  {
                     exceptions.add(new InvalidQueryException("Wrong first argument type for jcr:deref"));
                  }

                  SimpleNode literal = (SimpleNode) node.jjtGetChild(2).jjtGetChild(0);
                  if (literal.getId() == JJTSTRINGLITERAL)
                  {
                     String value = literal.getValue();
                     // strip quotes
                     value = value.substring(1, value.length() - 1);
                     if (!value.equals("*"))
                     {
                        InternalQName name = null;
                        try
                        {
                           name = decode(resolver.parseJCRName(value).getInternalName());
                        }
                        catch (RepositoryException e)
                        {
                           exceptions.add(new InvalidQueryException("Illegal name: " + value));
                        }
                        derefNode.setNameTest(name);
                     }
                  }
                  else
                  {
                     exceptions.add(new InvalidQueryException("Second argument for jcr:deref must be a String"));
                  }

                  // check if descendant
                  if (!descendant)
                  {
                     Node p = node.jjtGetParent();
                     for (int i = 0; i < p.jjtGetNumChildren(); i++)
                     {
                        SimpleNode c = (SimpleNode) p.jjtGetChild(i);
                        if (c == node)
                        {
                           break;
                        }
                        descendant = (c.getId() == JJTSLASHSLASH || c.getId() == JJTROOTDESCENDANTS);
                     }
                  }
                  derefNode.setIncludeDescendants(descendant);
                  pathNode.addPathStep(derefNode);
               }
               else
               {
                  exceptions.add(new InvalidQueryException("Unsupported location for jcr:deref()"));
               }
            }
         }
         else if (JCR_SCORE.equals(funName))
         {
            if (queryNode.getType() == QueryNode.TYPE_ORDER)
            {
               createOrderSpec(node, (OrderQueryNode) queryNode);
            }
            else
            {
               exceptions.add(new InvalidQueryException("Unsupported location for jcr:score()"));
            }
         }
         else if (FN_LOWER_CASE.equals(funName))
         {
            if (node.jjtGetNumChildren() == 2)
            {
               if (queryNode.getType() == QueryNode.TYPE_RELATION)
               {
                  RelationQueryNode relNode = (RelationQueryNode) queryNode;
                  relNode.addOperand(factory.createPropertyFunctionQueryNode(relNode,
                           PropertyFunctionQueryNode.LOWER_CASE));
                  // get property name
                  node.jjtGetChild(1).jjtAccept(this, relNode);
               }
               else
               {
                  exceptions.add(new InvalidQueryException("Unsupported location for fn:lower-case()"));
               }
            }
            else
            {
               exceptions.add(new InvalidQueryException("Wrong number of argument for fn:lower-case()"));
            }
         }
         else if (FN_UPPER_CASE.equals(funName))
         {
            if (node.jjtGetNumChildren() == 2)
            {
               if (queryNode.getType() == QueryNode.TYPE_RELATION)
               {
                  RelationQueryNode relNode = (RelationQueryNode) queryNode;
                  relNode.addOperand(factory.createPropertyFunctionQueryNode(relNode,
                           PropertyFunctionQueryNode.UPPER_CASE));
                  // get property name
                  node.jjtGetChild(1).jjtAccept(this, relNode);
               }
               else
               {
                  exceptions.add(new InvalidQueryException("Unsupported location for fn:upper-case()"));
               }
            }
            else
            {
               exceptions.add(new InvalidQueryException("Unsupported location for fn:upper-case()"));
            }
         }
         else if (REP_SIMILAR.equals(funName))
         {
            if (node.jjtGetNumChildren() == 3)
            {
               if (queryNode instanceof NAryQueryNode)
               {
                  NAryQueryNode parent = (NAryQueryNode) queryNode;
                  RelationQueryNode rel = factory.createRelationQueryNode(parent, RelationQueryNode.OPERATION_SIMILAR);
                  parent.addOperand(rel);
                  // assign path
                  node.jjtGetChild(1).jjtAccept(this, rel);

                  // get path string
                  node.jjtGetChild(2).jjtAccept(this, rel);
                  // check if string is set
                  if (rel.getStringValue() == null)
                  {
                     exceptions.add(new InvalidQueryException(
                              "Second argument for exo:similar() must be of type string"));
                  }
               }
               else
               {
                  exceptions.add(new InvalidQueryException("Unsupported location for exo:similar()"));
               }
            }
            else
            {
               exceptions.add(new InvalidQueryException("Wrong number of arguments for exo:similar()"));
            }
         }
         else if (REP_SPELLCHECK.equals(funName) && queryNode.getType() != QueryNode.TYPE_PATH)
         {
            if (node.jjtGetNumChildren() == 2)
            {
               if (queryNode instanceof NAryQueryNode)
               {
                  NAryQueryNode parent = (NAryQueryNode) queryNode;
                  RelationQueryNode rel =
                           factory.createRelationQueryNode(parent, RelationQueryNode.OPERATION_SPELLCHECK);
                  parent.addOperand(rel);

                  // get string to check
                  node.jjtGetChild(1).jjtAccept(this, rel);
                  // check if string is set
                  if (rel.getStringValue() == null)
                  {
                     exceptions.add(new InvalidQueryException("Argument for exo:spellcheck() must be of type string"));
                  }

                  // set a dummy property name
                  rel.addPathElement(new QPathEntry(Constants.JCR_PRIMARYTYPE, 0));
               }
               else
               {
                  exceptions.add(new InvalidQueryException("Unsupported location for exo:spellcheck()"));
               }
            }
            else
            {
               exceptions.add(new InvalidQueryException("Wrong number of arguments for exo:spellcheck()"));
            }
         }
         else if (queryNode.getType() == QueryNode.TYPE_RELATION)
         {
            // use function name as name of a pseudo property in a relation
            try
            {
               InternalQName name = resolver.parseJCRName(fName + "()").getInternalName();

               RelationQueryNode relNode = (RelationQueryNode) queryNode;
               relNode.addPathElement(new QPathEntry(name, 0));
            }
            catch (RepositoryException e)
            {
               exceptions.add(e);
            }
View Full Code Here

                  node.childrenAccept(this, queryNode);
               }
               else if (queryNode.getType() == QueryNode.TYPE_NOT)
               {
                  // is null expression
                  RelationQueryNode isNull =
                           factory.createRelationQueryNode(queryNode, RelationQueryNode.OPERATION_NULL);
                  applyRelativePath(isNull);
                  node.childrenAccept(this, isNull);
                  NotQueryNode notNode = (NotQueryNode) queryNode;
                  NAryQueryNode parent = (NAryQueryNode) notNode.getParent();
                  parent.removeOperand(notNode);
                  parent.addOperand(isNull);
               }
               else
               {
                  // not null expression
                  RelationQueryNode notNull =
                           factory.createRelationQueryNode(queryNode, RelationQueryNode.OPERATION_NOT_NULL);
                  applyRelativePath(notNull);
                  node.childrenAccept(this, notNull);
                  ((NAryQueryNode) queryNode).addOperand(notNull);
               }
            }
            else
            {
               if (queryNode.getType() == QueryNode.TYPE_PATH)
               {
                  createLocationStep(node, (NAryQueryNode) queryNode);
               }
               else if (queryNode.getType() == QueryNode.TYPE_TEXTSEARCH
                        || queryNode.getType() == QueryNode.TYPE_RELATION)
               {
                  node.childrenAccept(this, queryNode);
               }
               else
               {
                  // step within a predicate
                  RelationQueryNode tmp = factory.createRelationQueryNode(null, RelationQueryNode.OPERATION_NOT_NULL);
                  node.childrenAccept(this, tmp);
                  QPathEntry[] entr = tmp.getRelativePath().getEntries();
                  if (tmpRelPath == null)
                  {

                     tmpRelPath = new QPath(new QPathEntry[]
                     {entr[entr.length - 1]});
View Full Code Here

      else
      {
         exceptions.add(new InvalidQueryException("Unsupported ComparisonExpr type:" + node.getValue()));
      }

      final RelationQueryNode rqn = factory.createRelationQueryNode(queryNode, type);

      // traverse
      node.childrenAccept(this, rqn);

      // check if string transformation is valid
      rqn.acceptOperands(new DefaultQueryNodeVisitor()
      {
         public Object visit(PropertyFunctionQueryNode node, Object data)
         {
            String functionName = node.getFunctionName();
            if ((functionName.equals(PropertyFunctionQueryNode.LOWER_CASE) || functionName
                     .equals(PropertyFunctionQueryNode.UPPER_CASE))
                     && rqn.getValueType() != QueryConstants.TYPE_STRING)
            {
               String msg = "Upper and lower case function are only supported with String literals";
               exceptions.add(new InvalidQueryException(msg));
            }
            return data;
View Full Code Here

      QueryNode[] pred = node.getPredicates();
      for (int i = 0; i < pred.length; i++)
      {
         if (pred[i].getType() == QueryNode.TYPE_RELATION)
         {
            RelationQueryNode pos = (RelationQueryNode) pred[i];
            if (pos.getValueType() == QueryConstants.TYPE_POSITION)
            {
               node.setIndex(pos.getPositionValue());
            }
         }
      }

      TermQuery nameTest = null;
View Full Code Here

  // check for position predicate
  QueryNode[] pred = node.getPredicates();
  for (int i = 0; i < pred.length; i++) {
      if (pred[i].getType() == QueryNode.TYPE_RELATION) {
    RelationQueryNode pos = (RelationQueryNode) pred[i];
    if (pos.getValueType() == QueryConstants.TYPE_POSITION) {
        node.setIndex(pos.getPositionValue());
    }
      }
  }

  NameQuery nameTest = null;
View Full Code Here

                            || queryNode.getType() == QueryNode.TYPE_TEXTSEARCH) {
                        // traverse
                        node.childrenAccept(this, queryNode);
                    } else if (queryNode.getType() == QueryNode.TYPE_NOT) {
                        // is null expression
                        RelationQueryNode isNull
                                = factory.createRelationQueryNode(queryNode,
                                        RelationQueryNode.OPERATION_NULL);
                        applyRelativePath(isNull);
                        node.childrenAccept(this, isNull);
                        NotQueryNode notNode = (NotQueryNode) queryNode;
                        NAryQueryNode parent = (NAryQueryNode) notNode.getParent();
                        parent.removeOperand(notNode);
                        parent.addOperand(isNull);
                    } else {
                        // not null expression
                        RelationQueryNode notNull =
                                factory.createRelationQueryNode(queryNode,
                                        RelationQueryNode.OPERATION_NOT_NULL);
                        applyRelativePath(notNull);
                        node.childrenAccept(this, notNull);
                        ((NAryQueryNode) queryNode).addOperand(notNull);
                    }
                } else {
                    if (queryNode.getType() == QueryNode.TYPE_PATH) {
                        createLocationStep(node, (NAryQueryNode) queryNode);
                    } else if (queryNode.getType() == QueryNode.TYPE_TEXTSEARCH
                            || queryNode.getType() == QueryNode.TYPE_RELATION) {
                        node.childrenAccept(this, queryNode);
                    } else {
                        // step within a predicate
                        RelationQueryNode tmp = factory.createRelationQueryNode(
                                null, RelationQueryNode.OPERATION_NOT_NULL);
                        node.childrenAccept(this, tmp);
                        QPathEntry[] entr = tmp.getRelativePath().getEntries();
                        if (tmpRelPath == null)
                        {

                           tmpRelPath = new QPath(new QPathEntry[]{entr[entr.length - 1]});
                        }
View Full Code Here

            type = RelationQueryNode.OPERATION_NE_GENERAL;
        } else {
            exceptions.add(new InvalidQueryException("Unsupported ComparisonExpr type:" + node.getValue()));
        }

        final RelationQueryNode rqn = factory.createRelationQueryNode(queryNode, type);

        // traverse
        node.childrenAccept(this, rqn);

        // check if string transformation is valid
        try {
            rqn.acceptOperands(new DefaultQueryNodeVisitor() {
                public Object visit(PropertyFunctionQueryNode node, Object data) {
                    String functionName = node.getFunctionName();
                    if ((functionName.equals(PropertyFunctionQueryNode.LOWER_CASE)
                            || functionName.equals(PropertyFunctionQueryNode.UPPER_CASE))
                                && rqn.getValueType() != QueryConstants.TYPE_STRING) {
                        String msg = "Upper and lower case function are only supported with String literals";
                        exceptions.add(new InvalidQueryException(msg));
                    }
                    return data;
                }
View Full Code Here

                }
            } else if (XS_DATETIME.equals(funName)) {
                // check arguments
                if (node.jjtGetNumChildren() == 2) {
                    if (queryNode instanceof RelationQueryNode) {
                        RelationQueryNode rel = (RelationQueryNode) queryNode;
                        SimpleNode literal = (SimpleNode) node.jjtGetChild(1).jjtGetChild(0);
                        if (literal.getId() == JJTSTRINGLITERAL) {
                            String value = literal.getValue();
                            // strip quotes
                            value = value.substring(1, value.length() - 1);
                            Calendar c = ISO8601.parse(value);
                            if (c == null) {
                                exceptions.add(new InvalidQueryException("Unable to parse string literal for xs:dateTime: "
                                         + value));
                            } else {
                                rel.setDateValue(c.getTime());
                            }
                        } else {
                            exceptions.add(new InvalidQueryException("Wrong argument type for xs:dateTime"));
                        }
                    } else {
                        exceptions.add(new InvalidQueryException("Unsupported location for function xs:dateTime"));
                    }
                } else {
                    // wrong number of arguments
                    exceptions.add(new InvalidQueryException("Wrong number of arguments for xs:dateTime"));
                }
            } else if (JCR_CONTAINS.equals(funName)) {
                // check number of arguments
                if (node.jjtGetNumChildren() == 3) {
                    if (queryNode instanceof NAryQueryNode) {
                        SimpleNode literal = (SimpleNode) node.jjtGetChild(2).jjtGetChild(0);
                        if (literal.getId() == JJTSTRINGLITERAL) {
                            TextsearchQueryNode contains = factory.createTextsearchQueryNode(
                                    queryNode, unescapeQuotes(literal.getValue()));
                            // assign property name
                            SimpleNode path = (SimpleNode) node.jjtGetChild(1);
                            path.jjtAccept(this, contains);
                            ((NAryQueryNode) queryNode).addOperand(contains);
                        } else {
                            exceptions.add(new InvalidQueryException("Wrong argument type for jcr:contains"));
                        }
                    }
                } else {
                    // wrong number of arguments
                    exceptions.add(new InvalidQueryException("Wrong number of arguments for jcr:contains"));
                }
            } else if (JCR_LIKE.equals(funName)) {
                // check number of arguments
                if (node.jjtGetNumChildren() == 3) {
                    if (queryNode instanceof NAryQueryNode) {
                        RelationQueryNode like = factory.createRelationQueryNode(
                                queryNode, RelationQueryNode.OPERATION_LIKE);
                        ((NAryQueryNode) queryNode).addOperand(like);

                        // assign property name
                        node.jjtGetChild(1).jjtAccept(this, like);
                        // check property name
                        if (like.getRelativePath() == null) {
                            exceptions.add(new InvalidQueryException("Wrong first argument type for jcr:like"));
                        }

                        SimpleNode literal = (SimpleNode) node.jjtGetChild(2).jjtGetChild(0);
                        if (literal.getId() == JJTSTRINGLITERAL) {
                            like.setStringValue(unescapeQuotes(literal.getValue()));
                        } else {
                            exceptions.add(new InvalidQueryException("Wrong second argument type for jcr:like"));
                        }
                    } else {
                        exceptions.add(new InvalidQueryException("Unsupported location for function jcr:like"));
                    }
                } else {
                    // wrong number of arguments
                    exceptions.add(new InvalidQueryException("Wrong number of arguments for jcr:like"));
                }
            } else if (FN_TRUE.equals(funName)) {
                if (queryNode.getType() == QueryNode.TYPE_RELATION) {
                    RelationQueryNode rel = (RelationQueryNode) queryNode;
                    rel.setStringValue("true");
                } else {
                    exceptions.add(new InvalidQueryException("Unsupported location for true()"));
                }
            } else if (FN_FALSE.equals(funName)) {
                if (queryNode.getType() == QueryNode.TYPE_RELATION) {
                    RelationQueryNode rel = (RelationQueryNode) queryNode;
                    rel.setStringValue("false");
                } else {
                    exceptions.add(new InvalidQueryException("Unsupported location for false()"));
                }
            } else if (FN_POSITION.equals(funName)) {
                if (queryNode.getType() == QueryNode.TYPE_RELATION) {
                    RelationQueryNode rel = (RelationQueryNode) queryNode;
                    if (rel.getOperation() == RelationQueryNode.OPERATION_EQ_GENERAL) {
                        // set dummy value to set type of relation query node
                        // will be overwritten when the tree is furhter parsed.
                       rel.setPositionValue(1);
                       rel.addPathElement(new QPathEntry(FN_POSITION_FULL, 0));
                    } else {
                        exceptions.add(new InvalidQueryException("Unsupported expression with position(). Only = is supported."));
                    }
                } else {
                    exceptions.add(new InvalidQueryException("Unsupported location for position()"));
                }
            } else if (FN_FIRST.equals(funName)) {
                if (queryNode.getType() == QueryNode.TYPE_RELATION) {
                    ((RelationQueryNode) queryNode).setPositionValue(1);
                } else if (queryNode.getType() == QueryNode.TYPE_LOCATION) {
                    ((LocationStepQueryNode) queryNode).setIndex(1);
                } else {
                    exceptions.add(new InvalidQueryException("Unsupported location for first()"));
                }
            } else if (FN_LAST.equals(funName)) {
                if (queryNode.getType() == QueryNode.TYPE_RELATION) {
                    ((RelationQueryNode) queryNode).setPositionValue(LocationStepQueryNode.LAST);
                } else if (queryNode.getType() == QueryNode.TYPE_LOCATION) {
                    ((LocationStepQueryNode) queryNode).setIndex(LocationStepQueryNode.LAST);
                } else {
                    exceptions.add(new InvalidQueryException("Unsupported location for last()"));
                }
            } else if (JCR_DEREF.equals(funName)) {
                // check number of arguments
                if (node.jjtGetNumChildren() == 3) {
                    boolean descendant = false;
                    if (queryNode.getType() == QueryNode.TYPE_LOCATION) {
                        LocationStepQueryNode loc = (LocationStepQueryNode) queryNode;
                        // remember if descendant axis
                        descendant = loc.getIncludeDescendants();
                        queryNode = loc.getParent();
                        ((NAryQueryNode) queryNode).removeOperand(loc);
                    }
                    if (queryNode.getType() == QueryNode.TYPE_PATH) {
                        PathQueryNode pathNode = (PathQueryNode) queryNode;
                        DerefQueryNode derefNode = factory.createDerefQueryNode(pathNode, null, false);

                        // assign property name
                        node.jjtGetChild(1).jjtAccept(this, derefNode);
                        // check property name
                        if (derefNode.getRefProperty() == null) {
                            exceptions.add(new InvalidQueryException("Wrong first argument type for jcr:deref"));
                        }

                        SimpleNode literal = (SimpleNode) node.jjtGetChild(2).jjtGetChild(0);
                        if (literal.getId() == JJTSTRINGLITERAL) {
                            String value = literal.getValue();
                            // strip quotes
                            value = value.substring(1, value.length() - 1);
                            if (!value.equals("*")) {
                                InternalQName name = null;
                                try {
                                   name = decode(resolver.parseJCRName(value).getInternalName());
                                } catch (RepositoryException e) {
                                    exceptions.add(new InvalidQueryException("Illegal name: " + value));
                                }
                                derefNode.setNameTest(name);
                            }
                        } else {
                            exceptions.add(new InvalidQueryException("Second argument for jcr:deref must be a String"));
                        }

                        // check if descendant
                        if (!descendant) {
                            Node p = node.jjtGetParent();
                            for (int i = 0; i < p.jjtGetNumChildren(); i++) {
                                SimpleNode c = (SimpleNode) p.jjtGetChild(i);
                                if (c == node) { // NOSONAR
                                    break;
                                }
                                descendant = (c.getId() == JJTSLASHSLASH
                                        || c.getId() == JJTROOTDESCENDANTS);
                            }
                        }
                        derefNode.setIncludeDescendants(descendant);
                        pathNode.addPathStep(derefNode);
                    } else {
                        exceptions.add(new InvalidQueryException("Unsupported location for jcr:deref()"));
                    }
                }
            } else if (JCR_SCORE.equals(funName)) {
                if (queryNode.getType() == QueryNode.TYPE_ORDER) {
                    createOrderSpec(node, (OrderQueryNode) queryNode);
                } else {
                    exceptions.add(new InvalidQueryException("Unsupported location for jcr:score()"));
                }
            } else if (FN_LOWER_CASE.equals(funName)) {
                if (node.jjtGetNumChildren() == 2) {
                    if (queryNode.getType() == QueryNode.TYPE_RELATION) {
                        RelationQueryNode relNode = (RelationQueryNode) queryNode;
                        relNode.addOperand(factory.createPropertyFunctionQueryNode(
                                relNode, PropertyFunctionQueryNode.LOWER_CASE));
                        // get property name
                        node.jjtGetChild(1).jjtAccept(this, relNode);
                    } else {
                        exceptions.add(new InvalidQueryException("Unsupported location for fn:lower-case()"));
                    }
                } else {
                    exceptions.add(new InvalidQueryException("Wrong number of argument for fn:lower-case()"));
                }
            } else if (FN_UPPER_CASE.equals(funName)) {
                if (node.jjtGetNumChildren() == 2) {
                    if (queryNode.getType() == QueryNode.TYPE_RELATION) {
                        RelationQueryNode relNode = (RelationQueryNode) queryNode;
                        relNode.addOperand(factory.createPropertyFunctionQueryNode(
                                relNode, PropertyFunctionQueryNode.UPPER_CASE));
                        // get property name
                        node.jjtGetChild(1).jjtAccept(this, relNode);
                    } else {
                        exceptions.add(new InvalidQueryException("Unsupported location for fn:upper-case()"));
                    }
                } else {
                    exceptions.add(new InvalidQueryException("Unsupported location for fn:upper-case()"));
                }
            } else if (REP_SIMILAR.equals(funName)) {
                if (node.jjtGetNumChildren() == 3) {
                    if (queryNode instanceof NAryQueryNode) {
                        NAryQueryNode parent = (NAryQueryNode) queryNode;
                        RelationQueryNode rel = factory.createRelationQueryNode(
                                parent, RelationQueryNode.OPERATION_SIMILAR);
                        parent.addOperand(rel);
                        // assign path
                        node.jjtGetChild(1).jjtAccept(this, rel);

                        // get path string
                        node.jjtGetChild(2).jjtAccept(this, rel);
                        // check if string is set
                        if (rel.getStringValue() == null) {
                            exceptions.add(new InvalidQueryException(
                                    "Second argument for rep:similar() must be of type string"));
                        }
                    } else {
                        exceptions.add(new InvalidQueryException(
                                "Unsupported location for rep:similar()"));
                    }
                } else {
                    exceptions.add(new InvalidQueryException(
                            "Wrong number of arguments for rep:similar()"));
                }
            } else if (REP_SPELLCHECK.equals(funName)
                    && queryNode.getType() != QueryNode.TYPE_PATH) {
                if (node.jjtGetNumChildren() == 2) {
                    if (queryNode instanceof NAryQueryNode) {
                        NAryQueryNode parent = (NAryQueryNode) queryNode;
                        RelationQueryNode rel = factory.createRelationQueryNode(
                                parent, RelationQueryNode.OPERATION_SPELLCHECK);
                        parent.addOperand(rel);

                        // get string to check
                        node.jjtGetChild(1).jjtAccept(this, rel);
                        // check if string is set
                        if (rel.getStringValue() == null) {
                            exceptions.add(new InvalidQueryException(
                                    "Argument for rep:spellcheck() must be of type string"));
                        }

                        // set a dummy property name
                        rel.addPathElement(new QPathEntry(Constants.JCR_PRIMARYTYPE, 0));
                } else {
                        exceptions.add(new InvalidQueryException(
                                "Unsupported location for rep:spellcheck()"));
                    }
                } else {
                    exceptions.add(new InvalidQueryException(
                            "Wrong number of arguments for rep:spellcheck()"));
                }
            } else if (queryNode.getType() == QueryNode.TYPE_RELATION) {
                // use function name as name of a pseudo property in a relation
                try {
                   InternalQName name = resolver.parseJCRName(fName + "()").getInternalName();

                   RelationQueryNode relNode = (RelationQueryNode)queryNode;
                   relNode.addPathElement(new QPathEntry(name, 0));
                } catch (RepositoryException e) {
                    exceptions.add(e);
                }
            } else if (queryNode.getType() == QueryNode.TYPE_PATH) {
                // use function name as name of a pseudo property in select clause
View Full Code Here

TOP

Related Classes of org.exoplatform.services.jcr.impl.core.query.RelationQueryNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.