Class: FindingAidPDF

Inherits:
Object
  • Object
show all
Defined in:
public/app/models/finding_aid_pdf.rb

Constant Summary collapse

DEPTH_1_LEVELS =
['collection', 'recordgrp', 'series']
DEPTH_2_LEVELS =
['subgrp', 'subseries', 'subfonds']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_id, resource_id, archivesspace_client, base_url) ⇒ FindingAidPDF

Returns a new instance of FindingAidPDF.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'public/app/models/finding_aid_pdf.rb', line 10

def initialize(repo_id, resource_id, archivesspace_client, base_url)
  @repo_id = repo_id
  @resource_id = resource_id
  @archivesspace = archivesspace_client
  @base_url = base_url

  @resource = archivesspace.get_record("/repositories/#{repo_id}/resources/#{resource_id}")
  @ordered_records = archivesspace.get_record("/repositories/#{repo_id}/resources/#{resource_id}/ordered_records")

  # make sure finding aid title isn't only like /^\n$/
  if @resource.finding_aid['title'] and @resource.finding_aid['title'] =~ /\w/
    @short_title = @resource.finding_aid['title'].lstrip.split("\n")[0].strip
  end
end

Instance Attribute Details

#archivesspaceObject (readonly)

Returns the value of attribute archivesspace



8
9
10
# File 'public/app/models/finding_aid_pdf.rb', line 8

def archivesspace
  @archivesspace
end

#base_urlObject (readonly)

Returns the value of attribute base_url



8
9
10
# File 'public/app/models/finding_aid_pdf.rb', line 8

def base_url
  @base_url
end

#repo_codeObject (readonly)

Returns the value of attribute repo_code



8
9
10
# File 'public/app/models/finding_aid_pdf.rb', line 8

def repo_code
  @repo_code
end

#repo_idObject (readonly)

Returns the value of attribute repo_id



8
9
10
# File 'public/app/models/finding_aid_pdf.rb', line 8

def repo_id
  @repo_id
end

#resource_idObject (readonly)

Returns the value of attribute resource_id



8
9
10
# File 'public/app/models/finding_aid_pdf.rb', line 8

def resource_id
  @resource_id
end

Instance Method Details

#generateObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'public/app/models/finding_aid_pdf.rb', line 94

def generate
  out_html = source_file

  pdf_file = Tempfile.new
  pdf_file.close

  renderer = org.xhtmlrenderer.pdf.ITextRenderer.new
  renderer.set_document(java.io.File.new(out_html.path))

  # FIXME: We'll need to test this with a reverse proxy in front of it.
  renderer.shared_context.base_url = base_url

  renderer.layout

  pdf_output_stream = java.io.FileOutputStream.new(pdf_file.path)
  renderer.create_pdf(pdf_output_stream)
  pdf_output_stream.close

  out_html.unlink

  pdf_file
end

#short_titleObject



33
34
35
# File 'public/app/models/finding_aid_pdf.rb', line 33

def short_title
  @short_title || suggested_filename
end

#source_fileObject



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'public/app/models/finding_aid_pdf.rb', line 37

def source_file
  # We'll use the original controller so we can find and render the PDF
  # partials, but just for its ERB rendering.
  renderer = PdfController.new
  start_time = Time.now

  @repo_code = @resource.repository_information.fetch('top').fetch('repo_code')

  # .length == 1 would be just the resource itself.
  has_children = @ordered_records.entries.length > 1

  out_html = Tempfile.new

  # Use a NokogiriPushParser-based
  writer = Nokogiri::XML::SAX::PushParser.new(XMLCleaner.new(out_html))
  writer.write(renderer.render_to_string partial: 'header', layout: false, :locals => {:record => @resource})

  writer.write(renderer.render_to_string partial: 'titlepage', layout: false, :locals => {:record => @resource})

  # Drop the resource and filter the AOs
  toc_aos = @ordered_records.entries.drop(1).select {|entry|
    if entry.depth == 1
      DEPTH_1_LEVELS.include?(entry.level)
    elsif entry.depth == 2
      DEPTH_2_LEVELS.include?(entry.level)
    else
      false
    end
  }

  writer.write(renderer.render_to_string partial: 'toc', layout: false, :locals => {:resource => @resource, :has_children => has_children, :ordered_aos => toc_aos})

  writer.write(renderer.render_to_string partial: 'resource', layout: false, :locals => {:record => @resource, :has_children => has_children})

  page_size = 50

  @ordered_records.entries.drop(1).each_slice(page_size) do |entry_set|
    if AppConfig[:pui_pdf_timeout] && AppConfig[:pui_pdf_timeout] > 0 && (Time.now.to_i - start_time.to_i) >= AppConfig[:pui_pdf_timeout]
      raise TimeoutError.new("PDF generation timed out.  Sorry!")
    end

    uri_set = entry_set.map(&:uri)
    record_set = archivesspace.search_records(uri_set, {}, true).records

    record_set.zip(entry_set).each do |record, entry|
      next unless record.is_a?(ArchivalObject)

      writer.write(renderer.render_to_string partial: 'archival_object', layout: false, :locals => {:record => record, :level => entry.depth})
    end
  end

  writer.write(renderer.render_to_string partial: 'footer', layout: false, :locals => {:record => @resource})
  out_html.close

  out_html
end

#suggested_filenameObject



25
26
27
28
29
30
31
# File 'public/app/models/finding_aid_pdf.rb', line 25

def suggested_filename
  # Use the EAD ID.  If that's missing, use the 4-part identifier
  filename = (@resource.ead_id || @resource.four_part_identifier.reject(&:blank?).join('_'))

  # no spaces, please.
  filename.gsub(' ', '_') + '.pdf'
end