Class: SakaiInfo::ContentCollection

Inherits:
Content show all
Defined in:
lib/sakai-info/content.rb

Direct Known Subclasses

MissingContentCollection

Instance Attribute Summary collapse

Attributes inherited from Content

#parent_id

Attributes inherited from SakaiObject

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Content

all_serializations, #binary_entity, #child_summary_serialization, #effective_realm, #mod_details_serialization, #mod_serialization, #original_mod_details_serialization, #original_mod_serialization, #parent, #properties_serialization, #realm, #realm_serialization

Methods included from ModProps

included

Methods inherited from SakaiObject

all_serializations, #dbrow_only_serialization, #dbrow_serialization, descendants, #object_type_serialization, #serialize, #shell_serialization, #to_csv, #to_json, #to_yaml

Constructor Details

#initialize(dbrow) ⇒ ContentCollection

Returns a new instance of ContentCollection.



329
330
331
332
333
334
335
336
337
# File 'lib/sakai-info/content.rb', line 329

def initialize(dbrow)
  @dbrow = dbrow

  @id = @dbrow[:collection_id]
  @parent_id = @dbrow[:in_collection]

  @table_name = "content_collection"
  @id_column = "collection_id"
end

Instance Attribute Details

#dbrowObject (readonly)

Returns the value of attribute dbrow.



322
323
324
# File 'lib/sakai-info/content.rb', line 322

def dbrow
  @dbrow
end

Class Method Details

.clear_cacheObject



324
325
326
# File 'lib/sakai-info/content.rb', line 324

def self.clear_cache
  @@cache = {}
end

.count_by_parent(parent_id) ⇒ Object



467
468
469
# File 'lib/sakai-info/content.rb', line 467

def self.count_by_parent(parent_id)
  ContentCollection.query_by_parent(parent_id).count
end

.find(id) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/sakai-info/content.rb', line 339

def self.find(id)
  if id !~ /\/$/
    id += "/"
  end

  if @@cache[id].nil?
    row = DB.connect[:content_collection].where(:collection_id => id).first
    if row.nil?
      raise ObjectNotFoundException.new(ContentCollection, id)
    end
    @@cache[id] = ContentCollection.new(row)
  end
  @@cache[id]
end

.find!(id) ⇒ Object

return a content collection, even if it’s a missing one



355
356
357
358
359
360
361
# File 'lib/sakai-info/content.rb', line 355

def self.find!(id)
  begin
    ContentCollection.find(id)
  rescue ObjectNotFoundException
    MissingContentCollection.find(id)
  end
end

.find_by_parent(parent_id) ⇒ Object



471
472
473
474
475
476
477
478
# File 'lib/sakai-info/content.rb', line 471

def self.find_by_parent(parent_id)
  collections = []
  ContentCollection.query_by_parent(parent_id).all.each do |row|
    @@cache[row[:collection_id]] = ContentCollection.new(row)
    collections << @@cache[row[:collection_id]]
  end
  collections
end

.find_portfolio_interaction_collectionsObject



363
364
365
366
367
368
369
370
371
# File 'lib/sakai-info/content.rb', line 363

def self.find_portfolio_interaction_collections
  collections = []
  DB.connect[:content_collection].
    where(Sequel.like(:collection_id, '%/portfolio-interaction/')) do |row|
    @@cache[row[:collection_id]] = ContentCollection.new(row)
    collections << @@cache[row[:collection_id]]
  end
  collections
end

.query_by_parent(parent_id) ⇒ Object



463
464
465
# File 'lib/sakai-info/content.rb', line 463

def self.query_by_parent(parent_id)
  DB.connect[:content_collection].where(:in_collection => parent_id)
end

Instance Method Details

#child_countsObject



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/sakai-info/content.rb', line 388

def child_counts
  @child_collection_count ||= if @child_collections.nil?
                                ContentCollection.count_by_parent(@id)
                              else
                                @child_collections.length
                              end
  @child_resource_count ||= if @child_resources.nil?
                              ContentResource.count_by_parent(@id)
                            else
                              @child_resources.length
                            end
  {
    "collections" => @child_collection_count,
    "resources" => @child_resource_count,
    "total" => @child_collection_count + @child_resource_count
  }
end

#childrenObject



379
380
381
382
383
384
385
386
# File 'lib/sakai-info/content.rb', line 379

def children
  @child_collections ||= ContentCollection.find_by_parent(@id)
  @child_resources ||= ContentResource.find_by_parent(@id)
  {
    "collections" => @child_collections,
    "resources" => @child_resources
  }
end

#children_serializationObject



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/sakai-info/content.rb', line 427

def children_serialization
  result = {
    "collections" => self.children["collections"].collect { |cc|
      cc.serialize(:child_summary, :children)
    },
    "resources" => self.children["resources"].collect { |cr|
      cr.serialize(:child_summary)
    }
  }
  if result["collections"] == []
    result.delete("collections")
  end
  if result["resources"] == []
    result.delete("resources")
  end
  result
end

#default_serializationObject



406
407
408
409
410
411
412
# File 'lib/sakai-info/content.rb', line 406

def default_serialization
  {
    "id" => self.id,
    "size_on_disk" => self.size_on_disk,
    "children" => self.child_counts,
  }
end

#detailed_summary_serializationObject



420
421
422
423
424
425
# File 'lib/sakai-info/content.rb', line 420

def detailed_summary_serialization
  {
    "id" => self.id,
    "size_on_disk" => self.size_on_disk,
  }
end

#full_children_serializationObject



445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/sakai-info/content.rb', line 445

def full_children_serialization
  result = {
    "collections" => self.children["collections"].collect { |cc|
      cc.serialize(:detailed_summary, :full_children)
    },
    "resources" => self.children["resources"].collect { |cr|
      cr.serialize(:detailed_summary)
    }
  }
  if result["collections"] == []
    result.delete("collections")
  end
  if result["resources"] == []
    result.delete("resources")
  end
  result
end

#size_on_diskObject



373
374
375
376
377
# File 'lib/sakai-info/content.rb', line 373

def size_on_disk
  @size_on_disk ||=
    DB.connect[:content_resource].select(Sequel.function(:sum, :file_size).as(:total_size)).
    where(Sequel.like(:resource_id, "#{@id}%")).first[:total_size].to_i
end

#summary_serializationObject



414
415
416
417
418
# File 'lib/sakai-info/content.rb', line 414

def summary_serialization
  {
    "id" => self.id,
  }
end