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.put;
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 PutRiverStateRequest extends AcknowledgedRequest<PutRiverStateRequest> {
029
030    private String riverName;
031
032    private String riverType;
033
034    private RiverState riverState;
035
036    public PutRiverStateRequest setRiverName(String riverName) {
037        this.riverName = riverName;
038        return this;
039    }
040
041    public String getRiverName() {
042        return riverName;
043    }
044
045    public PutRiverStateRequest setRiverType(String riverType) {
046        this.riverType = riverType;
047        return this;
048    }
049
050    public String getRiverType() {
051        return riverType;
052    }
053
054
055    public PutRiverStateRequest setRiverState(RiverState riverState) {
056        this.riverState = riverState;
057        return this;
058    }
059
060    public RiverState getRiverState() {
061        return riverState;
062    }
063
064    @Override
065    public ActionRequestValidationException validate() {
066        ActionRequestValidationException validationException = null;
067        if (riverName == null) {
068            validationException = addValidationError("name is missing", null);
069        }
070        if (riverType == null) {
071            validationException = addValidationError("type is missing", validationException);
072        }
073        if (riverState == null) {
074            validationException = addValidationError("river state is missing", validationException);
075        }
076        return validationException;
077    }
078
079    @Override
080    public void readFrom(StreamInput in) throws IOException {
081        super.readFrom(in);
082        readTimeout(in);
083        this.riverName = in.readString();
084        this.riverType = in.readString();
085        if (in.readBoolean()) {
086            this.riverState = new RiverState();
087            riverState.readFrom(in);
088        }
089    }
090
091    @Override
092    public void writeTo(StreamOutput out) throws IOException {
093        super.writeTo(out);
094        writeTimeout(out);
095        out.writeString(riverName);
096        out.writeString(riverType);
097        if (riverState == null) {
098            out.writeBoolean(false);
099        } else {
100            out.writeBoolean(true);
101            riverState.writeTo(out);
102        }
103    }
104}