Module: UU::OS::Attachment

Extended by:
Attachment
Included in:
Attachment
Defined in:
uu_os_client-0.10.6/lib/uu/os/attachment.rb,
uu_os_client-0.10.6/lib/uu/os/attachment/stream_wrapper.rb,
uu_os_client-0.10.6/lib/uu/os/attachment/attachment_create.rb,
uu_os_client-0.10.6/lib/uu/os/attachment/attachment_check_in.rb,
uu_os_client-0.10.6/lib/uu/os/attachment/attachment_check_out.rb,
uu_os_client-0.10.6/lib/uu/os/attachment/attachment_attributes.rb,
uu_os_client-0.10.6/lib/uu/os/attachment/attachment_set_attributes.rb,
uu_os_client-0.10.6/lib/uu/os/attachment/attachment_get_attachment_list.rb

Overview

Attachment service.

Defined Under Namespace

Classes: AttachmentAttributes, AttachmentCheckIn, AttachmentCheckOut, AttachmentCreate, AttachmentGetAttachmentList, AttachmentSetAttributes, StreamWrapper

Constant Summary

PATH =

Service path

'ues/core/attachment/UESAttachment'

Instance Method Summary (collapse)

Instance Method Details

- (Object) check_in(attachment_uri, attachment = nil)

Updates the attached file in the system and unlocks it by default. The command succeeds if the attachment is unlocked or if it is locked by the current user. Otherwise, the command fails. At the very least, attachment data and the filename have to be specified in this command. The data are streamed.

Parameters:

  • attachment_uri (String, UU::OS::UESURI)

    UESURI of the attachment

  • attachment (AttachmentCheckIn) (defaults to: nil)

    DTO containing attributes of the checked-in attachment



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'uu_os_client-0.10.6/lib/uu/os/attachment.rb', line 193

def check_in(attachment_uri, attachment = nil)
  svc = UU::OS::REST::RemoteClient.new(Attachment)
  payload = Attachment::AttachmentCheckIn.new(attachment)

  # first we check all types of data and do basics
  if payload.data.kind_of?Hash
    # Hash should be interpreted as BinaryValue
    payload.data = UU::OS::REST::BinaryValue.new(payload.data)
  elsif payload.data.kind_of?String
    uri_in_str = check_for_uri_in_string_data(payload.data)
    if uri_in_str
      blob_uri = uri_in_str unless uri_in_str.nil?
    else
      payload.data = UU::OS::REST::BinaryValue.new(StringIO.new(payload.data))
      payload.data.name = "#{convert_to_filename(payload.name)}.txt"
    end
  elsif payload.data.kind_of?UESURI
    blob_uri = payload.data
  elsif payload.data.kind_of?File
    payload.data.rewind
    payload.data = UU::OS::REST::BinaryValue.new(payload.data)
  elsif payload.data.kind_of?IO
    payload.data = UU::OS::REST::BinaryValue.new(payload.data)
    payload.data.name = "#{convert_to_filename(payload.name)}.bin"
  elsif payload.data.kind_of?StringIO
    payload.data = UU::OS::REST::BinaryValue.new(payload.data)
    payload.data.name = "#{convert_to_filename(payload.name)}.txt"
  elsif payload.data.kind_of?UU::OS::REST::BinaryValue
    if payload.data.data.kind_of?String
      payload.data.data = StringIO.new(payload.data.data)
      extension = 'txt'
    elsif payload.data.data.kind_of?StringIO
      extension = 'txt'
    else
      extension = 'bin'
    end
    payload.data.name = "#{convert_to_filename(payload.name)}.#{extension}" if payload.data.name.nil? || payload.data.name.strip.empty?
  else
    raise ArgumentError.new("Data can be only instance of String, StringIO, IO, File, or BinaryValue, but was #{payload.data.class}.")
  end
  
  #backup data stream in case of uds upload failureend
  if (payload.data.kind_of?UESURI) || (payload.data.kind_of?String)
    backed_up = false
  else
    if payload.data.data.kind_of?StringIO
      tmp = StringIO.new(payload.data.data.string)
      data_is_uri = false
      backed_up = true
    elsif (payload.data.data.kind_of?IO) && (!payload.data().data.kind_of?File)
      tmp = Tempfile.new(payload.data.name)
      IO.copy_stream(payload.data.data,tmp)
      backed_up = true
    else
      backed_up = false
    end
  end

  # upload data by uds
  blob_uri = upload_to_uds(payload.data) unless blob_uri

  UU::OS::QoS::QoSHandler.auto_retry do
    if blob_uri
      payload.data = nil
      payload = payload.to_hash
      payload[:dataUri] = blob_uri
    else
      #reload stream from backup
      payload.data.data = tmp if backed_up
      #all data types are in BinaryValue
      bin_val = payload.data
      # Reset stream before each call in order to send all data
      payload.data = create_file_with_file_name(bin_val) if bin_val.data.kind_of?File
      # add method for file name recognition by rest client TODO add file name support to remote client directly
      payload.data = create_stream_with_file_name(bin_val) unless bin_val.data.kind_of?File
      payload = payload.to_hash
    end
    begin
      svc.post('checkIn', attachment_uri, payload)
    ensure
      if backed_up
        tmp.close unless tmp.closed?
        tmp.unlink if tmp.respond_to?(:unlink)
      end
      if blob_uri
        UU::UDS::Bucket.delete_blob(blob_uri)
      end
    end
  end
end

- (UU::OS::REST::BinaryValue) check_out(attachment_uri, options = nil)

Locks the attachment specified by the attachment_uri attribute. If the operation is successful or if the attachment is already locked by the current user, attachment stream handler or nil is returned depending on the options set in the attribute options. If the attachment cannot be locked because the attachment is locked by another user, the command will fail.

Parameters:

  • attachment_uri (String, UU::OS::UESURI)

    UESURI of the attachment

  • options (AttachmentCheckOut) (defaults to: nil)

    DTO with options containing a flag whether to return the stream with attachment data

Returns:



174
175
176
177
178
179
180
181
182
183
184
185
# File 'uu_os_client-0.10.6/lib/uu/os/attachment.rb', line 174

def check_out(attachment_uri, options = nil)
  svc = UU::OS::REST::RemoteClient.new(Attachment)
  payload = Attachment::AttachmentCheckOut.new(options).to_json
  UU::OS::QoS::QoSHandler.auto_retry do
    res = svc.post('checkOut', attachment_uri, payload)
    if res              
      return UU::OS::REST::BinaryValue.new(res, true)
    else
      return nil
    end
  end
end

- (UU::OS::UESURI) create(artifact_uri, attachment = nil)

Creates a new attachment to the specified artifact. At the very least, attachment data and filename have to be specified in this command. The data are streamed.

Parameters:

  • artifact_uriUESURI (String, UU::OS::UESURI)

    of the artifact where the new attachment is created

  • attachment (AttachmentCreate) (defaults to: nil)

    DTO containing attributes of a new attachment

Returns:



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'uu_os_client-0.10.6/lib/uu/os/attachment.rb', line 35

def create(artifact_uri, attachment = nil)
  svc = UU::OS::REST::RemoteClient.new(Attachment)
  payload = Attachment::AttachmentCreate.new(attachment)

  # first we check all types of data and do basics
  if payload.data.kind_of?Hash
    # Hash should be interpreted as BinaryValue
    payload.data = UU::OS::REST::BinaryValue.new(payload.data)
  elsif payload.data.kind_of?String
    uri_in_str = check_for_uri_in_string_data(payload.data)
    if uri_in_str
      blob_uri = uri_in_str unless uri_in_str.nil?
    else
      payload.data = UU::OS::REST::BinaryValue.new(StringIO.new(payload.data))
      payload.data.name = "#{convert_to_filename(payload.name)}.txt"
    end
  elsif payload.data.kind_of?UESURI
    blob_uri = payload.data
  elsif payload.data.kind_of?File
    payload.data.rewind
    payload.data = UU::OS::REST::BinaryValue.new(payload.data)
  elsif payload.data.kind_of?IO
    payload.data = UU::OS::REST::BinaryValue.new(payload.data)
    payload.data.name = "#{convert_to_filename(payload.name)}.bin"
  elsif payload.data.kind_of?StringIO
    payload.data = UU::OS::REST::BinaryValue.new(payload.data)
    payload.data.name = "#{convert_to_filename(payload.name)}.txt"
  elsif payload.data.kind_of?UU::OS::REST::BinaryValue
    if payload.data.data.kind_of?String
      payload.data.data = StringIO.new(payload.data.data)
      extension = 'txt'
    elsif payload.data.data.kind_of?StringIO
      extension = 'txt'
    else
      extension = 'bin'
    end
    payload.data.name = "#{convert_to_filename(payload.name)}.#{extension}" if payload.data.name.nil? || payload.data.name.strip.empty?
  else
    raise ArgumentError.new("Data can be only instance of String, StringIO, IO, File, or BinaryValue, but was #{payload.data.class}.")
  end
  
  #backup data stream in case of uds upload failure 
  if (payload.data.kind_of?UESURI) || (payload.data.kind_of?String)
    backed_up = false
  else
    if payload.data.data.kind_of?StringIO
      tmp = StringIO.new(payload.data.data.string)
      backed_up = true
    elsif (payload.data.data.kind_of?IO) && (!payload.data().data.kind_of?File)
      tmp = Tempfile.new(payload.data.name)
      IO.copy_stream(payload.data.data,tmp)
      payload.data.data.rewind
      backed_up = true
    else
      backed_up = false
    end
  end
  
  # upload data by uds
  blob_uri = upload_to_uds(payload.data) unless blob_uri
  
  UU::OS::QoS::QoSHandler.auto_retry do
    if blob_uri
      payload.data = nil
      payload = payload.to_hash
      payload[:dataUri] = blob_uri
    else
      #reload stream from backup
      payload.data.data = tmp if backed_up
      #all data types are in BinaryValue
      bin_val = payload.data
      # Reset stream before each call in order to send all data
      payload.data = create_file_with_file_name(bin_val) if bin_val.data.kind_of?File
      # add method for file name recognition by rest client TODO add support to remote client directly
      payload.data = create_stream_with_file_name(bin_val) unless bin_val.data.kind_of?File
      payload = payload.to_hash
    end
    begin
      res = svc.post('create', artifact_uri, payload)
      return UU::OS::UESURI.new(res)
    ensure
      if backed_up
        tmp.close unless tmp.closed?
        tmp.unlink if tmp.respond_to?(:unlink)
      end
      if blob_uri
        UU::UDS::Bucket.delete_blob(blob_uri)
      end
    end
  end
end

- (Object) delete(attachment_uri)

Deletes the specified attachment.

Parameters:

  • attachment_uri (String, UU::OS::UESURI)

    UESURI of the attachment which is about to be deleted.



303
304
305
306
307
308
# File 'uu_os_client-0.10.6/lib/uu/os/attachment.rb', line 303

def delete(attachment_uri)
  svc = UU::OS::REST::RemoteClient.new(Attachment)
  UU::OS::QoS::QoSHandler.auto_retry do
    svc.post('delete', attachment_uri)
  end
end

- (UU::OS::REST::BinaryValue) get_attachment_data(attachment_uri)

Returns stream handler containing data content of the attachment. If the attachment is locked by the same user, command returns data of the private version. If the attachment is locked by another user (or if the attachment is unlocked), command returns data of the public version. Command does not change the state of the attachment or its lock in the system.

Parameters:

  • attachment_uri (String, UU::OS::UESURI)

    UESURI of the attachment

Returns:



292
293
294
295
296
297
298
# File 'uu_os_client-0.10.6/lib/uu/os/attachment.rb', line 292

def get_attachment_data(attachment_uri)
  svc = UU::OS::REST::RemoteClient.new(Attachment)
  UU::OS::QoS::QoSHandler.auto_retry do
    res = svc.get('getAttachmentData', attachment_uri)
    return UU::OS::REST::BinaryValue.new(res, true)
  end
end

- (UU::OS::REST::ResultList<Attachment::AttachmentGetAttachmentList, Attachment::AttachmentAttributes>) get_attachment_list(artifact_uri, criteria = nil)

Retrieves a result list of attachments for the artifact specified by the artifact_uri parameter. The resulting attachment list is filtered according to the criteria provided by the criteria attribute. These criteria specify the page to return and name and code filters. The list is sorted (ascending) according to the attachment name and code.

Parameters:

  • artifact_uri (String, UU::OS::UESURI)

    UESURI of the artifact to get the list of attachments from.

  • criteria (AttachmentGetAttachmentList) (defaults to: nil)

    DTO containing paging and filtering criteria.

Returns:



153
154
155
156
157
158
159
160
161
162
163
# File 'uu_os_client-0.10.6/lib/uu/os/attachment.rb', line 153

def get_attachment_list(artifact_uri, criteria = nil)
  svc = UU::OS::REST::RemoteClient.new(Attachment)
  dto = Attachment::AttachmentGetAttachmentList.new(criteria)
  svc.add_parameter('pageIndex', dto.page_index)
  svc.add_parameter('pageSize', dto.page_size)
  svc.add_parameter('query', dto.query)
  UU::OS::QoS::QoSHandler.auto_retry do
    res = svc.get('getAttachmentList', artifact_uri)
    return UU::OS::REST::ResultList.new(Attachment::AttachmentGetAttachmentList, Attachment::AttachmentAttributes, res)
  end
end

- (AttachmentAttributes) get_attributes(attachment_uri)

Returns attributes representing the attachment specified by the attachment_uri attribute. If the attachment is locked by the current user, this command returns attributes of the private version. If the attachment is locked by another user (or if the attachment is unlocked), the command returns attributes of the public version. The command does not change the state of the attachment or its lock in the system.

Parameters:

  • attachment_uri (String, UU::OS::UESURI)

    UESURI of the attachment

Returns:



136
137
138
139
140
141
142
# File 'uu_os_client-0.10.6/lib/uu/os/attachment.rb', line 136

def get_attributes(attachment_uri)
  svc = UU::OS::REST::RemoteClient.new(Attachment)
  UU::OS::QoS::QoSHandler.auto_retry do
    res = svc.get('getAttributes', attachment_uri)
    return Attachment::AttachmentAttributes.new(res)
  end
end

- (Object) set_attributes(attachment_uri, attachment = nil)

Updates basic attributes of an attachment.

Parameters:

  • attachment_uriUESURI (String, UU::OS::UESURI)

    of the attachment whose attributes are to be changed.

  • attachment (AttachmentSetAttributes) (defaults to: nil)

    DTO containing the attachment attributes.

Returns:

  • UESURI of the attachment



316
317
318
319
320
321
322
323
# File 'uu_os_client-0.10.6/lib/uu/os/attachment.rb', line 316

def set_attributes(attachment_uri, attachment = nil)
  svc = UU::OS::REST::RemoteClient.new(Attachment)
  payload = UU::OS::Attachment::AttachmentSetAttributes.new(attachment).to_json
  UU::OS::QoS::QoSHandler.auto_retry do
    res = svc.post('setAttributes', attachment_uri, payload)
    return UU::OS::UESURI.new(res)
  end
end