Class: SakaiInfo::ContentCollection

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

Direct Known Subclasses

MissingContentCollection

Constant Summary collapse

@@cache =
{}

Instance Attribute Summary

Attributes inherited from Content

#parent_id

Attributes inherited from SakaiObject

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Content

#binary, #effective_realm, #parent, #realm

Methods inherited from SakaiObject

all_serializations, #dbrow_only_serialization, #dbrow_serialization, #object_type_serialization, #serialize, #to_json, #to_yaml

Constructor Details

#initialize(id, parent_id) ⇒ ContentCollection

Returns a new instance of ContentCollection.



149
150
151
152
153
154
155
# File 'lib/sakai-info/content.rb', line 149

def initialize(id, parent_id)
  @id = id
  @parent_id = parent_id

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

Class Method Details

.count_by_parent(parent_id) ⇒ Object



193
194
195
# File 'lib/sakai-info/content.rb', line 193

def self.count_by_parent(parent_id)
  DB.connect[:content_collection].filter(:in_collection => parent_id).count
end

.find(id) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/sakai-info/content.rb', line 158

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

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

.find!(id) ⇒ Object

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



174
175
176
177
178
179
180
# File 'lib/sakai-info/content.rb', line 174

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

.find_by_parent(parent_id) ⇒ Object



258
259
260
261
262
263
264
265
266
# File 'lib/sakai-info/content.rb', line 258

def self.find_by_parent(parent_id)
  collections = []
  DB.connect[:content_collection].filter(:in_collection => parent_id) do |row|
    @@cache[row[:collection_id]] =
      ContentCollection.new(row[:collection_id], row[:in_collection])
    collections << @@cache[row[:collection_id]]
  end
  collections
end

.find_portfolio_interaction_collectionsObject



182
183
184
185
186
187
188
189
190
191
# File 'lib/sakai-info/content.rb', line 182

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

Instance Method Details

#child_countsObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/sakai-info/content.rb', line 212

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



203
204
205
206
207
208
209
210
# File 'lib/sakai-info/content.rb', line 203

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



247
248
249
250
251
252
253
254
255
256
# File 'lib/sakai-info/content.rb', line 247

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

#default_serializationObject



230
231
232
233
234
235
236
237
238
# File 'lib/sakai-info/content.rb', line 230

def default_serialization
  {
    "id" => self.id,
    "parent" => self.parent_id,
    "size_on_disk" => self.size_on_disk,
    "children" => self.child_counts,
    "effective_realm" => (self.effective_realm.nil? ? nil : effective_realm.name)
  }
end

#size_on_diskObject



197
198
199
200
201
# File 'lib/sakai-info/content.rb', line 197

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

#summary_serializationObject



240
241
242
243
244
245
# File 'lib/sakai-info/content.rb', line 240

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