<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.crossdata.sh.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.ListIterator;
import java.util.Map;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.xml.sax.SAXException;

import com.stratio.crossdata.common.manifest.CrossdataManifest;
import com.stratio.crossdata.common.data.Cell;
import com.stratio.crossdata.common.data.ResultSet;
import com.stratio.crossdata.common.data.Row;
import com.stratio.crossdata.common.exceptions.ManifestException;
import com.stratio.crossdata.common.metadata.ColumnMetadata;
import com.stratio.crossdata.common.result.CommandResult;
import com.stratio.crossdata.common.result.ConnectResult;
import com.stratio.crossdata.common.result.MetadataResult;
import com.stratio.crossdata.common.result.QueryResult;
import com.stratio.crossdata.common.result.StorageResult;
import com.stratio.crossdata.common.manifest.ConnectorFactory;
import com.stratio.crossdata.common.manifest.ConnectorType;
import com.stratio.crossdata.common.manifest.DataStoreFactory;
import com.stratio.crossdata.common.manifest.DataStoreType;
import com.stratio.crossdata.common.result.ErrorResult;
import com.stratio.crossdata.common.result.Result;

import jline.console.ConsoleReader;
import jline.console.history.History;
import jline.console.history.MemoryHistory;

/**
 * Utility class for console related operations.
 */
public final class ConsoleUtils {

    /**
     * Class logger.
     */
    private static final Logger LOG = Logger.getLogger(ConsoleUtils.class);

    /**
     * Number of days a history entry is maintained.
     */
    private static final int DAYS_HISTORY_ENTRY_VALID = 30;

    private static final String DATASTORE_SCHEMA_PATH = "/com/stratio/crossdata/connector/DataStoreDefinition.xsd";
    private static final String CONNECTOR_SCHEMA_PATH = "/com/stratio/crossdata/connector/ConnectorDefinition.xsd";

    /**
     * Private class constructor as all methods are static.
     */
    private ConsoleUtils() {
    }

    /**
     * Convert Result {@link com.stratio.crossdata.common.result.Result} structure to String.
     *
     * @param result {@link com.stratio.crossdata.common.result.Result} from execution.
     * @return String representing the result.
     */
    public static String stringResult(Result result) {
        if (ErrorResult.class.isInstance(result)) {
            return ErrorResult.class.cast(result).getErrorMessage();
        }
        if (result instanceof QueryResult) {
            QueryResult queryResult = (QueryResult) result;
            return stringQueryResult(queryResult);
        } else if (result instanceof CommandResult) {
            CommandResult commandResult = (CommandResult) result;
            return String.class.cast(commandResult.getResult());
        } else if (result instanceof ConnectResult) {
            ConnectResult connectResult = (ConnectResult) result;
            return String.valueOf("Connected with SessionId=" + connectResult.getSessionId());
        } else if (result instanceof MetadataResult) {
            MetadataResult metadataResult = (MetadataResult) result;
            return metadataResult.toString();
        } else if (result instanceof StorageResult) {
            StorageResult storageResult = (StorageResult) result;
            return storageResult.toString();
        } else {
            return "Unknown result";
        }
    }

    /**
     * Convert QueryResult {@link com.stratio.crossdata.common.result.QueryResult} structure to String.
     *
     * @param queryResult {@link com.stratio.crossdata.common.result.QueryResult} from execution.
     * @return String representing the result.
     */

    private static String stringQueryResult(QueryResult queryResult) {
        if (queryResult.getResultSet().isEmpty()) {
            return System.lineSeparator() + "OK";
        }

        ResultSet resultSet = queryResult.getResultSet();

        Map<String, Integer> colWidths = calculateColWidths(resultSet);

        String bar =
                StringUtils.repeat('-', getTotalWidth(colWidths) + (colWidths.values().size() * 3) + 1);

        StringBuilder sb = new StringBuilder(System.lineSeparator());
        sb.append("Partial result: ");
        sb.append(!queryResult.isLastResultSet());
        sb.append(System.lineSeparator());
        sb.append(bar).append(System.lineSeparator());
        sb.append("| ");
        for (ColumnMetadata columnMetadata: resultSet.getColumnMetadata()) {
            sb.append(
                    StringUtils.rightPad(columnMetadata.getName().getColumnNameToShow(),
                            colWidths.get(columnMetadata.getName().getColumnNameToShow()) + 1)).append("| ");
        }

        sb.append(System.lineSeparator());
        sb.append(bar);
        sb.append(System.lineSeparator());

        for (Row row : resultSet) {
            sb.append("| ");
            for (Map.Entry<String, Cell> entry : row.getCells().entrySet()) {
                String str = String.valueOf(entry.getValue().getValue());
                sb.append(StringUtils.rightPad(str, colWidths.get(entry.getKey())));
                sb.append(" | ");
            }
            sb.append(System.lineSeparator());
        }
        sb.append(bar).append(System.lineSeparator());
        return sb.toString();
    }

    /**
     * In order to print the result, this method calculates the maximum width of every column.
     *
     * @param resultSet structure representing the result of a execution.
     * @return Map<String, Integer> where the key is the name of the column and Integer is the maximum
     * width.
     */
    private static Map<String, Integer> calculateColWidths(ResultSet resultSet) {
        Map<String, Integer> colWidths = new HashMap<>();

        // Get column names or aliases width
        for (ColumnMetadata columnMetadata: resultSet.getColumnMetadata()) {
            colWidths.put(columnMetadata.getName().getName(),
                    columnMetadata.getName().getColumnNameToShow().length());
        }

        // Find widest cell content of every column
        for (Row row: resultSet) {
            for (String key: row.getCells().keySet()) {
                String cellContent = String.valueOf(row.getCell(key).getValue());
                int currentWidth = colWidths.get(key);
                if (cellContent.length() > currentWidth) {
                    colWidths.put(key, cellContent.length());
                }
            }
        }

        return colWidths;
    }
<commit_msg>Mechanism to avoid colWidths issue added<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.crossdata.sh.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.ListIterator;
import java.util.Map;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.xml.sax.SAXException;

import com.stratio.crossdata.common.data.Cell;
import com.stratio.crossdata.common.data.ResultSet;
import com.stratio.crossdata.common.data.Row;
import com.stratio.crossdata.common.exceptions.ManifestException;
import com.stratio.crossdata.common.manifest.ConnectorFactory;
import com.stratio.crossdata.common.manifest.ConnectorType;
import com.stratio.crossdata.common.manifest.CrossdataManifest;
import com.stratio.crossdata.common.manifest.DataStoreFactory;
import com.stratio.crossdata.common.manifest.DataStoreType;
import com.stratio.crossdata.common.metadata.ColumnMetadata;
import com.stratio.crossdata.common.result.CommandResult;
import com.stratio.crossdata.common.result.ConnectResult;
import com.stratio.crossdata.common.result.ErrorResult;
import com.stratio.crossdata.common.result.MetadataResult;
import com.stratio.crossdata.common.result.QueryResult;
import com.stratio.crossdata.common.result.Result;
import com.stratio.crossdata.common.result.StorageResult;

import jline.console.ConsoleReader;
import jline.console.history.History;
import jline.console.history.MemoryHistory;

/**
 * Utility class for console related operations.
 */
public final class ConsoleUtils {

    /**
     * Class logger.
     */
    private static final Logger LOG = Logger.getLogger(ConsoleUtils.class);

    /**
     * Number of days a history entry is maintained.
     */
    private static final int DAYS_HISTORY_ENTRY_VALID = 30;

    private static final String DATASTORE_SCHEMA_PATH = "/com/stratio/crossdata/connector/DataStoreDefinition.xsd";
    private static final String CONNECTOR_SCHEMA_PATH = "/com/stratio/crossdata/connector/ConnectorDefinition.xsd";

    /**
     * Private class constructor as all methods are static.
     */
    private ConsoleUtils() {
    }

    /**
     * Convert Result {@link com.stratio.crossdata.common.result.Result} structure to String.
     *
     * @param result {@link com.stratio.crossdata.common.result.Result} from execution.
     * @return String representing the result.
     */
    public static String stringResult(Result result) {
        if (ErrorResult.class.isInstance(result)) {
            return ErrorResult.class.cast(result).getErrorMessage();
        }
        if (result instanceof QueryResult) {
            QueryResult queryResult = (QueryResult) result;
            return stringQueryResult(queryResult);
        } else if (result instanceof CommandResult) {
            CommandResult commandResult = (CommandResult) result;
            return String.class.cast(commandResult.getResult());
        } else if (result instanceof ConnectResult) {
            ConnectResult connectResult = (ConnectResult) result;
            return String.valueOf("Connected with SessionId=" + connectResult.getSessionId());
        } else if (result instanceof MetadataResult) {
            MetadataResult metadataResult = (MetadataResult) result;
            return metadataResult.toString();
        } else if (result instanceof StorageResult) {
            StorageResult storageResult = (StorageResult) result;
            return storageResult.toString();
        } else {
            return "Unknown result";
        }
    }

    /**
     * Convert QueryResult {@link com.stratio.crossdata.common.result.QueryResult} structure to String.
     *
     * @param queryResult {@link com.stratio.crossdata.common.result.QueryResult} from execution.
     * @return String representing the result.
     */

    private static String stringQueryResult(QueryResult queryResult) {
        if (queryResult.getResultSet().isEmpty()) {
            return System.lineSeparator() + "OK";
        }

        ResultSet resultSet = queryResult.getResultSet();

        Map<String, Integer> colWidths = calculateColWidths(resultSet);

        String bar =
                StringUtils.repeat('-', getTotalWidth(colWidths) + (colWidths.values().size() * 3) + 1);

        StringBuilder sb = new StringBuilder(System.lineSeparator());
        sb.append("Partial result: ");
        sb.append(!queryResult.isLastResultSet());
        sb.append(System.lineSeparator());
        sb.append(bar).append(System.lineSeparator());
        sb.append("| ");
        for (ColumnMetadata columnMetadata: resultSet.getColumnMetadata()) {
            sb.append(
                    StringUtils.rightPad(columnMetadata.getName().getColumnNameToShow(),
                            colWidths.get(columnMetadata.getName().getColumnNameToShow()) + 1)).append("| ");
        }

        sb.append(System.lineSeparator());
        sb.append(bar);
        sb.append(System.lineSeparator());

        for (Row row : resultSet) {
            sb.append("| ");
            for (Map.Entry<String, Cell> entry : row.getCells().entrySet()) {
                String str = String.valueOf(entry.getValue().getValue());
                sb.append(StringUtils.rightPad(str, colWidths.get(entry.getKey())));
                sb.append(" | ");
            }
            sb.append(System.lineSeparator());
        }
        sb.append(bar).append(System.lineSeparator());
        return sb.toString();
    }

    /**
     * In order to print the result, this method calculates the maximum width of every column.
     *
     * @param resultSet structure representing the result of a execution.
     * @return Map<String, Integer> where the key is the name of the column and Integer is the maximum
     * width.
     */
    private static Map<String, Integer> calculateColWidths(ResultSet resultSet) {
        LinkedHashMap<String, Integer> colWidths = new LinkedHashMap<>();

        // Get column names or aliases width
        for (ColumnMetadata columnMetadata: resultSet.getColumnMetadata()) {
            colWidths.put(columnMetadata.getName().getColumnNameToShow(),
                    columnMetadata.getName().getColumnNameToShow().length());
        }

        // Find widest cell content of every column
        for (Row row: resultSet) {
            int pos = 0;
            for (String key: row.getCells().keySet()) {
                String cellContent = String.valueOf(row.getCell(key).getValue());

                int currentWidth;
                if(colWidths.containsValue(key)){
                    currentWidth = colWidths.get(key);
                } else {
                    Iterator<Map.Entry<String, Integer>> iter = colWidths.entrySet().iterator();
                    int limit = 0;
                    while(limit < pos){
                        iter.next();
                        limit++;
                    }
                    currentWidth = iter.next().getKey().length();
                }

                if (cellContent.length() > currentWidth) {
                    colWidths.put(key, cellContent.length());
                }

                pos++;
            }
        }

        return colWidths;
    }

{'fraction_non_alphanumeric': 0.10333891814265199, 'fraction_numerical': 0.0014360938247965534, 'mean_word_length': 2.9585504500236857, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 16}, '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': '7490467', 'n_tokens_mistral': 4446, 'n_tokens_neox': 4368, 'n_words': 1081}