<commit_before>/*
 *  Copyright (c) 2017 <NAME> and others
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   and Apache License v2.0 which accompanies this distribution.
 *   The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *   and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
 *
 *   You may elect to redistribute this code under either of these licenses.
 *
 *   Contributors:
 *
 *   <NAME>
 */
package org.jnosql.artemis.graph.query;

import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.jnosql.artemis.Converters;
import org.jnosql.artemis.Pagination;
import org.jnosql.artemis.Repository;
import org.jnosql.artemis.graph.GraphConverter;
import org.jnosql.artemis.graph.GraphTemplate;
import org.jnosql.artemis.query.RepositoryType;
import org.jnosql.artemis.reflection.ClassMapping;
import org.jnosql.artemis.reflection.DynamicQueryMethodReturn;
import org.jnosql.artemis.reflection.DynamicReturn;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

import static java.util.stream.Collectors.toList;

/**
 * Template method to {@link Repository} proxy on Graph
 *
 * @param <T> the entity type
 * @param <K> the K entity
 */
abstract class AbstractGraphRepositoryProxy<T, K> implements InvocationHandler {


    private final SelectQueryConverter converter = new SelectQueryConverter();

    private final DeleteQueryConverter deleteConverter = new DeleteQueryConverter();

    protected abstract ClassMapping getClassMapping();

    protected abstract Repository getRepository();

    protected abstract Graph getGraph();

    protected abstract GraphConverter getConverter();

    protected abstract GraphTemplate getTemplate();

    protected abstract Converters getConverters();


    @Override
    public Object invoke(Object instance, Method method, Object[] args) throws Throwable {

        RepositoryType type = RepositoryType.of(method);
        Class<?> typeClass = getClassMapping().getClassInstance();

        switch (type) {
            case DEFAULT:
                return method.invoke(getRepository(), args);
            case FIND_BY:
                return findById(method, args, typeClass);
            case FIND_ALL:
                return findAll(method, typeClass);
            case DELETE_BY:
                return executeDeleteMethod(method, args);
            case OBJECT_METHOD:
                return method.invoke(this, args);
            case UNKNOWN:
            case JNOSQL_QUERY:
                DynamicQueryMethodReturn methodReturn = DynamicQueryMethodReturn.builder()
                        .withArgs(args)
                        .withMethod(method)
                        .withTypeClass(typeClass)
                        .withPrepareConverter(q -> getTemplate().prepare(q))
                        .withQueryConverter(q -> getTemplate().query(q)).build();
                return methodReturn.execute();
            default:
                return Void.class;

        }
    }

    private Object findAll(Method method, Class<?> typeClass) {

        Supplier<List<?>> querySupplier = () ->
                getGraph().traversal().V()
                        .hasLabel(getClassMapping().getName())
                        .toList()
                        .stream()
                        .map(getConverter()::toEntity)
                        .collect(toList());

        return converter(method, typeClass, querySupplier);
    }

    private Object findById(Method method, Object[] args, Class<?> typeClass) {

        Supplier<List<?>> querySupplier = () -> {
            GraphQueryMethod queryMethod = new GraphQueryMethod(getClassMapping(),
                    getGraph().traversal().V(),
                    getConverters(), method, args);

            Pagination pagination = DynamicReturn.findPagination(args);
            if (pagination != null) {
                queryMethod.getTraversal()
                        .limit(pagination.getLimit())
                        .skip(pagination.getSkip());
            }

            return converter.apply(queryMethod)
                    .stream()
                    .map(getConverter()::toEntity)
                    .collect(toList());
        };

        return converter(method, typeClass, querySupplier);
    }

    private Object converter(Method method, Class<?> typeClass, Supplier<List<?>> querySupplier) {
        Supplier<Optional<?>> singleSupplier =
                DynamicReturn.toSingleResult(method).apply(querySupplier);

        DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
                .withClassSource(typeClass)
                .withMethodSource(method)
                .withList(querySupplier)
                .withSingleResult(singleSupplier)
                .build();

        return dynamicReturn.execute();
    }

    private Object executeDeleteMethod(Method method, Object[] args) {

        GraphQueryMethod queryMethod = new GraphQueryMethod(getClassMapping(),
                getGraph().traversal().V(),
                getConverters(), method, args);

        List<Vertex> vertices = deleteConverter.apply(queryMethod);
        vertices.forEach(Vertex::remove);
        return Void.class;
    }

}<commit_msg>creates message about Graph and Repository<commit_after>/*
 *  Copyright (c) 2017 <NAME> and others
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   and Apache License v2.0 which accompanies this distribution.
 *   The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *   and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
 *
 *   You may elect to redistribute this code under either of these licenses.
 *
 *   Contributors:
 *
 *   <NAME>
 */
package org.jnosql.artemis.graph.query;

import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.jnosql.artemis.Converters;
import org.jnosql.artemis.DynamicQueryException;
import org.jnosql.artemis.Page;
import org.jnosql.artemis.Pagination;
import org.jnosql.artemis.Repository;
import org.jnosql.artemis.graph.GraphConverter;
import org.jnosql.artemis.graph.GraphTemplate;
import org.jnosql.artemis.query.RepositoryType;
import org.jnosql.artemis.reflection.ClassMapping;
import org.jnosql.artemis.reflection.DynamicQueryMethodReturn;
import org.jnosql.artemis.reflection.DynamicReturn;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;

import static java.util.stream.Collectors.toList;

/**
 * Template method to {@link Repository} proxy on Graph
 *
 * @param <T> the entity type
 * @param <K> the K entity
 */
abstract class AbstractGraphRepositoryProxy<T, K> implements InvocationHandler {


    private final SelectQueryConverter converter = new SelectQueryConverter();

    private final DeleteQueryConverter deleteConverter = new DeleteQueryConverter();

    protected abstract ClassMapping getClassMapping();

    protected abstract Repository getRepository();

    protected abstract Graph getGraph();

    protected abstract GraphConverter getConverter();

    protected abstract GraphTemplate getTemplate();

    protected abstract Converters getConverters();


    @Override
    public Object invoke(Object instance, Method method, Object[] args) throws Throwable {

        RepositoryType type = RepositoryType.of(method);
        Class<?> typeClass = getClassMapping().getClassInstance();

        switch (type) {
            case DEFAULT:
                return method.invoke(getRepository(), args);
            case FIND_BY:
                return findById(method, args, typeClass);
            case FIND_ALL:
                return findAll(method, typeClass, args);
            case DELETE_BY:
                return executeDeleteMethod(method, args);
            case OBJECT_METHOD:
                return method.invoke(this, args);
            case UNKNOWN:
            case JNOSQL_QUERY:
                DynamicQueryMethodReturn methodReturn = DynamicQueryMethodReturn.builder()
                        .withArgs(args)
                        .withMethod(method)
                        .withTypeClass(typeClass)
                        .withPrepareConverter(q -> getTemplate().prepare(q))
                        .withQueryConverter(q -> getTemplate().query(q)).build();
                return methodReturn.execute();
            default:
                return Void.class;

        }
    }

    private Object findAll(Method method, Class<?> typeClass, Object[] args) {

        Supplier<List<?>> querySupplier = () -> {

            GraphTraversal<Vertex, Vertex> traversal = getGraph().traversal().V().hasLabel(getClassMapping().getName());
            Pagination pagination = DynamicReturn.findPagination(args);
            if (pagination != null) {
                traversal
                        .limit(pagination.getLimit())
                        .skip(pagination.getSkip());
            }
            return traversal.toList()
                    .stream()
                    .map(getConverter()::toEntity)
                    .collect(toList());
        };

        return converter(method, typeClass, querySupplier, args);
    }

    private Object findById(Method method, Object[] args, Class<?> typeClass) {

        Supplier<List<?>> querySupplier = () -> {
            GraphQueryMethod queryMethod = new GraphQueryMethod(getClassMapping(),
                    getGraph().traversal().V(),
                    getConverters(), method, args);

            Pagination pagination = DynamicReturn.findPagination(args);
            if (pagination != null) {
                queryMethod.getTraversal()
                        .limit(pagination.getLimit())
                        .skip(pagination.getSkip());
            }

            return converter.apply(queryMethod)
                    .stream()
                    .map(getConverter()::toEntity)
                    .collect(toList());
        };

        return converter(method, typeClass, querySupplier, args);
    }

    private Object converter(Method method, Class<?> typeClass,
                             Supplier<List<?>> querySupplier,
                             Object[] args) {

        Supplier<Optional<?>> singleSupplier =
                DynamicReturn.toSingleResult(method).apply(querySupplier);

        Function<Pagination, Page<?>> pageFunction = p -> {
            throw new DynamicQueryException("Graph database repository does not support Page as return Type");
        };

        DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
                .withClassSource(typeClass)
                .withMethodSource(method)
                .withList(querySupplier)
                .withSingleResult(singleSupplier)
                .withPagination(DynamicReturn.findPagination(args))
                .withListPagination(p -> querySupplier.get())
                .withSingleResultPagination(p -> singleSupplier.get())
                .withPage(pageFunction)
                .build();

        return dynamicReturn.execute();
    }

    private Object executeDeleteMethod(Method method, Object[] args) {

        GraphQueryMethod queryMethod = new GraphQueryMethod(getClassMapping(),
                getGraph().traversal().V(),
                getConverters(), method, args);

        List<Vertex> vertices = deleteConverter.apply(queryMethod);
        vertices.forEach(Vertex::remove);
        return Void.class;
    }

}
{'fraction_non_alphanumeric': 0.09729013010690313, 'fraction_numerical': 0.0023203778901135326, 'mean_word_length': 2.387033398821218, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4235742', 'n_tokens_mistral': 3165, 'n_tokens_neox': 2967, 'n_words': 700}