Class: ActiveCMIS::Document

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

Instance Attribute Summary

Attributes inherited from Object

#key, #repository, #updated_attributes

Instance Method Summary collapse

Methods inherited from Object

#acl, #allowable_actions, #attribute, attributes, #attributes, #destroy, #file, from_atom_entry, from_parameters, #id, #initialize, #inspect, key, #method_missing, #name, #parent_folders, #save, #source_relations, #target_relations, #unfile, #update

Methods included from Internal::Caching

included

Constructor Details

This class inherits a constructor from ActiveCMIS::Object

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveCMIS::Object

Instance Method Details

#cancel_checkoutvoid

This method returns an undefined value.

This action may not be permitted (query allowable_actions to see whether it is permitted)



192
193
194
195
196
197
198
199
200
# File 'lib/active_cmis/document.rb', line 192

def cancel_checkout
  if !self.class.versionable
    raise Error::Constraint.new("Object is not versionable, can't cancel checkout")
  elsif working_copy?
    conn.delete(self_link)
  else
    raise Error::InvalidArgument.new("Not a working copy")
  end
end

#checkin(major, comment = "", updated_attributes = {}) ⇒ Document #checkin(comment = "", updated_attributes = {}) ⇒ Document #checkin(updated_attributes = {}) ⇒ Document

Overloads:

  • #checkin(major, comment = "", updated_attributes = {}) ⇒ Document

    Check in the private working copy. Raises a constraint error when the document is not a working copy

    Parameters:

    • major (Boolean)

      Whether the document will be checked in as a major version

    • comment (String) (defaults to: "")

      An optional comment to use when creating the new version

    • updated_attributes (Hash) (defaults to: {})

      A hash with updated attributes

    Returns:

    • (Document)

      The final version that results from the checkin

  • #checkin(comment = "", updated_attributes = {}) ⇒ Document

    Check in the private working copy. Raises a constraint error when the document is not a working copy The version will be the version set by set_versioning_state (default is a major version)

    Parameters:

    • comment (String) (defaults to: "")

      An optional comment to use when creating the new version

    • updated_attributes (Hash) (defaults to: {})

      A hash with updated attributes

    Returns:

    • (Document)

      The final version that results from the checkin

  • #checkin(updated_attributes = {}) ⇒ Document

    Check in the private working copy with an empty message. Raises a constraint error when the document is not a working copy The version will be the version set by set_versioning_state (default is a major version)

    Parameters:

    • updated_attributes (Hash) (defaults to: {})

      A hash with updated attributes

    Returns:

    • (Document)

      The final version that results from the checkin



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/active_cmis/document.rb', line 227

def checkin(*options)
  if options.length > 3
    raise ArgumentError, "Too many arguments for checkin"
  else
    major, comment, updated_attributes = *options
    if TrueClass === major or FalseClass === major
      # Nothing changes: only defaults need to be filled in (if necessary)
    elsif String === major
      updated_attributes = comment
      comment = major
      # major will be true if: @versioning_state == "major", or if it's not set
      major = @versioning_state != "minor"
    elsif Hash === major
      updated_attributes = major
      major = @versioning_state != "minor"
    end
    comment ||= ""
    updated_attributes ||= {}
  end

  if working_copy?
    update(updated_attributes)
    result = self
    updated_aspects([true, major, comment]).each do |hash|
      result = result.send(hash[:message], *hash[:parameters])
    end
    @versioning_state = nil
    result
  else
    raise Error::Constraint, "Not a working copy"
  end
end

#checkoutDocument

The checkout operation results in a Private Working Copy

Most properties should be the same as for the document that was checked out, certain properties may differ such as cmis:objectId and cmis:creationDate.

The content stream of the PWC may be identical to that of the document that was checked out, or it may be unset.

Returns:

  • (Document)

    The checked out version of this document



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/active_cmis/document.rb', line 172

def checkout
  body = render_atom_entry(self.class.attributes.reject {|k,v| k != "cmis:objectId"})

  response = conn.post_response(repository.checkedout.url, body)
  if 200 <= response.code.to_i && response.code.to_i < 300
    entry = Nokogiri::XML.parse(response.body, nil, nil, Nokogiri::XML::ParseOptions::STRICT).xpath("/at:entry", NS::COMBINED)
    result = self_or_new(entry)
    if result.working_copy? # Work around a bug in OpenCMIS where result returned is the version checked out not the PWC
      result
    else
      conn.logger.warn "Repository did not return working copy for checkout operation"
      result.working_copy
    end
  else
    raise response.body
  end
end

#content_streamRendition

Returns an ActiveCMIS::Rendition to the content stream or nil if there is none

Returns:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_cmis/document.rb', line 5

def content_stream
  if content = data.xpath("at:content", NS::COMBINED).first
    if content['src']
      ActiveCMIS::Rendition.new(repository, self, "href" => content['src'], "type" => content["type"])
    else
      if content['type'] =~ /\+xml$/
        content_data = content.to_xml # FIXME: this may not preserve whitespace
      else
        content_data = data.unpack("m*").first
      end
      ActiveCMIS::Rendition.new(repository, self, "data" => content_data, "type" => content["type"])
    end
  elsif content = data.xpath("cra:content", NS::COMBINED).first
    content.children.each do |node|
      next unless node.namespace and node.namespace.href == NS::CMIS_REST
      content_data = node.text if node.name == "base64"
      content_type = node.text if node.name == "mediaType"
    end
    data = content_data.unpack("m*").first
    ActiveCMIS::Rendition.new(repository, self, "data" => content_data, "type" => content_type)
  end
end

#latest?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/active_cmis/document.rb', line 128

def latest?
  attributes["cmis:isLatestVersion"]
end

#latest_major?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/active_cmis/document.rb', line 134

def latest_major?
  attributes["cmis:isLatestMajorVersion"]
end

#latest_versionDocument

Returns self if this is the latest version Note: There will allways be a latest version in a version series

Returns:



104
105
106
107
108
109
110
111
112
113
# File 'lib/active_cmis/document.rb', line 104

def latest_version
  link = data.xpath("at:link[@rel = 'current-version']/@href", NS::COMBINED)
  if link.first
    entry = conn.get_atom_entry(link.first.text)
    self_or_new(entry)
  else
    # FIXME: should somehow return the current version even for opencmis
    self
  end
end

#major?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/active_cmis/document.rb', line 131

def major?
  attributes["cmis:isMajorVersion"]
end

#reloadvoid

This method returns an undefined value.



261
262
263
264
# File 'lib/active_cmis/document.rb', line 261

def reload
  @updated_contents = nil
  super
end

#renditionsArray<Rendition>

Will reload if renditionFilter was not set or cmis:none, but not in other circumstances

Returns:



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_cmis/document.rb', line 31

def renditions
  filter = used_parameters["renditionFilter"]
  if filter.nil? || filter == "cmis:none"
    reload
  end

  links = data.xpath("at:link[@rel = 'alternate']", NS::COMBINED)
  links.map do |link|
    ActiveCMIS::Rendition.new(repository, self, link)
  end
end

#set_content_stream(options) ⇒ void

This method returns an undefined value.

Sets new content to be uploaded, does not alter values you will get from content_stream (for the moment)

Parameters:

  • options (Hash)

    A hash containing exactly one of :file or :data

Options Hash (options):

  • :file (String)

    The name of a file to upload

  • :data (#read)

    Data you want to upload (if #length is defined it should give the total length that can be read)

  • :overwrite (Boolean) — default: true

    Whether the contents should be overwritten (ignored in case of checkin)

  • :mime_type (String)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/active_cmis/document.rb', line 52

def set_content_stream(options)
  if key.nil?
    if self.class.content_stream_allowed == "notallowed"
      raise Error::StreamNotSupported.new("Documents of this type can't have content")
    end
  else
    updatability = repository.capabilities["ContentStreamUpdatability"]
    if updatability == "none"
      raise Error::NotSupported.new("Content can't be updated in this repository")
    elsif updatability == "pwconly" && !working_copy?
      raise Error::Constraint.new("Content can only be updated for working copies in this repository")
    end
  end
  @updated_contents = options
end

#set_versioning_state(state) ⇒ void

This method returns an undefined value.

Sets versioning state. Only possible on new documents, or PWC documents

Parameters:

  • state ("major", "minor", "none", "checkedout")

    A string of the desired versioning state

Raises:



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/active_cmis/document.rb', line 74

def set_versioning_state(state)
  raise Error::Constraint.new("Can only set a different version state on PWC documents, or unsaved new documents") unless key.nil? || working_copy?
  raise ArgumentError, "You must pass a String" unless state.is_a?(String)
  if key.nil?
    possible_values = %w[major minor none checkedout]
  else
    possible_values = %w[major minor]
  end
  raise ArgumentError, "Given state is invalid. Possible values are #{possible_values.join(", ")}" unless possible_values.include?(state)

  @versioning_state = state
end

#version_series_checked_outHash?

Returns information about the checked out status of this document

Returns:

  • (Hash, nil)

    Keys are :by for the owner of the PWC and :id for the CMIS ID, both can be unset according to the spec



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/active_cmis/document.rb', line 148

def version_series_checked_out
  attributes = self.attributes
  if attributes["cmis:isVersionSeriesCheckedOut"]
    result = {}
    if attributes.has_key? "cmis:versionSeriesCheckedOutBy"
      result[:by] = attributes["cmis:versionSeriesCheckedOutBy"]
    end
    if attributes.has_key? "cmis:versionSeriesCheckedOutId"
      result[:id] = attributes["cmis:versionSeriesCheckedOutId"]
    end
    result
  else
    nil
  end
end

#versionsCollection<Document>, Array(self)

Returns all documents in the version series of this document. Uses self to represent the version of this document

Returns:



90
91
92
93
94
95
96
97
98
# File 'lib/active_cmis/document.rb', line 90

def versions
  link = data.xpath("at:link[@rel = 'version-history']/@href", NS::COMBINED)
  if link = link.first
    Collection.new(repository, link) # Problem: does not in fact use self
  else
    # The document is not versionable
    [self]
  end
end

#working_copyDocument

Returns self if this is the working copy Returns nil if there is no working copy

Returns:



118
119
120
121
122
123
124
125
126
# File 'lib/active_cmis/document.rb', line 118

def working_copy
  link = data.xpath("at:link[@rel = 'working-copy']/@href", NS::COMBINED)
  if link.first
    entry = conn.get_atom_entry(link.first.text)
    self_or_new(entry)
  else
    nil
  end
end

#working_copy?Boolean

Returns:

  • (Boolean)


138
139
140
141
142
143
# File 'lib/active_cmis/document.rb', line 138

def working_copy?
  return false if key.nil?

  # NOTE: This may not be a sufficient condition, but according to the spec it should be
  !data.xpath("at:link[@rel = 'via']", NS::COMBINED).empty?
end