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.get;
017
018import org.elasticsearch.action.ActionResponse;
019import org.elasticsearch.common.collect.ImmutableList;
020import org.elasticsearch.common.io.stream.StreamInput;
021import org.elasticsearch.common.io.stream.StreamOutput;
022import org.elasticsearch.common.xcontent.ToXContent;
023import org.elasticsearch.common.xcontent.XContentBuilder;
024import org.xbib.elasticsearch.plugin.jdbc.state.RiverState;
025
026import java.io.IOException;
027
028import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
029
030public class GetRiverStateResponse extends ActionResponse implements ToXContent {
031
032    private ImmutableList<RiverState> states;
033
034    public GetRiverStateResponse() {
035        states = ImmutableList.of();
036    }
037
038    public GetRiverStateResponse(ImmutableList<RiverState> riverStates) {
039        states = riverStates;
040    }
041
042    public RiverState getRiverState() {
043        if (states == null || states.isEmpty()) {
044            return null;
045        } else {
046            return states.get(0);
047        }
048    }
049
050    public ImmutableList<RiverState> getRiverStates() {
051        return states;
052    }
053
054    public boolean exists(String name, String type) {
055        if (states != null && name != null && type != null) {
056            for (RiverState state : states) {
057                if (state != null && name.equals(state.getName()) && type.equals(state.getType())) {
058                    return true;
059                }
060            }
061        }
062        return false;
063    }
064
065    @Override
066    public void readFrom(StreamInput in) throws IOException {
067        super.readFrom(in);
068        int len = in.readInt();
069        ImmutableList.Builder<RiverState> builder = ImmutableList.builder();
070        for (int i = 0; i < len; i++) {
071            RiverState rs = new RiverState();
072            rs.readFrom(in);
073            builder.add(rs);
074        }
075        states = builder.build();
076    }
077
078    @Override
079    public void writeTo(StreamOutput out) throws IOException {
080        super.writeTo(out);
081        if (states == null) {
082            out.writeInt(0);
083        } else {
084            out.writeInt(states.size());
085            for (RiverState rs : states) {
086                rs.writeTo(out);
087            }
088        }
089    }
090
091    @Override
092    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
093        builder.field("state", states);
094        return builder;
095    }
096
097    @Override
098    public String toString() {
099        try {
100            XContentBuilder builder = jsonBuilder();
101            builder.startObject();
102            builder = toXContent(builder, EMPTY_PARAMS);
103            builder.endObject();
104            return builder.string();
105        } catch (IOException e) {
106            return "";
107        }
108    }
109
110}