Class: PushmiPullyu::AIP::FileListCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/pushmi_pullyu/aip/file_list_creator.rb

Defined Under Namespace

Classes: FirstProxyHasPrev, ListSourceFileSetMismatch, NoFirstProxyFound, NoProxyURIFound

Constant Summary collapse

IANA =
'http://www.iana.org/assignments/relation/'.freeze
PREDICATES =
{
  proxy_for: RDF::URI('http://www.openarchives.org/ore/terms/proxyFor'),
  first: RDF::URI(IANA + 'first'),
  last: RDF::URI(IANA + 'last'),
  prev: RDF::URI(IANA + 'prev'),
  next: RDF::URI(IANA + 'next'),
  has_part: RDF::URI('http://purl.org/dc/terms/hasPart')
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(list_source_uri, output_xml_file, file_set_uuids) ⇒ FileListCreator

Returns a new instance of FileListCreator.


22
23
24
25
26
27
28
29
30
31
# File 'lib/pushmi_pullyu/aip/file_list_creator.rb', line 22

def initialize(list_source_uri, output_xml_file, file_set_uuids)
  @uri = RDF::URI(list_source_uri)
  @auth_uri = RDF::URI(list_source_uri)
  @auth_uri.user = PushmiPullyu.options[:fedora][:user]
  @auth_uri.password = PushmiPullyu.options[:fedora][:password]
  @output_file = output_xml_file

  # These are the known fileset uuids, used for validation
  @file_set_uuids = file_set_uuids
end

Instance Method Details

#extract_list_source_uuidsObject

Raises:

  • (ProxyCountIncorrect)

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pushmi_pullyu/aip/file_list_creator.rb', line 40

def extract_list_source_uuids
  # Note: raises IOError if can't find
  #       raises RDF::ReaderError if can't parse
  @graph = RDF::Graph.load(@auth_uri, validate: true)
  @list_source_uuids = []

  # Fetch first FileSet in list source
  this_proxy = find_first_proxy

  while @list_source_uuids.count <= num_proxies
    @list_source_uuids << uuid_from_proxy(this_proxy)
    next_proxy = find_next_proxy(this_proxy)

    break if next_proxy.nil?

    raise NextPreviousProxyMismatch if this_proxy != find_prev_proxy(next_proxy)

    this_proxy = next_proxy
  end

  raise ProxyCountIncorrect if @list_source_uuids.count != num_proxies
  raise LastProxyFailsValidation if this_proxy != find_last_proxy
end

#find_first_proxyObject

Raises:


75
76
77
78
79
80
81
82
83
84
# File 'lib/pushmi_pullyu/aip/file_list_creator.rb', line 75

def find_first_proxy
  @graph.query(subject: @uri, predicate: PREDICATES[:first]) do |statement|
    first_uri = statement.object
    # Validate that the first proxy doesn't have a previous one
    raise FirstProxyHasPrev, @uri.to_s if find_prev_proxy(first_uri)

    return first_uri
  end
  raise NoFirstProxyFound, @uri.to_s
end

#find_last_proxyObject

Raises:

  • (LastProxyFound)

86
87
88
89
90
91
92
93
94
95
# File 'lib/pushmi_pullyu/aip/file_list_creator.rb', line 86

def find_last_proxy
  @graph.query(subject: @uri, predicate: PREDICATES[:last]) do |statement|
    last_uri = statement.object
    # Validate that the last proxy doesn't have a next one
    raise LastProxyHasNext, @uri.to_s if find_next_proxy(last_uri)

    return last_uri
  end
  raise LastProxyFound, @uri.to_s
end

#find_next_proxy(proxy_uri) ⇒ Object


97
98
99
100
101
102
# File 'lib/pushmi_pullyu/aip/file_list_creator.rb', line 97

def find_next_proxy(proxy_uri)
  @graph.query(subject: proxy_uri, predicate: PREDICATES[:next]) do |statement|
    return statement.object
  end
  nil
end

#find_prev_proxy(proxy_uri) ⇒ Object


104
105
106
107
108
109
# File 'lib/pushmi_pullyu/aip/file_list_creator.rb', line 104

def find_prev_proxy(proxy_uri)
  @graph.query(subject: proxy_uri, predicate: PREDICATES[:prev]) do |statement|
    return statement.object
  end
  nil
end

#num_proxiesObject


64
65
66
# File 'lib/pushmi_pullyu/aip/file_list_creator.rb', line 64

def num_proxies
  @num_proxies ||= @graph.query(subject: @uri, predicate: PREDICATES[:has_part]).count
end

#runObject


33
34
35
36
37
38
# File 'lib/pushmi_pullyu/aip/file_list_creator.rb', line 33

def run
  extract_list_source_uuids
  raise ListSourceFileSetMismatch, @uri.to_s if @list_source_uuids.sort != @file_set_uuids.sort

  write_output_file
end

#uuid_from_proxy(proxy_uri) ⇒ Object

Raises:


68
69
70
71
72
73
# File 'lib/pushmi_pullyu/aip/file_list_creator.rb', line 68

def uuid_from_proxy(proxy_uri)
  @graph.query(subject: proxy_uri, predicate: PREDICATES[:proxy_for]) do |statement|
    return statement.object.to_s.split('/').last
  end
  raise NoProxyURIFound, proxy_uri.to_s
end

#write_output_fileObject


111
112
113
114
115
116
117
118
119
# File 'lib/pushmi_pullyu/aip/file_list_creator.rb', line 111

def write_output_file
  File.open(@output_file, 'w') do |file|
    file.write("<file_order>\n")
    @list_source_uuids.each do |uuid|
      file.write("  <uuid>#{uuid}</uuid>\n")
    end
    file.write("</file_order>\n")
  end
end