/*
 * Copyright 2017, TeamDev Ltd. 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 org.spine3.server.storage.jdbc.command.query;

import org.spine3.base.CommandStatus;
import org.spine3.server.command.CommandRecord;
import org.spine3.server.storage.jdbc.DatabaseException;
import org.spine3.server.storage.jdbc.query.StorageQuery;
import org.spine3.server.storage.jdbc.table.CommandTable;
import org.spine3.server.storage.jdbc.util.ConnectionWrapper;
import org.spine3.server.storage.jdbc.util.DbIterator;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;

import static org.spine3.server.storage.jdbc.Sql.BuildingBlock.EQUAL;
import static org.spine3.server.storage.jdbc.Sql.BuildingBlock.SEMICOLON;
import static org.spine3.server.storage.jdbc.Sql.Query.FROM;
import static org.spine3.server.storage.jdbc.Sql.Query.PLACEHOLDER;
import static org.spine3.server.storage.jdbc.Sql.Query.SELECT;
import static org.spine3.server.storage.jdbc.Sql.Query.WHERE;
import static org.spine3.server.storage.jdbc.table.CommandTable.Column.command;
import static org.spine3.server.storage.jdbc.table.CommandTable.Column.command_status;

/**
 * Query that selects {@link CommandRecord} by {@link CommandStatus}.
 *
 * @author Alexander Litus
 * @author Andrey Lavrov
 */
public class SelectCommandByStatusQuery extends StorageQuery {

    private final CommandStatus status;

    private static final String QUERY_TEMPLATE =
            SELECT.toString() + command + FROM + CommandTable.TABLE_NAME +
            WHERE + command_status + EQUAL + PLACEHOLDER + SEMICOLON;

    /**
     * Creates a new query instance.
     */
    protected SelectCommandByStatusQuery(Builder builder) {
        super(builder);
        this.status = builder.status;
    }

    /**
     *
     */
    public static Builder newBuilder() {
        final Builder builder = new Builder();
        builder.setQuery(QUERY_TEMPLATE);
        return builder;
    }

    /**
     * Prepares SQL statement using the connection.
     */
    @Override
    public PreparedStatement prepareStatement(ConnectionWrapper connection) {
        final PreparedStatement statement = super.prepareStatement(connection);
        try {
            statement.setString(1, status.toString());
        } catch (SQLException e) {
            logFailedToPrepareStatement(e);
            throw new DatabaseException(e);
        }
        return statement;
    }

    public Iterator<CommandRecord> execute() throws DatabaseException {
        try (ConnectionWrapper connection = getConnection(true)) {
            final PreparedStatement statement = prepareStatement(connection);
            return new DbIterator<>(statement, command_status.name(), CommandRecord.getDescriptor());
        }
    }

    @SuppressWarnings("ClassNameSameAsAncestorName")
    public static class Builder extends StorageQuery.Builder<Builder, SelectCommandByStatusQuery> {

        private CommandStatus status;

        @Override
        public SelectCommandByStatusQuery build() {
            return new SelectCommandByStatusQuery(this);
        }

        public Builder setStatus(CommandStatus status) {
            this.status = status;
            return getThis();
        }

        @Override
        protected Builder getThis() {
            return this;
        }
    }
}

@@ -3646,23 +3646,16 @@
  command
-_status
 .name(),

Fix typo.