Class: Reactor::Cm::Obj

Inherits:
Object
  • Object
show all
Defined in:
lib/reactor/cm/obj.rb

Defined Under Namespace

Classes: ContentSetRequest, LinkAddRequest, LinkDeleteRequest, LinkSetRequest, ObjSetRequest, Request, ResolveRefsRequest, SimpleCommandRequest

Constant Summary collapse

OBJ_ATTRS =
%i(permalink objClass workflowName name suppressExport parent).freeze
PREDEFINED_ATTRS =
%i(blob body channels title).freeze
ATTR_LENGTH_CONSTRAINT =
{ name: 250, title: 250 }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#obj_idObject (readonly)

Returns the value of attribute obj_id.



6
7
8
# File 'lib/reactor/cm/obj.rb', line 6

def obj_id
  @obj_id
end

Class Method Details

.create(name, parent, objClass) ⇒ Object



13
14
15
16
17
# File 'lib/reactor/cm/obj.rb', line 13

def self.create(name, parent, objClass)
  obj = Obj.new(name)
  obj.send(:create, parent, objClass)
  obj
end

.delete_where(conditions) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/reactor/cm/obj.rb', line 40

def self.delete_where(conditions)
  request = XmlRequest.prepare do |xml|
    xml.tag!("obj-where") do
      conditions.each do |key, value|
        xml.tag!(key, value)
      end
    end
    xml.tag!("obj-delete")
  end
  request.execute!
end

.exists?(path_or_id) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
# File 'lib/reactor/cm/obj.rb', line 19

def self.exists?(path_or_id)
  obj = Obj.new
  begin
    obj.send(:load, path_or_id).ok?
  rescue StandardError
    false
  end
end

.extract_id(response) ⇒ Object



463
464
465
# File 'lib/reactor/cm/obj.rb', line 463

def self.extract_id(response)
  response.xpath("//obj/id").text
end

.get(path_or_id) ⇒ Object



34
35
36
37
38
# File 'lib/reactor/cm/obj.rb', line 34

def self.get(path_or_id)
  obj = Obj.new
  obj.send(:load, path_or_id)
  obj
end

.load(id) ⇒ Object



28
29
30
31
32
# File 'lib/reactor/cm/obj.rb', line 28

def self.load(id)
  obj = Obj.new
  obj.instance_variable_set("@obj_id", id)
  obj
end

Instance Method Details

#blob_ticket_id(content = "edited") ⇒ Object



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/reactor/cm/obj.rb', line 387

def blob_ticket_id(content = "edited")
  request = XmlRequest.prepare do |xml|
    xml.tag!("content-where") do
      xml.tag!("objectId", @obj_id)
      xml.tag!("state", content)
    end
    xml.tag!("content-get") do
      xml.tag!("blob")
    end
  end
  response = request.execute!
  possible_ticket_id = response.xpath("//blob").text
  encoding = response.xpath("//blob/@encoding").to_s
  # blob is smaller than
  # [systemConfig getKeys tuning.minStreamingDataLength]
  # and thus it is returned in-line
  raise Reactor::Cm::BlobTooSmallError if encoding != "stream"

  possible_ticket_id
end

#commit!(msg = nil) ⇒ Object



273
274
275
# File 'lib/reactor/cm/obj.rb', line 273

def commit!(msg = nil)
  simple_command("commit", msg)
end

#composite_save(attrs, links_to_add, links_to_remove, links_to_set, links_modified = false) {|attrs, links_to_add, links_to_remove, links_to_set| ... } ⇒ Object

Yields:

  • (attrs, links_to_add, links_to_remove, links_to_set)


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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/reactor/cm/obj.rb', line 150

def composite_save(attrs, links_to_add, links_to_remove, links_to_set, links_modified = false)
  set_multiple(attrs)

  skip_version_creation = @attrs.empty? && links_to_remove.empty? && links_to_set.empty? && !links_modified

  # The save procedure consists of two multiplexed CM requests.
  # Each request combines multiple operations for better performance.
  #
  # It is necessary to split the procedure into two request because
  # of the linklist handling. For objects with only released content
  # the edit operation copies all links (and generates new ids).
  # Only the new links from the edited content can be manipulated.
  # Thus it is neccessary after perform a read of links after the
  # edit operation.
  #
  # The second request may seem strange and redundant, but has very
  # important reasons for being structured the way it is.
  # The CM in all versions (as of this moment <= 7.0.1) contains
  # multiple race conditions regarding slave workers and the CRUL
  # interface.
  #
  # It is possible that the two requests are processed by two different
  # slave workers which have an invalid cache. Without careful
  # programing it may lead to unexpected results and/or data loss.
  # The reason behind it is simple: the cache invalidate requests
  # are distributed asynchronously between workers.
  #
  # It is impossible to ensure that the two requests are processed
  # by the same worker: a worker may get killed at any time, without
  # warning (producing errors on keep-alive connections). Furthermore
  # the workers regularly kill themselves (after X processed requests)
  # to contain memory leaks.
  #
  # Following cases are especially important to handle correctly:
  # Let Slave 1 be the worker receiving the first request and Slave 2
  # be the worker receiving the second request. Let Object be the
  # object on which the save operation is performed.
  #
  # 1. If the Object contains only released version, then Slave 2
  # may be unaware of the newly created edited version by the time
  # the second request is being processed. Thefore all operations
  # involving indirect content references for example adding links
  # (obj where ... editedContent addLinkTo) would get rejected
  # by the Slave 2.
  #
  # 2. If the Object contains an edited version, then Slave 2 may
  # be unaware of the changes perfomed on it, change of editor for
  # example, and can reject valid requests. The more dramatic
  # scenario involves Slave 2 persisting its invalid cached content
  # which would result in a data loss.
  #
  # The requests have been thus crafted to deal with those problems.
  # The solution is based on the precise source-code level knowledge
  # of the CM internals.
  resp = MultiXmlRequest.execute do |reqs|
    reqs.optional  { |xml| SimpleCommandRequest.build(xml, @obj_id, "take") } unless skip_version_creation
    reqs.optional  { |xml| SimpleCommandRequest.build(xml, @obj_id, "edit") } unless skip_version_creation

    unless @obj_attrs.empty?
      reqs.mandatory { |xml| ObjSetRequest.build(xml, @obj_id, @obj_attrs) }
    end # important! requires different permissions
    unless skip_version_creation
      reqs.mandatory { |xml| ContentSetRequest.build(xml, @obj_id, @attrs, @attr_options) }
    end
    reqs.mandatory { |xml| ResolveRefsRequest.build(xml, @obj_id) } unless skip_version_creation
  end

  resp.assert_success

  yield(attrs, links_to_add, links_to_remove, links_to_set) if block_given?

  unless skip_version_creation || (links_to_remove.empty? && links_to_add.empty? && links_to_set.empty?)
    resp = MultiXmlRequest.execute do |reqs|
      reqs.optional  { |xml| SimpleCommandRequest.build(xml, @obj_id, "take") }
      reqs.optional  { |xml| SimpleCommandRequest.build(xml, @obj_id, "edit") }

      links_to_remove.each do |link_id|
        reqs.mandatory { |xml| LinkDeleteRequest.build(xml, link_id) }
      end

      links_to_set.each do |(link_id, link)|
        reqs.mandatory { |xml| LinkSetRequest.build(xml, link_id, link) }
      end

      links_to_add.each do |(attr, link)|
        reqs.mandatory { |xml| LinkAddRequest.build(xml, @obj_id, attr, link) }
      end
    end
  end

  resp.assert_success
end

#copy(new_parent, recursive = false, new_name = nil) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/reactor/cm/obj.rb', line 294

def copy(new_parent, recursive = false, new_name = nil)
  request = XmlRequest.prepare do |xml|
    xml.tag!("obj-where") do
      xml.tag!("id", @obj_id)
    end
    xml.tag!("obj-copy") do
      xml.tag!("parent", new_parent)
      xml.tag!("name", new_name) if new_name
      xml.tag!("recursive", "1") if recursive
    end
  end
  response = request.execute!
  response.xpath("//obj/id").text
end

#delete!Object



309
310
311
# File 'lib/reactor/cm/obj.rb', line 309

def delete!
  simple_command("delete")
end

#edit!(msg = nil) ⇒ Object



261
262
263
# File 'lib/reactor/cm/obj.rb', line 261

def edit!(msg = nil)
  simple_command("edit", msg)
end

#edited?Boolean

Returns:

  • (Boolean)


341
342
343
344
345
346
347
348
# File 'lib/reactor/cm/obj.rb', line 341

def edited?
  request = XmlRequest.prepare do |xml|
    xml.where_key_tag!(base_name, "id", @obj_id)
    xml.get_key_tag!(base_name, "isEdited")
  end
  response = request.execute!
  response.xpath("//isEdited").text == "1"
end

#edited_contentObject



408
409
410
411
412
413
414
415
# File 'lib/reactor/cm/obj.rb', line 408

def edited_content
  request = XmlRequest.prepare do |xml|
    xml.where_key_tag!(base_name, "id", @obj_id)
    xml.get_key_tag!(base_name, "editedContent")
  end
  response = request.execute!
  response.xpath("//editedContent").text
end

#editorObject



375
376
377
378
379
380
381
382
383
384
385
# File 'lib/reactor/cm/obj.rb', line 375

def editor
  request = XmlRequest.prepare do |xml|
    xml.tag!("content-where") do
      xml.tag!("objectId", @obj_id)
      xml.tag!("state", "edited")
    end
    xml.get_key_tag!("content", "editor")
  end
  response = request.execute!
  response.xpath("//editor").text
end

#forward!(msg = nil) ⇒ Object



269
270
271
# File 'lib/reactor/cm/obj.rb', line 269

def forward!(msg = nil)
  simple_command("forward", msg)
end

#get(key) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/reactor/cm/obj.rb', line 60

def get(key)
  request = XmlRequest.prepare do |xml|
    xml.where_key_tag!(base_name, "id", @obj_id)
    xml.get_key_tag!(base_name, key)
  end
  response = request.execute!
  result = response.xpath("//#{key}")
  if result.children.map { |i| i.respond_to?(:name) && (i.name == "listitem") }.reduce(:&)
    result.children.map { |i| i.text.to_s }
  else
    result = result.text unless result.is_a? Array
    result
  end
end


123
124
125
# File 'lib/reactor/cm/obj.rb', line 123

def get_links(attr)
  get_link_ids(attr).map { |id| Link.get(id) }
end

#pathObject



332
333
334
335
336
337
338
339
# File 'lib/reactor/cm/obj.rb', line 332

def path
  request = XmlRequest.prepare do |xml|
    xml.where_key_tag!(base_name, "id", @obj_id)
    xml.get_key_tag!(base_name, "path")
  end
  response = request.execute!
  response.xpath("//obj/path").text
end

#permission_clear(permission) ⇒ Object



119
120
121
# File 'lib/reactor/cm/obj.rb', line 119

def permission_clear(permission)
  permission_set(permission, [])
end

#permission_grant(permission, groups) ⇒ Object



111
112
113
# File 'lib/reactor/cm/obj.rb', line 111

def permission_grant(permission, groups)
  permission_command("GrantTo", permission, groups)
end

#permission_granted_to(user, permission) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/reactor/cm/obj.rb', line 85

def permission_granted_to(user, permission)
  request = XmlRequest.prepare do |xml|
    xml.where_key_tag!(base_name, "id", @obj_id)
    xml.get_tag!(base_name) do
      xml.tag!("permissionGrantedTo", permission: permission, user: user)
    end
  end
  response = request.execute!
  response.xpath("//permissionGrantedTo/text()") == "1"
end

#permission_revoke(permission, groups) ⇒ Object



115
116
117
# File 'lib/reactor/cm/obj.rb', line 115

def permission_revoke(permission, groups)
  permission_command("RevokeFrom", permission, groups)
end

#permission_set(permission, groups) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/reactor/cm/obj.rb', line 96

def permission_set(permission, groups)
  request = XmlRequest.prepare do |xml|
    xml.where_key_tag!(base_name, :id, @obj_id)

    options = {
      permission: permission,
      type: :list
    }

    xml.set_key_tag!(base_name, :permission, groups, options)
  end

  request.execute!
end

#reasons_for_incomplete_stateObject



350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/reactor/cm/obj.rb', line 350

def reasons_for_incomplete_state
  request = XmlRequest.prepare do |xml|
    xml.tag!("content-where") do
      xml.tag!("objectId", @obj_id)
      xml.tag!("state", "edited")
    end
    xml.get_key_tag!("content", "reasonsForIncompleteState")
  end
  response = request.execute!
  result = response.xpath("//reasonsForIncompleteState/*")
  result.is_a?(Array) ? result.map(&:text).map(&:to_s) : [result.to_s]
end

#reject!(msg = nil) ⇒ Object



277
278
279
# File 'lib/reactor/cm/obj.rb', line 277

def reject!(msg = nil)
  simple_command("reject", msg)
end

#release!(msg = nil) ⇒ Object



253
254
255
# File 'lib/reactor/cm/obj.rb', line 253

def release!(msg = nil)
  simple_command("release", msg)
end

#remove_active_contents!Object



313
314
315
# File 'lib/reactor/cm/obj.rb', line 313

def remove_active_contents!
  simple_command("removeActiveContents")
end

#remove_archived_contents!Object



317
318
319
# File 'lib/reactor/cm/obj.rb', line 317

def remove_archived_contents!
  simple_command("removeArchivedContents")
end

#resolve_refs!Object



321
322
323
324
325
326
327
328
329
330
# File 'lib/reactor/cm/obj.rb', line 321

def resolve_refs!
  request = XmlRequest.prepare do |xml|
    xml.tag!("content-where") do
      xml.tag!("objectId", @obj_id)
      xml.tag!("state", "edited")
    end
    xml.tag!("content-resolveRefs")
  end
  request.execute!
end

#revert!(msg = nil) ⇒ Object



281
282
283
# File 'lib/reactor/cm/obj.rb', line 281

def revert!(msg = nil)
  simple_command("revert", msg)
end

#save!Object



243
244
245
246
247
248
249
250
251
# File 'lib/reactor/cm/obj.rb', line 243

def save!
  links_to_remove = @removed_links.map { |l| l.link_id }
  links_to_add = @links.map do |attr, links|
    links.map do |link|
      [attr, { destination_url: link.dest_url, title: link.title, target: link.target, position: link.position }]
    end.flatten
  end
  composite_save([], links_to_add, links_to_remove, [])
end

#set(key, value, options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/reactor/cm/obj.rb', line 75

def set(key, value, options = {})
  key = key.to_sym
  value = value[0, ATTR_LENGTH_CONSTRAINT[key]] if ATTR_LENGTH_CONSTRAINT[key] && value
  if OBJ_ATTRS.include?(key) then @obj_attrs[key] = value
  else
    @attrs[key] = value
  end
  @attr_options[key] = options
end


134
135
136
137
138
139
140
141
142
143
144
# File 'lib/reactor/cm/obj.rb', line 134

def set_link(attr, path)
  old_links = get_links(attr)
  if old_links.length == 1
    old_link = old_links.first
    old_link.dest_url = path
    @links[attr] = [old_link]
  else
    @removed_links = old_links
    @links[attr] = [Link.create_inside(self, attr, path)]
  end
end


127
128
129
130
131
132
# File 'lib/reactor/cm/obj.rb', line 127

def set_links(attr, new_links_as_hashes)
  get_links(attr).map(&:delete!)
  new_links_as_hashes.each do |link_hash|
    Link.create_inside(self, attr, link_hash[:destination_url], link_hash[:title], link_hash[:target])
  end
end

#set_multiple(attrs) ⇒ Object



146
147
148
# File 'lib/reactor/cm/obj.rb', line 146

def set_multiple(attrs)
  attrs.each { |a, (v, o)| set(a, v, o || {}) }
end

#sign!(msg = nil) ⇒ Object



285
286
287
# File 'lib/reactor/cm/obj.rb', line 285

def sign!(msg = nil)
  simple_command("sign", msg)
end

#take!(msg = nil) ⇒ Object



265
266
267
# File 'lib/reactor/cm/obj.rb', line 265

def take!(msg = nil)
  simple_command("take", msg)
end

#unrelease!(msg = nil) ⇒ Object



257
258
259
# File 'lib/reactor/cm/obj.rb', line 257

def unrelease!(msg = nil)
  simple_command("unrelease", msg)
end

#upload(data_or_io, extension) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/reactor/cm/obj.rb', line 52

def upload(data_or_io, extension)
  data = (data_or_io.is_a? IO) ? data_or_io.read : data_or_io
  base64_data = Base64.encode64(data)

  set(:contentType, extension)
  set(:blob, { base64_data => { encoding: "base64" } })
end

#valid_actionsObject



289
290
291
292
# File 'lib/reactor/cm/obj.rb', line 289

def valid_actions
  vcak = get("validControlActionKeys")
  (vcak || []).map(&:to_s)
end

#workflow_commentObject



363
364
365
366
367
368
369
370
371
372
373
# File 'lib/reactor/cm/obj.rb', line 363

def workflow_comment
  request = XmlRequest.prepare do |xml|
    xml.tag!("content-where") do
      xml.tag!("objectId", @obj_id)
      xml.tag!("state", "released")
    end
    xml.get_key_tag!("content", "workflowComment")
  end
  response = request.execute!
  response.xpath("//workflowComment/*").map { |x| x.text.to_s }.first
end