Class: ActiveCMIS::PropertyDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/active_cmis/property_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_type, property_definition) ⇒ PropertyDefinition

Returns a new instance of PropertyDefinition


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
# File 'lib/active_cmis/property_definition.rb', line 11

def initialize(object_type, property_definition)
  @object_type = object_type
  @property_definition = property_definition
  params = {}
  property_type = nil
  property_definition.map do |node|
    next unless node.namespace
    next unless node.namespace.href == NS::CMIS_CORE

    # FIXME: add support for "choices"
    case node.node_name
    when "id"
      @id = node.text
    when "localName"
      @local_name = node.text
    when "localNamespace"
      @local_namespace = node.text
    when "displayName"
      @display_name = node.text
    when "queryName"
      @query_name = node.text
    when "propertyType"
      # Will be post processed, but we need to know all the parameters before we can pick an atomic type
      property_type = node.text
    when "cardinality"
      @cardinality = node.text
    when "updatability"
      @updatability = node.text
    when "inherited"
      @inherited = AtomicType::Boolean.xml_to_bool(node.text)
    when "required"
      @required = AtomicType::Boolean.xml_to_bool(node.text)
    when "queryable"
      @queryable = AtomicType::Boolean.xml_to_bool(node.text)
    when "orderable"
      @orderable = AtomicType::Boolean.xml_to_bool(node.text)
    when "openChoice"
      @open_choice = AtomicType::Boolean.xml_to_bool(node.text)
    when "maxValue", "minValue", "resolution", "precision", "maxLength"
      params[node.node_name] = node.text
    end
  end

  if required and updatability == "readonly"
    logger.warn "The server behaved strange: attribute #{self.inspect} required but readonly, will set required to false"
    @required = false
  end
  if id == "cmis:objectTypeId" and updatability != "oncreate"
    logger.warn "The server behaved strange: cmis:objectTypeId should be updatable on create but #{updatability}"
    @updatability = "oncreate"
  end

  @property_type = case property_type.downcase
  when "string"
    max_length = params["maxLength"] ? params["maxLength"].to_i : nil
    AtomicType::String.new(max_length)
  when "decimal"
    min_value = params["minValue"] ? params["minValue"].to_f : nil
    max_value = params["maxValue"] ? params["maxValue"].to_f : nil
    AtomicType::Decimal.new(params["precision"].to_i, min_value, max_value)
  when "integer"
    min_value = params["minValue"] ? params["minValue"].to_i : nil
    max_value = params["maxValue"] ? params["maxValue"].to_i : nil
    AtomicType::Integer.new(min_value, max_value)
  when "datetime"
    AtomicType::DateTime.new(params["resolution"] || (logger.warn "No resolution for DateTime #{@id}"; "time") )
  when "html"
    AtomicType::HTML.new
  when "id"
    AtomicType::ID.new
  when "boolean"
    AtomicType::Boolean.new
  when "uri"
    AtomicType::URI.new
  else
    raise "Unknown property type #{property_type}"
  end
end

Instance Attribute Details

#cardinalityString (readonly)

Returns:

  • (String)

4
5
6
# File 'lib/active_cmis/property_definition.rb', line 4

def cardinality
  @cardinality
end

#choicesBoolean (readonly)

Returns:

  • (Boolean)

8
9
10
# File 'lib/active_cmis/property_definition.rb', line 8

def choices
  @choices
end

#default_valueString (readonly)

Returns:

  • (String)

4
5
6
# File 'lib/active_cmis/property_definition.rb', line 4

def default_value
  @default_value
end

#descriptionString (readonly)

Returns:

  • (String)

4
5
6
# File 'lib/active_cmis/property_definition.rb', line 4

def description
  @description
end

#display_nameString (readonly)

Returns:

  • (String)

4
5
6
# File 'lib/active_cmis/property_definition.rb', line 4

def display_name
  @display_name
end

#idString (readonly)

Returns:

  • (String)

4
5
6
# File 'lib/active_cmis/property_definition.rb', line 4

def id
  @id
end

#inheritedBoolean (readonly)

Returns:

  • (Boolean)

8
9
10
# File 'lib/active_cmis/property_definition.rb', line 8

def inherited
  @inherited
end

#local_nameString (readonly)

Returns:

  • (String)

4
5
6
# File 'lib/active_cmis/property_definition.rb', line 4

def local_name
  @local_name
end

#local_namespaceString (readonly)

Returns:

  • (String)

4
5
6
# File 'lib/active_cmis/property_definition.rb', line 4

def local_namespace
  @local_namespace
end

#object_typeString (readonly)

Returns:

  • (String)

4
5
6
# File 'lib/active_cmis/property_definition.rb', line 4

def object_type
  @object_type
end

#open_choiceBoolean (readonly)

Returns:

  • (Boolean)

8
9
10
# File 'lib/active_cmis/property_definition.rb', line 8

def open_choice
  @open_choice
end

#orderableBoolean (readonly)

Returns:

  • (Boolean)

8
9
10
# File 'lib/active_cmis/property_definition.rb', line 8

def orderable
  @orderable
end

#property_typeString (readonly)

Returns:

  • (String)

4
5
6
# File 'lib/active_cmis/property_definition.rb', line 4

def property_type
  @property_type
end

#query_nameString (readonly)

Returns:

  • (String)

4
5
6
# File 'lib/active_cmis/property_definition.rb', line 4

def query_name
  @query_name
end

#queryableBoolean (readonly)

Returns:

  • (Boolean)

8
9
10
# File 'lib/active_cmis/property_definition.rb', line 8

def queryable
  @queryable
end

#requiredBoolean (readonly)

Returns:

  • (Boolean)

8
9
10
# File 'lib/active_cmis/property_definition.rb', line 8

def required
  @required
end

#updatabilityString (readonly)

Returns:

  • (String)

4
5
6
# File 'lib/active_cmis/property_definition.rb', line 4

def updatability
  @updatability
end

Instance Method Details

#extract_property(properties) ⇒ Object


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
# File 'lib/active_cmis/property_definition.rb', line 138

def extract_property(properties)
  elements = properties.children.select do |n|
    n.node_name == property_name &&
      n["propertyDefinitionId"] == id &&
      n.namespace.href == NS::CMIS_CORE
  end
  if elements.empty?
    if required
      logger.warn "The server behaved strange: attribute #{self.inspect} required but not present among properties"
      # raise ActiveCMIS::Error.new("The server behaved strange: attribute #{self.inspect} required but not present among properties")
    end
    if repeating
      []
    else
      nil
    end
  elsif elements.length == 1
    values = elements.first.children.select {|node| node.name == 'value' && node.namespace && node.namespace.href == ActiveCMIS::NS::CMIS_CORE}
    if required && values.empty?
      logger.warn "The server behaved strange: attribute #{self.inspect} required but not present among properties"
      #raise ActiveCMIS::Error.new("The server behaved strange: attribute #{self.inspect} required but no values specified")
    end
    if !repeating && values.length > 1
      logger.warn "The server behaved strange: attribute #{self.inspect} required but not present among properties"
      #raise ActiveCMIS::Error.new("The server behaved strange: attribute #{self.inspect} not repeating but multiple values given")
    end
    values
  else
    raise "Property is not unique"
  end
end

#inspectString Also known as: to_s

Returns:

  • (String)

96
97
98
# File 'lib/active_cmis/property_definition.rb', line 96

def inspect
  "#{object_type.display_name}:#{id} => #{property_type}#{"[]" if repeating}"
end

#loggerLogger

Returns The logger of the repository

Returns:

  • (Logger)

    The logger of the repository


171
172
173
# File 'lib/active_cmis/property_definition.rb', line 171

def logger
  repository.logger
end

#property_nameString

Returns:

  • (String)

102
103
104
# File 'lib/active_cmis/property_definition.rb', line 102

def property_name
  "property#{property_type}"
end

#render_property(xml, value) ⇒ Object


107
108
109
110
111
112
113
114
115
116
117
# File 'lib/active_cmis/property_definition.rb', line 107

def render_property(xml, value)
  xml["c"].send(property_name, "propertyDefinitionId" => id) {
    if repeating
      value.each do |v|
        property_type.rb2cmis(xml, v)
      end
    else
      property_type.rb2cmis(xml, value)
    end
  }
end

#repeatingBoolean

Returns true if the attribute can have multiple values

Returns:

  • (Boolean)

    Returns true if the attribute can have multiple values


91
92
93
# File 'lib/active_cmis/property_definition.rb', line 91

def repeating
  cardinality == "multi"
end

#repositoryRepository

Returns The repository that the CMIS type is defined in

Returns:

  • (Repository)

    The repository that the CMIS type is defined in


175
176
177
# File 'lib/active_cmis/property_definition.rb', line 175

def repository
  object_type.repository
end

#validate_ruby_value(value) ⇒ Object

FIXME: should probably also raise error for out of bounds case


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/active_cmis/property_definition.rb', line 121

def validate_ruby_value(value)
  if updatability == "readonly" # FIXME: what about oncreate?
    raise "You are trying to update a readonly attribute (#{self})"
  elsif required && value.nil?
    raise "You are trying to unset a required attribute (#{self})"
  elsif repeating != (Array === value)
    raise "You are ignoring the cardinality for an attribute (#{self})"
  else
    if repeating && z = value.detect {|v| !property_type.can_handle?(v)}
      raise "Can't assign attribute with type #{z.class} to attribute with type #{property_type}"
    elsif !repeating && !property_type.can_handle?(value)
      raise "Can't assign attribute with type #{value.class} to attribute with type #{property_type}"
    end
  end
end