Class: SakaiInfo::SakaiXMLEntity

Inherits:
SakaiObject show all
Includes:
ModProps
Defined in:
lib/sakai-info/sakai_xml_entity.rb

Overview

This class is the base for Sakai XML-based entities, which are many of the earliest data types in the Sakai database. Most properties of these objects are stored in a large XML clob/text field, and some are base64-encoded as well. Thus each record must be deserialized to read the various properties.

Most XML entities consist of a top-level tag representing the entity with a number of attributes attached representing some of the properties of the entity. And then there are “property” tags inside the top-level tag which represent some other properties, including some defaults recording the creator and modifier and the times of those accesses.

This class extends SakaiObject and implements some additional serialization methods to reflect the common elements of XML entities.

Instance Attribute Summary collapse

Attributes inherited from SakaiObject

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ModProps

included

Methods inherited from SakaiObject

#dbrow_only_serialization, #default_serialization, descendants, #object_type_serialization, #serialize, #shell_serialization, #summary_serialization, #to_csv, #to_json, #to_yaml

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



40
41
42
# File 'lib/sakai-info/sakai_xml_entity.rb', line 40

def attributes
  @attributes
end

#propertiesObject (readonly)

Returns the value of attribute properties.



40
41
42
# File 'lib/sakai-info/sakai_xml_entity.rb', line 40

def properties
  @properties
end

#xmlObject (readonly)

Returns the value of attribute xml.



40
41
42
# File 'lib/sakai-info/sakai_xml_entity.rb', line 40

def xml
  @xml
end

#xmldocObject (readonly)

Returns the value of attribute xmldoc.



40
41
42
# File 'lib/sakai-info/sakai_xml_entity.rb', line 40

def xmldoc
  @xmldoc
end

Class Method Details

.all_serializationsObject



120
121
122
# File 'lib/sakai-info/sakai_xml_entity.rb', line 120

def self.all_serializations
  [ :default, :attributes, :properties, :xml ]
end

Instance Method Details

#attributes_serializationObject

serialize all attributes



85
86
87
88
89
# File 'lib/sakai-info/sakai_xml_entity.rb', line 85

def attributes_serialization
  {
    "attributes" => self.attributes
  }
end

#dbrow_serializationObject

tweak the dbrow out since we hacked dbrow for other purposes and since the xml field doesn’t display typically



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sakai-info/sakai_xml_entity.rb', line 107

def dbrow_serialization
  dbrow = super["dbrow"]
  dbrow[:xml] = self.xml
  dbrow.delete(:_xml_entity_created_by)
  dbrow.delete(:_xml_entity_created_at)
  dbrow.delete(:_xml_entity_modified_by)
  dbrow.delete(:_xml_entity_modified_at)

  {
    "dbrow" => dbrow
  }
end

#parse_xmlObject

this method parses the universal XML field for all entities down to two collections: attributes (XML attributes defined in the top-level tag) and properties (<property> tags inside the top-level tag). Properties are generally base64 encoded



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
# File 'lib/sakai-info/sakai_xml_entity.rb', line 52

def parse_xml
  if @xml.nil?
    @xml = ""
    REXML::Document.new(@dbrow[:xml].read).write(@xml, 2)
  end

  @xmldoc = REXML::Document.new(@xml)
  @attributes = {}
  @xmldoc.root.attributes.keys.each do |att_name|
    @attributes[att_name] = @xmldoc.root.attributes[att_name]
  end

  @properties = {}
  REXML::XPath.each(@xmldoc, "//property") do |prop_node|
    prop_name = prop_node.attributes["name"]
    prop_encoding = prop_node.attributes["enc"]
    prop_value = prop_node.attributes["value"]

    if prop_encoding == "BASE64"
      prop_value = Base64.decode64(prop_value)
    else
      raise UnrecognizedPropertyEncodingException.new(prop_name, prop_encoding, prop_value)
    end
    @properties[prop_name] = prop_value
  end

  @dbrow[:_xml_entity_created_by] = @properties["CHEF:creator"]
  @dbrow[:_xml_entity_modified_by] = @properties["CHEF:modifiedby"]
  @dbrow[:_xml_entity_created_at] = Util.format_entity_date(@properties["DAV:creationdate"])
  @dbrow[:_xml_entity_modified_at] = Util.format_entity_date(@properties["DAV:getlastmodified"])
end

#properties_serializationObject

serialize all properties



92
93
94
95
96
# File 'lib/sakai-info/sakai_xml_entity.rb', line 92

def properties_serialization
  {
    "properties" => self.properties
  }
end

#xml_serializationObject

xml dump serialization option



99
100
101
102
103
# File 'lib/sakai-info/sakai_xml_entity.rb', line 99

def xml_serialization
  {
    "xml" => self.xml
  }
end