<commit_before>/*
 * Licensed to STRATIO (C) under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.  The STRATIO (C) licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.stratio.connector.twitter;

import static com.codahale.metrics.MetricRegistry.name;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.codahale.metrics.Timer;
import com.stratio.connector.twitter.metadata.TwitterCluster;
import com.stratio.crossdata.common.connector.AbstractExtendedConnector;
import com.stratio.crossdata.common.connector.ConnectorClusterConfig;
import com.stratio.crossdata.common.connector.IConfiguration;
import com.stratio.crossdata.common.connector.IConnectorApp;
import com.stratio.crossdata.common.connector.IMetadataEngine;
import com.stratio.crossdata.common.connector.IQueryEngine;
import com.stratio.crossdata.common.connector.IStorageEngine;
import com.stratio.crossdata.common.data.ClusterName;
import com.stratio.crossdata.common.exceptions.ConnectionException;
import com.stratio.crossdata.common.exceptions.ExecutionException;
import com.stratio.crossdata.common.exceptions.InitializationException;
import com.stratio.crossdata.common.exceptions.UnsupportedException;
import com.stratio.crossdata.common.metadata.ColumnType;
import com.stratio.crossdata.common.metadata.TableMetadata;
import com.stratio.crossdata.common.security.ICredentials;
import com.stratio.crossdata.connectors.ConnectorApp;

import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterConnector extends AbstractExtendedConnector {

    /**
     * Class logger.
     */
    private static final Logger LOG = Logger.getLogger(TwitterConnector.class);
    public static final int MILLIS_IN_NANOSECONDS = 1000000;

    private final Timer connectTimer;

    private TwitterQueryEngine queryEngine;
    private TwitterStorageEngine storageEngine;
    private TwitterMetadataEngine metadataEngine;

    //https://apps.twitter.com/app/8096283/show
    private static final String CONSUMER_KEY = "consumer_key";
    private static final String CONSUMER_SECRET = "consumer_secret";
    private static final String ACCESS_TOKEN = "access_token";
    private static final String ACCESS_TOKEN_SECRET = "access_token_secret";

    private final Map<String, TwitterCluster> clusters = new HashMap<>();
    private final Map<String, ColumnType> allowedColumns = new HashMap<>();

    /**
     * Class constructor.
     *
     * @param connectorApp parent connector app.
     */
    public TwitterConnector(IConnectorApp connectorApp) {
        super(connectorApp);
        connectTimer = new Timer();
        String timerName = name(TwitterConnector.class, "connect");
        registerMetric(timerName, connectTimer);
        allowedColumns.put("Contributors", ColumnType.valueOf("NATIVE"));
        allowedColumns.put("CreatedAt", ColumnType.valueOf("NATIVE"));
        allowedColumns.put("CurrentUserRetweetId", ColumnType.valueOf("BIGINT"));
        allowedColumns.put("FavoriteCount", ColumnType.valueOf("INT"));
        allowedColumns.put("GeoLocationLongitude", ColumnType.valueOf("DOUBLE"));
        allowedColumns.put("GeoLocationLatitude", ColumnType.valueOf("DOUBLE"));
        allowedColumns.put("Id", ColumnType.valueOf("BIGINT"));
        allowedColumns.put("InReplyToScreenName", ColumnType.valueOf("TEXT"));
        allowedColumns.put("InReplyToStatusId", ColumnType.valueOf("BIGINT"));
        allowedColumns.put("InReplyToUserId", ColumnType.valueOf("BIGINT"));
        allowedColumns.put("Lang", ColumnType.valueOf("TEXT"));
        allowedColumns.put("PlaceCountry", ColumnType.valueOf("TEXT"));
        allowedColumns.put("PlaceStreet", ColumnType.valueOf("TEXT"));
        allowedColumns.put("RetweetCount", ColumnType.valueOf("INT"));
        allowedColumns.put("RetweetedStatus", ColumnType.valueOf("NATIVE"));
        allowedColumns.put("Scopes", ColumnType.valueOf("NATIVE"));
        allowedColumns.put("Source", ColumnType.valueOf("TEXT"));
        allowedColumns.put("Text", ColumnType.valueOf("TEXT"));
        allowedColumns.put("UserFollowers", ColumnType.valueOf("INT"));
        allowedColumns.put("UserLang", ColumnType.valueOf("TEXT"));
        allowedColumns.put("UserLocation", ColumnType.valueOf("TEXT"));
        allowedColumns.put("UserName", ColumnType.valueOf("TEXT"));
        allowedColumns.put("Favorited", ColumnType.valueOf("BOOLEAN"));
        allowedColumns.put("PossiblySensitive", ColumnType.valueOf("BOOLEAN"));
        allowedColumns.put("Retweet", ColumnType.valueOf("BOOLEAN"));
        allowedColumns.put("Retweeted", ColumnType.valueOf("BOOLEAN"));
        allowedColumns.put("RetweetedByMe", ColumnType.valueOf("BOOLEAN"));
        allowedColumns.put("Truncated", ColumnType.valueOf("BOOLEAN"));

    }



    @Override public String getConnectorManifestPath() {
        return getClass().getResource("TwitterConnector.xml").getPath();
    }

    @Override public String[] getDatastoreManifestPath() {
        String datastore[]=new String[1];
        datastore[0]= getClass().getResource("TwitterDataStore.xml").getPath();
        return datastore;
    }

    public Map<String, ColumnType> getAllowedColumns() {
        return allowedColumns;
    }

    public void addTableMetadata(String targetCluster, TableMetadata tableMetadata) {
        clusters.get(targetCluster).addTableMetadata(tableMetadata);
    }

    public TableMetadata getTableMetadata(String clusterName, String tableName) {
        return clusters.get(clusterName).getTableMetadata(tableName);<commit_msg>Minor bug fixed<commit_after>/*
 * Licensed to STRATIO (C) under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.  The STRATIO (C) licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.stratio.connector.twitter;

import static com.codahale.metrics.MetricRegistry.name;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.codahale.metrics.Timer;
import com.stratio.connector.twitter.metadata.TwitterCluster;
import com.stratio.crossdata.common.connector.AbstractExtendedConnector;
import com.stratio.crossdata.common.connector.ConnectorClusterConfig;
import com.stratio.crossdata.common.connector.IConfiguration;
import com.stratio.crossdata.common.connector.IConnectorApp;
import com.stratio.crossdata.common.connector.IMetadataEngine;
import com.stratio.crossdata.common.connector.IQueryEngine;
import com.stratio.crossdata.common.connector.IStorageEngine;
import com.stratio.crossdata.common.data.ClusterName;
import com.stratio.crossdata.common.exceptions.ConnectionException;
import com.stratio.crossdata.common.exceptions.ExecutionException;
import com.stratio.crossdata.common.exceptions.InitializationException;
import com.stratio.crossdata.common.exceptions.UnsupportedException;
import com.stratio.crossdata.common.metadata.ColumnType;
import com.stratio.crossdata.common.metadata.TableMetadata;
import com.stratio.crossdata.common.security.ICredentials;
import com.stratio.crossdata.connectors.ConnectorApp;

import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterConnector extends AbstractExtendedConnector {

    /**
     * Class logger.
     */
    private static final Logger LOG = Logger.getLogger(TwitterConnector.class);
    public static final int MILLIS_IN_NANOSECONDS = 1000000;

    private final Timer connectTimer;

    private TwitterQueryEngine queryEngine;
    private TwitterStorageEngine storageEngine;
    private TwitterMetadataEngine metadataEngine;

    //https://apps.twitter.com/app/8096283/show
    private static final String CONSUMER_KEY = "consumer_key";
    private static final String CONSUMER_SECRET = "consumer_secret";
    private static final String ACCESS_TOKEN = "access_token";
    private static final String ACCESS_TOKEN_SECRET = "access_token_secret";

    private final Map<String, TwitterCluster> clusters = new HashMap<>();
    private final Map<String, ColumnType> allowedColumns = new HashMap<>();

    /**
     * Class constructor.
     *
     * @param connectorApp parent connector app.
     */
    public TwitterConnector(IConnectorApp connectorApp) {
        super(connectorApp);
        connectTimer = new Timer();
        String timerName = name(TwitterConnector.class, "connect");
        registerMetric(timerName, connectTimer);
        allowedColumns.put("Contributors", ColumnType.valueOf("NATIVE"));
        allowedColumns.put("CreatedAt", ColumnType.valueOf("NATIVE"));
        allowedColumns.put("CurrentUserRetweetId", ColumnType.valueOf("BIGINT"));
        allowedColumns.put("FavoriteCount", ColumnType.valueOf("INT"));
        allowedColumns.put("GeoLocationLongitude", ColumnType.valueOf("DOUBLE"));
        allowedColumns.put("GeoLocationLatitude", ColumnType.valueOf("DOUBLE"));
        allowedColumns.put("Id", ColumnType.valueOf("BIGINT"));
        allowedColumns.put("InReplyToScreenName", ColumnType.valueOf("TEXT"));
        allowedColumns.put("InReplyToStatusId", ColumnType.valueOf("BIGINT"));
        allowedColumns.put("InReplyToUserId", ColumnType.valueOf("BIGINT"));
        allowedColumns.put("Lang", ColumnType.valueOf("TEXT"));
        allowedColumns.put("PlaceCountry", ColumnType.valueOf("TEXT"));
        allowedColumns.put("PlaceStreet", ColumnType.valueOf("TEXT"));
        allowedColumns.put("RetweetCount", ColumnType.valueOf("INT"));
        allowedColumns.put("RetweetedStatus", ColumnType.valueOf("NATIVE"));
        allowedColumns.put("Scopes", ColumnType.valueOf("NATIVE"));
        allowedColumns.put("Source", ColumnType.valueOf("TEXT"));
        allowedColumns.put("Text", ColumnType.valueOf("TEXT"));
        allowedColumns.put("UserFollowers", ColumnType.valueOf("INT"));
        allowedColumns.put("UserLang", ColumnType.valueOf("TEXT"));
        allowedColumns.put("UserLocation", ColumnType.valueOf("TEXT"));
        allowedColumns.put("UserName", ColumnType.valueOf("TEXT"));
        allowedColumns.put("Favorited", ColumnType.valueOf("BOOLEAN"));
        allowedColumns.put("PossiblySensitive", ColumnType.valueOf("BOOLEAN"));
        allowedColumns.put("Retweet", ColumnType.valueOf("BOOLEAN"));
        allowedColumns.put("Retweeted", ColumnType.valueOf("BOOLEAN"));
        allowedColumns.put("RetweetedByMe", ColumnType.valueOf("BOOLEAN"));
        allowedColumns.put("Truncated", ColumnType.valueOf("BOOLEAN"));

    }



    @Override public String getConnectorManifestPath() {
        return getClass().getResource("/TwitterConnector.xml").getPath();
    }

    @Override public String[] getDatastoreManifestPath() {
        String datastore[]=new String[1];
        datastore[0]= getClass().getResource("/TwitterDataStore.xml").getPath();
        return datastore;
    }

    public Map<String, ColumnType> getAllowedColumns() {
        return allowedColumns;
    }

    public void addTableMetadata(String targetCluster, TableMetadata tableMetadata) {
        clusters.get(targetCluster).addTableMetadata(tableMetadata);
    }

    public TableMetadata getTableMetadata(String clusterName, String tableName) {
        return clusters.get(clusterName).getTableMetadata(tableName);
{'fraction_non_alphanumeric': 0.1109514687100894, 'fraction_numerical': 0.0038314176245210726, 'mean_word_length': 4.744612563044475, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5456562', 'n_tokens_mistral': 3350, 'n_tokens_neox': 3295, 'n_words': 699}