001/*
002 * Copyright (C) 2014 Jörg Prante
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.xbib.elasticsearch.action.plugin.jdbc.state.post;
017
018import org.elasticsearch.action.ActionRequestValidationException;
019import org.elasticsearch.action.support.master.AcknowledgedRequest;
020import org.elasticsearch.common.io.stream.StreamInput;
021import org.elasticsearch.common.io.stream.StreamOutput;
022import org.xbib.elasticsearch.plugin.jdbc.state.RiverState;
023
024import java.io.IOException;
025
026import static org.elasticsearch.action.ValidateActions.addValidationError;
027
028public class PostRiverStateRequest extends AcknowledgedRequest<PostRiverStateRequest> {
029
030    private String riverName;
031
032    private String riverType;
033
034    private RiverState riverState;
035
036    private boolean abort;
037
038    private boolean suspend;
039
040    public PostRiverStateRequest setRiverName(String riverName) {
041        this.riverName = riverName;
042        return this;
043    }
044
045    public String getRiverName() {
046        return riverName;
047    }
048
049    public PostRiverStateRequest setRiverType(String riverType) {
050        this.riverType = riverType;
051        return this;
052    }
053
054    public String getRiverType() {
055        return riverType;
056    }
057
058
059    public PostRiverStateRequest setRiverState(RiverState riverState) {
060        this.riverState = riverState;
061        return this;
062    }
063
064    public RiverState getRiverState() {
065        return riverState;
066    }
067
068    public PostRiverStateRequest setAbort() {
069        this.abort = true;
070        return this;
071    }
072
073    public boolean isAbort() {
074        return abort;
075    }
076
077    public PostRiverStateRequest setSuspend() {
078        this.suspend = true;
079        return this;
080    }
081
082    public boolean isSuspend() {
083        return suspend;
084    }
085
086    public PostRiverStateRequest setResume() {
087        this.suspend = false;
088        return this;
089    }
090
091    public boolean isResume() {
092        return !suspend;
093    }
094
095    @Override
096    public ActionRequestValidationException validate() {
097        ActionRequestValidationException validationException = null;
098        if (riverName == null) {
099            validationException = addValidationError("name is missing", null);
100        }
101        if (riverType == null) {
102            validationException = addValidationError("type is missing", validationException);
103        }
104        return validationException;
105    }
106
107    @Override
108    public void readFrom(StreamInput in) throws IOException {
109        super.readFrom(in);
110        readTimeout(in);
111        this.riverName = in.readString();
112        this.riverType = in.readString();
113        if (in.readBoolean()) {
114            this.riverState = new RiverState();
115            riverState.readFrom(in);
116        }
117        this.abort = in.readBoolean();
118        this.suspend = in.readBoolean();
119    }
120
121    @Override
122    public void writeTo(StreamOutput out) throws IOException {
123        super.writeTo(out);
124        writeTimeout(out);
125        out.writeString(riverName);
126        out.writeString(riverType);
127        if (riverState == null) {
128            out.writeBoolean(false);
129        } else {
130            out.writeBoolean(true);
131            riverState.writeTo(out);
132        }
133        out.writeBoolean(abort);
134        out.writeBoolean(suspend);
135    }
136}