<commit_before>require 'zip'
require 'datacite/mapping'
require 'stash/wrapper'
require 'tempfile'
require 'stash_ezid/client'
require 'fileutils'
require 'stash_datacite/dublin_core_builder'
require 'stash_datacite/data_one_manifest_builder'

module StashDatacite
  module Resource
    class ResourceFileGeneration
      def initialize(resource, current_tenant)
        @resource = resource
        @current_tenant = current_tenant
        @version = @resource.next_version
        ResourceFileGeneration.set_pub_year(@resource)
        @client = StashEzid::Client.new(@current_tenant.identifier_service.to_h)
      end

      def generate_identifier
        if @resource.identifier
          "#{@resource.identifier.identifier_type.downcase}:#{@resource.identifier.identifier}"
        else
          @client.mint_id
        end
      end

      def generate_xml(target_url, identifier)
        simple_id = identifier.split(':', 2)[1]

        dm = Datacite::Mapping
        st = Stash::Wrapper


        @type = @resource.resource_type.resource_type_mapping_obj

        # # Based on "Example for a simple dataset"
        # # http://schema.datacite.org/meta/kernel-3/example/datacite-example-dataset-v3.0.xml

        resource = dm::Resource.new(
          identifier: dm::Identifier.new(value: simple_id),

          creators: @resource.creators.map do |creator|
            dm::Creator.new(name: "#{creator.creator_full_name}")
          end,

          contributors: @resource.contributors.map do |contributor|
            dm::Contributor.new(name: "#{contributor.contributor_name}", type: dm::ContributorType::FUNDER)
          end,

          titles: [
              dm::Title.new(value: "#{@resource.titles.where(title_type: nil).first.title}")
          ],

          publisher: "#{@resource.publisher.publisher || 'unknown'}",

          publication_year: Time.now.year,

          subjects: @resource.subjects.map do |subject|
            dm::Subject.new(value: "#{subject.subject}")
          end,

          language: 'en',

          resource_type: dm::ResourceType.new(resource_type_general: @type, value: nil ),

          version: @version,

          descriptions: [
            dm::Description.new(
                type: dm::DescriptionType::ABSTRACT,
                value: "#{@resource.descriptions.where(description_type: :abstract).first.description}"
            ),
            dm::Description.new(
                type: dm::DescriptionType::METHODS,
                value: "#{@resource.descriptions.where(description_type: :methods).first.description}"
            ),
            dm::Description.new(
                type: dm::DescriptionType::OTHER,
                value: "#{@resource.descriptions.where(description_type: :other).first.description}"
            )
          ],

          geo_locations:
              @resource.geolocations.map do |geo|
                dm::GeoLocation.new(
                  place: geo.datacite_mapping_place,
                  point: geo.datacite_mapping_point,
                  box: geo.datacite_mapping_box)
              end
        )

        datacite_to_wrapper = resource.save_to_xml
        datacite_root = resource.write_xml
        @client.update_metadata(identifier, datacite_root, target_url) # add target as 3rd parameter

        # datacite_target = "#{@resource.id}_datacite.xml"
        # datacite_directory = "#{Rails.root}/uploads"
        # puts Dir.pwd
        #f = File.open("#{datacite_directory}/#{datacite_target}", 'w') { |f| f.write(datacite_root) }

        identifier = st::Identifier.new(
          type: st::IdentifierType::DOI,
          value: '10.14749/1407399498'
        )

        version = st::Version.new(
          number: @version,
          date:  Date.tomorrow,
          note: 'Sample wrapped Datacite document'
        )

        r = @resource.rights.try(:first)
        license = st::License.new(name: r.rights , uri: r.rights_uri) if r

        embargo = st::Embargo.new(
          type: st::EmbargoType::NONE,
          period: 'none',
          start_date: Date.tomorrow,
          end_date: Date.tomorrow,
        )

        uploads = uploads_list(@resource)
        files = uploads.map do |d|
          st::StashFile.new(
            pathname: "#{d[:name]}", size_bytes: d[:size], mime_type: "#{d[:type]}"
          )
        end
        inventory = st::Inventory.new(files: files)

        wrapper = st::StashWrapper.new(
          identifier: identifier,
          version: version,
          license: license,
          embargo: embargo,
          inventory: inventory,
          descriptive_elements: [datacite_to_wrapper]
        )

        stash_wrapper = wrapper.write_xml
        # stash_wrapper_target = "#{@resource.id}_stash_wrapper.xml"
        # stash_wrapper_directory = "#{Rails.root}/uploads"
        # puts Dir.pwd
        # f = File.open("#{stash_wrapper_directory}/#{stash_wrapper_target}", 'w') { |f| f.write(stash_wrapper) }
        return [datacite_root, stash_wrapper]
      end

      def generate_dublincore
        DublinCoreBuilder.new(resource: @resource, tenant: @current_tenant).build_xml_string
      end<commit_msg>Use datacite 3 for EZID<commit_after>require 'zip'
require 'datacite/mapping'
require 'stash/wrapper'
require 'tempfile'
require 'stash_ezid/client'
require 'fileutils'
require 'stash_datacite/dublin_core_builder'
require 'stash_datacite/data_one_manifest_builder'

module StashDatacite
  module Resource
    class ResourceFileGeneration
      def initialize(resource, current_tenant)
        @resource = resource
        @current_tenant = current_tenant
        @version = @resource.next_version
        ResourceFileGeneration.set_pub_year(@resource)
        @client = StashEzid::Client.new(@current_tenant.identifier_service.to_h)
      end

      def generate_identifier
        if @resource.identifier
          "#{@resource.identifier.identifier_type.downcase}:#{@resource.identifier.identifier}"
        else
          @client.mint_id
        end
      end

      def generate_xml(target_url, identifier)
        simple_id = identifier.split(':', 2)[1]

        dm = Datacite::Mapping
        st = Stash::Wrapper


        @type = @resource.resource_type.resource_type_mapping_obj

        # # Based on "Example for a simple dataset"
        # # http://schema.datacite.org/meta/kernel-3/example/datacite-example-dataset-v3.0.xml

        resource = dm::Resource.new(
          identifier: dm::Identifier.new(value: simple_id),

          creators: @resource.creators.map do |creator|
            dm::Creator.new(name: "#{creator.creator_full_name}")
          end,

          contributors: @resource.contributors.map do |contributor|
            dm::Contributor.new(name: "#{contributor.contributor_name}", type: dm::ContributorType::FUNDER)
          end,

          titles: [
              dm::Title.new(value: "#{@resource.titles.where(title_type: nil).first.title}")
          ],

          publisher: "#{@resource.publisher.publisher || 'unknown'}",

          publication_year: Time.now.year,

          subjects: @resource.subjects.map do |subject|
            dm::Subject.new(value: "#{subject.subject}")
          end,

          language: 'en',

          resource_type: dm::ResourceType.new(resource_type_general: @type, value: nil ),

          version: @version,

          descriptions: [
            dm::Description.new(
                type: dm::DescriptionType::ABSTRACT,
                value: "#{@resource.descriptions.where(description_type: :abstract).first.description}"
            ),
            dm::Description.new(
                type: dm::DescriptionType::METHODS,
                value: "#{@resource.descriptions.where(description_type: :methods).first.description}"
            ),
            dm::Description.new(
                type: dm::DescriptionType::OTHER,
                value: "#{@resource.descriptions.where(description_type: :other).first.description}"
            )
          ],

          geo_locations:
              @resource.geolocations.map do |geo|
                dm::GeoLocation.new(
                  place: geo.datacite_mapping_place,
                  point: geo.datacite_mapping_point,
                  box: geo.datacite_mapping_box)
              end
        )

        datacite3_xml = resource.write_xml(mapping: :datacite_3)
        @client.update_metadata(identifier, datacite3_xml, target_url)

        # datacite_target = "#{@resource.id}_datacite.xml"
        # datacite_directory = "#{Rails.root}/uploads"
        # puts Dir.pwd
        #f = File.open("#{datacite_directory}/#{datacite_target}", 'w') { |f| f.write(datacite_root) }

        identifier = st::Identifier.new(
          type: st::IdentifierType::DOI,
          value: '10.14749/1407399498'
        )

        version = st::Version.new(
          number: @version,
          date:  Date.tomorrow,
          note: 'Sample wrapped Datacite document'
        )

        r = @resource.rights.try(:first)
        license = r && st::License.new(name: r.rights , uri: r.rights_uri)

        embargo = st::Embargo.new(
          type: st::EmbargoType::NONE,
          period: 'none',
          start_date: Date.tomorrow,
          end_date: Date.tomorrow,
        )

        uploads = uploads_list(@resource)
        files = uploads.map do |d|
          st::StashFile.new(
            pathname: "#{d[:name]}", size_bytes: d[:size], mime_type: "#{d[:type]}"
          )
        end
        inventory = st::Inventory.new(files: files)

        datacite4_element = resource.save_to_xml
        wrapper = st::StashWrapper.new(
          identifier: identifier,
          version: version,
          license: license,
          embargo: embargo,
          inventory: inventory,
          descriptive_elements: [datacite4_element]
        )

        [resource.write_xml, wrapper.write_xml]
      end

      def generate_dublincore
        DublinCoreBuilder.new(resource: @resource, tenant: @current_tenant).build_xml_string
      end
{'fraction_non_alphanumeric': 0.11646024770275669, 'fraction_numerical': 0.005093887335197763, 'mean_word_length': 2.294833826916749, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, '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': '7319760', 'n_tokens_mistral': 3046, 'n_tokens_neox': 2915, 'n_words': 494}