labimotion 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/lib/labimotion/api.rb +19 -0
  3. data/lib/labimotion/apis/converter_api.rb +68 -0
  4. data/lib/labimotion/apis/generic_dataset_api.rb +50 -0
  5. data/lib/labimotion/apis/generic_element_api.rb +339 -0
  6. data/lib/labimotion/apis/labimotion_hub_api.rb +53 -0
  7. data/lib/labimotion/apis/segment_api.rb +144 -0
  8. data/lib/labimotion/entities/application_entity.rb +88 -0
  9. data/lib/labimotion/entities/dataset_entity.rb +16 -0
  10. data/lib/labimotion/entities/dataset_klass_entity.rb +9 -0
  11. data/lib/labimotion/entities/element_entity.rb +108 -0
  12. data/lib/labimotion/entities/element_klass_entity.rb +10 -0
  13. data/lib/labimotion/entities/element_revision_entity.rb +57 -0
  14. data/lib/labimotion/entities/eln_element_entity.rb +110 -0
  15. data/lib/labimotion/entities/generic_entity.rb +54 -0
  16. data/lib/labimotion/entities/generic_klass_entity.rb +14 -0
  17. data/lib/labimotion/entities/generic_public_entity.rb +25 -0
  18. data/lib/labimotion/entities/klass_revision_entity.rb +20 -0
  19. data/lib/labimotion/entities/segment_entity.rb +62 -0
  20. data/lib/labimotion/entities/segment_klass_entity.rb +8 -0
  21. data/lib/labimotion/entities/segment_revision_entity.rb +55 -0
  22. data/lib/labimotion/helpers/converter_helpers.rb +13 -0
  23. data/lib/labimotion/helpers/dataset_helpers.rb +38 -0
  24. data/lib/labimotion/helpers/element_helpers.rb +268 -0
  25. data/lib/labimotion/helpers/generic_helpers.rb +252 -0
  26. data/lib/labimotion/helpers/repository_helpers.rb +14 -0
  27. data/lib/labimotion/helpers/sample_association_helpers.rb +126 -0
  28. data/lib/labimotion/helpers/search_helpers.rb +62 -0
  29. data/lib/labimotion/helpers/segment_helpers.rb +97 -0
  30. data/lib/labimotion/libs/converter.rb +325 -0
  31. data/lib/labimotion/libs/export_dataset.rb +121 -0
  32. data/lib/labimotion/libs/nmr_mapper.rb +265 -0
  33. data/lib/labimotion/libs/nmr_mapper_repo.rb +263 -0
  34. data/lib/labimotion/libs/template_hub.rb +55 -0
  35. data/lib/labimotion/models/collections_element.rb +42 -0
  36. data/lib/labimotion/models/concerns/attachment_converter.rb +42 -0
  37. data/lib/labimotion/models/concerns/datasetable.rb +50 -0
  38. data/lib/labimotion/models/concerns/generic_klass_revisions.rb +39 -0
  39. data/lib/labimotion/models/concerns/generic_revisions.rb +43 -0
  40. data/lib/labimotion/models/concerns/segmentable.rb +74 -0
  41. data/lib/labimotion/models/dataset.rb +14 -0
  42. data/lib/labimotion/models/dataset_klass.rb +25 -0
  43. data/lib/labimotion/models/dataset_klasses_revision.rb +9 -0
  44. data/lib/labimotion/models/datasets_revision.rb +9 -0
  45. data/lib/labimotion/models/element.rb +121 -0
  46. data/lib/labimotion/models/element_klass.rb +25 -0
  47. data/lib/labimotion/models/element_klasses_revision.rb +9 -0
  48. data/lib/labimotion/models/elements_element.rb +11 -0
  49. data/lib/labimotion/models/elements_revision.rb +8 -0
  50. data/lib/labimotion/models/elements_sample.rb +11 -0
  51. data/lib/labimotion/models/segment.rb +14 -0
  52. data/lib/labimotion/models/segment_klass.rb +24 -0
  53. data/lib/labimotion/models/segment_klasses_revision.rb +9 -0
  54. data/lib/labimotion/models/segments_revision.rb +9 -0
  55. data/lib/labimotion/utils/con_state.rb +13 -0
  56. data/lib/labimotion/utils/export.rb +112 -0
  57. data/lib/labimotion/utils/import.rb +186 -0
  58. data/lib/labimotion/utils/search.rb +112 -0
  59. data/lib/labimotion/utils/serializer.rb +78 -0
  60. data/lib/labimotion/version.rb +6 -0
  61. data/lib/labimotion.rb +95 -0
  62. metadata +119 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2d2c94d9d6147cab5fd823c611f65c6b41f5140bd018424aae86ca1bd697e1b0
4
+ data.tar.gz: e5ac11f1be750a698f7285776fc62a1850876b4fa5b53814460f9325d90a832a
5
+ SHA512:
6
+ metadata.gz: 1430bdaa9a7721c6d5c09b35abde14ee6d2946a7f394fb65a12b9456b026c6734e027c1c36c24195cd36ec4e5701c7f4bba778f4d2fa31f4e6984e491f05eecb
7
+ data.tar.gz: 5518460a7151dce59e3c20e6e8ca0422a3d443b8aff7c95fecf63d915aa0a6c2e1555b221548bc58ef7194798c48cb4252c3f89454798d27366d9c4a844b1348
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ ## This is the first version of the dataset class
4
+ module Labimotion
5
+ ## This is the first version of the dataset class
6
+ class Api
7
+ def initialize
8
+ # Initialization code
9
+ end
10
+
11
+ def gget_data
12
+ 'Hello, worlddddddddddddddddddddd!'
13
+ end
14
+
15
+ def self.hello
16
+ 'Hellooooooooooooooooooooo!'
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labimotion
4
+ class ConverterAPI < Grape::API
5
+ helpers do
6
+ end
7
+ resource :converter do
8
+ resource :profiles do
9
+ before do
10
+ @conf = Rails.configuration.try(:converter).try(:url)
11
+ @profile = Rails.configuration.try(:converter).try(:profile)
12
+ error!(406) unless @conf && @profile
13
+ end
14
+ desc 'fetch profiles'
15
+ get do
16
+ profiles = Labimotion::Converter.fetch_profiles
17
+ { profiles: profiles, client: @profile }
18
+ end
19
+ desc 'create profile'
20
+ post do
21
+ Labimotion::Converter.create_profile(params)
22
+ end
23
+ desc 'update profile'
24
+ route_param :id do
25
+ put do
26
+ Labimotion::Converter.update_profile(params)
27
+ end
28
+ end
29
+ desc 'delete profile'
30
+ route_param :id do
31
+ delete do
32
+ id = params[:id]
33
+ Labimotion::Converter.delete_profile(id)
34
+ end
35
+ end
36
+ end
37
+
38
+ resource :options do
39
+ before do
40
+ error!(401) unless current_user.profile&.data['converter_admin'] == true
41
+ @conf = Rails.configuration.try(:converter).try(:url)
42
+ @profile = Rails.configuration.try(:converter).try(:profile)
43
+ error!(406) unless @conf && @profile
44
+ end
45
+ desc 'fetch options'
46
+ get do
47
+ options = Labimotion::Converter.fetch_options
48
+ { options: options, client: @profile }
49
+ end
50
+ end
51
+
52
+ resource :tables do
53
+ before do
54
+ error!(401) unless current_user.profile&.data['converter_admin'] == true
55
+ @conf = Rails.configuration.try(:converter).try(:url)
56
+ @profile = Rails.configuration.try(:converter).try(:profile)
57
+ error!(406) unless @conf && @profile
58
+ end
59
+ desc 'create tables'
60
+ post do
61
+ res = Labimotion::Converter.create_tables(params[:file][0]['tempfile']) unless params[:file].empty?
62
+ res['metadata']['file_name'] = params[:file][0]['filename']
63
+ res
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,50 @@
1
+ module Labimotion
2
+ ## Generic Dataset API
3
+ class GenericDatasetAPI < Grape::API
4
+ include Grape::Kaminari
5
+ helpers Labimotion::GenericHelpers
6
+ helpers Labimotion::DatasetHelpers
7
+
8
+ resource :generic_dataset do
9
+ namespace :klasses do
10
+ desc 'get dataset klasses'
11
+ get do
12
+ list = klass_list(true)
13
+ present list.sort_by(&:place), with: Labimotion::DatasetKlassEntity, root: 'klass'
14
+ end
15
+ end
16
+
17
+ namespace :list_dataset_klass do
18
+ desc 'list Generic Dataset Klass'
19
+ params do
20
+ optional :is_active, type: Boolean, desc: 'Active or Inactive Dataset'
21
+ end
22
+ get do
23
+ list = klass_list(params[:is_active])
24
+ present list, with: Labimotion::DatasetKlassEntity, root: 'klass'
25
+ end
26
+ end
27
+
28
+ namespace :fetch_repo do
29
+ desc 'fetch Generic Dataset Klass from Chemotion Repository'
30
+ get do
31
+ fetch_repo('DatasetKlass')
32
+ end
33
+ end
34
+
35
+ namespace :create_repo_klass do
36
+ desc 'create Generic Dataset Klass'
37
+ params do
38
+ requires :identifier, type: String, desc: 'Identifier'
39
+ end
40
+ post do
41
+ create_repo_klass(params, current_user)
42
+ present DatasetKlass.all, with: Labimotion::DatasetKlassEntity, root: 'klass'
43
+ rescue => e
44
+ Labimotion.log_exception(e, current_user)
45
+ { error: e.message }
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,339 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'labimotion/version'
4
+ module Labimotion
5
+ # Generic Element API
6
+ class GenericElementAPI < Grape::API
7
+ include Grape::Kaminari
8
+ helpers ContainerHelpers
9
+ helpers ParamsHelpers
10
+ helpers CollectionHelpers
11
+ helpers Labimotion::SampleAssociationHelpers
12
+ helpers Labimotion::GenericHelpers
13
+ helpers Labimotion::ElementHelpers
14
+
15
+ resource :generic_elements do
16
+ namespace :klass do
17
+ desc 'get klass info'
18
+ params do
19
+ requires :name, type: String, desc: 'element klass name'
20
+ end
21
+ get do
22
+ ek = Labimotion::ElementKlass.find_by(name: params[:name])
23
+ present ek, with: Labimotion::ElementKlassEntity, root: 'klass'
24
+ end
25
+ end
26
+
27
+ namespace :klasses do
28
+ desc 'get klasses'
29
+ params do
30
+ optional :generic_only, type: Boolean, desc: 'list generic element only'
31
+ end
32
+ get do
33
+ list = klass_list(params[:generic_only])
34
+ present list, with: Labimotion::ElementKlassEntity, root: 'klass'
35
+ end
36
+ end
37
+
38
+ namespace :create_element_klass do
39
+ desc 'create Generic Element Klass'
40
+ params do
41
+ requires :name, type: String, desc: 'Element Klass Name'
42
+ requires :label, type: String, desc: 'Element Klass Label'
43
+ requires :klass_prefix, type: String, desc: 'Element Klass Short Label Prefix'
44
+ optional :icon_name, type: String, desc: 'Element Klass Icon Name'
45
+ optional :desc, type: String, desc: 'Element Klass Desc'
46
+ optional :properties_template, type: Hash, desc: 'Element Klass properties template'
47
+ end
48
+ post do
49
+ authenticate_admin!('elements')
50
+ create_element_klass(current_user, params)
51
+ status 201
52
+ rescue ActiveRecord::RecordInvalid => e
53
+ { error: e.message }
54
+ end
55
+ end
56
+
57
+ namespace :update_element_klass do
58
+ desc 'update Generic Element Klass'
59
+ params do
60
+ requires :id, type: Integer, desc: 'Element Klass ID'
61
+ optional :label, type: String, desc: 'Element Klass Label'
62
+ optional :klass_prefix, type: String, desc: 'Element Klass Short Label Prefix'
63
+ optional :icon_name, type: String, desc: 'Element Klass Icon Name'
64
+ optional :desc, type: String, desc: 'Element Klass Desc'
65
+ optional :place, type: String, desc: 'Element Klass Place'
66
+ end
67
+ post do
68
+ authenticate_admin!('elements')
69
+ update_element_klass(current_user, params)
70
+ end
71
+ end
72
+
73
+ namespace :klass_revisions do
74
+ desc 'list Generic Element Revisions'
75
+ params do
76
+ requires :id, type: Integer, desc: 'Generic Element Klass Id'
77
+ requires :klass, type: String, desc: 'Klass', values: %w[ElementKlass SegmentKlass DatasetKlass]
78
+ end
79
+ get do
80
+ list = list_klass_revisions(params)
81
+ present list, with: Labimotion::KlassRevisionEntity, root: 'revisions'
82
+ end
83
+ end
84
+
85
+ namespace :element_revisions do
86
+ desc 'list Generic Element Revisions'
87
+ params do
88
+ requires :id, type: Integer, desc: 'Generic Element Id'
89
+ end
90
+ get do
91
+ list = element_revisions(params)
92
+ present list, with: Labimotion::ElementRevisionEntity, root: 'revisions'
93
+ end
94
+ end
95
+
96
+ namespace :delete_klass_revision do
97
+ desc 'delete Klass Revision'
98
+ params do
99
+ requires :id, type: Integer, desc: 'Revision ID'
100
+ requires :klass_id, type: Integer, desc: 'Klass ID'
101
+ requires :klass, type: String, desc: 'Klass', values: %w[ElementKlass SegmentKlass DatasetKlass]
102
+ end
103
+ post do
104
+ authenticate_admin!(params[:klass].gsub(/(Klass)/, 's').downcase)
105
+ delete_klass_revision(params)
106
+ status 201
107
+ end
108
+ end
109
+
110
+ namespace :delete_revision do
111
+ desc 'delete Generic Element Revisions'
112
+ params do
113
+ requires :id, type: Integer, desc: 'Revision Id'
114
+ requires :element_id, type: Integer, desc: 'Element ID'
115
+ requires :klass, type: String, desc: 'Klass', values: %w[Element Segment Dataset]
116
+ end
117
+ post do
118
+ delete_revision(params)
119
+ status 201
120
+ end
121
+ end
122
+
123
+ namespace :segment_revisions do
124
+ desc 'list Generic Element Revisions'
125
+ params do
126
+ optional :id, type: Integer, desc: 'Generic Element Id'
127
+ end
128
+ get do
129
+ klass = Labimotion::Segment.find(params[:id])
130
+ list = klass.segments_revisions unless klass.nil?
131
+ present list&.sort_by(&:created_at).reverse, with: Labimotion::SegmentRevisionEntity, root: 'revisions'
132
+ end
133
+ end
134
+
135
+ namespace :upload_generics_files do
136
+ desc 'upload generic files'
137
+ params do
138
+ requires :att_id, type: Integer, desc: 'Element Id'
139
+ requires :att_type, type: String, desc: 'Element Type'
140
+ end
141
+ after_validation do
142
+ if params[:att_type] == 'Sample' || params[:att_type] == 'Reaction' || params[:att_type] == 'ResearchPlan'
143
+ el = "#{params[:att_type]}".constantize.find_by(id: params[:att_id])
144
+ else
145
+ el = "Labimotion::#{params[:att_type]}".constantize.find_by(id: params[:att_id])
146
+ end
147
+ error!('401 Unauthorized', 401) if el.nil?
148
+
149
+ policy_updatable = ElementPolicy.new(current_user, el).update?
150
+ error!('401 Unauthorized', 401) unless policy_updatable
151
+ end
152
+ post do
153
+ upload_generics_files(current_user, params)
154
+ true
155
+ end
156
+ end
157
+
158
+ namespace :klasses_all do
159
+ desc 'get all klasses for admin function'
160
+ get do
161
+ list = Labimotion::ElementKlass.all.sort_by { |e| e.place }
162
+ present list, with: Labimotion::ElementKlassEntity, root: 'klass'
163
+ end
164
+ end
165
+
166
+ namespace :de_activate_klass do
167
+ desc 'activate or deactivate Generic Klass'
168
+ params do
169
+ requires :klass, type: String, desc: 'Klass', values: %w[ElementKlass SegmentKlass DatasetKlass]
170
+ requires :id, type: Integer, desc: 'Klass ID'
171
+ requires :is_active, type: Boolean, desc: 'Active or Inactive Klass'
172
+ end
173
+ after_validation do
174
+ authenticate_admin!(params[:klass].gsub(/(Klass)/, 's').downcase)
175
+ @klz = fetch_klass(params[:klass], params[:id])
176
+ end
177
+ post do
178
+ deactivate_klass(params)
179
+ end
180
+ end
181
+
182
+ namespace :delete_klass do
183
+ desc 'delete Generic Klass'
184
+ params do
185
+ requires :klass, type: String, desc: 'Klass', values: %w[ElementKlass SegmentKlass DatasetKlass]
186
+ requires :id, type: Integer, desc: 'Klass ID'
187
+ end
188
+ delete ':id' do
189
+ delete_klass(params)
190
+ status 201
191
+ end
192
+ end
193
+
194
+ namespace :update_template do
195
+ desc 'update Generic Properties Template'
196
+ params do
197
+ requires :klass, type: String, desc: 'Klass', values: %w[ElementKlass SegmentKlass DatasetKlass]
198
+ requires :id, type: Integer, desc: 'Klass ID'
199
+ requires :properties_template, type: Hash
200
+ optional :is_release, type: Boolean, default: false
201
+ end
202
+ after_validation do
203
+ authenticate_admin!(params[:klass].gsub(/(Klass)/, 's').downcase)
204
+ @klz = fetch_klass(params[:klass], params[:id])
205
+ end
206
+ post do
207
+ update_template(params)
208
+ end
209
+ end
210
+
211
+ desc 'Return serialized elements of current user'
212
+ params do
213
+ optional :collection_id, type: Integer, desc: 'Collection id'
214
+ optional :sync_collection_id, type: Integer, desc: 'SyncCollectionsUser id'
215
+ optional :el_type, type: String, desc: 'element klass name'
216
+ optional :from_date, type: Integer, desc: 'created_date from in ms'
217
+ optional :to_date, type: Integer, desc: 'created_date to in ms'
218
+ optional :filter_created_at, type: Boolean, desc: 'filter by created at or updated at'
219
+ end
220
+ paginate per_page: 7, offset: 0, max_per_page: 100
221
+ get do
222
+
223
+ scope = list_serialized_elements(params, current_user)
224
+
225
+ reset_pagination_page(scope)
226
+ if Labimotion::IS_RAILS5 == true
227
+ generic_elements = paginate(scope).map { |s| ElementListPermissionProxy.new(current_user, s, user_ids).serialized }
228
+ else
229
+ generic_elements = paginate(scope).map do |element|
230
+ Labimotion::ElementEntity.represent(
231
+ element,
232
+ displayed_in_list: true,
233
+ detail_levels: ElementDetailLevelCalculator.new(user: current_user, element: element).detail_levels,
234
+ )
235
+ end
236
+ end
237
+ { generic_elements: generic_elements }
238
+ end
239
+
240
+ desc 'Return serialized element by id'
241
+ params do
242
+ requires :id, type: Integer, desc: 'Element id'
243
+ end
244
+ route_param :id do
245
+ before do
246
+ error!('401 Unauthorized', 401) unless current_user.matrix_check_by_name('genericElement') && ElementPolicy.new(current_user, Element.find(params[:id])).read?
247
+ end
248
+
249
+ get do
250
+ element = Labimotion::Element.find(params[:id])
251
+ if Labimotion::IS_RAILS5 == true
252
+ {
253
+ element: ElementPermissionProxy.new(current_user, element, user_ids).serialized,
254
+ attachments: Entities::AttachmentEntity.represent(element.attachments)
255
+ }
256
+ else
257
+ {
258
+ element: Labimotion::ElementEntity.represent(
259
+ element,
260
+ detail_levels: ElementDetailLevelCalculator.new(user: current_user, element: element).detail_levels,
261
+ ),
262
+ attachments: Entities::AttachmentEntity.represent(element.attachments),
263
+ }
264
+ end
265
+ end
266
+ end
267
+
268
+ desc 'Create a element'
269
+ params do
270
+ requires :element_klass, type: Hash
271
+ requires :name, type: String
272
+ optional :properties, type: Hash
273
+ optional :properties_release, type: Hash
274
+ optional :collection_id, type: Integer
275
+ requires :container, type: Hash
276
+ optional :segments, type: Array, desc: 'Segments'
277
+ end
278
+ post do
279
+ begin
280
+ element = create_element(current_user, params)
281
+ if Labimotion::IS_RAILS5 == true
282
+ {
283
+ element: ElementPermissionProxy.new(current_user, element, user_ids).serialized,
284
+ attachments: Entities::AttachmentEntity.represent(element.attachments)
285
+ }
286
+ else
287
+ present(
288
+ element,
289
+ with: Labimotion::ElementEntity,
290
+ root: :element,
291
+ detail_levels: ElementDetailLevelCalculator.new(user: current_user, element: element).detail_levels,
292
+ )
293
+ end
294
+ rescue => e
295
+ Labimotion.log_exception(e, current_user)
296
+ raise e
297
+ end
298
+ end
299
+
300
+ desc 'Update element by id'
301
+ params do
302
+ requires :id, type: Integer, desc: 'element id'
303
+ optional :name, type: String
304
+ requires :properties, type: Hash
305
+ optional :properties_release, type: Hash
306
+ requires :container, type: Hash
307
+ optional :segments, type: Array, desc: 'Segments'
308
+ end
309
+ route_param :id do
310
+ before do
311
+ error!('401 Unauthorized', 401) unless ElementPolicy.new(current_user, Labimotion::Element.find(params[:id])).update?
312
+ end
313
+
314
+ put do
315
+ begin
316
+ element = update_element_by_id(current_user, params)
317
+ if Labimotion::IS_RAILS5 == true
318
+ {
319
+ element: ElementPermissionProxy.new(current_user, element, user_ids).serialized,
320
+ attachments: Entities::AttachmentEntity.represent(element.attachments)
321
+ }
322
+ else
323
+ {
324
+ element: Labimotion::ElementEntity.represent(
325
+ element,
326
+ detail_levels: ElementDetailLevelCalculator.new(user: current_user, element: element).detail_levels,
327
+ ),
328
+ attachments: Entities::AttachmentEntity.represent(element.attachments),
329
+ }
330
+ end
331
+ rescue => e
332
+ Labimotion.log_exception(e, current_user)
333
+ raise e
334
+ end
335
+ end
336
+ end
337
+ end
338
+ end
339
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'open-uri'
4
+
5
+ # Belong to Chemotion module
6
+ module Labimotion
7
+ # API for Public data
8
+ class LabimotionHubAPI < Grape::API
9
+ include Grape::Kaminari
10
+
11
+ namespace :labimotion_hub do
12
+ namespace :list do
13
+ desc "get active generic templates"
14
+ params do
15
+ requires :klass, type: String, desc: 'Klass', values: %w[ElementKlass SegmentKlass DatasetKlass]
16
+ optional :with_props, type: Boolean, desc: 'With Properties', default: false
17
+ end
18
+ get do
19
+ list = "Labimotion::#{params[:klass]}".constantize.where(is_active: true).where.not(released_at: nil)
20
+ list = list.where(is_generic: true) if params[:klass] == 'ElementKlass'
21
+ entities = Labimotion::GenericPublicEntity.represent(list, displayed: params[:with_props])
22
+ # entities.length > 1 ? de_encode_json(entities) : []
23
+ end
24
+ end
25
+ namespace :fetch do
26
+ desc "get active generic templates"
27
+ params do
28
+ requires :klass, type: String, desc: 'Klass', values: %w[ElementKlass SegmentKlass DatasetKlass]
29
+ # requires :fqdn, type: Integer, desc: 'FQDN'
30
+ requires :identifier, type: String, desc: 'Identifier'
31
+ end
32
+ get do
33
+ entity = "Labimotion::#{params[:klass]}".constantize.find_by(identifier: params[:identifier])
34
+ "Labimotion::#{params[:klass]}Entity".constantize.represent(entity)
35
+ # entities.length > 1 ? de_encode_json(entities) : []
36
+ end
37
+ end
38
+
39
+ namespace :element_klasses_name do
40
+ desc "get klasses"
41
+ params do
42
+ optional :generic_only, type: Boolean, desc: "list generic element only"
43
+ end
44
+ get do
45
+ list = Labimotion::ElementKlass.where(is_active: true) if params[:generic_only].present? && params[:generic_only] == true
46
+ list = Labimotion::ElementKlass.where(is_active: true) unless params[:generic_only].present? && params[:generic_only] == true
47
+ list.pluck(:name)
48
+ end
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,144 @@
1
+ module Labimotion
2
+ class SegmentAPI < Grape::API
3
+ include Grape::Kaminari
4
+ helpers Labimotion::GenericHelpers
5
+ helpers Labimotion::SegmentHelpers
6
+
7
+ resource :segments do
8
+ namespace :klasses do
9
+ desc "get segment klasses"
10
+ params do
11
+ optional :element, type: String, desc: "Klass Element, e.g. Sample, Reaction, Mof,..."
12
+ end
13
+ get do
14
+ list = klass_list(params[:element], true)
15
+ present list, with: Labimotion::SegmentKlassEntity, root: 'klass'
16
+ end
17
+ end
18
+
19
+ namespace :list_segment_klass do
20
+ desc 'list Generic Segment Klass'
21
+ params do
22
+ optional :is_active, type: Boolean, desc: 'Active or Inactive Segment'
23
+ end
24
+ get do
25
+ list = klass_list(nil, params[:is_active])
26
+ present list, with: Labimotion::SegmentKlassEntity, root: 'klass'
27
+ end
28
+ end
29
+
30
+ namespace :create_segment_klass do
31
+ desc 'create Generic Segment Klass'
32
+ params do
33
+ requires :label, type: String, desc: 'Segment Klass Label'
34
+ requires :element_klass, type: Integer, desc: 'Element Klass Id'
35
+ optional :desc, type: String, desc: 'Segment Klass Desc'
36
+ optional :place, type: String, desc: 'Segment Klass Place', default: '100'
37
+ optional :properties_template, type: Hash, desc: 'Element Klass properties template'
38
+ end
39
+ after_validation do
40
+ authenticate_admin!('segments')
41
+ @klass = fetch_klass('ElementKlass', params[:element_klass])
42
+ end
43
+ post do
44
+ create_segment_klass(current_user, params)
45
+ rescue ActiveRecord::RecordInvalid => e
46
+ { error: e.message }
47
+ end
48
+ end
49
+
50
+ namespace :update_segment_klass do
51
+ desc 'update Generic Segment Klass'
52
+ params do
53
+ requires :id, type: Integer, desc: 'Segment Klass ID'
54
+ optional :label, type: String, desc: 'Segment Klass Label'
55
+ optional :desc, type: String, desc: 'Segment Klass Desc'
56
+ optional :place, type: String, desc: 'Segment Klass Place', default: '100'
57
+ optional :identifier, type: String, desc: 'Segment Identifier'
58
+ end
59
+ after_validation do
60
+ authenticate_admin!('segments')
61
+ end
62
+ post do
63
+ update_segment_klass(current_user, params)
64
+ rescue => e
65
+ Labimotion.log_exception(e, current_user)
66
+ { error: e.message }
67
+ end
68
+ end
69
+
70
+ namespace :fetch_repo_generic_template do
71
+ desc 'fetch segment templates from repository'
72
+ params do
73
+ requires :identifier, type: String, desc: 'identifier'
74
+ end
75
+ post do
76
+ sk_obj = fetch_repo_generic_template('Segment', params[:identifier])
77
+ sk_obj = sk_obj.deep_symbolize_keys[:generic_template]
78
+ return { error: 'No template data found' } unless sk_obj.present?
79
+
80
+ ek_obj = Labimotion::ElementKlass.find_by(name: sk_obj.dig(:element_klass, :klass_name))
81
+ return { error: 'No related element data found' } unless ek_obj.present?
82
+
83
+ segment_klass = Labimotion::SegmentKlass.find_or_create_by(
84
+ identifier: sk_obj.dig(:identifier),
85
+ )
86
+ segment_klass.update(sk_obj.slice(
87
+ :label,
88
+ :desc,
89
+ :place,
90
+ :properties_release,
91
+ :uuid,
92
+ ).merge(
93
+ is_active: true,
94
+ properties_template: sk_obj.dig(:properties_release), # properties_release,
95
+ element_klass: ek_obj,
96
+ created_by: current_user.id,
97
+ released_at: DateTime.now,
98
+ sync_time: DateTime.now,
99
+ ))
100
+
101
+ present segment_klass, with: Labimotion::SegmentKlassEntity, root: 'klass'
102
+ rescue => e
103
+ Labimotion.log_exception(e, current_user)
104
+ { error: e.message }
105
+ end
106
+ end
107
+
108
+ namespace :fetch_repo_generic_template_list do
109
+ desc 'fetch segment templates from repository'
110
+ get do
111
+ fetch_repo_generic_template_list('Segment')
112
+ rescue => e
113
+ Labimotion.log_exception(e, current_user)
114
+ { error: e.message }
115
+ end
116
+ end
117
+
118
+ namespace :fetch_repo do
119
+ desc 'fetch Generic Segment Klass from Chemotion Repository'
120
+ get do
121
+ fetch_repo('SegmentKlass')
122
+ rescue => e
123
+ Labimotion.log_exception(e, current_user)
124
+ { error: e.message }
125
+ end
126
+ end
127
+
128
+ namespace :create_repo_klass do
129
+ desc 'create Generic Segment Klass'
130
+ params do
131
+ requires :identifier, type: String, desc: 'Identifier'
132
+ end
133
+ post do
134
+ create_repo_klass(params, current_user)
135
+ present SegmentKlass.all, with: Labimotion::SegmentKlassEntity, root: 'klass'
136
+ rescue => e
137
+ Labimotion.log_exception(e, current_user)
138
+ ## { error: e.message }
139
+ raise e
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end