-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.ArchivesSpace.Record_Buf.rb
220 lines (203 loc) · 8.62 KB
/
class.ArchivesSpace.Record_Buf.rb
1
2
3
4
5
6
7
8
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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
=begin
Abbreviations, AO = archival object(Everything's an AO, but there's also uri "archive_objects". It's confusing...)
AS = ArchivesSpace
IT = instance type
TC = top container
SC = Sub-container
_H = Hash
_A = Array
_I = Index(of Array)
_O = Object
_0R = Zero Relative
=end
class Record_Buf < Buffer_Base
def initialize( p1_aspace_O )
super( )
@cant_change_A << K.uri
@http_calls_O = p1_aspace_O.http_calls_O
end
attr_reader :record_H, :http_calls_O
# Shouldn't be an attr_writer :record_H here.
def jsonmodel_filter__what_to_keep( jsonmodel_type )
if ( jsonmodel_type == nil or jsonmodel_type == '' )
then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "jsonmodel_type is nil or ''"
raise
end
jsonmodel_H = Record_Format.new( jsonmodel_type ).record_H
return nil if ( jsonmodel_H == {} )
#
# Additional stuff to keep:
h = { K.child_count => '',
K.parent_id => '',
K.position => '',
K.ref => '',
K.ref_id => '',
K.tree => '',
K.uri => '',
K.lock_version => "", # Needed to update a record.
}
jsonmodel_H.merge!( h )
return jsonmodel_H
end
def what_to_throw_away( )
h = { K.created_by => '',
K.last_modified_by => '',
K.create_time => '',
K.system_mtime => '',
K.user_mtime => '',
}
return h
end
def filter_jsonmodel( jsonmodel_H )
if ( not jsonmodel_H.is_a?( Hash )) then
SE.puts "#{SE.lineno}: =============================================="
SE.ap "jsonmodel_H is a #{jsonmodel_H.class} not a hash:", jsonmodel_H
raise
end
if ( jsonmodel_H.has_key?( K.jsonmodel_type )) then
jsonmodel_filter__what_to_keep = jsonmodel_filter__what_to_keep( jsonmodel_H[ K.jsonmodel_type ])
end
h = {}
jsonmodel_H.sort.to_h.each_pair do | k, v |
# puts "k = #{k}, v = #{v}, v.class = #{v.class}"
if ( v.is_a?( Array )) then
a = v.map do | e |
if ( e.is_a?( Hash ) and e.has_key?( K.jsonmodel_type )) then
filter_jsonmodel( e )
else
e
end
end
h.merge!( { k => a } )
elsif ( v.is_a?( Hash ) ) then
h.merge!( { k => filter_jsonmodel( v ) } )
else
if ( jsonmodel_filter__what_to_keep ) then
h.merge!( { k => v } ) if ( jsonmodel_filter__what_to_keep.has_key?( k ))
else
h.merge!( { k => v } ) if ( not what_to_throw_away.has_key?( k ))
end
end
end
return h
end
def load( external_record_H, filter_record_B = false)
# SE.puts "#{SE.lineno}"
# SE.ap "external_record_H:", external_record_H
# SE.ap "filter_jsonmodel_template_H:", filter_jsonmodel_template_H
if (!( external_record_H[K.jsonmodel_type] and external_record_H[K.jsonmodel_type] == @rec_jsonmodel_type)) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "I was expecting a #{@rec_jsonmodel_type} jsonmodel_type record."
SE.puts "@uri = #{@uri}"
SE.ap "external_record_H:", external_record_H
raise
end
if ( @uri == nil or @num == nil ) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "Was expecting the @uri and @num variables to be set"
SE.puts "@uri = #{@uri}"
SE.puts "@num = #{@num}"
SE.ap "external_record_H:", external_record_H
raise
end
if ( ! ( external_record_H.has_key?( K.jsonmodel_type ) ) )
SE.puts "#{SE.lineno}: =============================================="
SE.puts "Was expecting a jsonmodel_type in external_record_H"
SE.puts "@uri = #{@uri}"
SE.ap "external_record_H:", external_record_H
raise
end
load_result_H = Hash.new
if ( filter_record_B ) then
# SE.ap "Before:", external_record_H
load_result_H.merge!( filter_jsonmodel( external_record_H ))
# SE.ap "After:", external_record_H
else
load_result_H.merge!( external_record_H )
end
# SE.ap "external_record_H:", external_record_H
return load_result_H
end
def read( filter_record_B = false )
http_response_body_H = @http_calls_O.get( @uri )
# SE.puts "#{SE.lineno}"
# SE.ap "http_response_body_H:", http_response_body_H
# SE.ap "filter_jsonmodel_template_H:", filter_jsonmodel_template_H
if (!( http_response_body_H[K.jsonmodel_type] and http_response_body_H[K.jsonmodel_type] == @rec_jsonmodel_type)) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "I was expecting a #{@rec_jsonmodel_type} jsonmodel_type record."
SE.puts "@uri = #{@uri}"
SE.ap "http_response_body_H:", http_response_body_H
raise
end
if ( filter_record_B ) then
# SE.ap "Before:", http_response_body_H
http_response_body_H = filter_jsonmodel( http_response_body_H )
# SE.ap "After:", http_response_body_H
end
# SE.ap "http_response_body_H:", http_response_body_H
return http_response_body_H
end
def store
# SE.ap @record_H
if (!( @record_H[K.jsonmodel_type] and @record_H[K.jsonmodel_type] == @rec_jsonmodel_type)) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "I was expecting a #{@rec_jsonmodel_type} jsonmodel_type record."
SE.puts "@uri = #{@uri}"
SE.ap "@record_H:", @record_H
raise
end
if (@record_H == nil or ( @record_H.has_key?( K.uri) and @record_H[ K.uri ] != @uri )) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "Current @record_H[ K.uri ] != @uri"
SE.puts "@uri = #{@uri}"
SE.ap "Current @record_H:", @record_H
raise
end
if ( @http_calls_O.aspace_O.allow_updates ) then
http_response_body_H = @http_calls_O.post_with_body( @uri, @record_H )
if ( ! http_response_body_H[K.status].in?( [ 'Created', 'Updated' ] ) ) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "Store failed!"
SE.puts "@uri = #{@uri}"
SE.puts "@record_H:", @record_H
SE.ap "http_response_body_H:", http_response_body_H
raise
end
else
http_response_body_H = {K.uri => "NO UPDATE MODE"}
end
return http_response_body_H
end
def delete
if (!( @record_H[K.jsonmodel_type] and @record_H[K.jsonmodel_type] == @rec_jsonmodel_type)) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "I was expecting a #{@rec_jsonmodel_type} jsonmodel_type record."
SE.puts "@uri = #{@uri}"
SE.ap "@record_H:", @record_H
raise
end
if (@record_H == nil or not ( @record_H.has_key?( K.uri) and @record_H[ K.uri ] == @uri )) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "Current @record_H[ K.uri ] != @uri"
SE.puts "@uri = #{@uri}"
SE.ap "Current @record_H:", @record_H
raise
end
if ( @http_calls_O.aspace_O.allow_updates ) then
http_response_body_H = @http_calls_O.delete( @uri, { } )
if ( http_response_body_H[K.status] != 'Deleted' ) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "Delete failed!"
SE.puts "@uri = #{@uri}"
SE.ap "http_response_body_H:", http_response_body_H
raise
end
else
http_response_body_H = {K.uri => "NO UPDATE MODE"}
end
return http_response_body_H
end
end