Class: Cul::Fedora::Arm::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/cul/fedora/arm/builder.rb

Overview

This class is for building ARM resource models in Fedora

Authors

James Stuart (james.stuart at columbia.edu), Benjamin Armintor (mailto: ba2213 at columbia.edu)

License
Dependencies

activesupport

Constant Summary collapse

DEFAULT_TEMPLATE_HEADER =

list of columns for a template with no header row

[:sequence, :target, :model_type, :source, :template_type, :dc_format, :id, :pid, :action, :license]
REQUIRED_COLUMNS =

columns that must have values

[:sequence]
MANDATORY_COLUMNS =

columns that must be included in a template

[:sequence, :target, :model_type]
VALID_COLUMNS =

list of columns which may have values

[:sequence, :target, :model_type, :source, :template_type, :dc_format, :title_attr, :mime, :id, :pid, :action, :license]
FOXML_BUILDER =
FoxmlBuilder.new()

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Builder

[:user]

designates a fedora user

[:pwd]

designates a fedora user credential



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cul/fedora/arm/builder.rb', line 44

def initialize(*args)
  options = args.extract_options!

  @parts = []
  @connector = options.delete(:connector)
  @namespace = options.delete(:ns)
  
  if (template = options.delete(:template)) || (file = options.delete(:file))
    template ||= File.open(file,"r")

    header = options.delete(:header) 
    header = true if header.nil?

    raise ArgumentError, "arguments should only include one of the following: :template, :file" unless options.empty?
    
    parse_template(template, header)
  else
    
    raise ArgumentError, "arguments should only include one of the following: :template, :file" unless options.empty?
  end
  
end

Instance Attribute Details

#connectorObject (readonly)

array of individual hash: each hash corresponds to a metadata or resource.



31
32
33
# File 'lib/cul/fedora/arm/builder.rb', line 31

def connector
  @connector
end

#partsObject (readonly)

array of individual hash: each hash corresponds to a metadata or resource.



31
32
33
# File 'lib/cul/fedora/arm/builder.rb', line 31

def parts
  @parts
end

Instance Method Details

#add_part(*args) ⇒ Object

adds one part to the parts array arguments are a hash array of column name (underscored symbols) to values



69
70
71
72
73
74
75
76
77
78
# File 'lib/cul/fedora/arm/builder.rb', line 69

def add_part(*args)
  value_hash = args.extract_options!
  
  test_for_invalid_columns(value_hash.keys)
  test_for_required_columns(value_hash)
  
  raise "Sequence ID already taken" if part_by_sequence(value_hash[:sequence])
  
  @parts << value_hash
end

#insert_aggregator(value_hash) ⇒ Object



113
114
115
116
117
118
# File 'lib/cul/fedora/arm/builder.rb', line 113

def insert_aggregator(value_hash)
  data = FOXML_BUILDER.build(value_hash)
  task = Tasks::InsertFoxmlTask.new(data)
  task.post(@connector)
  task.response
end

#insert_metadata(value_hash) ⇒ Object



119
120
121
122
123
124
# File 'lib/cul/fedora/arm/builder.rb', line 119

def (value_hash)
  data = FOXML_BUILDER.build(value_hash)
  task = Tasks::InsertFoxmlTask.new(data)
  task.post(@connector)
  task.response
end

#insert_resource(value_hash) ⇒ Object



125
126
127
128
129
130
# File 'lib/cul/fedora/arm/builder.rb', line 125

def insert_resource(value_hash)
  data = FOXML_BUILDER.build(value_hash)
  task = Tasks::InsertFoxmlTask.new(data)
  task.post(@connector)
  task.response
end

#part_by_pid(pid) ⇒ Object

looks for a part by :pid key



86
87
88
# File 'lib/cul/fedora/arm/builder.rb', line 86

def part_by_pid(pid)
  @parts.detect { |p| p[:pid] == pid}
end

#part_by_sequence(sequence_id) ⇒ Object

looks for a part by :sequence key note: if loading from a template, sequence will not be an integer, but rather a string



82
83
84
# File 'lib/cul/fedora/arm/builder.rb', line 82

def part_by_sequence(sequence_id)
  @parts.detect { |p| p[:sequence] == sequence_id}
end

#process(value_hash) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/cul/fedora/arm/builder.rb', line 104

def process(value_hash)
  # part is a hash
  op = value_hash[:action] + "_" + value_hash[:model_type]
  op.downcase!
  op = op.intern
  raise "Unknown operation #{op}" unless method(op)
  return method(op).call(value_hash) 
end

#process_partsObject



97
98
99
100
101
102
# File 'lib/cul/fedora/arm/builder.rb', line 97

def process_parts()
  reserve_pids(parts)
  parts.each { |part_hash|
    process(part_hash)
  }
end

#purge(pid) ⇒ Object



90
91
92
93
94
95
# File 'lib/cul/fedora/arm/builder.rb', line 90

def purge(pid)
  if(@connector)
    purge = Tasks::PurgeTask.new(pid)
    purge.post(@connector)
  end
end

#reserve_pids(parts = @parts) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/cul/fedora/arm/builder.rb', line 146

def reserve_pids(parts=@parts)
  assigned = []
  if (parts.nil?)
    return assigned
  end
  missing = 0
  # count missing pids
  parts.each { |part|
    test_for_required_columns(part)
    if ( part[:action].strip().eql?('insert'))
      if ( !part.has_key?(:pid) or part[:pid].strip().empty?)
        missing += 1
      end
    end
  }
  task = Tasks::ReservePidsTask.new(missing,@namespace)
  task.post(@connector)
  pids = (missing == 1)? [task.response.pid]:task.response.pid
  # assign new pids
  parts.each { |part|
    if ( part[:action].strip().eql?('insert'))
      if ( !part.has_key?(:pid) or part[:pid].strip().empty?)
        part[:pid] = pids.delete_at(0)
        assigned.push(part[:pid])
      end
    end
  }
  # make substitutions in target values
  parts.each { |part|
    if(part.has_key?(:target))
      target = part[:target]
      targets = target.split(';')
      targets.collect! { |t|
        if (t =~ /^\d+$/)
          t = part_by_sequence(t)[:pid]
        end
        t
      }
      part[:target] = targets.join(';')
    end
  }
  assigned
end

#update_aggregator(value_hash) ⇒ Object



132
133
134
135
# File 'lib/cul/fedora/arm/builder.rb', line 132

def update_aggregator(value_hash)
  self.purge(value_hash[:pid])
  insert_aggregator(value_hash)
end

#update_metadata(value_hash) ⇒ Object



136
137
138
139
# File 'lib/cul/fedora/arm/builder.rb', line 136

def (value_hash)
  self.purge(value_hash[:pid])
  (value_hash)
end

#update_resource(value_hash) ⇒ Object



140
141
142
143
# File 'lib/cul/fedora/arm/builder.rb', line 140

def update_resource(value_hash)
  self.purge(value_hash[:pid])
  insert_resource(value_hash)
end