
/*
 * Copyright 2017 Otavio Santana and others
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.jnosql.artemis.document;

import org.jnosql.artemis.reflection.ClassRepresentation;
import org.jnosql.diana.api.document.Document;
import org.jnosql.diana.api.document.DocumentCondition;
import org.jnosql.diana.api.document.DocumentQuery;

/**
 * Class the returns a {@link org.jnosql.diana.api.document.DocumentQuery}
 * on {@link DocumentCrudRepositoryProxy}
 */
class DocumentQueryParser {


    private static final String PREFIX = "findBy";
    private static final String AND = "AND";
    private static final String OR = "OR";
    private static final String ORDER_BY = "OrderBy";

    DocumentQuery parse(String query, Object[] args, ClassRepresentation classRepresentation) {
        DocumentQuery documentQuery = DocumentQuery.of(classRepresentation.getName());
        String[] tokens = query.replace(PREFIX, "").split("(?=AND|OR|OrderBy)");
        int index = 0;
        for (String token : tokens) {
            if (token.startsWith(AND)) {
                String field = token.replace(AND, "");
                DocumentCondition condition = toCondition(field, index, args);
                documentQuery.and(condition);
            } else if (token.startsWith(OR)) {
                String field = token.replace(OR, "");
                DocumentCondition condition = toCondition(field, index, args);
                documentQuery.or(condition);
            } else if (token.startsWith(ORDER_BY)) {

            } else {
                DocumentCondition condition = toCondition(token, index, args);
                documentQuery.and(condition);
            }
            index++;
        }
        return documentQuery;
    }
    //AND
    //OR
    //Between
    //LessThan
    //GreaterThan
    //LessThanEqual
    //GreaterThanEqual
    //Like

    private DocumentCondition toCondition(String token, int index, Object[] args) {

        if (token.contains("Between")) {

        } else if (token.contains("LessThan")) {
            String name = String.valueOf(Character.toLowerCase(token.charAt(0)))
                    .concat(token.substring(1)).replace("LessThan", "");
            return DocumentCondition.lt(Document.of(name, args[index]));
        } else if (token.contains("GreaterThan")) {
            String name = String.valueOf(Character.toLowerCase(token.charAt(0)))
                    .concat(token.substring(1)).replace("GreaterThan", "");
            return DocumentCondition.gt(Document.of(name, args[index]));
        } else if (token.contains("LessThanEqual")) {
            String name = String.valueOf(Character.toLowerCase(token.charAt(0)))
                    .concat(token.substring(1)).replace("LessThanEqual", "");
            return DocumentCondition.lte(Document.of(name, args[index]));
        } else if (token.contains("GreaterThanEqual")) {
            String name = String.valueOf(Character.toLowerCase(token.charAt(0)))
                    .concat(token.substring(1)).replace("GreaterThanEqual", "");
            return DocumentCondition.gte(Document.of(name, args[index]));
        } else if (token.contains("Like")) {
            String name = String.valueOf(Character.toLowerCase(token.charAt(0)))
                    .concat(token.substring(1)).replace("Like", "");
            return DocumentCondition.like(Document.of(name, args[index]));
        }
        String name = String.valueOf(Character.toLowerCase(token.charAt(0)))
                .concat(token.substring(1));
        return DocumentCondition.eq(Document.of(name, args[index]));

    }
}

@@ -1439,16 +1439,374 @@
 rderBy%22;
+%0A    private static final String BETWEEN = %22Between%22;%0A    private static final String LESS_THAN = %22LessThan%22;%0A    private static final String GREATER_THAN = %22GreaterThan%22;%0A    private static final String LESS_THAN_EQUAL = %22LessThanEqual%22;%0A    private static final String GREATER_THAN_EQUAL = %22GreaterThanEqual%22;%0A    private static final String LIKE = %22Like%22;
 %0A%0A    Do
@@ -3084,25 +3084,23 @@
 ontains(
-%22Between%22
+BETWEEN
 )) %7B%0A%0A  
@@ -3131,26 +3131,25 @@
 ontains(
-%22LessThan%22
+LESS_THAN
 )) %7B%0A   
@@ -3282,26 +3282,25 @@
 replace(
-%22LessThan%22
+LESS_THAN
 , %22%22);%0A 
@@ -3405,29 +3405,28 @@
 ontains(
-%22GreaterThan%22
+GREATER_THAN
 )) %7B%0A   
@@ -3559,29 +3559,28 @@
 replace(
-%22GreaterThan%22
+GREATER_THAN
 , %22%22);%0A 
@@ -3685,31 +3685,31 @@
 ontains(
-%22LessThanEqual%22
+LESS_THAN_EQUAL
 )) %7B%0A   
@@ -3842,31 +3842,31 @@
 replace(
-%22LessThanEqual%22
+LESS_THAN_EQUAL
 , %22%22);%0A 
@@ -3972,34 +3972,34 @@
 ontains(
-%22GreaterThanEqual%22
+GREATER_THAN_EQUAL
 )) %7B%0A   
@@ -4132,34 +4132,34 @@
 replace(
-%22GreaterThanEqual%22
+GREATER_THAN_EQUAL
 , %22%22);%0A 
@@ -4265,22 +4265,20 @@
 ontains(
-%22Like%22
+LIKE
 )) %7B%0A   
@@ -4411,22 +4411,20 @@
 replace(
-%22Like%22
+LIKE
 , %22%22);%0A 

Create constant on document query parser