Class: PrintToPDFRunner

Inherits:
JobRunner show all
Includes:
JSONModel
Defined in:
backend/app/lib/job_runners/print_to_pdf_runner.rb

Constant Summary

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Instance Method Summary collapse

Methods included from JSONModel

JSONModel, #JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, check_valid_refs, client_mode?, custom_validations, destroy_model, enum_default_value, enum_values, handle_error, init, load_schema, #models, models, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_publish_flags!, set_repository, strict_mode, strict_mode?, validate_schema, with_repository

Methods inherited from JobRunner

#add_success_hook, #cancelation_signaler, #canceled?, for, #initialize, #parse_job_params_string, register_for_job_type, registered_job_types, registered_runner_for, #success!, #symbol_keys

Constructor Details

This class inherits a constructor from JobRunner

Instance Method Details

#runObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'backend/app/lib/job_runners/print_to_pdf_runner.rb', line 9

def run
  begin
    RequestContext.open( :repo_id => @job.repo_id) do
      parsed = JSONModel.parse_reference(@json.job["source"])
      resource = Resource.get_or_die(parsed[:id])
      resource_jsonmodel = Resource.to_jsonmodel(resource)

      @job.write_output("Generating PDF for #{resource_jsonmodel["title"]}  ")

      obj = URIResolver.resolve_references(resource_jsonmodel,
                                           [ "repository", "linked_agents", "subjects", "digital_objects", 'top_container', 'top_container::container_profile'])
      opts = {
        :include_unpublished => @json.job["include_unpublished"] || false,
        :include_daos => true,
        :use_numbered_c_tags => false
      }

      if obj["repository"]["_resolved"]["image_url"]
        image_for_pdf = obj["repository"]["_resolved"]["image_url"]
      else
        image_for_pdf = nil
      end

      record = JSONModel(:resource).new(obj)

      if record['publish'] === false
        @job.write_output("-" * 50)
        @job.write_output("Warning: this resource has not been published")
        @job.write_output("-" * 50)
      end

      ead = ASpaceExport.model(:ead).from_resource(record, resource.tree(:all, mode = :sparse), opts)
      xml = ""
      ASpaceExport.stream(ead).each { |x| xml << x }
      pdf = ASFop.new(xml, image_for_pdf).to_pdf
      job_file = @job.add_file( pdf )
      @job.write_output("File generated at #{job_file.full_file_path.inspect} ")

      # pdf will be either a Tempfile or File object, depending on whether it was created externally.
      if pdf.class == Tempfile
        pdf.unlink
      elsif pdf.class == File
        File.unlink(pdf.path)
      end

      @job.record_modified_uris( [@json.job["source"]] )
      @job.write_output("All done. Please click refresh to view your download link.")
      self.success!
      job_file
    end
  rescue Exception => e
    @job.write_output(e.message)
    @job.write_output(e.backtrace)
    raise e
  end
end