<commit_before>/***********************************************************************
* Copyright (c) 2013-2016 Commonwealth Computer Research, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at
* http://www.opensource.org/licenses/apache2.0.php.
*************************************************************************/

package org.locationtech.geomesa.tools

import java.io.File

import com.beust.jcommander.ParameterException
import com.typesafe.config.{ConfigParseOptions, ConfigFactory}
import com.typesafe.scalalogging.LazyLogging
import org.apache.commons.io.FileUtils
import org.geotools.feature.simple.SimpleFeatureTypeBuilder
import org.locationtech.geomesa.utils.geotools.{SimpleFeatureTypeLoader, SimpleFeatureTypes}
import org.opengis.feature.simple.SimpleFeatureType

import scala.util.{Failure, Success, Try}

/**
 * Parsers SimpleFeatureType specification from a variety of arguments
 * including sft strings (e.g. name:String,age:Integer,*geom:Point)
 * and typesafe config.
 */
object SftArgParser extends LazyLogging {

  // Important to setAllowMissing to false bc else you'll get a config but it will be empty
  val parseOpts =
    ConfigParseOptions.defaults()
      .setAllowMissing(false)
      .setClassLoader(null)
      .setIncluder(null)
      .setOriginDescription(null)
      .setSyntax(null)
  /**
   * @throws ParameterException if the SFT cannot be parsed
   * @return the SFT parsed from the Args
   */
  @throws[ParameterException]
  def getSft(specArg: String, featureName: String = null): SimpleFeatureType =
    getLoadedSft(specArg, featureName)
        .orElse(parseSpecString(specArg, featureName))
        .orElse(parseSpecConf(specArg, featureName))
        .orElse(parseSpecStringFile(specArg, featureName))
        .orElse(parseSpecConfFile(specArg, featureName))
        .getOrElse {
          throw new ParameterException("Unable to parse Simple Feature type from sft config or string")
        }

  // gets an sft from simple feature type providers on the classpath
  private[SftArgParser] def getLoadedSft(specArg: String, name: String): Option[SimpleFeatureType] = {
    SimpleFeatureTypeLoader.sfts.find(_.getTypeName == specArg).map { sft =>
      if (name == null || name == sft.getTypeName) sft else renameSft(sft, name)
    }
  }

  // gets an sft based on a spec string
  private[SftArgParser] def parseSpecString(specArg: String, name: String): Option[SimpleFeatureType] =
    Option(name).flatMap { featureName =>
      Try(SimpleFeatureTypes.createType(featureName, specArg)) match {
        case Success(sft) => Some(sft)
        case Failure(e) =>
          logger.debug(s"Unable to parse sft spec from string $specArg with error ${e.getMessage}")
          None
      }
    }

  // gets an sft based on a spec string
  private[SftArgParser] def parseSpecStringFile(specArg: String, name: String): Option[SimpleFeatureType] =
    Option(specArg).map(new File(_)).flatMap { file =>
      Try(SimpleFeatureTypes.createType (name, FileUtils.readFileToString(file))) match {
        case Success(sft) => Some(sft)
        case Failure(e) =>
          logger.debug(s"Unable to parse sft spec from string $specArg with error ${e.getMessage}")
          None
      }
    }

  // gets an sft based on a spec conf string
  private[SftArgParser] def parseSpecConf(specArg: String, name: String): Option[SimpleFeatureType] = {
    Try(SimpleFeatureTypes.createType(ConfigFactory.parseString(specArg, parseOpts))) match {
      case Success(sft) if name == null || name == sft.getTypeName => Some(sft)
      case Success(sft) => Some(renameSft(sft, name))
      case Failure(e) =>
        logger.debug(s"Unable to parse sft spec from string $specArg as conf with error ${e.getMessage}")
        None
    }
  }

  // parse spec conf file
  private[SftArgParser] def parseSpecConfFile(specArg: String, name: String): Option[SimpleFeatureType] = {
    Try(SimpleFeatureTypes.createType(ConfigFactory.parseFile(new File(specArg)))) match {
      case Success(sft) if name == null || name == sft.getTypeName => Some(sft)
      case Success(sft) => Some(renameSft(sft, name))
      case Failure(e) =>
        logger.debug(s"Unable to parse sft spec from file $specArg as conf with error ${e.getMessage}")
        None
    }
  }

  private[SftArgParser] def renameSft(sft: SimpleFeatureType, name: String) = {
    val builder = new SimpleFeatureTypeBuilder()
    builder.init(sft)
    builder.setName(name)
    builder.buildFeatureType()
  }
}<commit_msg>GEOMESA-1005 Fixing renaming of simple feature type in tools ingest<commit_after>/***********************************************************************
* Copyright (c) 2013-2016 Commonwealth Computer Research, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at
* http://www.opensource.org/licenses/apache2.0.php.
*************************************************************************/

package org.locationtech.geomesa.tools

import java.io.File

import com.beust.jcommander.ParameterException
import com.typesafe.config.{ConfigParseOptions, ConfigFactory}
import com.typesafe.scalalogging.LazyLogging
import org.apache.commons.io.FileUtils
import org.geotools.feature.simple.SimpleFeatureTypeBuilder
import org.locationtech.geomesa.utils.geotools.{SimpleFeatureTypeLoader, SimpleFeatureTypes}
import org.opengis.feature.simple.SimpleFeatureType

import scala.util.{Failure, Success, Try}

/**
 * Parsers SimpleFeatureType specification from a variety of arguments
 * including sft strings (e.g. name:String,age:Integer,*geom:Point)
 * and typesafe config.
 */
object SftArgParser extends LazyLogging {

  // Important to setAllowMissing to false bc else you'll get a config but it will be empty
  val parseOpts =
    ConfigParseOptions.defaults()
      .setAllowMissing(false)
      .setClassLoader(null)
      .setIncluder(null)
      .setOriginDescription(null)
      .setSyntax(null)
  /**
   * @throws ParameterException if the SFT cannot be parsed
   * @return the SFT parsed from the Args
   */
  @throws[ParameterException]
  def getSft(specArg: String, featureName: String = null): SimpleFeatureType =
    getLoadedSft(specArg, featureName)
        .orElse(parseSpecString(specArg, featureName))
        .orElse(parseSpecConf(specArg, featureName))
        .orElse(parseSpecStringFile(specArg, featureName))
        .orElse(parseSpecConfFile(specArg, featureName))
        .getOrElse {
          throw new ParameterException("Unable to parse Simple Feature type from sft config or string")
        }

  // gets an sft from simple feature type providers on the classpath
  private[SftArgParser] def getLoadedSft(specArg: String, name: String): Option[SimpleFeatureType] = {
    SimpleFeatureTypeLoader.sfts.find(_.getTypeName == specArg).map { sft =>
      if (name == null || name == sft.getTypeName) sft else renameSft(sft, name)
    }
  }

  // gets an sft based on a spec string
  private[SftArgParser] def parseSpecString(specArg: String, name: String): Option[SimpleFeatureType] =
    Option(name).flatMap { featureName =>
      Try(SimpleFeatureTypes.createType(featureName, specArg)) match {
        case Success(sft) => Some(sft)
        case Failure(e) =>
          logger.debug(s"Unable to parse sft spec from string $specArg with error ${e.getMessage}")
          None
      }
    }

  // gets an sft based on a spec string
  private[SftArgParser] def parseSpecStringFile(specArg: String, name: String): Option[SimpleFeatureType] =
    Option(specArg).map(new File(_)).flatMap { file =>
      Try(SimpleFeatureTypes.createType (name, FileUtils.readFileToString(file))) match {
        case Success(sft) => Some(sft)
        case Failure(e) =>
          logger.debug(s"Unable to parse sft spec from string $specArg with error ${e.getMessage}")
          None
      }
    }

  // gets an sft based on a spec conf string
  private[SftArgParser] def parseSpecConf(specArg: String, name: String): Option[SimpleFeatureType] = {
    Try(SimpleFeatureTypes.createType(ConfigFactory.parseString(specArg, parseOpts))) match {
      case Success(sft) if name == null || name == sft.getTypeName => Some(sft)
      case Success(sft) => Some(renameSft(sft, name))
      case Failure(e) =>
        logger.debug(s"Unable to parse sft spec from string $specArg as conf with error ${e.getMessage}")
        None
    }
  }

  // parse spec conf file
  private[SftArgParser] def parseSpecConfFile(specArg: String, name: String): Option[SimpleFeatureType] = {
    Try(SimpleFeatureTypes.createType(ConfigFactory.parseFile(new File(specArg)))) match {
      case Success(sft) if name == null || name == sft.getTypeName => Some(sft)
      case Success(sft) => Some(renameSft(sft, name))
      case Failure(e) =>
        logger.debug(s"Unable to parse sft spec from file $specArg as conf with error ${e.getMessage}")
        None
    }
  }

  private[SftArgParser] def renameSft(sft: SimpleFeatureType, name: String) = {
    val builder = new SimpleFeatureTypeBuilder()
    builder.init(sft)
    builder.setName(name)
    val renamed = builder.buildFeatureType()
    renamed.getUserData.putAll(sft.getUserData)
    renamed
  }
}
{'fraction_non_alphanumeric': 0.12839270568278202, 'fraction_numerical': 0.0029686174724342664, 'mean_word_length': 3.983095615425251, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '6101518', 'n_tokens_mistral': 2763, 'n_tokens_neox': 2710, 'n_words': 737}