<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

        case @resource.resource_type.resource_type_ui
          when "Spreadsheet"
            @type = dm::ResourceTypeGeneral::DATASET
          when "MultipleTypes"
            @type = dm::ResourceTypeGeneral::COLLECTION
          when "Image"
            @type = dm::ResourceTypeGeneral::IMAGE
          when "Sound"
            @type = dm::ResourceTypeGeneral::SOUND
          when "Video"
            @type = dm::ResourceTypeGeneral::AUDIOVISUAL
          when "Text"
            @type = dm::ResourceTypeGeneral::TEXT
          when "Software"
            @type = dm::ResourceTypeGeneral::SOFTWARE
          else
            @type = dm::ResourceTypeGeneral::OTHER
        end
        # # 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: "#{@resource.resource_type.resource_type}" ),

          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}"
            )
          ]
        )

        datacite_to_wrapper = resource.save_to_xml
        datacite_root = resource.write_xml<commit_msg>Fixing datacite.xml generation to be a little more succinct and also fixing resourcetype issue.<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}"
            )
          ]
        )

        datacite_to_wrapper = resource.save_to_xml
        datacite_root = resource.write_xml
{'fraction_non_alphanumeric': 0.11313868613138686, 'fraction_numerical': 0.0015206812652068127, 'mean_word_length': 2.467053241960991, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 9}, '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': '2274934', 'n_tokens_mistral': 1907, 'n_tokens_neox': 1831, 'n_words': 310}