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.river.jdbc.strategy.column;
017
018import org.elasticsearch.common.unit.TimeValue;
019import org.elasticsearch.common.xcontent.XContentBuilder;
020import org.elasticsearch.common.xcontent.XContentHelper;
021import org.xbib.elasticsearch.river.jdbc.strategy.simple.SimpleRiverContext;
022
023import java.io.IOException;
024import java.util.HashMap;
025import java.util.Map;
026
027import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
028
029public class ColumnRiverContext extends SimpleRiverContext {
030    /**
031     * Column name that contains creation time (for column strategy)
032     */
033    private String columnCreatedAt;
034
035    /**
036     * Column name that contains last update time (for column strategy)
037     */
038    private String columnUpdatedAt;
039
040    /**
041     * Column name that contains deletion time (for column strategy)
042     */
043    private String columnDeletedAt;
044
045    /**
046     * Contains overlap value for last run timestamp.
047     */
048    private TimeValue lastRunTimeStampOverlap;
049
050    /**
051     * Columns name should be automatically escaped by proper db quote mark or not (for column strategy)
052     */
053    private boolean columnEscape;
054
055    public ColumnRiverContext columnUpdatedAt(String updatedAt) {
056        this.columnUpdatedAt = updatedAt;
057        return this;
058    }
059
060    public String columnUpdatedAt() {
061        return columnUpdatedAt;
062    }
063
064    public ColumnRiverContext columnCreatedAt(String createdAt) {
065        this.columnCreatedAt = createdAt;
066        return this;
067    }
068
069    public String columnCreatedAt() {
070        return columnCreatedAt;
071    }
072
073    public ColumnRiverContext columnDeletedAt(String deletedAt) {
074        this.columnDeletedAt = deletedAt;
075        return this;
076    }
077
078    public String columnDeletedAt() {
079        return columnDeletedAt;
080    }
081
082    public ColumnRiverContext columnEscape(boolean escape) {
083        this.columnEscape = escape;
084        return this;
085    }
086
087    public boolean columnEscape() {
088        return this.columnEscape;
089    }
090
091    public TimeValue getLastRunTimeStampOverlap() {
092        return lastRunTimeStampOverlap;
093    }
094
095    public ColumnRiverContext setLastRunTimeStampOverlap(TimeValue lastRunTimeStampOverlap) {
096        this.lastRunTimeStampOverlap = lastRunTimeStampOverlap;
097        return this;
098    }
099
100    public Map<String, Object> asMap() {
101        Map<String, Object> map = super.asMap();
102        try {
103            XContentBuilder builder = jsonBuilder()
104                    .startObject()
105                    .field("columnCreatedAt", columnCreatedAt)
106                    .field("columnUpdatedAt", columnUpdatedAt)
107                    .field("columnDeletedAt", columnDeletedAt)
108                    .field("columnEscape", columnEscape)
109                    .endObject();
110            map.putAll(XContentHelper.convertToMap(builder.bytes(), true).v2());
111            return map;
112        } catch (IOException e) {
113            // should really not happen
114            return new HashMap<String, Object>();
115        }
116    }
117}