<commit_before>require 'zip'
require 'datacite/mapping'
require 'stash/wrapper'
require 'tempfile'
require 'stash_ezid/client'
require 'fileutils'
require 'stash_datacite/datacite_xml_builder'
require 'stash_datacite/stash_wrapper_builder'
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.version_number
        @client = StashEzid::Client.new(@current_tenant.identifier_service.to_h)
        PublicationYear.ensure_pub_year(@resource) # TODO: call this only when needed
      end

      def identifier_str
        @identifier_str ||= @resource.identifier_str || @client.mint_id
      end

      def generate_xml(target_url)
        doi_value = identifier_str.split(':', 2)[1]
        uploads = uploads_list(@resource)

        datacite_xml_builder = Datacite::Mapping::DataciteXMLBuilder.new(
          doi_value: doi_value,
          se_resource: @resource,
          total_size_bytes: uploads.inject(0) { |sum, upload| sum + upload[:size] },
          version: @version
        )

        dc3_xml = datacite_xml_builder.build_datacite_xml(datacite_3: true)
        File.open("tmp/#{doi_value.gsub('/', '-')}-dc3.xml", 'w') { |f| f.write(dc3_xml) }
        @client.update_metadata(identifier_str, dc3_xml, target_url)

        dc4_resource = datacite_xml_builder.build_resource
        wrapper_xml = generate_stash_wrapper(dc4_resource, @version, uploads)

        [dc4_resource.write_xml, wrapper_xml]
      end

      def generate_stash_wrapper(dc4_resource, version_number, uploads)
        Stash::Wrapper::StashWrapperBuilder.new(
          dcs_resource: dc4_resource,
          version_number: version_number,
          uploads: uploads
        ).build_xml
      end

      def generate_dublincore
        DublinCoreBuilder.new(resource: @resource, tenant: @current_tenant).build_xml_string
      end

      def generate_dataone
        DataONEManifestBuilder.new(uploads_list(@resource)).build_dataone_manifest
      end

      def generate_merritt_zip(folder, target_url)
        target_url = target_url
        FileUtils.mkdir_p(folder)

        uploads = @resource.file_uploads.newly_created.map do |i|
          { name: i.upload_file_name, type: i.upload_content_type, size: i.upload_file_size }
        end
        purge_existing_files

        zipfile_name = "#{folder}/#{@resource.id}_archive.zip"
        datacite_xml, stashwrapper_xml = generate_xml(target_url)

        File.open("#{folder}/#{@resource.id}_mrt-datacite.xml", 'w') do |f|
          f.write datacite_xml
        end
        File.open("#{folder}/#{@resource.id}_stash-wrapper.xml", 'w') do |f|
          f.write stashwrapper_xml
        end
        File.open("#{folder}/#{@resource.id}_mrt-oaidc.xml", 'w') do |f|
          f.write(generate_dublincore)
        end
        File.open("#{folder}/#{@resource.id}_mrt-dataone-manifest.txt", 'w') do |f|
          f.write(generate_dataone)
        end
        del_fn = create_delete_file(folder)

        Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
          zipfile.add('mrt-datacite.xml', "#{folder}/#{@resource.id}_mrt-datacite.xml")
          zipfile.add('stash-wrapper.xml', "#{folder}/#{@resource.id}_stash-wrapper.xml")
          zipfile.add('mrt-oaidc.xml', "#{folder}/#{@resource.id}_mrt-oaidc.xml")
          zipfile.add('mrt-dataone-manifest.txt', "#{folder}/#{@resource.id}_mrt-dataone-manifest.txt")
          zipfile.add('mrt-delete.txt', del_fn) unless del_fn.nil?
          uploads.each do |d|
            zipfile.add((d[:name]).to_s, "#{folder}/#{@resource.id}/#{d[:name]}")<commit_msg>Write DC3 in mrt-datacite.xml but DC4 in stash-wrapper.xml<commit_after>require 'zip'
require 'datacite/mapping'
require 'stash/wrapper'
require 'tempfile'
require 'stash_ezid/client'
require 'fileutils'
require 'stash_datacite/datacite_xml_builder'
require 'stash_datacite/stash_wrapper_builder'
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.version_number
        @client = StashEzid::Client.new(@current_tenant.identifier_service.to_h)
        PublicationYear.ensure_pub_year(@resource) # TODO: call this only when needed
      end

      def identifier_str
        @identifier_str ||= @resource.identifier_str || @client.mint_id
      end

      def generate_xml(target_url)
        doi_value = identifier_str.split(':', 2)[1]
        uploads = uploads_list(@resource)

        datacite_xml_builder = Datacite::Mapping::DataciteXMLBuilder.new(
          doi_value: doi_value,
          se_resource: @resource,
          total_size_bytes: uploads.inject(0) { |sum, upload| sum + upload[:size] },
          version: @version
        )

        dc3_xml = datacite_xml_builder.build_datacite_xml(datacite_3: true)
        @client.update_metadata(identifier_str, dc3_xml, target_url)

        dc4_resource = datacite_xml_builder.build_resource
        wrapper_xml = generate_stash_wrapper(dc4_resource, @version, uploads)

        [dc3_xml, wrapper_xml]
      end

      def generate_stash_wrapper(dc4_resource, version_number, uploads)
        Stash::Wrapper::StashWrapperBuilder.new(
          dcs_resource: dc4_resource,
          version_number: version_number,
          uploads: uploads
        ).build_xml
      end

      def generate_dublincore
        DublinCoreBuilder.new(resource: @resource, tenant: @current_tenant).build_xml_string
      end

      def generate_dataone
        DataONEManifestBuilder.new(uploads_list(@resource)).build_dataone_manifest
      end

      def generate_merritt_zip(folder, target_url)
        target_url = target_url
        FileUtils.mkdir_p(folder)

        uploads = @resource.file_uploads.newly_created.map do |i|
          { name: i.upload_file_name, type: i.upload_content_type, size: i.upload_file_size }
        end
        purge_existing_files

        zipfile_name = "#{folder}/#{@resource.id}_archive.zip"
        dc3_xml, stashwrapper_xml = generate_xml(target_url)

        File.open("#{folder}/#{@resource.id}_mrt-datacite.xml", 'w') do |f|
          f.write dc3_xml
        end
        File.open("#{folder}/#{@resource.id}_stash-wrapper.xml", 'w') do |f|
          f.write stashwrapper_xml
        end
        File.open("#{folder}/#{@resource.id}_mrt-oaidc.xml", 'w') do |f|
          f.write(generate_dublincore)
        end
        File.open("#{folder}/#{@resource.id}_mrt-dataone-manifest.txt", 'w') do |f|
          f.write(generate_dataone)
        end
        del_fn = create_delete_file(folder)

        Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
          zipfile.add('mrt-datacite.xml', "#{folder}/#{@resource.id}_mrt-datacite.xml")
          zipfile.add('stash-wrapper.xml', "#{folder}/#{@resource.id}_stash-wrapper.xml")
          zipfile.add('mrt-oaidc.xml', "#{folder}/#{@resource.id}_mrt-oaidc.xml")
          zipfile.add('mrt-dataone-manifest.txt', "#{folder}/#{@resource.id}_mrt-dataone-manifest.txt")
          zipfile.add('mrt-delete.txt', del_fn) unless del_fn.nil?
          uploads.each do |d|
            zipfile.add((d[:name]).to_s, "#{folder}/#{@resource.id}/#{d[:name]}")
{'fraction_non_alphanumeric': 0.12203838517538054, 'fraction_numerical': 0.0037061548643282593, 'mean_word_length': 3.174585635359116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 58}, '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': '3434676', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2498, 'n_words': 377}