lorj 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +46 -0
  4. data/Gemfile +15 -15
  5. data/README.md +22 -17
  6. data/Rakefile +12 -2
  7. data/bin/cloud_test.rb +13 -65
  8. data/example/students_1/process/students.rb +39 -0
  9. data/example/students_1/students.rb +22 -5
  10. data/example/students_2/process/students.rb +48 -0
  11. data/example/students_2/students.rb +39 -16
  12. data/example/students_3/controller/yaml_students.rb +50 -43
  13. data/example/students_3/controller/yaml_students_controller.rb +100 -87
  14. data/example/students_3/process/students.rb +161 -97
  15. data/example/students_3/students.rb +85 -56
  16. data/example/yaml_students/students.rb +40 -40
  17. data/example/yaml_students/yaml_students.rb +103 -90
  18. data/lib/core/core.rb +356 -696
  19. data/lib/core/core_controller.rb +227 -0
  20. data/lib/core/core_internal.rb +339 -0
  21. data/lib/core/core_model.rb +328 -0
  22. data/lib/core/core_object_data.rb +330 -0
  23. data/lib/core/core_object_params.rb +230 -0
  24. data/lib/core/core_process.rb +391 -0
  25. data/lib/core/core_process_setup.rb +353 -0
  26. data/lib/core/core_setup_ask.rb +241 -0
  27. data/lib/core/core_setup_encrypt.rb +146 -0
  28. data/lib/core/core_setup_init.rb +229 -0
  29. data/lib/core/core_setup_list.rb +160 -0
  30. data/lib/core/definition.rb +647 -469
  31. data/lib/core/definition_internal.rb +264 -308
  32. data/lib/core/lorj_basecontroller.rb +95 -0
  33. data/lib/core/lorj_basedefinition.rb +307 -0
  34. data/lib/core/lorj_baseprocess.rb +265 -0
  35. data/lib/core/lorj_data.rb +583 -0
  36. data/lib/core/lorj_keypath.rb +119 -0
  37. data/lib/core_process/cloud/process/common.rb +63 -0
  38. data/lib/core_process/cloud/process/connection.rb +93 -0
  39. data/lib/core_process/cloud/process/external_network.rb +94 -0
  40. data/lib/core_process/cloud/process/flavor.rb +99 -0
  41. data/lib/core_process/cloud/process/images.rb +87 -0
  42. data/lib/core_process/cloud/process/internet_network.rb +34 -0
  43. data/lib/core_process/cloud/process/internet_server.rb +30 -0
  44. data/lib/core_process/cloud/process/keypairs.rb +276 -0
  45. data/lib/core_process/cloud/process/network.rb +108 -0
  46. data/lib/core_process/cloud/process/public_ip.rb +100 -0
  47. data/lib/core_process/cloud/process/router.rb +260 -0
  48. data/lib/core_process/cloud/process/rules.rb +120 -0
  49. data/lib/core_process/cloud/process/security_groups.rb +121 -0
  50. data/lib/core_process/cloud/process/server.rb +127 -0
  51. data/lib/core_process/cloud/process/server_log.rb +35 -0
  52. data/lib/core_process/cloud/process/subnetwork.rb +108 -0
  53. data/lib/core_process/cloud_process.rb +30 -0
  54. data/lib/logging.rb +298 -0
  55. data/lib/lorj/version.rb +18 -1
  56. data/lib/lorj.rb +58 -18
  57. data/lib/lorj_account.rb +556 -0
  58. data/lib/lorj_config.rb +468 -0
  59. data/lib/lorj_defaults.rb +278 -0
  60. data/lib/prc.rb +136 -104
  61. data/lib/prc_base_config.rb +285 -0
  62. data/lib/prc_core_config.rb +878 -0
  63. data/lib/prc_section_config.rb +57 -0
  64. data/lib/providers/hpcloud/compute.rb +81 -93
  65. data/lib/providers/hpcloud/hpcloud.rb +462 -0
  66. data/lib/providers/hpcloud/network.rb +96 -98
  67. data/lib/providers/hpcloud/security_groups.rb +41 -40
  68. data/lib/providers/mock/mock.rb +144 -0
  69. data/lib/providers/openstack/openstack.rb +45 -0
  70. data/lib/providers/templates/compute.rb +21 -23
  71. data/lib/providers/templates/mycloud.rb +72 -0
  72. data/lib/providers/templates/network.rb +11 -12
  73. data/lib/rh.rb +339 -0
  74. data/lorj-spec/defaults.yaml +4 -0
  75. data/lorj.gemspec +6 -0
  76. data/spec/00_lorj_log_spec.rb +53 -0
  77. data/spec/01_hash_rh_spec.rb +243 -0
  78. data/spec/02_prc_base_config_spec.rb +216 -0
  79. data/spec/04_prc_core_config_spec.rb +83 -0
  80. data/spec/11_lorj_config_spec.rb +263 -0
  81. data/spec/12_lorj_account_spec.rb +181 -0
  82. metadata +76 -28
  83. data/Gemfile.lock +0 -37
  84. data/example/students_1/process/Students.rb +0 -20
  85. data/example/students_2/process/Students.rb +0 -27
  86. data/example/students_4/controller/yaml_students.rb +0 -82
  87. data/example/students_4/controller/yaml_students_controller.rb +0 -141
  88. data/example/students_4/process/students.rb +0 -112
  89. data/example/students_4/students.rb +0 -103
  90. data/lib/core/lorj-basecontroller.rb +0 -90
  91. data/lib/core/lorj-basedefinition.rb +0 -1139
  92. data/lib/core/lorj-baseprocess.rb +0 -236
  93. data/lib/core/lorj-data.rb +0 -567
  94. data/lib/core/lorj-keypath.rb +0 -115
  95. data/lib/core_process/CloudProcess.rb +0 -337
  96. data/lib/core_process/global_process.rb +0 -502
  97. data/lib/core_process/network_process.rb +0 -605
  98. data/lib/prc-account.rb +0 -339
  99. data/lib/prc-config.rb +0 -1030
  100. data/lib/prc-logging.rb +0 -261
  101. data/lib/providers/hpcloud/Hpcloud.rb +0 -426
  102. data/lib/providers/mock/Mock.rb +0 -141
  103. data/lib/providers/openstack/Openstack.rb +0 -47
  104. data/lib/providers/templates/core.rb +0 -61
  105. data/spec/forj-account_spec.rb +0 -75
  106. data/spec/forj-config_spec.rb +0 -196
@@ -1,1139 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- # (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
- # Module Lorj which contains several classes.
18
- #
19
- # Those classes describes :
20
- # - processes (BaseProcess) : How to create/delete/edit/query object.
21
- # - controler (BaseControler) : If a provider is defined, define how will do object creation/etc...
22
- # - definition(BaseDefinition): Functions to declare objects, query/data mapping and setup
23
- # this task to make it to work.
24
-
25
- require 'highline/import'
26
- require 'erb'
27
-
28
- module Lorj
29
- # This class limits ERC template to access only to config object data.
30
- class ERBConfig
31
- attr_reader :config
32
-
33
- def initialize(config)
34
- @config = config
35
- end
36
-
37
- # Bind this limited class with ERB templates
38
- def get_binding()
39
- binding()
40
- end
41
- end
42
-
43
- # Following class defines class levels function to
44
- # declare framework objects.
45
- # As each process needs to define new object to deal with
46
- # require that process to define it with definition functions
47
- # See definition.rb for functions to use.
48
-
49
- class BaseDefinition
50
- # Capitalized function are called to start a process. It is done by Core class.
51
-
52
- # Call meta lorj object creation process.
53
- # The creation process can implement any logic like:
54
- # - create an object in the DB.
55
- # - check object existence in the DB. If not exists, create it.
56
- #
57
- # * *Args* :
58
- # - +ObjectType+ : Meta object type to create.
59
- # - +Config+ : Optional. Hash containing list of data to use for creation.
60
- #
61
- # * *Returns* :
62
- # - +Object+ : Lorj::Data object of type +ObjectType+ created.
63
- #
64
- # * *Raises* :
65
- # - Warning if the create_e process handler did not return any data. (nil)
66
- # - Warning if the Config data passed are not required by the meta object (or dependencies) at creation time.
67
- # - Error if ObjectType has never been declared.
68
- # - Error if the dependencies create_e process handler did not return any data. (nil) - Loop detection.
69
- # - Error if the create_e process handler raise an error.
70
- #
71
- def Create(sObjectType, hConfig = {})
72
-
73
- if hConfig.length > 0
74
- # cleanup runtime data to avoid conflicts between multiple calls
75
- valid_keys = _identify_data(sObjectType, :create_e)
76
- valid_keys.each { | sKey|
77
- value = Lorj::rhGet(hConfig, sKey)
78
- @oForjConfig[sKey] = value if not @oForjConfig.exist?(sKey) or @oForjConfig.exist?(sKey) == 'runtime'
79
- Lorj::rhSet(hConfig, nil, sKey)
80
- }
81
- if hConfig.length > 0
82
- PrcLib::warning("'%s' has been passed but not declared to '%s' object. Those are ignored until you add it in the model definition of '%s'" % [hConfig.keys, sObjectType, sObjectType])
83
- end
84
- end
85
-
86
- return nil if not sObjectType
87
- raise Lorj::PrcError.new(), "%s.Create: '%s' is not a known object type." % [self.class, sObjectType] if Lorj::rhExist?(@@meta_obj, sObjectType) != 1
88
-
89
- pProc = Lorj::rhGet(@@meta_obj, sObjectType, :lambdas, :create_e)
90
-
91
- # Check required parameters
92
- oObjMissing = _check_required(sObjectType, :create_e, pProc).reverse
93
-
94
- while oObjMissing.length >0
95
- sElem = oObjMissing.pop
96
-
97
- raise Lorj::PrcError.new(),"Unable to create Object '%s'" % sElem if not Create(sElem)
98
- oObjMissing = _check_required(sObjectType, :create_e, pProc).reverse
99
-
100
- raise Lorj::PrcError.new(), "loop detection: '%s' is required but Create(%s) did not loaded it." % [sElem, sElem] if oObjMissing.include?(sElem)
101
- end
102
- @RuntimeContext[:oCurrentObj] = sObjectType # Context: Default object used.
103
-
104
- if pProc.nil?
105
- # This object is a meta object, without any data.
106
- # Used to build other kind of objects.
107
- oObject = Lorj::Data.new
108
- oObject.set({}, sObjectType) {}
109
- else
110
- # build Function params to pass to the event handler.
111
- aParams = _get_object_params(sObjectType, :create_e, pProc,)
112
- Lorj.debug(2, "Create Object '%s' - Running '%s'" % [sObjectType, pProc])
113
-
114
- # Call the process function.
115
- # At some point, the process will call the controller, via the framework.
116
- # This controller call via the framework has the role to
117
- # create an ObjectData well formatted, with _return_map function
118
- # See Definition.connect/create/update/query/get functions (lowercase)
119
- oObject = @oForjProcess.method(pProc).call(sObjectType, aParams)
120
- # return usually is the main object that the process called should provide.
121
- # Save Object if the object has been created by the process, without controller
122
- end
123
-
124
- unless oObject.nil?
125
- query_cleanup(sObjectType)
126
- @ObjectData.add(oObject)
127
- else
128
- PrcLib::warning("'%s' has returned no data for object Lorj::Data '%s'!" % [pProc, sObjectType])
129
- end
130
- end
131
-
132
- # Call meta lorj object deletion process
133
- # There is no implementation of cascade deletion. It is up to the process to do it or not.
134
- #
135
- # * *Args* :
136
- # - +ObjectType+ : Meta object type to create.
137
- # - +Config+ : Optional. Hash containing list of data to use for creation.
138
- #
139
- # * *Returns* :
140
- # - +Deleted+ : true if deleted or false otherwise.
141
- #
142
- # * *Raises* :
143
- # - Warning if the create_e process handler did not return any data. (nil)
144
- # - Warning if the Config data passed are not required by the meta object (or dependencies) at creation time.
145
- # - Error if ObjectType has never been declared.
146
- # - Error if the dependencies query_e process handler did not return any data. (nil) - Loop detection.
147
- # - Error if the query_e process handler raise an error.
148
- #
149
- def Delete(sCloudObj, hConfig = {})
150
- return nil if not sCloudObj
151
-
152
- raise Lorj::PrcError.new(), "%s.Delete: '%s' is not a known object type." % [self.class, sCloudObj] if Lorj::rhExist?(@@meta_obj, sCloudObj) != 1
153
-
154
- if hConfig.length > 0
155
- # cleanup runtime data to avoid conflicts between multiple calls
156
- valid_keys = _identify_data(sCloudObj, :delete_e)
157
- valid_keys.each { | sKey|
158
- value = Lorj::rhGet(hConfig, sKey)
159
- @oForjConfig[sKey] = value if not @oForjConfig.exist?(sKey) or @oForjConfig.exist?(sKey) == 'runtime'
160
- Lorj::rhSet(hConfig, nil, sKey)
161
- }
162
- if hConfig.length > 0
163
- PrcLib::warning("'%s' has been passed but not declared to '%s' object. Those are ignored until you add it in the model definition of '%s'" % [hConfig.keys, sCloudObj, sCloudObj])
164
- end
165
- end
166
-
167
- pProc = Lorj::rhGet(@@meta_obj, sCloudObj, :lambdas, :delete_e)
168
-
169
- return nil if pProc.nil?
170
-
171
- # Check required parameters
172
- oObjMissing = _check_required(sCloudObj, :delete_e, pProc).reverse
173
-
174
- while oObjMissing.length >0
175
- sElem = oObjMissing.pop
176
- if sElem == sCloudObj
177
- debug(2, "'%s' object is not previously loaded or created." % sCloudObj) if sElem == sCloudObj
178
- next
179
- end
180
- raise Lorj::PrcError.new(),"Unable to create Object '%s'" % sElem if not Create(sElem)
181
- oObjMissing = _check_required(sCloudObj, :delete_e, pProc).reverse
182
- raise Lorj::PrcError.new(), "Unable to delete '%s' object. required '%s' data was not loaded after 'Create(%s)'. Check create handler for object type '%s'." % [sCloudObj, sElem, sElem, sElem] if oObjMissing.include?(sElem)
183
- end
184
- @RuntimeContext[:oCurrentObj] = sCloudObj # Context: Default object used.
185
-
186
- # build Function params to pass to the event handler.
187
- aParams = _get_object_params(sCloudObj, :delete_e, pProc)
188
-
189
- bState = @oForjProcess.method(pProc).call(sCloudObj, aParams)
190
- # return usually is the main object that the process called should provide.
191
- if bState
192
- @ObjectData.delete(sCloudObj)
193
- end
194
-
195
- end
196
-
197
- # Function to clean the cache for a specific meta lorj object queried.
198
- #
199
- # * *Args* :
200
- # - +ObjectType+ : Meta object type to cleanup.
201
- #
202
- # * *Returns* :
203
- # no data
204
- #
205
- # * *Raises* :
206
- #
207
- def query_cleanup(sCloudObj)
208
- oList = @ObjectData[:query, sCloudObj]
209
- unless oList.nil?
210
- @ObjectData.delete(oList)
211
- Lorj.debug(2, "Query cache for object '%s' cleaned." % sCloudObj)
212
- end
213
- end
214
-
215
- # Function to clean the cache for a specific meta lorj object.
216
- #
217
- # * *Args* :
218
- # - +ObjectType+ : Meta object type to cleanup.
219
- #
220
- # * *Returns* :
221
- # no data
222
- #
223
- # * *Raises* :
224
- #
225
- def object_cleanup(sCloudObj)
226
- oObject = @ObjectData[sCloudObj, :ObjectData]
227
- unless oObject.nil?
228
- @ObjectData.delete(oObject)
229
- end
230
- end
231
-
232
- # Function to execute a query process. This function returns a Lorj::Data of type :list.
233
- #
234
- # * *Args* :
235
- # - +ObjectType+ : Meta object type to query.
236
- # - +Query+ : Hash. Represent the query to execute.
237
- # - +Config+ : Optional. Hash containing list of data to use for query.
238
- #
239
- # * *Returns* :
240
- # Lorj::Data of type :list
241
- #
242
- # * *Raises* :
243
- #
244
- #
245
- def Query(sCloudObj, hQuery, hConfig = {})
246
-
247
- return nil if not sCloudObj
248
-
249
- raise Lorj::PrcError.new(), "%s.Get: '%s' is not a known object type." % [self.class, sCloudObj] if Lorj::rhExist?(@@meta_obj, sCloudObj) != 1
250
-
251
- if hConfig.length > 0
252
- # cleanup runtime data to avoid conflicts between multiple calls
253
- valid_keys = _identify_data(sCloudObj, :query_e)
254
- valid_keys.each { | sKey|
255
- value = Lorj::rhGet(hConfig, sKey)
256
- @oForjConfig[sKey] = value if not @oForjConfig.exist?(sKey) or @oForjConfig.exist?(sKey) == 'runtime'
257
- Lorj::rhSet(hConfig, nil, sKey)
258
- }
259
- if hConfig.length > 0
260
- PrcLib::warning("'%s' has been passed but not declared to '%s' object. Those are ignored until you add it in the model definition of '%s'" % [hConfig.keys, sCloudObj, sCloudObj])
261
- end
262
- end
263
-
264
- # Check if we can re-use a previous query
265
- oList = @ObjectData[:query, sCloudObj]
266
- unless oList.nil?
267
- if oList[:query] == hQuery
268
- Lorj.debug(3, "Using Object '%s' query cache : %s" % [sCloudObj, hQuery])
269
- return oList
270
- end
271
- end
272
-
273
- pProc = Lorj::rhGet(@@meta_obj, sCloudObj, :lambdas, :query_e)
274
-
275
- return nil if pProc.nil?
276
-
277
- # Check required parameters
278
- oObjMissing = _check_required(sCloudObj, :query_e, pProc).reverse
279
-
280
- while oObjMissing.length >0
281
- sElem = oObjMissing.pop
282
- raise Lorj::PrcError.new(),"Unable to create Object '%s'" % sElem if not Create(sElem)
283
- oObjMissing = _check_required(sCloudObj, :query_e, pProc).reverse
284
- raise Lorj::PrcError.new(), "loop detection: '%s' is required but Query(%s) did not loaded it." % [sElem, sElem] if oObjMissing.include?(sElem)
285
- end
286
- @RuntimeContext[:oCurrentObj] = sCloudObj # Context: Default object used.
287
-
288
- # build Function params to pass to the Process Event handler.
289
- aParams = _get_object_params(sCloudObj, :query_e, pProc)
290
-
291
- # Call the process function.
292
- # At some point, the process will call the controller, via the framework.
293
- # This controller call via the framework has the role to
294
- # create an ObjectData well formatted, with _return_map function
295
- # See Definition.connect/create/update/query/get functions (lowercase)
296
- oObject = @oForjProcess.method(pProc).call(sCloudObj, hQuery, aParams)
297
- # return usually is the main object that the process called should provide.
298
- unless oObject.nil?
299
- # Save Object if the object has been created by the process, without controller
300
- @ObjectData.add(oObject)
301
- else
302
- PrcLib::warning("'%s' returned no collection of objects Lorj::Data for '%s'" % [pProc, sCloudObj])
303
- end
304
- end
305
-
306
- # Function to execute a get process. This function returns a Lorj::Data of type :object.
307
- #
308
- # * *Args* :
309
- # - +ObjectType+ : Meta object type to query.
310
- # - +UniqId+ : Uniq ID.
311
- # - +Config+ : Optional. Hash containing list of data to use for getting.
312
- #
313
- # * *Returns* :
314
- # Lorj::Data of type :object
315
- #
316
- # * *Raises* :
317
- # - Warning if the Config data passed are not required by the meta object (or dependencies) at creation time.
318
- # - Error if ObjectType has never been declared.
319
- # - Error if the dependencies get_e process handler did not return any data. (nil) - Loop detection.
320
- # - Error if the get_e process handler raise an error.
321
- #
322
- def Get(sCloudObj, sUniqId, hConfig = {})
323
-
324
- return nil if not sCloudObj
325
-
326
- raise Lorj::PrcError.new(), "$s.Get: '%s' is not a known object type." % [self.class, sCloudObj] if Lorj::rhExist?(@@meta_obj, sCloudObj) != 1
327
-
328
- if hConfig.length > 0
329
- # cleanup runtime data to avoid conflicts between multiple calls
330
- valid_keys = _identify_data(sCloudObj, :get_e)
331
- valid_keys.each { | sKey|
332
- value = Lorj::rhGet(hConfig, sKey)
333
- @oForjConfig[sKey] = value if not @oForjConfig.exist?(sKey) or @oForjConfig.exist?(sKey) == 'runtime'
334
- Lorj::rhSet(hConfig, nil, sKey)
335
- }
336
- if hConfig.length > 0
337
- Lorj::warning("'%s' has been passed but not declared to '%s' object. Those are ignored until you add it in the model definition of '%s'" % [hConfig.keys, sCloudObj, sCloudObj])
338
- end
339
- end
340
-
341
- pProc = Lorj::rhGet(@@meta_obj, sCloudObj, :lambdas, :get_e)
342
-
343
- return nil if pProc.nil?
344
-
345
- # Check required parameters
346
- oObjMissing = _check_required(sCloudObj, :get_e, pProc).reverse
347
-
348
- while oObjMissing.length >0
349
- sElem = oObjMissing.pop
350
- raise Lorj::PrcError.new(),"Unable to create Object '%s'" % sElem if not Create(sElem)
351
- oObjMissing = _check_required(sCloudObj, :get_e, pProc).reverse
352
- raise Lorj::PrcError.new(), "loop detection: '%s' is required but Get(%s) did not loaded it." % [sElem, sElem] if oObjMissing.include?(sElem)
353
- end
354
- @RuntimeContext[:oCurrentObj] = sCloudObj # Context: Default object used.
355
-
356
- # build Function params to pass to the Process Event handler.
357
- aParams = _get_object_params(sCloudObj, :get_e, pProc)
358
-
359
- # Call the process function.
360
- # At some point, the process will call the controller, via the framework.
361
- # This controller call via the framework has the role to
362
- # create an ObjectData well formatted, with _return_map function
363
- # See Definition.connect/create/update/query/get functions (lowercase)
364
- oObject = @oForjProcess.method(pProc).call(sCloudObj, sUniqId, aParams)
365
- # return usually is the main object that the process called should provide.
366
- unless oObject.nil?
367
- # Save Object if the object has been created by the process, without controller
368
- @ObjectData.add(oObject)
369
- end
370
- end
371
-
372
- # Function to execute a update process. This function returns a Lorj::Data of type :object.
373
- #
374
- # * *Args* :
375
- # - +ObjectType+ : Meta object type to query.
376
- # - +Config+ : Optional. Hash containing list of data to use for updating.
377
- #
378
- # * *Returns* :
379
- # Lorj::Data of type :object
380
- #
381
- # * *Raises* :
382
- # - Warning if the Config data passed are not required by the meta object (or dependencies) at creation time.
383
- # - Error if ObjectType has never been declared.
384
- # - Error if the dependencies get_e process handler did not return any data. (nil) - Loop detection.
385
- # - Error if the get_e process handler raise an error.
386
- #
387
- def Update(sCloudObj, hConfig = {})
388
-
389
- return nil if not sCloudObj
390
-
391
- raise Lorj::PrcError.new(), "$s.Update: '%s' is not a known object type." % [self.class, sCloudObj] if Lorj::rhExist?(@@meta_obj, sCloudObj) != 1
392
-
393
- if hConfig.length > 0
394
- # cleanup runtime data to avoid conflicts between multiple calls
395
- valid_keys = _identify_data(sCloudObj, :update_e)
396
- valid_keys.each { | sKey|
397
- value = Lorj::rhGet(hConfig, sKey)
398
- @oForjConfig[sKey] = value if not @oForjConfig.exist?(sKey) or @oForjConfig.exist?(sKey) == 'runtime'
399
- Lorj::rhSet(hConfig, nil, sKey)
400
- }
401
- if hConfig.length > 0
402
- PrcLib::warning("'%s' has been passed but not declared to '%s' object. Those are ignored until you add it in the model definition of '%s'" % [hConfig.keys, sCloudObj, sCloudObj])
403
- end
404
- end
405
-
406
- pProc = Lorj::rhGet(@@meta_obj, sCloudObj, :lambdas, :update_e)
407
-
408
- return nil if pProc.nil?
409
-
410
- # Check required parameters
411
- oObjMissing = _check_required(sCloudObj, :update_e, pProc).reverse
412
-
413
- while oObjMissing.length >0
414
- sElem = oObjMissing.pop
415
- raise Lorj::PrcError.new(),"Unable to create Object '%s'" % sElem if not Create(sElem)
416
- oObjMissing = _check_required(sCloudObj, :update_e, pProc).reverse
417
- raise Lorj::PrcError.new(), "loop detection: '%s' is required but Update(%s) did not loaded it." % [sElem, sElem] if oObjMissing.include?(sElem)
418
- end
419
- @RuntimeContext[:oCurrentObj] = sCloudObj # Context: Default object used.
420
-
421
- # build Function params to pass to the event handler.
422
- aParams = _get_object_params(sCloudObj, :update_e, pProc)
423
-
424
- oObject = @oForjProcess.method(pProc).call(sCloudObj, aParams)
425
- # return usually is the main object that the process called should provide.
426
- unless oObject.nil?
427
- # Save Object if the object has been created by the process, without controller
428
- @ObjectData.add(oObject)
429
- end
430
- end
431
-
432
- # Function to execute a setup process.
433
- # The setup process will ask the end user to enter values as defined by the defaults.yaml application defaults.
434
- # For setup features implemented, see Lorj::BaseDefinition::meta_data class variable definition.
435
- #
436
- # Setup by default, will ask any kind of data required by a meta object and all dependencies.
437
- # If the setup needs to add some extra fields to ask to the user, set it in /:setup/:ask_step/*:add/* section
438
- #
439
- # Setup uses a Config model object which requires to have at least following functions:
440
- # - value = get(key, default)
441
- # - set(key, value)
442
- #
443
- # Setup keep end user data in memory. After setup, you HAVE to save them
444
- # If you are using Lorj::Account, use function ac_save
445
- # If you are using Lorj::Config, use function configSave
446
- #
447
- # * *Args* :
448
- # - +ObjectType+ : Meta object type to query.
449
- # - +Config+ : Optional. Hash containing list of data to use for updating.
450
- #
451
- # * *Returns* :
452
- # Lorj::Data of type :object
453
- #
454
- # * *Raises* :
455
- #
456
- def Setup(sObjectType, sAccountName)
457
- # Loop in dependencies to get list of data object to setup
458
- raise Lorj::PrcError,new(), "Setup: '%s' not a valid object type." if Lorj::rhExist?(@@meta_obj, sObjectType) != 1
459
-
460
- hAskStep = Lorj::Default.get(:ask_step, :setup)
461
- aSetup = []
462
- hAskStep.each{ | value |
463
- aSetup << {
464
- :desc => value[:desc],
465
- :explanation => value[:explanation],
466
- :pre_step_handler => value[:pre_step_function],
467
- :order => [[]],
468
- :post_step_handler => value[:post_step_function]
469
- }
470
-
471
- }
472
- oInspectedObjects = []
473
- oInspectObj = [sObjectType]
474
-
475
- @oForjConfig.ac_load(sAccountName) if sAccountName
476
-
477
- Lorj.debug(2, "Setup is identifying account data to ask for '%s'" % sObjectType)
478
- while oInspectObj.length() >0
479
- # Identify data to ask
480
- # A data to ask is a data needs from an object type
481
- # which is declared in section of defaults.yaml
482
- # and is declared :account to true (in defaults.yaml or in process declaration - define_data)
483
-
484
- sObjectType = oInspectObj.pop
485
- sAsk_step = 0
486
- Lorj.debug(1, "Checking '%s'" % sObjectType)
487
- hTopParams = Lorj::rhGet(@@meta_obj,sObjectType, :params)
488
- if hTopParams[:keys].nil?
489
- Lorj.debug(1, "Warning! Object '%s' has no data/object needs. Check the process" % sObjectType)
490
- next
491
- end
492
- hTopParams[:keys].each { |sKeypath, hParams|
493
- oKeyPath = KeyPath.new(sKeypath)
494
- sKey = oKeyPath.sKey
495
- case hParams[:type]
496
- when :data
497
- hMeta = _get_meta_data(sKey)
498
- next if hMeta.nil?
499
- sAsk_step = hMeta[:ask_step] if Lorj::rhExist?(hMeta, :ask_step) == 1 and hMeta[:ask_step].is_a?(Fixnum)
500
- Lorj.debug(3, "#{sKey} is part of setup step #{sAsk_step}")
501
- aOrder = aSetup[sAsk_step][:order]
502
-
503
- if oInspectedObjects.include?(sKey)
504
- Lorj.debug(2, "#{sKey} is already asked. Ignored.")
505
- next
506
- end
507
- if hMeta[:account].is_a?(TrueClass)
508
- if not hMeta[:depends_on].is_a?(Array)
509
- PrcLib.warning("'%s' depends_on definition have to be an array." % oKeyPath.sFullPath) unless hMeta[:depends_on].nil?
510
- iLevel = 0
511
- bFound = true
512
- else
513
- # Searching highest level from dependencies.
514
- bFound = false
515
- iLevel = 0
516
- hMeta[:depends_on].each { |depend_key|
517
- aOrder.each_index { |iCurLevel|
518
- if aOrder[iCurLevel].include?(depend_key)
519
- bFound = true
520
- iLevel = [iLevel, iCurLevel + 1].max
521
- end
522
- }
523
- aOrder[iLevel] = [] if aOrder[iLevel].nil?
524
- }
525
- end
526
- if bFound
527
- if not aOrder[iLevel].include?(sKey)
528
- if hMeta[:ask_sort].is_a?(Fixnum)
529
- iOrder = hMeta[:ask_sort]
530
- if aOrder[iLevel][iOrder].nil?
531
- aOrder[iLevel][iOrder] = sKey
532
- else
533
- aOrder[iLevel].insert(iOrder, sKey)
534
- end
535
- Lorj.debug(3, "S%s/L%s/O%s: '%s' added in setup list. " % [sAsk_step, iLevel, iOrder, sKey])
536
- else
537
- aOrder[iLevel] << sKey
538
- Lorj.debug(3, "S%s/L%s/Last: '%s' added in setup list." % [sAsk_step, iLevel, sKey])
539
- end
540
- end
541
- end
542
- oInspectedObjects << sKey
543
- else
544
- Lorj.debug(2, "#{sKey} used by #{sObjectType} won't be asked during setup. :account = true not set.")
545
- end
546
- when :CloudObject
547
- oInspectObj << sKey if not oInspectObj.include?(sKey) and not oInspectedObjects.include?(sKey)
548
- end
549
- }
550
- oInspectedObjects << sObjectType
551
- end
552
- Lorj.debug(2, "Setup check if needs to add unrelated data in the process")
553
- hAskStep.each_index{ | iStep |
554
- value = hAskStep[iStep]
555
- if Lorj::rhExist?(value, :add) == 1
556
- sKeysToAdd = Lorj::rhGet(value, :add)
557
- sKeysToAdd.each { | sKeyToAdd |
558
- bFound = false
559
- aSetup[iStep][:order].each_index { | iOrder |
560
- sKeysToAsk = aSetup[iStep][:order][iOrder]
561
- unless sKeysToAsk.index(sKeyToAdd).nil?
562
- bFound = true
563
- break
564
- end
565
- }
566
- next if bFound
567
- iLevel = 0
568
- iOrder = aSetup[iStep][:order].length
569
- iAtStep = iStep
570
- hMeta = _get_meta_data(sKeyToAdd)
571
- if Lorj::rhExist?(hMeta, :after) == 1
572
- sAfterKeys = hMeta[:after]
573
- sAfterKeys = [ sAfterKeys ] if not sAfterKeys.is_a?(Array)
574
- sAfterKeys.each{ |sAfterKey |
575
- bFound = false
576
- aSetup.each_index { |iStepToCheck|
577
- aSetup[iStepToCheck][:order].each_index { | iLevelToCheck |
578
- sKeysToAsk = aSetup[iStepToCheck][:order][iLevelToCheck]
579
- iOrderToCheck = sKeysToAsk.index(sAfterKey)
580
- unless iOrderToCheck.nil?
581
- iAtStep = iStepToCheck if iStepToCheck > iAtStep
582
- iLevel = iLevelToCheck if iLevelToCheck > iLevel
583
- iOrder = iOrderToCheck + 1 if iOrderToCheck + 1 > iOrder
584
- bFound = true
585
- break
586
- end
587
- }
588
- }
589
- }
590
- end
591
- aSetup[iAtStep][:order][iLevel].insert(iOrder, sKeyToAdd)
592
- Lorj.debug(3, "S%s/L%s/O%s: '%s' added in setup list at position." % [iAtStep, iLevel, iOrder, sKeyToAdd])
593
- }
594
- end
595
- }
596
-
597
- Lorj.debug(2, "Setup will ask for :\n %s" % aSetup.to_yaml)
598
-
599
- PrcLib.info("Configuring account : '#{config[:account_name]}', provider '#{config[:provider_name]}'")
600
-
601
- # Ask for user input
602
- aSetup.each_index { | iStep |
603
- Lorj.debug(2, "Ask step %s:" % iStep)
604
- puts "%s%s%s" % [ANSI.bold, aSetup[iStep][:desc], ANSI.clear] unless aSetup[iStep][:desc].nil?
605
- begin
606
- puts "%s\n\n" % ANSI.yellow(erb(aSetup[iStep][:explanation])) unless aSetup[iStep][:explanation].nil?
607
- rescue => e
608
- PrcLib.Error "setup step '%d/:explanation' : %s" % [iStep, e.message]
609
- end
610
- aOrder = aSetup[iStep][:order]
611
- aOrder.each_index { | iIndex |
612
- Lorj.debug(2, "Ask order %s:" % iIndex)
613
- aOrder[iIndex].each { | sKey |
614
- hParam = _get_meta_data(sKey)
615
- hParam = {} if hParam.nil?
616
-
617
- bOk = false
618
-
619
- if hParam[:pre_step_function]
620
- pProc = hParam[:pre_step_function]
621
- bOk = not(@oForjProcess.method(pProc).call(sKey))
622
- end
623
-
624
-
625
- sDesc = "'%s' value" % sKey
626
- begin
627
- puts "#{sKey}: %s" % [erb(hParam[:explanation])] unless hParam[:explanation].nil?
628
- rescue => e
629
- PrcLib.Error "setup key '%s/:explanation' : %s" % [sKey, e.message]
630
- end
631
- begin
632
- sDesc = erb(hParam[:desc]) unless hParam[:desc].nil?
633
- rescue => e
634
- PrcLib.Error "setup key '%s/:desc' : %s" % [sKey, e.message]
635
- end
636
- sDefault = @oForjConfig.get(sKey, hParam[:default_value])
637
- rValidate = nil
638
-
639
- rValidate = hParam[:validate] unless hParam[:validate].nil?
640
- bRequired = (hParam[:required] == true)
641
- while not bOk
642
- bOk = true
643
- if not hParam[:list_values].nil?
644
- hValues = hParam[:list_values]
645
- sObjectToLoad = hValues[:object]
646
-
647
- bListStrict = (hValues[:validate] == :list_strict)
648
-
649
- case hValues[:query_type]
650
- when :controller_call
651
- oObject = @ObjectData[sObjectToLoad, :ObjectData]
652
- PrcLib.message("Loading #{sObjectToLoad}.")
653
- oObject = Create(sObjectToLoad) if oObject.nil?
654
- return nil if oObject.nil?
655
- oParams = ObjectData.new
656
- oParams.add(oObject)
657
- oParams << hValues[:query_params]
658
- raise Lorj::PrcError.new(), "#{sKey}: query_type => :controller_call requires missing :query_call declaration (Controller function)" if hValues[:query_call].nil?
659
- pProc = hValues[:query_call]
660
- begin
661
- aList = @oProvider.method(pProc).call(sObjectToLoad, oParams)
662
- rescue => e
663
- raise Lorj::PrcError.new(), "Error during call of '%s':\n%s" % [pProc, e.message]
664
- end
665
- when :query_call
666
- sQuery = {}
667
- sQuery = hValues[:query_params] unless hValues[:query_params].nil?
668
- PrcLib.message("Querying #{sObjectToLoad}.")
669
- oObjectList = Query(sObjectToLoad, sQuery)
670
- aList = []
671
- oObjectList.each { | oElem |
672
- aList << oElem[hValues[:value]]
673
- }
674
- aList.sort!
675
- when :process_call
676
- raise Lorj::PrcError.new(), "#{sKey}: query_type => :process_call requires missing :query_call declaration (Provider function)" if hValues[:query_call].nil?
677
- pProc = hValues[:query_call]
678
- sObjectToLoad = hValues[:object]
679
-
680
- # Building Process function hParams parameter
681
- oParams = ObjectData.new
682
- oParams << { default_value: sDefault }
683
-
684
- unless hValues[:query_params].nil?
685
- hValues[:query_params].each { | key, value |
686
- if mRes = value.match(/lorj::config\[(.*)\]/)
687
- aExtract = mRes[1].split(', ')
688
- aExtract.map! { | v | v = v[1..-1].to_sym if v[0] == ':' }
689
- oParams << { key => config[aExtract] }
690
- else
691
- oParams << { key => value }
692
- end
693
- }
694
- end
695
-
696
- begin
697
- hProcResult = @oForjProcess.method(pProc).call(sObjectToLoad, oParams)
698
- rescue => e
699
- raise Lorj::PrcError.new(), "Error during call of '%s':\n%s" % [pProc, e.message]
700
- end
701
-
702
- if hProcResult.is_a?(Hash)
703
- sDefault = hProcResult[:default_value].nil? ? sDefault : hProcResult[:default_value]
704
- if hProcResult[:list].nil? or ! hProcResult[:list].is_a?(Array)
705
- PrcLib.debug("Process function '%s' did not return an :list => Array of values." % [ hValues[:query_call] ])
706
- else
707
- aList = hProcResult[:list]
708
- end
709
- else
710
- PrcLib.debug("Process function '%s' did not return an Hash with :list and :default_value")
711
- aList = []
712
- end
713
- else
714
- raise Lorj::PrcError.new, "'%s' invalid. %s/list_values/values_type supports %s. " % [hValues[:values_type], sKey, [:provider_function]]
715
- end
716
- PrcLib.fatal(1, "%s requires a value from the '%s' query which is empty." % [sKey, sObjectToLoad])if aList.nil? and bListStrict
717
- aList = [] if aList.nil?
718
- if not bListStrict
719
- aList << "other"
720
- end
721
- say("Enter %s" % ((sDefault.nil?)? sDesc : sDesc + " |%s|" % sDefault))
722
- value = choose { | q |
723
- q.choices(*aList)
724
- q.default = sDefault if sDefault
725
- }
726
- if not bListStrict and value == "other"
727
- value = _ask(sDesc, sDefault, rValidate, hParam[:encrypted], bRequired)
728
- end
729
- else
730
- pValidateProc = hParam[:validate_function]
731
- pAskProc = hParam[:ask_function]
732
-
733
- if pAskProc.nil?
734
- unless pValidateProc.nil?
735
- value = _ask(sDesc, sDefault, rValidate, hParam[:encrypted], bRequired)
736
- while not @oForjProcess.method(pValidateProc).call(value)
737
- value = _ask(sDesc, sDefault, rValidate, hParam[:encrypted], bRequired)
738
- end
739
- else
740
- value = _ask(sDesc, sDefault, rValidate, hParam[:encrypted], bRequired)
741
- end
742
- else
743
- unless pValidateProc.nil?
744
- value = @oForjProcess.method(pAskProc).call(sDesc, sDefault, rValidate, hParam[:encrypted], bRequired)
745
- while not @oForjProcess.method(pValidateProc).call(value)
746
- value = @oForjProcess.method(pAskProc).call(sDesc, sDefault, rValidate, hParam[:encrypted], bRequired)
747
- end
748
- else
749
- value = @oForjProcess.method(pAskProc).call(sDesc, sDefault, rValidate, hParam[:encrypted], bRequired)
750
- end
751
- end
752
- end
753
-
754
- @oForjConfig.set(sKey, value)
755
- if hParam[:post_step_function]
756
- pProc = hParam[:post_step_function]
757
- bResult = @oForjProcess.method(pProc).call()
758
- if bResult.boolean?
759
- bOk = bResult
760
- else
761
- PrcLib.debug("Warning: '%s' did not return any boolean value. Ignored" % [pProc])
762
- end
763
- end
764
- end
765
- }
766
- }
767
- }
768
- end
769
-
770
- # Initialize Lorj BaseDefinition object
771
-
772
- def initialize(oForjConfig, oForjProcess, oForjProvider = nil)
773
- # Object Data object. Contains all loaded object data.
774
- # This object is used to build hParams as well.
775
- @ObjectData = ObjectData.new(true)
776
- #
777
- @RuntimeContext = {
778
- :oCurrentObj => nil
779
- }
780
-
781
- @oForjConfig = oForjConfig
782
- @oERBConfig = ERBConfig.new(oForjConfig)
783
- raise Lorj::PrcError.new(), "'%s' is not a valid ForjAccount or ForjConfig Object." % [oForjConfig.class] if not oForjConfig.is_a?(Lorj::Account) and not oForjConfig.is_a?(Lorj::Config)
784
-
785
- @oProvider = oForjProvider
786
- if oForjProvider
787
- raise Lorj::PrcError.new(), "'%s' is not a valid ForjProvider Object type." % [oForjProvider.class] if not oForjProvider.is_a?(BaseController)
788
- end
789
-
790
- @oForjProcess = oForjProcess
791
- raise Lorj::PrcError.new(), "'%s' is not a valid BaseProcess Object type." % [oForjProcess.class] if not oForjProcess.is_a?(BaseProcess)
792
-
793
- @oForjProcess.set_BaseObject(self)
794
- end
795
-
796
- # ------------------------------------------------------
797
- # Functions used by processes functions
798
- # ------------------------------------------------------
799
- # Ex: object.set_data(...)
800
- # config
801
-
802
-
803
- # Function to manipulate the config object.
804
- # 2 kind of functions:
805
- # - set (key, value) and []=(key, value)
806
- # From processes, you can set a runtime data with:
807
- # config.set(key, value)
808
- # OR
809
- # config[key] = value
810
- #
811
- # - get (key, default) and [](key, default)
812
- # default is an optional value.
813
- # From processes, you can get a data (runtime/account/config.yaml or defaults.yaml) with:
814
- # config.get(key)
815
- # OR
816
- # config[key]
817
-
818
- def config
819
- raise Lorj::PrcError.new(), "No config object loaded." if not @oForjConfig
820
- @oForjConfig
821
- end
822
-
823
- def format_query(sObjectType, oControlerObject, hQuery)
824
- {
825
- :object => oControlerObject,
826
- :object_type => :object_list,
827
- :list_type => sObjectType,
828
- :list => [],
829
- :query => hQuery
830
- }
831
- end
832
-
833
- def format_object(sCloudObj, oMiscObject)
834
- return nil if not sCloudObj or not [String, Symbol].include?(sCloudObj.class)
835
-
836
- sCloudObj = sCloudObj.to_sym if sCloudObj.class == String
837
-
838
- oCoreObject = {
839
- :object_type => sCloudObj,
840
- :attrs => {},
841
- :object => oMiscObject,
842
- }
843
- end
844
-
845
- def get_data_metadata(sKey)
846
- _get_meta_data(sKey)
847
- end
848
-
849
- # Before doing a query, mapping fields
850
- # Transform Object query field to Provider query Fields
851
- def query_map(sCloudObj, hParams)
852
- return nil if not sCloudObj or not [String, Symbol].include?(sCloudObj.class)
853
- return {} if not hParams
854
-
855
- sCloudObj = sCloudObj.to_sym if sCloudObj.class == String
856
-
857
- hReturn = {}
858
- hMap = Lorj::rhGet(@@meta_obj, sCloudObj, :query_mapping)
859
- hParams.each { |key, value|
860
- oKeyPath = KeyPath.new(key)
861
- sKeyPath = oKeyPath.sFullPath
862
- raise Lorj::PrcError.new(), "Forj query field '%s.%s' not defined by class '%s'.\n" % [sCloudObj, oKeyPath.sKey, self.class ] +
863
- "#{ANSI.bold}ACTION REQUIRED#{ANSI.clear}:\n" +
864
- "Missing data model 'def_attribute' or 'def_query_attribute' for '%s'??? Check the object '%s' data model." % [oKeyPath.sKey, sCloudObj] if not hMap.key?(oKeyPath.sFullPath)
865
- oMapPath = KeyPath.new(hMap[oKeyPath.sFullPath])
866
- hValueMapping = Lorj::rhGet(@@meta_obj, sCloudObj, :value_mapping, sKeyPath)
867
- if hValueMapping
868
- raise Lorj::PrcError.new(), "'%s.%s': No value mapping for '%s'" % [sCloudObj, oKeyPath.sKey, value] if Lorj::rhExist?(hValueMapping, value) != 1
869
-
870
- Lorj::rhSet(hReturn, hValueMapping[value], oMapPath.aTree)
871
- else
872
- Lorj::rhSet(hReturn, value, oMapPath.aTree)
873
- end
874
- }
875
- hReturn
876
- end
877
-
878
- # Obsolete. Used by the Process.
879
- # Ask controller get_attr to get a data
880
- # The result is the data of a defined data attribute.
881
- # If the value is normally mapped (value mapped), the value is
882
- # returned as a recognized data attribute value.
883
- def get_attr(oObject, key)
884
-
885
- raise Lorj::PrcError.new(), "'%s' is not a valid Object type. " % [oObject.class] if not oObject.is_a?(Hash) and Lorj::rhExist?(oObject, :object_type) != 1
886
- sCloudObj = oObject[:object_type]
887
- oKeyPath = KeyPath.new(key)
888
- raise Lorj::PrcError.new(), "'%s' key is not declared as data of '%s' CloudObject. You may need to add obj_needs..." % [oKeyPath.sKey, sCloudObj] if Lorj::rhExist?(@@meta_obj, sCloudObj, :returns, oKeyPath.sFullPath) != 3
889
- begin
890
- oMapPath = KeyPath.new Lorj::rhGet(@@meta_obj, sCloudObj, :returns, oKeyPath.sFullPath)
891
- hMap = oMapPath.sFullPath
892
- value = @oProvider.get_attr(get_cObject(oObject), hMap)
893
-
894
- hValueMapping = Lorj::rhGet(@@meta_obj, sCloudObj, :value_mapping, oKeyPath.sFullPath)
895
-
896
- if hValueMapping
897
- hValueMapping.each { | found_key, found_value |
898
- if found_value == value
899
- value = found_key
900
- break
901
- end
902
- }
903
- end
904
- rescue => e
905
- raise Lorj::PrcError.new(), "'%s.get_attr' fails to provide value of '%s'" % [oProvider.class, key]
906
- end
907
- end
908
-
909
- # Register the object to the internal @ObjectData instance
910
- def register(oObject, sObjectType = nil, sDataType = :object)
911
- if oObject.is_a?(Lorj::Data)
912
- oDataObject = oObject
913
- else
914
- raise Lorj::PrcError.new(), "Unable to register an object '%s' as Lorj::Data object if ObjectType is not given." % [ oObject.class ] if not sObjectType
915
- oDataObject = Lorj::Data.new(sDataType)
916
- oDataObject.set(oObject, sObjectType) { | sObjType, oControlerObject |
917
- _return_map(sObjType, oControlerObject)
918
- }
919
- end
920
- @ObjectData.add oDataObject
921
- end
922
-
923
- def DataObjects(sObjectType, *key)
924
- @ObjectData[sObjectType, key]
925
- end
926
-
927
- # get an attribute/object/... from an object.
928
- def get_data(oObj, *key)
929
- if oObj.is_a?(Hash) and oObj.key?(:object_type)
930
- oObjData = ObjectData.new
931
- oObjData << oObj
932
- else
933
- oObjData = @ObjectData
934
- end
935
- oObjData[oObj, *key]
936
- end
937
-
938
- #~ def hParams(sCloudObj, hParams)
939
- #~ aParams = _get_object_params(sCloudObj, ":ObjectData.hParams")
940
- #~ end
941
-
942
- def get_cObject(oObject)
943
- return nil if Lorj::rhExist?(oObject, :object) != 1
944
- Lorj::rhGet(oObject, :object)
945
- end
946
-
947
- # a Process can execute any kind of predefined controler task.
948
- # Those function build hParams with Provider compliant data (mapped)
949
- # Results are formatted as usual framework Data object and stored.
950
- def connect(sObjectType)
951
-
952
- hParams = _get_object_params(sObjectType, :create_e, :connect, true)
953
- oControlerObject = @oProvider.connect(sObjectType, hParams)
954
- oDataObject = Lorj::Data.new
955
- oDataObject.set(oControlerObject, sObjectType) { | sObjType, oObject |
956
- begin
957
- _return_map(sObjType, oObject)
958
- rescue => e
959
- raise Lorj::PrcError.new(), "connect %s.%s : %s" % [@oForjProcess.class, sObjectType, e.message]
960
- end
961
- }
962
- @ObjectData.add oDataObject
963
- oDataObject
964
- end
965
-
966
- def create(sObjectType)
967
- # The process ask the controller to create the object.
968
- # hParams have to be fully readable by the controller.
969
- hParams = _get_object_params(sObjectType, :create_e, :create, true)
970
- oControlerObject = @oProvider.create(sObjectType, hParams)
971
- oDataObject = Lorj::Data.new
972
- oDataObject.set(oControlerObject, sObjectType) { | sObjType, oObject |
973
- begin
974
- _return_map(sObjType, oObject)
975
- rescue => e
976
- raise Lorj::PrcError.new(), "create %s.%s : %s" % [@oForjProcess.class, sObjectType, e.message]
977
- end
978
- }
979
- @ObjectData.add oDataObject
980
-
981
- oDataObject
982
- end
983
-
984
- # The controller must return true to inform about the real deletion
985
- def delete(sObjectType)
986
- hParams = _get_object_params(sObjectType, :delete_e, :delete, true)
987
- raise Lorj::PrcError.new(), "delete Controller - %s: Object '%s' is not loaded." % [@oProvider.class, key] if not hParams.exist?(sObjectType)
988
- bState = @oProvider.delete(sObjectType, hParams)
989
- @ObjectData.delete(sObjectType) if bState
990
- bState
991
- end
992
-
993
- def get(sObjectType, sUniqId)
994
-
995
- hParams = _get_object_params(sObjectType, :get_e, :get, true)
996
-
997
- oControlerObject = @oProvider.get(sObjectType, sUniqId, hParams)
998
- oDataObject = Lorj::Data.new
999
- oDataObject.set(oControlerObject, sObjectType) { | sObjType, oObject |
1000
- begin
1001
- _return_map(sObjType, oObject)
1002
- rescue => e
1003
- raise Lorj::PrcError.new(), "get %s.%s : %s" % [@oForjProcess.class, sObjectType, e.message]
1004
- end
1005
- }
1006
- @ObjectData.add oDataObject
1007
-
1008
- oDataObject
1009
- end
1010
-
1011
- def query(sObjectType, hQuery)
1012
-
1013
- # Check if we can re-use a previous query
1014
- oList = @ObjectData[:query, sObjectType]
1015
- unless oList.nil?
1016
- if oList[:query] == hQuery
1017
- Lorj.debug(3, "Using Object '%s' query cache : %s" % [sObjectType, hQuery])
1018
- return oList
1019
- end
1020
- end
1021
-
1022
-
1023
- hParams = _get_object_params(sObjectType, :query_e, :query, true)
1024
- sProviderQuery = query_map(sObjectType, hQuery)
1025
-
1026
- oControlerObject = @oProvider.query(sObjectType, sProviderQuery, hParams)
1027
-
1028
- oDataObjects = Lorj::Data.new :list
1029
- oDataObjects.set(oControlerObject, sObjectType, hQuery) { | sObjType, key |
1030
- begin
1031
- _return_map(sObjType, key)
1032
- rescue => e
1033
- raise Lorj::PrcError.new(), "query %s.%s : %s" % [@oForjProcess.class, sObjectType, e.message]
1034
- end
1035
- }
1036
-
1037
- Lorj.debug(2, "Object %s - queried. Found %s object(s)." % [sObjectType, oDataObjects.length()])
1038
-
1039
- @ObjectData.add oDataObjects
1040
- oDataObjects
1041
- end
1042
-
1043
- def update(sObjectType)
1044
- # Need to detect data updated and update the Controler object with the controler
1045
-
1046
- hParams = _get_object_params(sObjectType, :update_e, :update, true)
1047
-
1048
- oObject = @ObjectData[sObjectType, :ObjectData]
1049
- oControlerObject = oObject[:object]
1050
-
1051
- bUpdated = false
1052
- oObject[:attrs].each { |key, value |
1053
- oKeyPath = KeyPath.new(key)
1054
- oMapPath = KeyPath.new Lorj::rhGet(@@meta_obj, sObjectType, :returns, oKeyPath.sFullPath)
1055
- old_value = @oProvider.get_attr(oControlerObject, oMapPath.aTree)
1056
- if value != old_value
1057
- bUpdated = true
1058
- @oProvider.set_attr(oControlerObject, oMapPath.aTree, value)
1059
- Lorj.debug(2, "%s.%s - Updating: %s = %s (old : %s)" % [@oForjProcess.class, sObjectType, key, value, old_value])
1060
- end
1061
- }
1062
-
1063
- bDone = @oProvider.update(sObjectType, oObject, hParams) if bUpdated
1064
-
1065
- raise Lorj::PrcError.new, "Controller function 'update' must return True or False. Class returned: '%s'" % bDone.class if not [TrueClass, FalseClass].include?(bDone.class)
1066
-
1067
- Lorj.debug(1, "%s.%s - updated." % [@oForjProcess.class, sObjectType]) if bDone
1068
- oObject.set(oControlerObject, sObjectType) { | sObjType, oObject |
1069
- begin
1070
- _return_map(sObjType, oObject)
1071
- rescue => e
1072
- raise Lorj::PrcError.new(), "update %s.%s : %s" % [@oForjProcess.class, sObjectType, e.message]
1073
- end
1074
- }
1075
- bDone
1076
- end
1077
-
1078
- private
1079
-
1080
- # -------------------------------------------------------------------------
1081
- # Functions available for Process to communicate with the controler Object
1082
- # -------------------------------------------------------------------------
1083
- def cloud_obj_requires(sCloudObj, res = {})
1084
- aCaller = caller
1085
- aCaller.pop
1086
-
1087
- return res if @ObjectData.exist?(sCloudObj)
1088
- #~ return res if Lorj::rhExist?(@CloudData, sCloudObj) == 1
1089
-
1090
- Lorj::rhGet(@@meta_obj,sCloudObj, :params).each { |key, hParams|
1091
- case hParams[:type]
1092
- when :data
1093
- if hParams.key?(:array)
1094
- hParams[:array].each{ | aElem |
1095
- aElem = aElem.clone
1096
- aElem.pop # Do not go until last level, as used to loop next.
1097
- Lorj::rhGet(hParams, aElem).each { | subkey, hSubParam |
1098
- next if aElem.length == 0 and [:array, :type].include?(subkey)
1099
- if hSubParams[:required] and @oForjConfig.get(subkey).nil?
1100
- res[subkey] = hSubParams
1101
- end
1102
- }
1103
- }
1104
- else
1105
- if hParams[:required] and @oForjConfig.get(key).nil?
1106
- res[key] = hParams
1107
- end
1108
- end
1109
- when :CloudObject
1110
- #~ if hParams[:required] and Lorj::rhExist?(@CloudData, sCloudObj) != 1
1111
- if hParams[:required] and not @ObjectData.exist?(sCloudObj)
1112
- res[key] = hParams
1113
- cloud_obj_requires(key, res)
1114
- end
1115
- end
1116
- }
1117
- res
1118
- end
1119
-
1120
- def get_object(sCloudObj)
1121
- #~ return nil if Lorj::rhExist?(@CloudData, sCloudObj) != 1
1122
- return nil if not @ObjectData.exist?(sCloudObj)
1123
- @ObjectData[sCloudObj, :ObjectData]
1124
- #~ Lorj::rhGet(@CloudData, sCloudObj)
1125
- end
1126
-
1127
- def objectExist?(sCloudObj)
1128
- @ObjectData.exist?(sCloudObj)
1129
- #~ (Lorj::rhExist?(@CloudData, sCloudObj) != 1)
1130
- end
1131
-
1132
- def get_forjKey(oCloudData, key)
1133
- return nil if not @ObjectData.exist?(sCloudObj)
1134
- @ObjectData[sCloudObj, :attrs, key]
1135
- #~ return nil if Lorj::rhExist?(oCloudData, sCloudObj) != 1
1136
- #~ Lorj::rhGet(oCloudData, sCloudObj, :attrs, key)
1137
- end
1138
- end
1139
- end