/*
 * Copyright 2020, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.server.storage.jdbc.delivery;

import com.google.common.collect.ImmutableList;
import com.google.protobuf.Descriptors;
import io.spine.server.delivery.CatchUp;
import io.spine.server.delivery.CatchUpId;
import io.spine.server.delivery.InboxMessage;
import io.spine.server.storage.jdbc.DataSourceWrapper;
import io.spine.server.storage.jdbc.Type;
import io.spine.server.storage.jdbc.TypeMapping;
import io.spine.server.storage.jdbc.message.MessageTable;
import io.spine.server.storage.jdbc.query.IdColumn;
import io.spine.type.TypeUrl;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Iterator;

import static io.spine.server.storage.jdbc.Type.STRING_255;

/**
 * A table in the DB responsible for storing the statuses of the ongoing
 * {@link io.spine.server.delivery.CatchUp catch-up} processes.
 */
public class CatchUpTable extends MessageTable<CatchUpId, CatchUp> {

    private static final String NAME = "catchup";

    CatchUpTable(DataSourceWrapper dataSource, TypeMapping typeMapping) {
        super(NAME, IdColumn.of(CatchUpTable.Column.ID, CatchUpId.class), dataSource, typeMapping);
    }

    @Override
    protected Descriptors.Descriptor messageDescriptor() {
        return CatchUp.getDescriptor();
    }

    @Override
    protected Iterable<? extends MessageTable.Column<CatchUp>> messageSpecificColumns() {
        return ImmutableList.copyOf(Column.values());
    }

    Iterable<CatchUp> readByType(TypeUrl type) {
        SelectCatchUpByTypeQuery query = SelectCatchUpByTypeQuery
                .newBuilder()
                .setDataSource(dataSource())
                .setTableName(name())
                .setProjectionType(type)
                .build();
        Iterator<CatchUp> iterator = query.execute();
        return ImmutableList.copyOf(iterator);
    }

    public Iterable<CatchUp> readAll() {
        SelectAllCatchUpsQuery query = SelectAllCatchUpsQuery
                .newBuilder()
                .setDataSource(dataSource())
                .setTableName(name())
                .build();
        Iterator<CatchUp> iterator = query.execute();
        return ImmutableList.copyOf(iterator);
    }

    /**
     * The columns of {@link InboxMessage} DB representation.
     */
    enum Column implements MessageTable.Column<CatchUp> {

        ID(CatchUp::getId),

        PROJECTION_TYPE(STRING_255, (m) -> m.getId()
                                            .getProjectionType());

        private final @Nullable Type type;
        private final Getter<CatchUp> getter;

        Column(Type type, Getter<CatchUp> getter) {
            this.type = type;
            this.getter = getter;
        }

        Column(Getter<CatchUp> getter) {
            this.type = null;
            this.getter = getter;
        }

        @Override
        public @Nullable Type type() {
            return type;
        }

        @Override
        public boolean isPrimaryKey() {
            return this == ID;
        }

        @Override
        public boolean isNullable() {
            return false;
        }

        @Override
        public Getter<CatchUp> getter() {
            return getter;
        }
    }
}

@@ -1974,16 +1974,267 @@
 chup%22;%0A%0A
+    /**%0A     * Creates a new instance of the table.%0A     *%0A     * @param dataSource%0A     *         wrapper over the RDBMS data source%0A     * @param typeMapping%0A     *         mapping of types, specific to the RDBMS for the current application%0A     */%0A
     Catc
@@ -2687,24 +2687,129 @@
 ());%0A    %7D%0A%0A
+    /**%0A     * Reads all %7B@code CatchUp%7D statuses by the type URL of the catching-up projection.%0A     */%0A
     Iterable
@@ -3195,24 +3195,258 @@
 or);%0A    %7D%0A%0A
+    /**%0A     * Reads all %7B@code CatchUp%7D statuses.%0A     *%0A     * @implNote Realistically, there should not be lots of the catch-up processes in the%0A     *         system. Therefore, no pagination is implemented at this point.%0A     */%0A
     public I
@@ -3932,28 +3932,205 @@
     
-ID(CatchUp::getId),%0A
+/**%0A         * Identifier of the %7B@code CatchUp%7D.%0A         */%0A        ID(CatchUp::getId),%0A%0A        /**%0A         * The type URL of the catching-up projection, stored as a %7B@code String%7D.%0A         */
 %0A   

Document the table class.