// Path: src/main/java/mtsar/api/Answer.java
// @FreeBuilder
// @XmlRootElement
// @JsonDeserialize(builder = Answer.Builder.class)
// public interface Answer {
//     @Nullable
//     @JsonProperty
//     Integer getId();
// 
//     @JsonProperty
//     String getStage();
// 
//     @JsonProperty
//     Timestamp getDateTime();
// 
//     @JsonProperty
//     List<String> getTags();
// 
//     @JsonIgnore
//     Map<String, String> getMetadata();
// 
//     @JsonProperty
//     String getType();
// 
//     @JsonProperty
//     Integer getWorkerId();
// 
//     @JsonProperty
//     Integer getTaskId();
// 
//     @JsonProperty
//     List<String> getAnswers();
// 
//     @JsonIgnore
//     default Optional<String> getAnswer() {
//         if (getAnswers().isEmpty()) return Optional.empty();
//         return Optional.of(getAnswers().get(0));
//     }
// 
//     @JsonIgnore
//     String getTagsTextArray();
// 
//     @JsonIgnore
//     String getMetadataJSON();
// 
//     @JsonIgnore
//     String getAnswersTextArray();
// 
//     @JsonPOJOBuilder(withPrefix = "set")
//     class Builder extends Answer_Builder {
//         public Builder() {
//             setDateTime(DateTimeUtils.now());
//             setType(AnswerDAO.ANSWER_TYPE_DEFAULT);
//         }
// 
//         public Builder setMetadata(String json) {
//             return setMetadataJSON(json).putAllMetadata(PostgresUtils.parseJSONString(json));
//         }
// 
//         public Answer build() {
//             setTagsTextArray(PostgresUtils.buildArrayString(getTags()));
//             setMetadataJSON(PostgresUtils.buildJSONString(getMetadata()));
//             setAnswersTextArray(PostgresUtils.buildArrayString(getAnswers()));
//             return super.build();
//         }
//     }
// }


import java.util.stream.Collectors;
import mtsar.api.Answer;
import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.sqlobject.*;
import org.skife.jdbi.v2.sqlobject.customizers.BatchChunkSize;
import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;
import org.skife.jdbi.v2.sqlobject.stringtemplate.UseStringTemplate3StatementLocator;
import org.skife.jdbi.v2.tweak.ResultSetMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/*
 * Copyright 2015 Dmitry Ustalov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package mtsar.api.sql;



@UseStringTemplate3StatementLocator
@RegisterMapper(AnswerDAO.Mapper.class)
public interface AnswerDAO {
    String ANSWER_TYPE_ANSWER = "answer";
    String ANSWER_TYPE_DEFAULT = ANSWER_TYPE_ANSWER;
    String ANSWER_TYPE_SKIP = "skip";

    @SqlQuery("select * from answers where stage = :stage")
    List<Answer> listForStage(@Bind("stage") String stage);

    @SqlQuery("select * from answers where task_id = :taskId and stage = :stage")
    List<Answer> listForTask(@Bind("taskId") Integer taskId, @Bind("stage") String stage);

    @SqlQuery("select * from answers where worker_id = :workerId and stage = :stage")
// Path: src/main/java/mtsar/api/Answer.java
// @FreeBuilder
// @XmlRootElement
// @JsonDeserialize(builder = Answer.Builder.class)
// public interface Answer {
//     @Nullable
//     @JsonProperty
//     Integer getId();
// 
//     @JsonProperty
//     String getStage();
// 
//     @JsonProperty
//     Timestamp getDateTime();
// 
//     @JsonProperty
//     List<String> getTags();
// 
//     @JsonIgnore
//     Map<String, String> getMetadata();
// 
//     @JsonProperty
//     String getType();
// 
//     @JsonProperty
//     Integer getWorkerId();
// 
//     @JsonProperty
//     Integer getTaskId();
// 
//     @JsonProperty
//     List<String> getAnswers();
// 
//     @JsonIgnore
//     default Optional<String> getAnswer() {
//         if (getAnswers().isEmpty()) return Optional.empty();
//         return Optional.of(getAnswers().get(0));
//     }
// 
//     @JsonIgnore
//     String getTagsTextArray();
// 
//     @JsonIgnore
//     String getMetadataJSON();
// 
//     @JsonIgnore
//     String getAnswersTextArray();
// 
//     @JsonPOJOBuilder(withPrefix = "set")
//     class Builder extends Answer_Builder {
//         public Builder() {
//             setDateTime(DateTimeUtils.now());
//             setType(AnswerDAO.ANSWER_TYPE_DEFAULT);
//         }
// 
//         public Builder setMetadata(String json) {
//             return setMetadataJSON(json).putAllMetadata(PostgresUtils.parseJSONString(json));
//         }
// 
//         public Answer build() {
//             setTagsTextArray(PostgresUtils.buildArrayString(getTags()));
//             setMetadataJSON(PostgresUtils.buildJSONString(getMetadata()));
//             setAnswersTextArray(PostgresUtils.buildArrayString(getAnswers()));
//             return super.build();
//         }
//     }
// }


// Path: src/main/java/mtsar/api/sql/AnswerDAO.java
import java.util.stream.Collectors;
import mtsar.api.Answer;
import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.sqlobject.*;
import org.skife.jdbi.v2.sqlobject.customizers.BatchChunkSize;
import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;
import org.skife.jdbi.v2.sqlobject.stringtemplate.UseStringTemplate3StatementLocator;
import org.skife.jdbi.v2.tweak.ResultSetMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/*
 * Copyright 2015 Dmitry Ustalov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package mtsar.api.sql;



@UseStringTemplate3StatementLocator
@RegisterMapper(AnswerDAO.Mapper.class)
public interface AnswerDAO {
    String ANSWER_TYPE_ANSWER = "answer";
    String ANSWER_TYPE_DEFAULT = ANSWER_TYPE_ANSWER;
    String ANSWER_TYPE_SKIP = "skip";

    @SqlQuery("select * from answers where stage = :stage")
    List<Answer> listForStage(@Bind("stage") String stage);

    @SqlQuery("select * from answers where task_id = :taskId and stage = :stage")
    List<Answer> listForTask(@Bind("taskId") Integer taskId, @Bind("stage") String stage);

    @SqlQuery("select * from answers where worker_id = :workerId and stage = :stage")
    List<Answer> listForWorker(@Bind("workerId") Integer workerId, @Bind("stage") String stage);