All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jnosql.artemis.document.AbsctractDocumentWorkflow Maven / Gradle / Ivy

The newest version!
/*
 *  Copyright (c) 2017 Otávio Santana 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:
 *
 *   Otavio Santana
 */
package org.jnosql.artemis.document;

import org.jnosql.diana.api.document.DocumentEntity;

import java.util.Objects;
import java.util.function.Function;
import java.util.function.UnaryOperator;

/**
 * The template method to {@link DocumentWorkflow}
 */
public abstract class AbsctractDocumentWorkflow implements DocumentWorkflow {

    protected abstract DocumentEventPersistManager getDocumentEventPersistManager();


    protected abstract  DocumentEntityConverter getConverter();


    @Override
    public  T flow(T entity, UnaryOperator action) {

        Function flow = getFlow(entity, action);

        return flow.apply(entity);

    }

    private  Function getFlow(T entity, UnaryOperator action) {
        UnaryOperator validation = t -> Objects.requireNonNull(t, "entity is required");

        UnaryOperator firePreEntity = t -> {
            getDocumentEventPersistManager().firePreEntity(t);
            return t;
        };

        UnaryOperator firePreDocumentEntity = t -> {
            getDocumentEventPersistManager().firePreDocumentEntity(t);
            return t;
        };


        Function converterDocument = t -> getConverter().toDocument(t);

        UnaryOperator firePreDocument = t -> {
            getDocumentEventPersistManager().firePreDocument(t);
            return t;
        };

        UnaryOperator firePostDocument = t -> {
            getDocumentEventPersistManager().firePostDocument(t);
            return t;
        };

        Function converterEntity = t -> getConverter().toEntity(entity, t);

        UnaryOperator firePostEntity = t -> {
            getDocumentEventPersistManager().firePostEntity(t);
            return t;
        };

        UnaryOperator firePostDocumentEntity = t -> {
            getDocumentEventPersistManager().firePostDocumentEntity(t);
            return t;
        };


        return validation
                .andThen(firePreEntity)
                .andThen(firePreDocumentEntity)
                .andThen(converterDocument)
                .andThen(firePreDocument)
                .andThen(action)
                .andThen(firePostDocument)
                .andThen(converterEntity)
                .andThen(firePostEntity)
                .andThen(firePostDocumentEntity);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy