/*
 * 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 com.spotify.reaper.storage.postgresql;

import com.google.common.collect.ImmutableList;

import com.spotify.reaper.core.RepairSchedule;

import org.apache.cassandra.repair.RepairParallelism;
import org.apache.commons.lang.ArrayUtils;
import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.tweak.ResultSetMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

public class RepairScheduleMapper implements ResultSetMapper<RepairSchedule> {

  @Override
  public RepairSchedule map(int index, ResultSet r, StatementContext ctx) throws SQLException {
    Long[] runHistory = ArrayUtils.toObject((long[]) r.getArray("run_history").getArray());
    return new RepairSchedule.Builder(
        r.getLong("repair_unit_id"),
        RepairSchedule.State.valueOf(r.getString("state")),
        r.getInt("days_between"),
        RepairRunMapper.getDateTimeOrNull(r, "next_activation"),
        runHistory != null ? ImmutableList.copyOf(runHistory) : ImmutableList.<Long>of(),
        r.getInt("segment_count"),
        RepairParallelism.valueOf(r.getString("repair_parallelism")),
        r.getDouble("intensity"),
        RepairRunMapper.getDateTimeOrNull(r, "creation_time"))
        .owner(r.getString("owner"))
        .pauseTime(RepairRunMapper.getDateTimeOrNull(r, "pause_time"))
        .build(r.getLong("id"));
  }
}

@@ -1139,20 +1139,23 @@
 n %7B%0A    
-Long
+Integer
 %5B%5D runHi
@@ -1187,12 +1187,11 @@
 ct((
-long
+int
 %5B%5D) 
@@ -1229,16 +1229,320 @@
 ray());%0A
+    Long%5B%5D runHistoryLong;%0A    if (null != runHistory && runHistory.length %3E 0) %7B%0A      runHistoryLong = new Long%5BrunHistory.length%5D;%0A      for (int i = 0; i %3C runHistory.length; i++) %7B%0A        runHistoryLong%5Bi%5D = runHistory%5Bi%5D.longValue();%0A      %7D%0A    %7D else %7B%0A      runHistoryLong = new Long%5B0%5D;%0A    %7D%0A
     retu
@@ -1779,29 +1779,8 @@
     
- runHistory != null ?
  Imm
@@ -1811,35 +1811,12 @@
 tory
-) : ImmutableList.%3CLong%3Eof(
+Long
 ),%0A 

fix issue in mapping int SQL array to long array