ddr-batch 2.0.0.alpha.1 → 2.0.0.alpha.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f604d559b09d360d0f9357113a314fbbe33d5777
4
- data.tar.gz: 4ed2ac035ff250220a8b7b07e6766f338a9da348
3
+ metadata.gz: 824038c27b5269a2816f0d253891c345a69e42c6
4
+ data.tar.gz: 06b2646d38c6975037358d695ddd83d8a6231ad4
5
5
  SHA512:
6
- metadata.gz: 8a495a43eb4b92a0753a1ac80ca16a08a40693f91de7928ff7a7de7c81dc6b8733c38063526f07cb027bc744adfca8bafd47b7bc9afc6dac33490859e56c51d6
7
- data.tar.gz: 3c3abce0902a76666701f79c80f65fd7ef5d6b019cce4927a462a7dbb398b4c6218088584f5a82d3fc5aef4631485c8b542b5c5faf0612fddc634ed4e9fac0e4
6
+ metadata.gz: 293bf55a24e1fc20b7139af536965a1bff028e23c27033fca4b879c633c381faa39ef7b81d1c1888cb55d3811aaec7fec27f183ae7c0db7ddd43267e30386078
7
+ data.tar.gz: 66cbc236d73feb005e5c276b8072450462e121c69e81ebdf0fc46e36cef4e734bb28f8199b892cd220d75a769d1c245546db07354ee6f7413f2cf94a0c6ec4ec
@@ -31,8 +31,11 @@ module Ddr::Batch
31
31
  return pids
32
32
  end
33
33
 
34
+ def error_prefix
35
+ I18n.t('ddr.batch.errors.prefix', :identifier => identifier, :id => id)
36
+ end
37
+
34
38
  def validate
35
- @error_prefix = I18n.t('ddr.batch.errors.prefix', :identifier => identifier, :id => id)
36
39
  errors = []
37
40
  errors += validate_model if model
38
41
  errors += validate_datastreams if batch_object_datastreams
@@ -66,7 +69,7 @@ module Ddr::Batch
66
69
  begin
67
70
  model.constantize
68
71
  rescue NameError
69
- errs << "#{@error_prefix} Invalid model name: #{model}"
72
+ errs << "#{error_prefix} Invalid model name: #{model}"
70
73
  end
71
74
  return errs
72
75
  end
@@ -76,23 +79,23 @@ module Ddr::Batch
76
79
  batch_object_datastreams.each do |d|
77
80
  if model_datastream_keys.present?
78
81
  unless model_datastream_keys.include?(d.name.to_sym)
79
- errs << "#{@error_prefix} Invalid datastream name for #{model}: #{d.name}"
82
+ errs << "#{error_prefix} Invalid datastream name for #{model}: #{d.name}"
80
83
  end
81
84
  end
82
85
  unless BatchObjectDatastream::PAYLOAD_TYPES.include?(d.payload_type)
83
- errs << "#{@error_prefix} Invalid payload type for #{d.name} datastream: #{d.payload_type}"
86
+ errs << "#{error_prefix} Invalid payload type for #{d.name} datastream: #{d.payload_type}"
84
87
  end
85
88
  if d.payload_type.eql?(BatchObjectDatastream::PAYLOAD_TYPE_FILENAME)
86
89
  unless File.readable?(d.payload)
87
- errs << "#{@error_prefix} Missing or unreadable file for #{d[:name]} datastream: #{d[:payload]}"
90
+ errs << "#{error_prefix} Missing or unreadable file for #{d[:name]} datastream: #{d[:payload]}"
88
91
  end
89
92
  end
90
93
  if d.checksum && !d.checksum_type
91
- errs << "#{@error_prefix} Must specify checksum type if providing checksum for #{d.name} datastream"
94
+ errs << "#{error_prefix} Must specify checksum type if providing checksum for #{d.name} datastream"
92
95
  end
93
96
  if d.checksum_type
94
97
  unless Ddr::Datastreams::CHECKSUM_TYPES.include?(d.checksum_type)
95
- errs << "#{@error_prefix} Invalid checksum type for #{d.name} datastream: #{d.checksum_type}"
98
+ errs << "#{error_prefix} Invalid checksum type for #{d.name} datastream: #{d.checksum_type}"
96
99
  end
97
100
  end
98
101
  end
@@ -102,44 +105,8 @@ module Ddr::Batch
102
105
  def validate_relationships
103
106
  errs = []
104
107
  batch_object_relationships.each do |r|
105
- obj_model = nil
106
- unless BatchObjectRelationship::OBJECT_TYPES.include?(r[:object_type])
107
- errs << "#{@error_prefix} Invalid object_type for #{r[:name]} relationship: #{r[:object_type]}"
108
- end
109
- if r[:object_type].eql?(BatchObjectRelationship::OBJECT_TYPE_PID)
110
- if batch.present? && batch.found_pids.keys.include?(r[:object])
111
- obj_model = batch.found_pids[r[:object]]
112
- else
113
- begin
114
- obj = ActiveFedora::Base.find(r[:object], :cast => true)
115
- obj_model = obj.class.name
116
- if batch.present?
117
- batch.add_found_pid(obj.pid, obj_model)
118
- end
119
- rescue ActiveFedora::ObjectNotFoundError
120
- pid_in_batch = false
121
- if batch.present?
122
- if batch.pre_assigned_pids.include?(r[:object])
123
- pid_in_batch = true
124
- end
125
- end
126
- unless pid_in_batch
127
- errs << "#{@error_prefix} #{r[:name]} relationship object does not exist: #{r[:object]}"
128
- end
129
- end
130
- end
131
- if obj_model
132
- relationship_reflection = Ddr::Utils.relationship_object_reflection(model, r[:name])
133
- if relationship_reflection
134
- klass = Ddr::Utils.reflection_object_class(relationship_reflection)
135
- if klass
136
- errs << "#{@error_prefix} #{r[:name]} relationship object #{r[:object]} exists but is not a(n) #{klass}" unless batch.found_pids[r[:object]].eql?(klass.name)
137
- end
138
- else
139
- errs << "#{@error_prefix} #{model} does not define a(n) #{r[:name]} relationship"
140
- end
141
- end
142
- end
108
+ r.valid?
109
+ errs += r.errors.messages.values.map { |msg| "#{error_prefix} msg" }
143
110
  end
144
111
  return errs
145
112
  end
@@ -147,8 +114,8 @@ module Ddr::Batch
147
114
  def verify_repository_object
148
115
  verifications = {}
149
116
  begin
150
- repo_object = ActiveFedora::Base.find(pid, :cast => true)
151
- rescue ActiveFedora::ObjectNotFound
117
+ repo_object = ActiveFedora::Base.find(pid)
118
+ rescue ActiveFedora::ObjectNotFoundError
152
119
  verifications["Object exists in repository"] = VERIFICATION_FAIL
153
120
  else
154
121
  verifications["Object exists in repository"] = VERIFICATION_PASS
@@ -221,11 +188,23 @@ module Ddr::Batch
221
188
  end
222
189
 
223
190
  def verify_relationship(repo_object, relationship)
191
+ # if PID, proceed as below
192
+ # if AR rec ID,
193
+ # retrieve AR rec
194
+ # if AR rec has PID, proceed as below using AR rec PID
195
+ # if not, error (should not occur)
196
+ repo_object_id = case
197
+ when relationship.object_rec_id?
198
+ referent = batch.batch_objects.find(relationship.object)
199
+ referent.pid
200
+ when relationship.object_repo_id?
201
+ relationship.object
202
+ end
224
203
  relationship_reflection = Ddr::Utils.relationship_object_reflection(model, relationship.name)
225
204
  relationship_object_class = Ddr::Utils.reflection_object_class(relationship_reflection)
226
205
  relationship_object = repo_object.send(relationship.name)
227
206
  if !relationship_object.nil? &&
228
- relationship_object.pid.eql?(relationship.object) &&
207
+ relationship_object.pid.eql?(repo_object_id) &&
229
208
  relationship_object.is_a?(relationship_object_class)
230
209
  VERIFICATION_PASS
231
210
  else
@@ -275,11 +254,19 @@ module Ddr::Batch
275
254
  end
276
255
 
277
256
  def add_relationship(repo_object, relationship)
278
- relationship_object = case relationship[:object_type]
279
- when BatchObjectRelationship::OBJECT_TYPE_PID
280
- ActiveFedora::Base.find(relationship[:object], :cast => true)
257
+ repo_object_id = case
258
+ when relationship.object_rec_id?
259
+ referent = batch.batch_objects.find(relationship[:object])
260
+ referent.pid
261
+ when relationship.object_repo_id?
262
+ relationship[:object]
263
+ end
264
+ if repo_object_id.present?
265
+ relationship_object = ActiveFedora::Base.find(repo_object_id)
266
+ repo_object.send("#{relationship[:name]}=", relationship_object)
267
+ else
268
+ raise Ddr::Batch::Error, "Unable to determine repository ID for relationship #{relationship.id}"
281
269
  end
282
- repo_object.send("#{relationship[:name]}=", relationship_object)
283
270
  return repo_object
284
271
  end
285
272
 
@@ -1,7 +1,6 @@
1
1
  module Ddr::Batch
2
-
3
2
  class BatchObjectRelationship < ActiveRecord::Base
4
- # attr_accessible :name, :object, :object_type, :operation, :batch_object
3
+
5
4
  belongs_to :batch_object, :inverse_of => :batch_object_relationships
6
5
 
7
6
  RELATIONSHIP_ADMIN_POLICY = "admin_policy"
@@ -15,12 +14,85 @@ module Ddr::Batch
15
14
  RELATIONSHIP_COMPONENT, RELATIONSHIP_ATTACHED_TO ]
16
15
 
17
16
  OPERATION_ADD = "ADD"
18
- OPERATION_UPDATE = "UPDATE"
19
17
  OPERATION_DELETE = "DELETE"
18
+ OPERATION_UPDATE = "UPDATE"
20
19
 
21
- OBJECT_TYPE_PID = "PID"
20
+ OPERATIONS = [ OPERATION_ADD, OPERATION_DELETE, OPERATION_UPDATE ]
22
21
 
23
- OBJECT_TYPES = [ OBJECT_TYPE_PID ]
24
- end
22
+ OBJECT_TYPE_REC_ID = "REC_ID"
23
+ OBJECT_TYPE_REPO_ID = "REPO_ID"
24
+
25
+ OBJECT_TYPES = [ OBJECT_TYPE_REC_ID, OBJECT_TYPE_REPO_ID ]
26
+
27
+ validates_presence_of :object, :batch_object
28
+
29
+ validates_inclusion_of :name, in: RELATIONSHIPS, message: 'Invalid relationship name'
30
+ validates_inclusion_of :operation, in: OPERATIONS, message: 'Invalid relationship operation'
31
+ validates_inclusion_of :object_type, in: OBJECT_TYPES, message: 'Invalid relationship object type'
32
+ validate :record_must_be_in_batch, if: :object_rec_id?
33
+ validate :repo_object_must_exist, if: :object_repo_id?
34
+ validate :relationship_name_must_be_valid_for_model
35
+
36
+ delegate :batch, to: :batch_object
37
+ delegate :batch_objects, to: :batch
38
+ delegate :found_pids, to: :batch
39
+ delegate :add_found_pid, to: :batch
25
40
 
41
+ def object_rec_id?
42
+ object_type == OBJECT_TYPE_REC_ID
43
+ end
44
+
45
+ def object_repo_id?
46
+ object_type == OBJECT_TYPE_REPO_ID
47
+ end
48
+
49
+ def record_must_be_in_batch
50
+ batch_objects.find(object).present?
51
+ rescue ActiveRecord::RecordNotFound
52
+ errors.add(:object, "#{object} not found in this batch")
53
+ end
54
+
55
+ def repo_object_must_exist
56
+ unless found_pids.keys.include?(object)
57
+ obj = ActiveFedora::Base.find(object)
58
+ add_found_pid(obj.id, obj.class.name)
59
+ end
60
+ rescue ActiveFedora::ObjectNotFoundError
61
+ errors.add(:object, "#{object} not found in repository")
62
+ end
63
+
64
+ def relationship_name_must_be_valid_for_model
65
+ unless Ddr::Utils.relationship_object_reflection(batch_object.model, name).present?
66
+ errors.add(:name, "#{batch_object.model} does not define a(n) #{name} relationship")
67
+ end
68
+ end
69
+
70
+ def object_model_must_be_correct_for_relationship
71
+ if relationship_reflection = Ddr::Utils.relationship_object_reflection(batch_object.model, name)
72
+ klass = Ddr::Utils.reflection_object_class(relationship_reflection)
73
+ if klass.present?
74
+ unless object_model == klass.name
75
+ errors.add(:object, "#{name} relationship object #{object} exists but is not a(n) #{klass}")
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ private
82
+
83
+ def object_model
84
+ case object_type
85
+ when OBJECT_TYPE_REC_ID
86
+ batch_objects.find(object).model
87
+ when OBJECT_TYPE_REPO_ID
88
+ if found_pids.keys.include?(object)
89
+ found_pids[object]
90
+ else
91
+ obj = ActiveFedora::Base.find(object)
92
+ add_found_pid(obj.id, obj.class.name)
93
+ end
94
+ end
95
+ end
96
+
97
+ end
26
98
  end
@@ -0,0 +1,6 @@
1
+ module Ddr::Batch
2
+
3
+ # Base class for custom Ddr::Batch exceptions
4
+ class Error < StandardError; end
5
+
6
+ end
@@ -4,8 +4,7 @@ module Ddr::Batch
4
4
 
5
5
  def local_validations
6
6
  errors = []
7
- errors << "#{@error_prefix} Model required for INGEST operation" unless model
8
- errors += validate_pre_assigned_pid if pid
7
+ errors << "#{error_prefix} Model required for INGEST operation" unless model
9
8
  errors
10
9
  end
11
10
 
@@ -28,12 +27,6 @@ module Ddr::Batch
28
27
 
29
28
  private
30
29
 
31
- def validate_pre_assigned_pid
32
- errs = []
33
- errs << "#{@error_prefix} #{pid} already exists in repository" if ActiveFedora::Base.exists?(pid)
34
- return errs
35
- end
36
-
37
30
  def ingest(user, opts = {})
38
31
  repo_object = create_repository_object
39
32
  if !repo_object.nil? && !repo_object.new_record?
@@ -4,12 +4,12 @@ module Ddr::Batch
4
4
 
5
5
  def local_validations
6
6
  errs = []
7
- errs << "#{@error_prefix} PID required for UPDATE operation" unless pid
7
+ errs << "#{error_prefix} PID required for UPDATE operation" unless pid
8
8
  if pid
9
9
  if ActiveFedora::Base.exists?(pid)
10
- errs << "#{@error_prefix} #{batch.user.user_key} not permitted to edit #{pid}" unless batch.user.can?(:edit, ActiveFedora::Base.find(pid, :cast => true))
10
+ errs << "#{error_prefix} #{batch.user.user_key} not permitted to edit #{pid}" unless batch.user.can?(:edit, ActiveFedora::Base.find(pid, :cast => true))
11
11
  else
12
- errs << "#{@error_prefix} PID #{pid} not found in repository" unless ActiveFedora::Base.exists?(pid)
12
+ errs << "#{error_prefix} PID #{pid} not found in repository" unless ActiveFedora::Base.exists?(pid)
13
13
  end
14
14
  end
15
15
  errs
@@ -1,5 +1,5 @@
1
1
  module Ddr
2
2
  module Batch
3
- VERSION = "2.0.0.alpha.1"
3
+ VERSION = "2.0.0.alpha.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ddr-batch
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.alpha.1
4
+ version: 2.0.0.alpha.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Coble
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-10-28 00:00:00.000000000 Z
12
+ date: 2015-11-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -183,6 +183,7 @@ files:
183
183
  - app/models/ddr/batch/batch_object_attribute.rb
184
184
  - app/models/ddr/batch/batch_object_datastream.rb
185
185
  - app/models/ddr/batch/batch_object_relationship.rb
186
+ - app/models/ddr/batch/error.rb
186
187
  - app/models/ddr/batch/ingest_batch_object.rb
187
188
  - app/models/ddr/batch/update_batch_object.rb
188
189
  - app/scripts/ddr/batch/batch_processor.rb