dynamicloud 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/configuration.rb +50 -0
- data/lib/dynamic_api.rb +800 -0
- data/lib/dynamic_criteria.rb +424 -0
- data/lib/dynamic_model.rb +105 -0
- data/lib/dynamic_service.rb +66 -0
- data/lib/dynamicloud/version.rb +5 -0
- data/lib/exceptions.rb +5 -0
- metadata +113 -0
data/lib/dynamic_api.rb
ADDED
@@ -0,0 +1,800 @@
|
|
1
|
+
require_relative 'exceptions'
|
2
|
+
require_relative 'dynamic_service'
|
3
|
+
require_relative 'dynamic_model'
|
4
|
+
require_relative '../lib/configuration'
|
5
|
+
require 'json'
|
6
|
+
require 'rubygems'
|
7
|
+
require 'open-uri'
|
8
|
+
|
9
|
+
module Dynamicloud
|
10
|
+
class API
|
11
|
+
# This class contains data returned from Dynamicloud servers.
|
12
|
+
# @author Eleazar Gomez
|
13
|
+
# @version 1.0.0
|
14
|
+
# @since 8/26/15
|
15
|
+
class RecordResults
|
16
|
+
attr_accessor(:records, :total_records, :fast_returned_size)
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
@records = []
|
20
|
+
@total_records = 0
|
21
|
+
@fast_returned_size = 0
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class RecordQuery
|
26
|
+
|
27
|
+
DEFAULT_COUNT = 15
|
28
|
+
|
29
|
+
def initialize(mid)
|
30
|
+
@mid = mid
|
31
|
+
@credentials = nil
|
32
|
+
@order_by = nil
|
33
|
+
@group_by = nil
|
34
|
+
#@projection = nil
|
35
|
+
@offset = -1
|
36
|
+
@count = -1
|
37
|
+
@current_callback = nil
|
38
|
+
@list_was_called = false
|
39
|
+
@conditions = []
|
40
|
+
@current_projection = nil
|
41
|
+
end
|
42
|
+
|
43
|
+
# Apply a desc ordering to the current order by object
|
44
|
+
# An IllegalStateException will be thrown if orderBy object is nil
|
45
|
+
#
|
46
|
+
# @return this instance of Query
|
47
|
+
def desc
|
48
|
+
if @order_by.nil?
|
49
|
+
raise Exceptions::IllegalStateException, 'You must call order_by method before call this method'
|
50
|
+
end
|
51
|
+
|
52
|
+
@order_by.asc = false
|
53
|
+
|
54
|
+
self
|
55
|
+
end
|
56
|
+
|
57
|
+
# Apply a asc ordering to the current order by object
|
58
|
+
# An IllegalStateException will be thrown if orderBy object is nil
|
59
|
+
#
|
60
|
+
# @return this instance of Query
|
61
|
+
def asc
|
62
|
+
if @order_by.nil?
|
63
|
+
raise Exceptions::IllegalStateException, 'You must call order_by method before call this method'
|
64
|
+
end
|
65
|
+
|
66
|
+
@order_by.asc = true
|
67
|
+
|
68
|
+
self
|
69
|
+
end
|
70
|
+
|
71
|
+
# This method will add a new condition to an AND list of conditions.
|
72
|
+
#
|
73
|
+
# @param condition new condition to a list of conditions to use
|
74
|
+
# @return this instance of Query
|
75
|
+
def add(condition)
|
76
|
+
@conditions.push(condition)
|
77
|
+
self
|
78
|
+
end
|
79
|
+
|
80
|
+
# This method sets the projection to use in this query. The def execution will return those projection.
|
81
|
+
# If projection == nil then, this def will returns all model's projection.
|
82
|
+
#
|
83
|
+
# @param projection projection in this query
|
84
|
+
# @return this instance of Query
|
85
|
+
#def add_projection(projection)
|
86
|
+
# @projection = projection
|
87
|
+
#self
|
88
|
+
#end
|
89
|
+
|
90
|
+
# Sets an offset to this query to indicates the page of a big data result.
|
91
|
+
#
|
92
|
+
# @param offset new offset
|
93
|
+
# @return this instance of Query
|
94
|
+
def set_offset(offset)
|
95
|
+
@offset = offset
|
96
|
+
self
|
97
|
+
end
|
98
|
+
|
99
|
+
# Sets how many items per page (offset) this def will fetch
|
100
|
+
#
|
101
|
+
# @param count how many items
|
102
|
+
# @return this instance of Query
|
103
|
+
def set_count(count)
|
104
|
+
@count = count
|
105
|
+
self
|
106
|
+
end
|
107
|
+
|
108
|
+
# Gets the current count
|
109
|
+
# If count == 0 then will return default count (DEFAULT_COUNT)
|
110
|
+
#
|
111
|
+
# @return the current count
|
112
|
+
def get_count
|
113
|
+
@count <= 0 ? DEFAULT_COUNT : @count;
|
114
|
+
end
|
115
|
+
|
116
|
+
# This method will execute a query and returns a list of records
|
117
|
+
# @param projection projection to use in this operation
|
118
|
+
def get_results(projection = nil)
|
119
|
+
selection = Dynamicloud::API::DynamicloudHelper.build_string(get_conditions, get_group_by, get_order_by,
|
120
|
+
(Dynamicloud::API::DynamicloudHelper.build_projection(projection)))
|
121
|
+
@current_projection = projection
|
122
|
+
|
123
|
+
url = Configuration::PROPERTIES.get_property :url
|
124
|
+
if projection
|
125
|
+
url_get_records = Configuration::PROPERTIES.get_property :url_get_specific_fields
|
126
|
+
else
|
127
|
+
url_get_records = Configuration::PROPERTIES.get_property :url_get_records
|
128
|
+
end
|
129
|
+
|
130
|
+
url_get_records = url_get_records.gsub '{csk}', URI::encode(@credentials[:csk])
|
131
|
+
url_get_records = url_get_records.gsub '{aci}', URI::encode(@credentials[:aci])
|
132
|
+
url_get_records = url_get_records.gsub '{mid}', @mid.to_s
|
133
|
+
url_get_records = url_get_records.gsub '{count}', get_count.to_s
|
134
|
+
url_get_records = url_get_records.gsub '{offset}', get_current_offset.to_s
|
135
|
+
|
136
|
+
params = {
|
137
|
+
:criteria => selection
|
138
|
+
}
|
139
|
+
|
140
|
+
response = DynamicService::ServiceCaller.call_service url + url_get_records, params, 'post'
|
141
|
+
|
142
|
+
Dynamicloud::API::DynamicloudHelper.build_record_results response
|
143
|
+
|
144
|
+
end
|
145
|
+
|
146
|
+
# This method adds an order by condition. The condition will have an asc ordering by default.
|
147
|
+
#
|
148
|
+
# @param attribute attribute by this query will be ordered.
|
149
|
+
# @return this instance of Query
|
150
|
+
def order_by(attribute)
|
151
|
+
@order_by = OrderByClause.asc(attribute)
|
152
|
+
self
|
153
|
+
end
|
154
|
+
|
155
|
+
# This method create a groupBy condition using attribute
|
156
|
+
#
|
157
|
+
# @param attribute attribute by this query will group.
|
158
|
+
# @return this instance of Query
|
159
|
+
def group_by(attribute)
|
160
|
+
@group_by = GroupByClause.new(attribute)
|
161
|
+
self
|
162
|
+
end
|
163
|
+
|
164
|
+
# get the current conditions
|
165
|
+
#
|
166
|
+
# @return the conditions
|
167
|
+
def get_conditions
|
168
|
+
@conditions
|
169
|
+
end
|
170
|
+
|
171
|
+
# Gets the current offset so far. This attribute will increase according calls of method next(RecordCallback<T> callback)
|
172
|
+
#
|
173
|
+
# @return int of current offset
|
174
|
+
def get_current_offset
|
175
|
+
@offset < 0 ? 0 : @offset
|
176
|
+
end
|
177
|
+
|
178
|
+
# Will execute a list operation with an offset += count and will use the same callback object in list method.
|
179
|
+
# This method will return a RecordResults object
|
180
|
+
def next
|
181
|
+
@offset = get_current_offset + get_count
|
182
|
+
get_results @current_projection
|
183
|
+
end
|
184
|
+
|
185
|
+
# Returns the current model id associated to this query
|
186
|
+
# @return model id
|
187
|
+
def get_model_id
|
188
|
+
@mid
|
189
|
+
end
|
190
|
+
|
191
|
+
# Sets the credentials to use
|
192
|
+
#
|
193
|
+
# @param credentials credentials to execute operations.
|
194
|
+
def set_credentials(credentials)
|
195
|
+
@credentials = credentials
|
196
|
+
end
|
197
|
+
|
198
|
+
# get the current orderBy condition
|
199
|
+
# @return the order by condition
|
200
|
+
def get_order_by
|
201
|
+
@order_by
|
202
|
+
end
|
203
|
+
|
204
|
+
# get the current groupBy condition
|
205
|
+
#
|
206
|
+
# @return the group by condition
|
207
|
+
def get_group_by
|
208
|
+
@group_by
|
209
|
+
end
|
210
|
+
|
211
|
+
# This method create a groupBy condition using projection
|
212
|
+
#
|
213
|
+
# @param attributes projection by this query will group.
|
214
|
+
def set_group_by(attributes)
|
215
|
+
@group_by = GroupByClause.new(attributes)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
# This class represents a record in Dynamicloud
|
220
|
+
# @author Eleazar Gomez
|
221
|
+
# @version 1.0.0
|
222
|
+
# @since 8/24/15
|
223
|
+
class RecordImpl
|
224
|
+
def initialize
|
225
|
+
@map = {}
|
226
|
+
end
|
227
|
+
|
228
|
+
# gets the value paired with attribute
|
229
|
+
# @param attribute attribute to use
|
230
|
+
# @return the value paired with attribute
|
231
|
+
def get_value(attribute)
|
232
|
+
obj = @map[attribute]
|
233
|
+
if obj
|
234
|
+
if obj.is_a?(String)
|
235
|
+
return obj.to_s
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
raise IllegalStateException, "The attribute #{attribute} doesn't have a paired string."
|
240
|
+
end
|
241
|
+
|
242
|
+
# get the values paired with attribute
|
243
|
+
# @param attribute attribute to use
|
244
|
+
# @return the values paired with attribute
|
245
|
+
def get_values(attribute)
|
246
|
+
obj = @map[attribute]
|
247
|
+
if obj
|
248
|
+
if obj.respond_to?(:each)
|
249
|
+
return obj
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
raise IllegalStateException, "Tha attribute #{attribute} doesn't have a paired string array."
|
254
|
+
end
|
255
|
+
|
256
|
+
# Adds a new value paired with attribute
|
257
|
+
# @param attribute attribute to be paired
|
258
|
+
# @param value value
|
259
|
+
def add_value(attribute, value)
|
260
|
+
@map[attribute] = value
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
# This class represents an error from Dynamicloud servers
|
265
|
+
# @author Eleazar Gomez
|
266
|
+
# @version 1.0.0
|
267
|
+
# @since 8/25/15
|
268
|
+
class RecordError
|
269
|
+
attr_accessor(:message, :code)
|
270
|
+
|
271
|
+
def initialize
|
272
|
+
@message = ''
|
273
|
+
@code = ''
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
# This class has two attributes CSK and ACI keys. This information is provided at moment the registration in Dynamicloud.
|
278
|
+
# @author Eleazar Gomez
|
279
|
+
# @version 1.0.0
|
280
|
+
# @since 8/25/15
|
281
|
+
class DynamicProvider
|
282
|
+
def initialize(credential)
|
283
|
+
@credential = credential
|
284
|
+
end
|
285
|
+
|
286
|
+
# This method will load a record using rid and will instantiate a hash with attributes bound to Model's fields.
|
287
|
+
# @param rid record id
|
288
|
+
# @param mid model id
|
289
|
+
# @return a hash with record information
|
290
|
+
def load_record(rid, mid)
|
291
|
+
url = Configuration::PROPERTIES.get_property :url
|
292
|
+
url_get_records = Configuration::PROPERTIES.get_property :url_get_record_info
|
293
|
+
|
294
|
+
url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
|
295
|
+
url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
|
296
|
+
url_get_records = url_get_records.gsub '{mid}', mid.to_s
|
297
|
+
url_get_records = url_get_records.gsub '{rid}', rid.to_s
|
298
|
+
|
299
|
+
response = DynamicService::ServiceCaller.call_service url + url_get_records, {}, 'get'
|
300
|
+
|
301
|
+
json = JSON.parse(response)
|
302
|
+
record = json['record']
|
303
|
+
|
304
|
+
unless record
|
305
|
+
raise 'Record key doesn\'t present in response from Dynamicloud server.'
|
306
|
+
end
|
307
|
+
|
308
|
+
Dynamicloud::API::DynamicloudHelper.normalize_record record
|
309
|
+
end
|
310
|
+
|
311
|
+
# This method will call an update operation in Dynamicloud servers
|
312
|
+
# using model and data object
|
313
|
+
# @param mid model id
|
314
|
+
# @param data this hash should contain a key rid (RecordId) otherwise an error will be thrown
|
315
|
+
def update_record(mid, data)
|
316
|
+
rid = data['rid']
|
317
|
+
|
318
|
+
unless rid
|
319
|
+
raise "rid attribute isn't present in hash data."
|
320
|
+
end
|
321
|
+
|
322
|
+
url = Configuration::PROPERTIES.get_property :url
|
323
|
+
url_get_records = Configuration::PROPERTIES.get_property :url_update_record
|
324
|
+
|
325
|
+
url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
|
326
|
+
url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
|
327
|
+
url_get_records = url_get_records.gsub '{mid}', mid.to_s
|
328
|
+
url_get_records = url_get_records.gsub '{rid}', rid.to_s
|
329
|
+
|
330
|
+
params = {
|
331
|
+
:fields => Dynamicloud::API::DynamicloudHelper.build_fields_json(data)
|
332
|
+
}
|
333
|
+
|
334
|
+
response = DynamicService::ServiceCaller.call_service url + url_get_records, params, 'post'
|
335
|
+
|
336
|
+
json = JSON.parse(response)
|
337
|
+
unless json['status'] == 200
|
338
|
+
raise json['message']
|
339
|
+
end
|
340
|
+
end
|
341
|
+
|
342
|
+
# This method will call a save operation in DynamiCloud servers
|
343
|
+
# using model and BoundInstance object
|
344
|
+
# @param mid model id
|
345
|
+
# @param data all data needed to save a record in model (mid)
|
346
|
+
def save_record(mid, data)
|
347
|
+
url = Configuration::PROPERTIES.get_property :url
|
348
|
+
url_get_records = Configuration::PROPERTIES.get_property :url_save_record
|
349
|
+
|
350
|
+
url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
|
351
|
+
url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
|
352
|
+
url_get_records = url_get_records.gsub '{mid}', mid.to_s
|
353
|
+
|
354
|
+
params = {
|
355
|
+
:fields => Dynamicloud::API::DynamicloudHelper.build_fields_json(data)
|
356
|
+
}
|
357
|
+
|
358
|
+
response = DynamicService::ServiceCaller.call_service url + url_get_records, params, 'post'
|
359
|
+
|
360
|
+
json = JSON.parse(response)
|
361
|
+
unless json['status'] == 200
|
362
|
+
raise json['message']
|
363
|
+
end
|
364
|
+
|
365
|
+
data['rid'] = json['rid']
|
366
|
+
|
367
|
+
data
|
368
|
+
end
|
369
|
+
|
370
|
+
# This method will call a delete operation in DynamiCloud servers
|
371
|
+
# using model id and Record id
|
372
|
+
# @param mid model id
|
373
|
+
# @param rid record id
|
374
|
+
def delete_record(mid, rid)
|
375
|
+
url = Configuration::PROPERTIES.get_property :url
|
376
|
+
url_get_records = Configuration::PROPERTIES.get_property :url_delete_record
|
377
|
+
|
378
|
+
url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
|
379
|
+
url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
|
380
|
+
url_get_records = url_get_records.gsub '{mid}', mid.to_s
|
381
|
+
url_get_records = url_get_records.gsub '{rid}', rid.to_s
|
382
|
+
|
383
|
+
response = DynamicService::ServiceCaller.call_service url + url_get_records, {}, 'delete'
|
384
|
+
|
385
|
+
json = JSON.parse(response)
|
386
|
+
unless json['status'] == 200
|
387
|
+
raise json['message']
|
388
|
+
end
|
389
|
+
end
|
390
|
+
|
391
|
+
# Will create a RecordQuery and sets to this provider
|
392
|
+
# @param mid model id to use to execute operations
|
393
|
+
# @return this Dynamicloud::API::RecordQuery instance
|
394
|
+
def create_query(mid)
|
395
|
+
query = Dynamicloud::API::RecordQuery.new mid
|
396
|
+
query.set_credentials @credential
|
397
|
+
|
398
|
+
query
|
399
|
+
end
|
400
|
+
|
401
|
+
# Gets model record information from DynamiCloud servers.
|
402
|
+
# @param mid model id in DynamiClod servers
|
403
|
+
# @return RecordModel object
|
404
|
+
def load_model(mid)
|
405
|
+
url = Configuration::PROPERTIES.get_property :url
|
406
|
+
url_get_records = Configuration::PROPERTIES.get_property :url_get_model_info
|
407
|
+
|
408
|
+
url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
|
409
|
+
url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
|
410
|
+
url_get_records = url_get_records.gsub '{mid}', mid.to_s
|
411
|
+
|
412
|
+
response = DynamicService::ServiceCaller.call_service url + url_get_records, {}, 'get'
|
413
|
+
|
414
|
+
json = JSON.parse(response)
|
415
|
+
unless json['status'] == 200
|
416
|
+
raise json['message']
|
417
|
+
end
|
418
|
+
|
419
|
+
model = Dynamicloud::API::Model::RecordModel.new mid
|
420
|
+
model.name = json['name']
|
421
|
+
model.description = json['description']
|
422
|
+
|
423
|
+
model
|
424
|
+
end
|
425
|
+
|
426
|
+
# Loads all models related to CSK and ACI keys in Dynamicloud servers
|
427
|
+
# @return list of models
|
428
|
+
def load_models
|
429
|
+
url = Configuration::PROPERTIES.get_property :url
|
430
|
+
url_get_records = Configuration::PROPERTIES.get_property :url_get_models
|
431
|
+
|
432
|
+
url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
|
433
|
+
url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
|
434
|
+
|
435
|
+
response = DynamicService::ServiceCaller.call_service url + url_get_records, {}, 'get'
|
436
|
+
|
437
|
+
json = JSON.parse(response)
|
438
|
+
unless json['status'] == 200
|
439
|
+
raise json['message']
|
440
|
+
end
|
441
|
+
|
442
|
+
models = []
|
443
|
+
array = json['models']
|
444
|
+
array.each do |item|
|
445
|
+
model = Dynamicloud::API::Model::RecordModel.new item['id'].to_i
|
446
|
+
model.name = item['name']
|
447
|
+
model.description = item['description']
|
448
|
+
|
449
|
+
models.push model
|
450
|
+
end
|
451
|
+
|
452
|
+
models
|
453
|
+
end
|
454
|
+
|
455
|
+
# Loads all model's fields according ModelID
|
456
|
+
# @param mid model id
|
457
|
+
# @return list of model's fields.
|
458
|
+
def load_fields(mid)
|
459
|
+
url = Configuration::PROPERTIES.get_property :url
|
460
|
+
url_get_records = Configuration::PROPERTIES.get_property :url_get_fields
|
461
|
+
|
462
|
+
url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
|
463
|
+
url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
|
464
|
+
url_get_records = url_get_records.gsub '{mid}', mid.to_s
|
465
|
+
|
466
|
+
response = DynamicService::ServiceCaller.call_service url + url_get_records, {}, 'get'
|
467
|
+
|
468
|
+
json = JSON.parse(response)
|
469
|
+
unless json['status'] == 200
|
470
|
+
raise json['message']
|
471
|
+
end
|
472
|
+
|
473
|
+
fields = []
|
474
|
+
fs = json['fields']
|
475
|
+
fs.each do |key, jf|
|
476
|
+
field = Dynamicloud::API::Model::RecordField.new
|
477
|
+
field.id = jf['id'].to_i
|
478
|
+
field.identifier = jf['identifier']
|
479
|
+
field.label = jf['label']
|
480
|
+
field.comment = jf['comment']
|
481
|
+
field.uniqueness = jf['uniqueness']
|
482
|
+
field.required = jf['required']
|
483
|
+
field.type = Dynamicloud::API::Model::RecordFieldType.get_field_type jf['field_type'].to_i
|
484
|
+
field.items = Dynamicloud::API::DynamicloudHelper.build_items jf['items']
|
485
|
+
field.mid = mid.to_i
|
486
|
+
|
487
|
+
fields.push field
|
488
|
+
end
|
489
|
+
|
490
|
+
fields
|
491
|
+
end
|
492
|
+
|
493
|
+
# Uploads a file in record <b>rid</b>
|
494
|
+
# @param mid owner model id of this record <b>rid<b/>
|
495
|
+
# @param rid record id
|
496
|
+
# @param field_name fieldName target
|
497
|
+
# @param file file to upload
|
498
|
+
# @param content_type contentType of this file
|
499
|
+
# @param preferred_name preferred name to later downloads
|
500
|
+
def upload_file(mid, rid, field_name, file, content_type, preferred_name)
|
501
|
+
url = Configuration::PROPERTIES.get_property :url
|
502
|
+
url_get_records = Configuration::PROPERTIES.get_property :url_upload_file
|
503
|
+
|
504
|
+
url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
|
505
|
+
url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
|
506
|
+
url_get_records = url_get_records.gsub '{mid}', mid.to_s
|
507
|
+
url_get_records = url_get_records.gsub '{rid}', rid.to_s
|
508
|
+
|
509
|
+
params = {
|
510
|
+
:pickedFileName => File.basename(file.path),
|
511
|
+
:file_type => content_type,
|
512
|
+
:file_name => preferred_name,
|
513
|
+
:identifier => field_name,
|
514
|
+
'files[]' => file
|
515
|
+
}
|
516
|
+
|
517
|
+
response = DynamicService::ServiceCaller.call_service url + url_get_records, params, 'post'
|
518
|
+
|
519
|
+
json = JSON.parse(response)
|
520
|
+
unless json['status'] == 200
|
521
|
+
raise json['message']
|
522
|
+
end
|
523
|
+
end
|
524
|
+
|
525
|
+
# This method will make a request to generate a link to download the file related to this recordId and fieldName
|
526
|
+
# @param mid model id
|
527
|
+
# @param rid record id
|
528
|
+
# @param field_name field name
|
529
|
+
# @return link to share file
|
530
|
+
def share_file(mid, rid, field_name)
|
531
|
+
url = Configuration::PROPERTIES.get_property :url
|
532
|
+
url_get_records = Configuration::PROPERTIES.get_property :url_share_file
|
533
|
+
|
534
|
+
url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
|
535
|
+
url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
|
536
|
+
url_get_records = url_get_records.gsub '{mid}', mid.to_s
|
537
|
+
url_get_records = url_get_records.gsub '{rid}', rid.to_s
|
538
|
+
url_get_records = url_get_records.gsub '{identifier}', field_name
|
539
|
+
|
540
|
+
response = DynamicService::ServiceCaller.call_service url + url_get_records, {}, 'get'
|
541
|
+
|
542
|
+
json = JSON.parse(response)
|
543
|
+
unless json['status'] == 200
|
544
|
+
raise json['message']
|
545
|
+
end
|
546
|
+
|
547
|
+
json['link']
|
548
|
+
end
|
549
|
+
|
550
|
+
# Downloads a file according <b>rid</b> and <b>fieldName</b>
|
551
|
+
# @param mid owner model id of this record <b>rid<b/>
|
552
|
+
# @param rid record id
|
553
|
+
# @param field_name fieldName target
|
554
|
+
# @param file_destiny destiny file. If this file doesn't exist then will be created
|
555
|
+
def download_file(mid, rid, field_name, file_destiny)
|
556
|
+
url = Configuration::PROPERTIES.get_property :url
|
557
|
+
url_get_records = Configuration::PROPERTIES.get_property :url_download_file
|
558
|
+
|
559
|
+
url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
|
560
|
+
url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
|
561
|
+
url_get_records = url_get_records.gsub '{mid}', mid.to_s
|
562
|
+
url_get_records = url_get_records.gsub '{rid}', rid.to_s
|
563
|
+
url_get_records = url_get_records.gsub '{identifier}', field_name
|
564
|
+
|
565
|
+
DynamicService::ServiceCaller.call_service url + url_get_records, {}, {}, {}, file_destiny
|
566
|
+
end
|
567
|
+
|
568
|
+
# Executes an update using query as a selection and data with values
|
569
|
+
# Dynamicloud will normalize the key pair values. That is, will be used field identifiers only.
|
570
|
+
# @param data data that will be sent to Dynamicloud servers
|
571
|
+
# @param query selection
|
572
|
+
def update(query, data = {})
|
573
|
+
selection = Dynamicloud::API::DynamicloudHelper.build_string(query.get_conditions, nil, nil, nil)
|
574
|
+
fields = '{"updates": ' + Dynamicloud::API::DynamicloudHelper.build_fields_json(data) + '}'
|
575
|
+
|
576
|
+
url = Configuration::PROPERTIES.get_property :url
|
577
|
+
url_get_records = Configuration::PROPERTIES.get_property :url_update_selection
|
578
|
+
|
579
|
+
url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
|
580
|
+
url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
|
581
|
+
url_get_records = url_get_records.gsub '{mid}', query.get_model_id.to_s
|
582
|
+
|
583
|
+
params = {
|
584
|
+
:fields => fields,
|
585
|
+
:selection => selection
|
586
|
+
}
|
587
|
+
|
588
|
+
response = DynamicService::ServiceCaller.call_service url + url_get_records, params, 'post'
|
589
|
+
|
590
|
+
json = JSON.parse(response)
|
591
|
+
unless json['status'] == 200
|
592
|
+
raise json['message']
|
593
|
+
end
|
594
|
+
end
|
595
|
+
|
596
|
+
# Executes a delete using query as a selection
|
597
|
+
# @param query selection
|
598
|
+
# @param mid model id
|
599
|
+
def delete(query, mid)
|
600
|
+
selection = Dynamicloud::API::DynamicloudHelper.build_string(query.get_conditions, nil, nil, nil)
|
601
|
+
|
602
|
+
url = Configuration::PROPERTIES.get_property :url
|
603
|
+
url_get_records = Configuration::PROPERTIES.get_property :url_delete_selection
|
604
|
+
|
605
|
+
url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
|
606
|
+
url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
|
607
|
+
url_get_records = url_get_records.gsub '{mid}', mid.to_s
|
608
|
+
|
609
|
+
params = {
|
610
|
+
:selection => selection
|
611
|
+
}
|
612
|
+
|
613
|
+
response = DynamicService::ServiceCaller.call_service url + url_get_records, params, 'post'
|
614
|
+
|
615
|
+
json = JSON.parse(response)
|
616
|
+
unless json['status'] == 200
|
617
|
+
raise json['message']
|
618
|
+
end
|
619
|
+
end
|
620
|
+
end
|
621
|
+
|
622
|
+
# This is a class with utility methods
|
623
|
+
# @author Eleazar Gomez
|
624
|
+
# @version 1.0.0
|
625
|
+
# @since 8/26/15
|
626
|
+
class DynamicloudHelper
|
627
|
+
#This method will normalize the response from Dynamicloud servers
|
628
|
+
def self.normalize_record(record)
|
629
|
+
normalized = {}
|
630
|
+
record.each do |key, value|
|
631
|
+
if value.is_a?(Hash)
|
632
|
+
#This hash has only one key
|
633
|
+
value.each do |ik, iv|
|
634
|
+
if iv.respond_to?(:each)
|
635
|
+
array = []
|
636
|
+
iv.each do |item|
|
637
|
+
array.push item.to_s
|
638
|
+
end
|
639
|
+
|
640
|
+
normalized[key] = array
|
641
|
+
elsif iv.is_a?(String)
|
642
|
+
normalized[key] = iv.to_s
|
643
|
+
end
|
644
|
+
|
645
|
+
#This hash has only one key
|
646
|
+
break
|
647
|
+
end
|
648
|
+
else
|
649
|
+
normalized[key] = value
|
650
|
+
end
|
651
|
+
end
|
652
|
+
|
653
|
+
normalized
|
654
|
+
end
|
655
|
+
|
656
|
+
# Builds a compatible string to update a record.
|
657
|
+
# This method will get field name and its value form data hash.
|
658
|
+
# @param instance Object where data is extracted
|
659
|
+
# @return compatible string
|
660
|
+
def self.build_fields_json(data)
|
661
|
+
result = data.clone
|
662
|
+
data.each do |key, value|
|
663
|
+
if value.respond_to?(:each)
|
664
|
+
array = ''
|
665
|
+
value.each do |item|
|
666
|
+
if item
|
667
|
+
array = array + (array == '' ? '' : ',') + item.to_s
|
668
|
+
end
|
669
|
+
end
|
670
|
+
result[key] = array
|
671
|
+
end
|
672
|
+
end
|
673
|
+
|
674
|
+
result.to_json
|
675
|
+
end
|
676
|
+
|
677
|
+
# Builds an array of RecordFieldItems according JSONArray
|
678
|
+
# @param array JSONArray with pair value, text.
|
679
|
+
def self.build_items(array)
|
680
|
+
items = []
|
681
|
+
if array.length > 0
|
682
|
+
array.each do |item|
|
683
|
+
ri = Dynamicloud::API::Model::RecordFieldItem.new
|
684
|
+
ri.text = item['text']
|
685
|
+
ri.value = item['value']
|
686
|
+
|
687
|
+
items.push ri
|
688
|
+
end
|
689
|
+
end
|
690
|
+
|
691
|
+
items
|
692
|
+
end
|
693
|
+
|
694
|
+
# Builds a compatible String to use in service executions
|
695
|
+
# @return compatible String
|
696
|
+
def self.build_string(conditions, group_by, order_by, projection)
|
697
|
+
built = '{' + (projection ? projection : '') + '"where": {'
|
698
|
+
|
699
|
+
if conditions.length > 0
|
700
|
+
global = conditions[0]
|
701
|
+
if conditions.length > 1
|
702
|
+
conditions = conditions[1..conditions.length]
|
703
|
+
conditions.each do |condition|
|
704
|
+
global = Dynamicloud::API::Criteria::ANDCondition.new global, condition
|
705
|
+
end
|
706
|
+
end
|
707
|
+
|
708
|
+
built = built + global.to_record_string(Dynamicloud::API::Criteria::Condition::ROOT)
|
709
|
+
end
|
710
|
+
|
711
|
+
built = built + '}'
|
712
|
+
|
713
|
+
if group_by
|
714
|
+
built = built + ',' + group_by.to_record_string(Dynamicloud::API::Criteria::Condition::ROOT)
|
715
|
+
end
|
716
|
+
|
717
|
+
if order_by
|
718
|
+
built = built + ',' + order_by.to_record_string(Dynamicloud::API::Criteria::Condition::ROOT)
|
719
|
+
end
|
720
|
+
|
721
|
+
built + '}'
|
722
|
+
end
|
723
|
+
|
724
|
+
# Build a compatible string using projection
|
725
|
+
# @return string using projection
|
726
|
+
def self.build_projection(projection)
|
727
|
+
unless (projection) && (projection.length > 0)
|
728
|
+
return ''
|
729
|
+
end
|
730
|
+
|
731
|
+
columns = '"columns": ['
|
732
|
+
cols = ''
|
733
|
+
projection.each do |field|
|
734
|
+
cols = cols + (cols == '' ? '' : ',') + '"' + field + '"'
|
735
|
+
end
|
736
|
+
|
737
|
+
columns + cols + '], '
|
738
|
+
end
|
739
|
+
|
740
|
+
# This utility will build a RecordResults object
|
741
|
+
# @param response ServiceResponse from Dynamicloud servers
|
742
|
+
def self.build_record_results(response)
|
743
|
+
results = Dynamicloud::API::RecordResults.new
|
744
|
+
|
745
|
+
json = JSON.parse(response)
|
746
|
+
|
747
|
+
data = json['records']
|
748
|
+
|
749
|
+
results.total_records = data['total']
|
750
|
+
results.fast_returned_size = data['size']
|
751
|
+
results.records = get_record_list(data)
|
752
|
+
|
753
|
+
results
|
754
|
+
end
|
755
|
+
|
756
|
+
# This method will extract data and Bind each field with attributes in mapper:getInstance method instance
|
757
|
+
# @param data json with all data from Dynamicloud servers
|
758
|
+
# @return list of records
|
759
|
+
def self.get_record_list(data)
|
760
|
+
record_list = []
|
761
|
+
|
762
|
+
records = data['records']
|
763
|
+
|
764
|
+
records.each do |jr|
|
765
|
+
record_list.push(build_record(jr))
|
766
|
+
end
|
767
|
+
|
768
|
+
record_list
|
769
|
+
end
|
770
|
+
|
771
|
+
# Builds the record hash with data from jr JSON object
|
772
|
+
def self.build_record(jr)
|
773
|
+
record = {}
|
774
|
+
|
775
|
+
jr.each do |key, value|
|
776
|
+
if value.is_a?(Hash)
|
777
|
+
value.each do |k, v|
|
778
|
+
if v.respond_to?(:each)
|
779
|
+
values = []
|
780
|
+
v.each do |item|
|
781
|
+
values.push(item.to_s)
|
782
|
+
end
|
783
|
+
|
784
|
+
record[key] = values
|
785
|
+
else
|
786
|
+
record[key] = value
|
787
|
+
end
|
788
|
+
|
789
|
+
break
|
790
|
+
end
|
791
|
+
else
|
792
|
+
record[key] = value
|
793
|
+
end
|
794
|
+
end
|
795
|
+
|
796
|
+
record
|
797
|
+
end
|
798
|
+
end
|
799
|
+
end
|
800
|
+
end
|