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.plugin.jdbc.client;
017
018import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest;
019import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
020import org.elasticsearch.client.Client;
021import org.elasticsearch.common.io.Streams;
022import org.elasticsearch.common.settings.ImmutableSettings;
023import org.elasticsearch.common.settings.Settings;
024import org.elasticsearch.common.xcontent.XContentBuilder;
025
026import java.io.IOException;
027import java.io.InputStream;
028import java.io.InputStreamReader;
029import java.io.StringWriter;
030import java.util.Map;
031
032import static org.elasticsearch.common.collect.Maps.newHashMap;
033import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
034
035public class ConfigHelper {
036
037    private ImmutableSettings.Builder settingsBuilder;
038
039    private Settings settings;
040
041    private Map<String, String> mappings = newHashMap();
042
043    public ConfigHelper reset() {
044        settingsBuilder = ImmutableSettings.settingsBuilder();
045        return this;
046    }
047
048    public ConfigHelper settings(Settings settings) {
049        this.settings = settings;
050        return this;
051    }
052
053    public ConfigHelper setting(String key, String value) {
054        if (settingsBuilder == null) {
055            settingsBuilder = ImmutableSettings.settingsBuilder();
056        }
057        settingsBuilder.put(key, value);
058        return this;
059    }
060
061    public ConfigHelper setting(String key, Boolean value) {
062        if (settingsBuilder == null) {
063            settingsBuilder = ImmutableSettings.settingsBuilder();
064        }
065        settingsBuilder.put(key, value);
066        return this;
067    }
068
069    public ConfigHelper setting(String key, Integer value) {
070        if (settingsBuilder == null) {
071            settingsBuilder = ImmutableSettings.settingsBuilder();
072        }
073        settingsBuilder.put(key, value);
074        return this;
075    }
076
077    public ConfigHelper setting(InputStream in) throws IOException {
078        if (settingsBuilder == null) {
079            settingsBuilder = ImmutableSettings.settingsBuilder();
080        }
081        settingsBuilder = ImmutableSettings.settingsBuilder().loadFromStream(".json", in);
082        return this;
083    }
084
085    public ImmutableSettings.Builder settingsBuilder() {
086        return settingsBuilder != null ? settingsBuilder : ImmutableSettings.settingsBuilder();
087    }
088
089    public Settings settings() {
090        if (settings != null) {
091            return settings;
092        }
093        if (settingsBuilder == null) {
094            settingsBuilder = ImmutableSettings.settingsBuilder();
095        }
096        return settingsBuilder.build();
097    }
098
099    public ConfigHelper mapping(String type, InputStream in) throws IOException {
100        if (type == null) {
101            return this;
102        }
103        StringWriter sw = new StringWriter();
104        Streams.copy(new InputStreamReader(in), sw);
105        mappings.put(type, sw.toString());
106        return this;
107    }
108
109    public ConfigHelper mapping(String type, String mapping) {
110        if (type == null) {
111            return this;
112        }
113        this.mappings.put(type, mapping);
114        return this;
115    }
116
117    public ConfigHelper putMapping(Client client, String index) {
118        if (!mappings.isEmpty()) {
119            for (Map.Entry<String, String> me : mappings.entrySet()) {
120                client.admin().indices().putMapping(new PutMappingRequest(index).type(me.getKey()).source(me.getValue())).actionGet();
121            }
122        }
123        return this;
124    }
125
126    public ConfigHelper deleteMapping(Client client, String index, String type) {
127        client.admin().indices().deleteMapping(new DeleteMappingRequest(index).types(type)).actionGet();
128        return this;
129    }
130
131    public String defaultMapping() throws IOException {
132        XContentBuilder b = jsonBuilder()
133                .startObject()
134                .startObject("_default_")
135                .field("date_detection", true);
136        b.endObject()
137                .endObject();
138        return b.string();
139    }
140
141    public Map<String, String> mappings() {
142        return mappings.isEmpty() ? null : mappings;
143    }
144
145}