<commit_before># frozen_string_literal: true

#
# <Description>
#
class VectorUpload
  include ActiveModel::Model
  include ActiveModel::AttributeMethods

  attr_accessor :type, :file, :attributes, :mapped_attributes, :geojson, :title, :file_name

  #
  # <Description>
  #
  # @param [<Type>] attributes <description>
  # @option attributes [<Type>] :<key> <description>
  # @option attributes [<Type>] :<key> <description>
  # @option attributes [<Type>] :<key> <description>
  #
  def initialize(attributes = {})
    super

    raise(
      VectorUploadException, "File must be an instance of ActionDispatch::Http::UploadedFile. You pased a `#{file.class}`"
    ) unless file.instance_of?(ActionDispatch::Http::UploadedFile)

    accepted_types = %w(zip json geojson blob xlsx csv)
    set_type

    raise(VectorUploadException, "File must be of type #{accepted_types.join(', ')}") unless accepted_types.include?(type)

    unzip if type == 'zip'

    set_file_name if title.present?
  end

  #
  # <Description>
  #
  # @return [<Type>] <description>
  #
  def set_type
    file_ext = file.original_filename.split('.').last
    self.type = file_ext
  end

  #
  # <Description>
  #
  # @return [<Type>] <description>
  #
  def set_attributes
    case type
    when 'zip'
      attributes_from_shape
    when 'xlsx', 'csv'
      attributes_from_spreadsheet
    else
      attributes_from_json
    end
  end

  #
  # <Description>
  #
  # @return [<Type>] <description>
  #
  def amend_attributes
    case type
    when 'xlsx', 'csv'
      return build_geojson
    else
      geojson = type == 'zip' ? to_geojson : file
      json = JSON.parse(File.read(geojson))

      if mapped_attributes['break'].present?
        break_property = mapped_attributes.delete('break')
        json['breakProperty'] = validate_value(break_property)
      end

      json['features'].each do |feature|
        # original_properties = feature['properties']
        # feature['properties'] = { originalProperties: {} }

        # original_properties.each do |key, _value|
        #   feature['properties'][:originalProperties][key] = validate_value(original_properties[key])
        # end

        mapped_attributes.each do |key, value|
          feature['properties'][key] = validate_value(feature['properties'][value])
        end
      end
      return json
    end
  end

  #
  # <Description>
  #
  # @return [<Type>] <description>
  #
  def make_shapefile
    geojson_file = File.join('/data/tmp', "#{file_name}.json")
    FileUtils.mkdir_p(File.join('/data/tmp', file_name))
    new_shapefile = File.join('/data/tmp', file_name, "#{file_name}.shp")
    File.open(geojson_file, 'w') do |f|
      f.write(geojson)
      f.close
      system("ogr2ogr -f 'ESRI SHAPEFILE' #{new_shapefile} '#{f.path}'")
    end
  end

  #
  # <Description>
  #
  # @param [<Type>] options <description>
  #
  # @return [<Type>] <description>
  #
  def make_vector_layer(options)
    geojson, title, description = options.values_at(:geojson, :title, :description)
    geojson = JSON.parse(geojson) if geojson.is_a?(String)
    layer = VectorLayer.create!(
      tmp_geojson: geojson,
      name: SecureRandom.uuid,
      title: title,
      description: description,
      institution: Institution.second,
      data_format: 'geojson',
      active: true,
      default_break_property: geojson['breakProperty']
    )
    layer.save!
    return layer
  end

  private

  def unzip
    destination = file.to_path.gsub('.zip', '')
    FileUtils.mkdir_p(destination)
    Zip::File.open(file) do |zip_file|
      zip_file.each do |entry|
        file_path = File.join(destination, entry.name)
        begin<commit_msg>Set break property and value in geojson<commit_after># frozen_string_literal: true

#
# <Description>
#
class VectorUpload
  include ActiveModel::Model
  include ActiveModel::AttributeMethods

  attr_accessor :type, :file, :attributes, :mapped_attributes, :geojson, :title, :file_name

  #
  # <Description>
  #
  # @param [<Type>] attributes <description>
  # @option attributes [<Type>] :<key> <description>
  # @option attributes [<Type>] :<key> <description>
  # @option attributes [<Type>] :<key> <description>
  #
  def initialize(attributes = {})
    super

    raise(
      VectorUploadException, "File must be an instance of ActionDispatch::Http::UploadedFile. You pased a `#{file.class}`"
    ) unless file.instance_of?(ActionDispatch::Http::UploadedFile)

    accepted_types = %w(zip json geojson blob xlsx csv)
    set_type

    raise(VectorUploadException, "File must be of type #{accepted_types.join(', ')}") unless accepted_types.include?(type)

    unzip if type == 'zip'

    set_file_name if title.present?
  end

  #
  # <Description>
  #
  # @return [<Type>] <description>
  #
  def set_type
    file_ext = file.original_filename.split('.').last
    self.type = file_ext
  end

  #
  # <Description>
  #
  # @return [<Type>] <description>
  #
  def set_attributes
    case type
    when 'zip'
      attributes_from_shape
    when 'xlsx', 'csv'
      attributes_from_spreadsheet
    else
      attributes_from_json
    end
  end

  #
  # <Description>
  #
  # @return [<Type>] <description>
  #
  def amend_attributes
    case type
    when 'xlsx', 'csv'
      return build_geojson
    else
      geojson = type == 'zip' ? to_geojson : file
      json = JSON.parse(File.read(geojson))

      if mapped_attributes['break'].present?
        break_property = mapped_attributes.delete('break')
        json['breakProperty'] = validate_value(break_property)
      end

      json['features'].each do |feature|
        # original_properties = feature['properties']
        # feature['properties'] = { originalProperties: {} }

        # original_properties.each do |key, _value|
        #   feature['properties'][:originalProperties][key] = validate_value(original_properties[key])
        # end

        mapped_attributes.each do |key, value|
          feature['properties'][key] = validate_value(feature['properties'][value])
        end
      end
      return json
    end
  end

  #
  # <Description>
  #
  # @return [<Type>] <description>
  #
  def make_shapefile
    geojson_file = File.join('/data/tmp', "#{file_name}.json")
    FileUtils.mkdir_p(File.join('/data/tmp', file_name))
    new_shapefile = File.join('/data/tmp', file_name, "#{file_name}.shp")
    File.open(geojson_file, 'w') do |f|
      f.write(geojson)
      f.close
      system("ogr2ogr -f 'ESRI SHAPEFILE' #{new_shapefile} '#{f.path}'")
    end
  end

  #
  # <Description>
  #
  # @param [<Type>] options <description>
  #
  # @return [<Type>] <description>
  #
  def make_vector_layer(options)
    geojson, title, description = options.values_at(:geojson, :title, :description)
    geojson = JSON.parse(geojson) if geojson.is_a?(String)
    layer = VectorLayer.create!(
      tmp_geojson: geojson,
      name: SecureRandom.uuid,
      title: title,
      description: description,
      institution: Institution.second,
      data_format: 'geojson',
      active: true,
      default_break_property: geojson['breakProperty']
    )
    if geojson['breakProperty']
      layer.tmp_geojson['features'].each do |feature|
        feature['properties']['breakProperty'] = geojson['breakProperty']
        feature['properties']['breakValue'] = feature['properties'][geojson['breakProperty']]
      end
    end
    layer.save!
    return layer
  end

  private

  def unzip
    destination = file.to_path.gsub('.zip', '')
    FileUtils.mkdir_p(destination)
    Zip::File.open(file) do |zip_file|
      zip_file.each do |entry|
        file_path = File.join(destination, entry.name)
        begin
{'fraction_non_alphanumeric': 0.13136589187777487, 'fraction_numerical': 0.00026116479498563595, 'mean_word_length': 3.240863787375415, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3799392', 'n_tokens_mistral': 2511, 'n_tokens_neox': 2406, 'n_words': 520}