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

com.eventsourcing.cep.protocols.DescriptionProtocol Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2016, All Contributors (see CONTRIBUTORS file)
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package com.eventsourcing.cep.protocols;

import com.eventsourcing.EntityHandle;
import com.eventsourcing.Protocol;
import com.eventsourcing.Repository;
import com.eventsourcing.cep.events.DescriptionChanged;
import com.eventsourcing.queries.ModelCollectionQuery;
import com.eventsourcing.queries.ModelLoader;
import com.eventsourcing.queries.ModelQueries;
import com.googlecode.cqengine.resultset.ResultSet;
import org.unprotocols.coss.Draft;
import org.unprotocols.coss.RFC;

import java.util.Optional;
import java.util.stream.Stream;

import static com.eventsourcing.index.EntityQueryFactory.equal;
import static com.googlecode.cqengine.stream.StreamFactory.streamOf;

@Draft @RFC(url = "http://rfc.eventsourcing.com/spec:3/CEP")
public interface DescriptionProtocol extends Protocol, ModelQueries {
    default String description() {
        Optional last = latestAssociatedEntity(DescriptionChanged.class, DescriptionChanged.REFERENCE_ID, DescriptionChanged.TIMESTAMP);
        if (last.isPresent()) {
            return last.get().description();
        } else {
            return null;
        }
    }

    class DescribedModelCollectionQuery implements ModelCollectionQuery {

        private final String description;
        private final ModelLoader loader;

        public DescribedModelCollectionQuery(String description, ModelLoader loader) {
            this.description = description;
            this.loader = loader;
        }

        @Override public Stream getCollectionStream(Repository repository) {
            ResultSet> resultSet = repository
                    .query(DescriptionChanged.class, equal(DescriptionChanged.DESCRIPTION, description));
            return streamOf(resultSet)
                    .map(h -> loader.load(repository, h.get().reference()).get())
                    .onClose(resultSet::close);
        }
    }

    static  ModelCollectionQuery described(String description, ModelLoader loader) {
        return new DescribedModelCollectionQuery<>(description, loader);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy