/**
 * Copyright 2016 Pinterest, Inc.
 *
 * 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.pinterest.deployservice.bean;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;

/**
 * Keep the bean and table in sync
 * <p>
 * CREATE TABLE IF NOT EXISTS schedules (
 *    id                  VARCHAR(22)     NOT NULL,
 *    total_sessions      INT             NOT NULL,
 *    cooldown_times      VARCHAR(32)     NOT NULL,
 *    host_numbers        VARCHAR(32)     NOT NULL,
 *    current_session     INT             NOT NULL,
 *    state               VARCHAR(32)     NOT NULL,
 *    state_start_time    BIGINT          NOT NULL,
 *    PRIMARY KEY (id)
 * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 * );
 */
public class ScheduleBean implements Updatable {

    @JsonProperty("id")
    private String id;
    
    @JsonProperty("totalSessions")
    private Integer total_sessions;

    @JsonProperty("cooldownTimes")
    private String cooldown_times;

    @JsonProperty("hostNumbers")
    private String host_numbers;

    @JsonProperty("currentSession")
    private Integer current_session;

    @JsonProperty("state")
    private ScheduleState state;

    @JsonProperty("stateStartTime")
    private Long state_start_time;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Integer getTotal_sessions() {
        return total_sessions;
    }

    public void setTotal_sessions(Integer total_sessions) {
        this.total_sessions = total_sessions;
    }

    public String getCooldown_times() {
        return cooldown_times;
    }

    public void setCooldown_times(String cooldown_times) {
        this.cooldown_times = cooldown_times;
    }

    public String getHost_numbers() {
        return host_numbers;
    }

    public void setHost_numbers(String host_numbers) {
        this.host_numbers = host_numbers;
    }

    public Integer getCurrent_session() {
        return current_session;
    }

    public void setCurrent_session(Integer current_session) {
        this.current_session = current_session;
    }

    public ScheduleState getState() {
        return state;
    }

    public void setState(ScheduleState state) {
        this.state = state;
    }

    public Long getState_start_time() {
        return state_start_time;
    }

    public void setState_start_time(Long state_start_time) {
        this.state_start_time = state_start_time;
    }

    @Override
    public SetClause genSetClause() {
        SetClause clause = new SetClause();
        clause.addColumn("id", id);
        clause.addColumn("total_sessions", total_sessions);
        clause.addColumn("cooldown_times", cooldown_times);
        clause.addColumn("host_numbers", host_numbers);
        clause.addColumn("current_session", current_session);
        clause.addColumn("state", state);
        clause.addColumn("state_start_time", state_start_time);
        return clause;
    }

    public final static String UPDATE_CLAUSE =
        "id=VALUES(id)," +
            "total_sessions=VALUES(total_sessions)," +
            "cooldown_times=VALUES(cooldown_times)," +
            "host_numbers=VALUES(host_numbers)," +
            "current_session=VALUES(current_session)," +
            "state=VALUES(state)," +
            "state_start_time=VALUES(state_start_time)";

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }
}

@@ -2625,24 +2625,403 @@
 _session) %7B%0A
+        //Do validation here, user input may mess it up. If we get wrong data, UI will crash%0A        if (current_session%3Ethis.getTotal_sessions())%7B%0A            throw new IllegalArgumentException(String.format(%22Current session %25s cannot be bigger than total_session %25s %22,%0A                        current_session,%0A                this.getTotal_sessions()%0A            ));%0A        %7D%0A
         this

Add validation for schedule to avoid incorrect user input results UI fail to rendering (#425)