-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathConceptSetService.java
More file actions
1120 lines (1013 loc) · 45.4 KB
/
ConceptSetService.java
File metadata and controls
1120 lines (1013 loc) · 45.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2015 fdefalco.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ohdsi.webapi.service;
import java.io.ByteArrayOutputStream;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import javax.transaction.Transactional;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.cache.CacheManager;
import javax.cache.configuration.MutableConfiguration;
import org.apache.shiro.authz.UnauthorizedException;
import org.ohdsi.circe.vocabulary.ConceptSetExpression;
import org.ohdsi.vocabulary.Concept;
import org.ohdsi.webapi.check.CheckResult;
import org.ohdsi.webapi.check.checker.conceptset.ConceptSetChecker;
import org.ohdsi.webapi.conceptset.ConceptSet;
import org.ohdsi.webapi.conceptset.ConceptSetExport;
import org.ohdsi.webapi.conceptset.ConceptSetGenerationInfo;
import org.ohdsi.webapi.conceptset.ConceptSetGenerationInfoRepository;
import org.ohdsi.webapi.conceptset.ConceptSetItem;
import org.ohdsi.webapi.conceptset.dto.ConceptSetVersionFullDTO;
import org.ohdsi.webapi.conceptset.annotation.ConceptSetAnnotation;
import org.ohdsi.webapi.exception.ConceptNotExistException;
import org.ohdsi.webapi.security.PermissionService;
import org.ohdsi.webapi.service.annotations.SearchDataTransformer;
import org.ohdsi.webapi.service.dto.AnnotationDetailsDTO;
import org.ohdsi.webapi.service.dto.ConceptSetDTO;
import org.ohdsi.webapi.service.dto.LockedConceptSetsResponse;
import org.ohdsi.webapi.service.dto.SaveConceptSetAnnotationsRequest;
import org.ohdsi.webapi.service.dto.AnnotationDTO;
import org.ohdsi.webapi.service.dto.CopyAnnotationsRequest;
import org.ohdsi.webapi.service.lock.ConceptSetLockingService;
import org.ohdsi.webapi.service.lock.dto.ConceptSetSnapshotActionRequest;
import org.ohdsi.webapi.service.lock.dto.ConceptSetSnapshotParameters;
import org.ohdsi.webapi.service.lock.dto.GetConceptSetSnapshotItemsRequest;
import org.ohdsi.webapi.service.lock.dto.GetConceptSetSnapshotItemsResponse;
import org.ohdsi.webapi.service.lock.dto.IsLockedBatchCheckRequest;
import org.ohdsi.webapi.service.lock.dto.IsLockedBatchCheckResponse;
import org.ohdsi.webapi.shiro.Entities.UserEntity;
import org.ohdsi.webapi.shiro.Entities.UserRepository;
import org.ohdsi.webapi.shiro.management.Security;
import org.ohdsi.webapi.shiro.management.datasource.SourceAccessor;
import org.ohdsi.webapi.source.Source;
import org.ohdsi.webapi.source.SourceInfo;
import org.ohdsi.webapi.source.SourceService;
import org.ohdsi.webapi.tag.domain.HasTags;
import org.ohdsi.webapi.tag.dto.TagNameListRequestDTO;
import org.ohdsi.webapi.util.CacheHelper;
import org.ohdsi.webapi.util.ExportUtil;
import org.ohdsi.webapi.util.NameUtils;
import org.ohdsi.webapi.util.ExceptionUtils;
import org.ohdsi.webapi.versioning.domain.ConceptSetVersion;
import org.ohdsi.webapi.versioning.domain.Version;
import org.ohdsi.webapi.versioning.domain.VersionBase;
import org.ohdsi.webapi.versioning.domain.VersionType;
import org.ohdsi.webapi.versioning.dto.VersionDTO;
import org.ohdsi.webapi.versioning.dto.VersionUpdateDTO;
import org.ohdsi.webapi.versioning.service.VersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Component;
/**
* Provides REST services for working with
* concept sets.
*
* @summary Concept Set
*/
@Component
@Transactional
@Path("/conceptset/")
public class ConceptSetService extends AbstractDaoService implements HasTags<Integer> {
//create cache
@Component
public static class CachingSetup implements JCacheManagerCustomizer {
public static final String CONCEPT_SET_LIST_CACHE = "conceptSetList";
@Override
public void customize(CacheManager cacheManager) {
// due to unit tests causing application contexts to reload cache manager caches, we
// have to check for the existance of a cache before creating it
Set<String> cacheNames = CacheHelper.getCacheNames(cacheManager);
// Evict when a cohort definition is created or updated, or permissions, or tags
if (!cacheNames.contains(CONCEPT_SET_LIST_CACHE)) {
cacheManager.createCache(CONCEPT_SET_LIST_CACHE, new MutableConfiguration<String, Collection<ConceptSetDTO>>()
.setTypes(String.class, (Class<Collection<ConceptSetDTO>>) (Class<?>) List.class)
.setStoreByValue(false)
.setStatisticsEnabled(true));
}
}
}
@Autowired
private ConceptSetGenerationInfoRepository conceptSetGenerationInfoRepository;
@Autowired
private VocabularyService vocabService;
@Autowired
private SourceService sourceService;
@Autowired
private SourceAccessor sourceAccessor;
@Autowired
private UserRepository userRepository;
@Autowired
private GenericConversionService conversionService;
@Autowired
private Security security;
@Autowired
private PermissionService permissionService;
@Autowired
private ConceptSetChecker checker;
@Autowired
private VersionService<ConceptSetVersion> versionService;
@Autowired
private SearchDataTransformer searchDataTransformer;
@Autowired
private ObjectMapper mapper;
@Autowired
private ConceptSetLockingService conceptSetLockingService;
@Value("${security.defaultGlobalReadPermissions}")
private boolean defaultGlobalReadPermissions;
public static final String COPY_NAME = "copyName";
/**
* Get the concept set based in the identifier
*
* @summary Get concept set by ID
* @param id The concept set ID
* @return The concept set definition
*/
@Path("{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public ConceptSetDTO getConceptSet(@PathParam("id") final int id) {
ConceptSet conceptSet = getConceptSetRepository().findById(id);
ExceptionUtils.throwNotFoundExceptionIfNull(conceptSet, String.format("There is no concept set with id = %d.", id));
return conversionService.convert(conceptSet, ConceptSetDTO.class);
}
/**
* Get the full list of concept sets in the WebAPI database
*
* @summary Get all concept sets
* @return A list of all concept sets in the WebAPI database
*/
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Cacheable(cacheNames = ConceptSetService.CachingSetup.CONCEPT_SET_LIST_CACHE, key = "@permissionService.getSubjectCacheKey()")
public Collection<ConceptSetDTO> getConceptSets() {
return getTransactionTemplate().execute(
transactionStatus -> StreamSupport.stream(getConceptSetRepository().findAll().spliterator(), false)
.filter(!defaultGlobalReadPermissions ? entity -> permissionService.hasReadAccess(entity) : entity -> true)
.map(conceptSet -> {
ConceptSetDTO dto = conversionService.convert(conceptSet, ConceptSetDTO.class);
permissionService.fillWriteAccess(conceptSet, dto);
permissionService.fillReadAccess(conceptSet, dto);
return dto;
})
.collect(Collectors.toList()));
}
/**
* Get the list of concept sets that were locked using the snapshot lock feature
*
* @summary Get all locked concept sets
* @return A list of locked concept sets
*/
@GET
@Path("/locked")
@Produces(MediaType.APPLICATION_JSON)
public Collection<LockedConceptSetsResponse> getLockedConceptSets() {
return conceptSetLockingService.getLockedConceptSets(defaultGlobalReadPermissions);
}
/**
* Get the concept set items for a selected concept set ID.
*
* @summary Get the concept set items
* @param id The concept set identifier
* @return A list of concept set items
*/
@GET
@Path("{id}/items")
@Produces(MediaType.APPLICATION_JSON)
public Iterable<ConceptSetItem> getConceptSetItems(@PathParam("id") final int id) {
return getConceptSetItemRepository().findAllByConceptSetId(id);
}
/**
* Get the concept set expression for a selected version of the expression
*
* @summary Get concept set expression by version
* @param id The concept set ID
* @param version The version identifier
* @return The concept set expression
*/
@GET
@Path("{id}/version/{version}/expression")
@Produces(MediaType.APPLICATION_JSON)
public ConceptSetExpression getConceptSetExpression(@PathParam("id") final int id,
@PathParam("version") final int version) {
SourceInfo sourceInfo = sourceService.getPriorityVocabularySourceInfo();
if (sourceInfo == null) {
throw new UnauthorizedException();
}
return getConceptSetExpression(id, version, sourceInfo);
}
/**
* Get the concept set expression by version for the selected
* source key. NOTE: This method requires the specification
* of a source key but it does not appear to be used by the underlying
* code.
*
* @summary Get concept set expression by version and source.
* @param id The concept set identifier
* @param version The version of the concept set
* @param sourceKey The source key
* @return The concept set expression for the selected version
*/
@GET
@Path("{id}/version/{version}/expression/{sourceKey}")
@Produces(MediaType.APPLICATION_JSON)
public ConceptSetExpression getConceptSetExpression(@PathParam("id") final int id,
@PathParam("version") final int version,
@PathParam("sourceKey") final String sourceKey) {
SourceInfo sourceInfo = sourceService.getPriorityVocabularySourceInfo();
if (sourceInfo == null) {
throw new UnauthorizedException();
}
return getConceptSetExpression(id, version, sourceInfo);
}
/**
* Get the concept set expression by identifier
*
* @summary Get concept set by ID
* @param id The concept set identifier
* @return The concept set expression
*/
@GET
@Path("{id}/expression")
@Produces(MediaType.APPLICATION_JSON)
public ConceptSetExpression getConceptSetExpression(@PathParam("id") final int id) {
SourceInfo sourceInfo = sourceService.getPriorityVocabularySourceInfo();
if (sourceInfo == null) {
throw new UnauthorizedException();
}
return getConceptSetExpression(id, null, sourceInfo);
}
/**
* Get the concept set expression by identifier and source key
*
* @summary Get concept set by ID and source
* @param id The concept set ID
* @param sourceKey The source key
* @return The concept set expression
*/
@GET
@Path("{id}/expression/{sourceKey}")
@Produces(MediaType.APPLICATION_JSON)
public ConceptSetExpression getConceptSetExpression(@PathParam("id") final int id, @PathParam("sourceKey") final String sourceKey) {
Source source = sourceService.findBySourceKey(sourceKey);
sourceAccessor.checkAccess(source);
return getConceptSetExpression(id, null, source.getSourceInfo());
}
private ConceptSetExpression getConceptSetExpression(int id, Integer version, SourceInfo sourceInfo) {
HashMap<Long, Concept> map = new HashMap<>();
// create our expression to return
ConceptSetExpression expression = new ConceptSetExpression();
ArrayList<ConceptSetExpression.ConceptSetItem> expressionItems = new ArrayList<>();
List<ConceptSetItem> repositoryItems = new ArrayList<>();
if (Objects.isNull(version)) {
getConceptSetItems(id).forEach(repositoryItems::add);
} else {
ConceptSetVersionFullDTO dto = getVersion(id, version);
repositoryItems.addAll(dto.getItems());
}
// collect the unique concept IDs so we can load the concept object later.
for (ConceptSetItem csi : repositoryItems) {
map.put(csi.getConceptId(), null);
}
// lookup the concepts we need information for
long[] identifiers = new long[map.size()];
int identifierIndex = 0;
for (Long identifier : map.keySet()) {
identifiers[identifierIndex] = identifier;
identifierIndex++;
}
String sourceKey;
if (Objects.isNull(sourceInfo)) {
sourceKey = sourceService.getPriorityVocabularySource().getSourceKey();
} else {
sourceKey = sourceInfo.sourceKey;
}
Collection<Concept> concepts = vocabService.executeIdentifierLookup(sourceKey, identifiers);
if (concepts.size() != identifiers.length) {
String ids = Arrays.stream(identifiers).boxed()
.filter(identifier -> concepts.stream().noneMatch(c -> c.conceptId.equals(identifier)))
.map(String::valueOf)
.collect(Collectors.joining(",", "(", ")"));
throw new ConceptNotExistException("Current data source does not contain required concepts " + ids);
}
for(Concept concept : concepts) {
map.put(concept.conceptId, concept); // associate the concept object to the conceptID in the map
}
// put the concept information into the expression along with the concept set item information
for (ConceptSetItem repositoryItem : repositoryItems) {
ConceptSetExpression.ConceptSetItem currentItem = new ConceptSetExpression.ConceptSetItem();
currentItem.concept = map.get(repositoryItem.getConceptId());
currentItem.includeDescendants = (repositoryItem.getIncludeDescendants() == 1);
currentItem.includeMapped = (repositoryItem.getIncludeMapped() == 1);
currentItem.isExcluded = (repositoryItem.getIsExcluded() == 1);
expressionItems.add(currentItem);
}
expression.items = expressionItems.toArray(new ConceptSetExpression.ConceptSetItem[0]); // this will return a new array
return expression;
}
/**
* Check if the concept set name exists (DEPRECATED)
*
* @summary DO NOT USE
* @deprecated
* @param id The concept set ID
* @param sourceKey The source key
* @return The concept set expression
*/
@Deprecated
@GET
@Path("{id}/{name}/exists")
@Produces(MediaType.APPLICATION_JSON)
public Response getConceptSetExistsDeprecated(@PathParam("id") final int id, @PathParam("name") String name) {
String warningMessage = "This method will be deprecated in the next release. Instead, please use the new REST endpoint: conceptset/{id}/exists?name={name}";
Collection<ConceptSet> cs = getConceptSetRepository().conceptSetExists(id, name);
return Response.ok(cs).header("Warning: 299", warningMessage).build();
}
/**
* Check if a concept set with the same name exists in the WebAPI
* database. The name is checked against the selected concept set ID
* to ensure that only the selected concept set ID has the name specified.
*
* @summary Concept set with same name exists
* @param id The concept set ID
* @param name The name of the concept set
* @return The count of concept sets with the name, excluding the
* specified concept set ID.
*/
@GET
@Path("/{id}/exists")
@Produces(MediaType.APPLICATION_JSON)
public int getCountCSetWithSameName(@PathParam("id") @DefaultValue("0") final int id, @QueryParam("name") String name) {
return getConceptSetRepository().getCountCSetWithSameName(id, name);
}
/**
* Update the concept set items for the selected concept set ID in the
* WebAPI database.
*
* The concept set has two parts: 1) the elements of the ConceptSetDTO that
* consist of the identifier, name, etc. 2) the concept set items which
* contain the concepts and their mapping (i.e. include descendants).
*
* @summary Update concept set items
* @param id The concept set ID
* @param items An array of ConceptSetItems
* @return Boolean: true if the save is successful
*/
@PUT
@Path("{id}/items")
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public boolean saveConceptSetItems(@PathParam("id") final int id, ConceptSetItem[] items) {
getConceptSetItemRepository().deleteByConceptSetId(id);
for (ConceptSetItem csi : items) {
// ID must be set to null in case of copying from version, so the new item will be created
csi.setId(0);
csi.setConceptSetId(id);
getConceptSetItemRepository().save(csi);
}
return true;
}
/**
* Exports a list of concept sets, based on the conceptSetList argument,
* to one or more comma separated value (CSV) file(s), compresses the files
* into a ZIP file and sends the ZIP file to the client.
*
* @summary Export concept set list to CSV files
* @param conceptSetList A list of concept set identifiers in the format
* conceptset=<concept_set_id_1>+<concept_set_id_2>+<concept_set_id_n>
* @return
* @throws Exception
*/
@GET
@Path("/exportlist")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response exportConceptSetList(@QueryParam("conceptsets") final String conceptSetList) throws Exception {
ArrayList<Integer> conceptSetIds = new ArrayList<>();
try {
String[] conceptSetItems = conceptSetList.split("\\+");
for(String csi : conceptSetItems) {
conceptSetIds.add(Integer.valueOf(csi));
}
if (conceptSetIds.size() <= 0) {
throw new IllegalArgumentException("You must supply a querystring value for conceptsets that is of the form: ?conceptset=<concept_set_id_1>+<concept_set_id_2>+<concept_set_id_n>");
}
} catch (Exception e) {
throw e;
}
ByteArrayOutputStream baos;
Source source = sourceService.getPriorityVocabularySource();
ArrayList<ConceptSetExport> cs = new ArrayList<>();
Response response = null;
try {
// Load all of the concept sets requested
for (int i = 0; i < conceptSetIds.size(); i++) {
// Get the concept set information
cs.add(getConceptSetForExport(conceptSetIds.get(i), new SourceInfo(source)));
}
// Write Concept Set Expression to a CSV
baos = ExportUtil.writeConceptSetExportToCSVAndZip(cs);
response = Response
.ok(baos)
.type(MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment; filename=\"conceptSetExport.zip\"")
.build();
} catch (Exception ex) {
throw ex;
}
return response;
}
/**
* Exports a single concept set to a comma separated value (CSV)
* file, compresses to a ZIP file and sends to the client.
* @param id The concept set ID
* @return A zip file containing the exported concept set
* @throws Exception
*/
@GET
@Path("{id}/export")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response exportConceptSetToCSV(@PathParam("id") final String id) throws Exception {
return this.exportConceptSetList(id);
}
/**
* Save a new concept set to the WebAPI database
*
* @summary Create a new concept set
* @param conceptSetDTO The concept set to save
* @return The concept set saved with the concept set identifier
*/
@Path("/")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@CacheEvict(cacheNames = CachingSetup.CONCEPT_SET_LIST_CACHE, allEntries = true)
public ConceptSetDTO createConceptSet(ConceptSetDTO conceptSetDTO) {
UserEntity user = userRepository.findByLogin(security.getSubject());
ConceptSet conceptSet = conversionService.convert(conceptSetDTO, ConceptSet.class);
ConceptSet updated = new ConceptSet();
updated.setCreatedBy(user);
updated.setCreatedDate(new Date());
updated.setTags(null);
updateConceptSet(updated, conceptSet);
return conversionService.convert(updated, ConceptSetDTO.class);
}
/**
* Creates a concept set name, based on the selected concept set ID,
* that is used when generating a copy of an existing concept set. This
* function is generally used in conjunction with the copy endpoint to
* create a unique name and then save a copy of an existing concept set.
*
* @sumamry Get concept set name suggestion for copying
* @param id The concept set ID
* @return A map of the new concept set name and the existing concept set
* name
*/
@GET
@Path("/{id}/copy-name")
@Produces(MediaType.APPLICATION_JSON)
public Map<String, String> getNameForCopy (@PathParam("id") final int id){
ConceptSetDTO source = getConceptSet(id);
String name = NameUtils.getNameForCopy(source.getName(), this::getNamesLike, getConceptSetRepository().findByName(source.getName()));
return Collections.singletonMap(COPY_NAME, name);
}
public List<String> getNamesLike(String copyName) {
return getConceptSetRepository().findAllByNameStartsWith(copyName).stream().map(ConceptSet::getName).collect(Collectors.toList());
}
/**
* Updates the concept set for the selected concept set.
*
* The concept set has two parts: 1) the elements of the ConceptSetDTO that
* consist of the identifier, name, etc. 2) the concept set items which
* contain the concepts and their mapping (i.e. include descendants).
*
* @summary Update concept set
* @param id The concept set identifier
* @param conceptSetDTO The concept set header
* @return The
* @throws Exception
*/
@Path("/{id}")
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
@CacheEvict(cacheNames = CachingSetup.CONCEPT_SET_LIST_CACHE, allEntries = true)
public ConceptSetDTO updateConceptSet(@PathParam("id") final int id, ConceptSetDTO conceptSetDTO) throws Exception {
ConceptSet updated = getConceptSetRepository().findById(id);
if (updated == null) {
throw new Exception("Concept Set does not exist.");
}
saveVersion(id);
ConceptSet conceptSet = conversionService.convert(conceptSetDTO, ConceptSet.class);
return conversionService.convert(updateConceptSet(updated, conceptSet), ConceptSetDTO.class);
}
@Path("/{id}/snapshots")
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<ConceptSetSnapshotParameters> listSnapshots(@PathParam("id") final int id) throws Exception {
return conceptSetLockingService.listSnapshotsByConceptSetId(id);
}
@Path("/{id}/snapshot")
@GET
@Produces(MediaType.APPLICATION_JSON)
public ConceptSetSnapshotParameters getLastSnapshot(@PathParam("id") final int id) throws Exception {
return conceptSetLockingService.getLastSnapshotByConceptSetId(id);
}
@POST
@Path("/{id}/snapshot")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response invokeSnapshotAction(@PathParam("id") final int id, ConceptSetSnapshotActionRequest snapshotActionRequest) {
try {
Supplier<ConceptSetExpression> conceptSetExpressionSupplier = () -> getConceptSetExpression(id, snapshotActionRequest.getSourceKey());
conceptSetLockingService.invokeSnapshotAction(id, snapshotActionRequest, conceptSetExpressionSupplier);
return Response.ok().entity("Snapshot action successfully invoked.").build();
} catch (Exception e) {
log.error("Invoke snapshot action failed", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("Invoke snapshot action failed: " + e.getMessage())
.build();
}
}
@POST
@Path("/locked")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response checkIsLockedBatch(IsLockedBatchCheckRequest isLockedBatchCheckRequest) {
IsLockedBatchCheckResponse response = new IsLockedBatchCheckResponse();
try {
List<Integer> ids = isLockedBatchCheckRequest.getConceptSetIds();
Map<Integer, Boolean> lockStatuses = conceptSetLockingService.areLocked(ids);
response.setLockStatus(lockStatuses);
return Response.ok(response).build();
} catch (Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("Error checking lock statuses: " + e.getMessage())
.build();
}
}
@POST
@Path("/snapshot-items")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response getSnapshotItems(GetConceptSetSnapshotItemsRequest request) {
try {
List<ConceptSetExpression.ConceptSetItem> conceptSetItems = conceptSetLockingService.getConceptSetSnapshotItemsBySnapshotId(request.getSnapshotId(), request.getSnapshotItemType());
GetConceptSetSnapshotItemsResponse response = new GetConceptSetSnapshotItemsResponse();
response.setConceptSetItems(conceptSetItems);
return Response.ok(response).build();
} catch (Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("Error fetching snapshot items: " + e.getMessage())
.build();
}
}
private ConceptSet updateConceptSet(ConceptSet dst, ConceptSet src) {
UserEntity user = userRepository.findByLogin(security.getSubject());
dst.setName(src.getName());
dst.setDescription(src.getDescription());
dst.setModifiedDate(new Date());
dst.setModifiedBy(user);
dst = this.getConceptSetRepository().save(dst);
return dst;
}
private ConceptSetExport getConceptSetForExport(int conceptSetId, SourceInfo vocabSource) {
ConceptSetExport cs = new ConceptSetExport();
// Set the concept set id
cs.ConceptSetId = conceptSetId;
// Get the concept set information
cs.ConceptSetName = this.getConceptSet(conceptSetId).getName();
// Get the concept set expression
cs.csExpression = this.getConceptSetExpression(conceptSetId);
// Lookup the identifiers
cs.identifierConcepts = vocabService.executeIncludedConceptLookup(vocabSource.sourceKey, cs.csExpression); //vocabService.executeIdentifierLookup(vocabSource.sourceKey, conceptIds);
// Lookup the mapped items
cs.mappedConcepts = vocabService.executeMappedLookup(vocabSource.sourceKey, cs.csExpression);
return cs;
}
/**
* Get the concept set generation information for the selected concept
* set ID. This function only works with the configuration of the CEM
* data source.
*
* @link https://github.com/OHDSI/CommonEvidenceModel/wiki
*
* @summary Get concept set generation info
* @param id The concept set identifier.
* @return A collection of concept set generation info objects
*/
@GET
@Path("{id}/generationinfo")
@Produces(MediaType.APPLICATION_JSON)
public Collection<ConceptSetGenerationInfo> getConceptSetGenerationInfo(@PathParam("id") final int id) {
return this.conceptSetGenerationInfoRepository.findAllByConceptSetId(id);
}
/**
* Delete the selected concept set by concept set identifier
*
* @summary Delete concept set
* @param id The concept set ID
*/
@DELETE
@Transactional(rollbackOn = Exception.class, dontRollbackOn = EmptyResultDataAccessException.class)
@Path("{id}")
@CacheEvict(cacheNames = CachingSetup.CONCEPT_SET_LIST_CACHE, allEntries = true)
public void deleteConceptSet(@PathParam("id") final int id) {
// Remove any generation info
try {
this.conceptSetGenerationInfoRepository.deleteByConceptSetId(id);
} catch (EmptyResultDataAccessException e) {
// Ignore - there may be no data
log.warn("Failed to delete Generation Info by ConceptSet with ID = {}, {}", id, e);
}
catch (Exception e) {
throw e;
}
// Remove the concept set items
try {
getConceptSetItemRepository().deleteByConceptSetId(id);
} catch (EmptyResultDataAccessException e) {
// Ignore - there may be no data
log.warn("Failed to delete ConceptSet items with ID = {}, {}", id, e);
}
catch (Exception e) {
throw e;
}
// Remove the concept set
try {
getConceptSetRepository().delete(id);
} catch (EmptyResultDataAccessException e) {
// Ignore - there may be no data
log.warn("Failed to delete ConceptSet with ID = {}, {}", id, e);
}
catch (Exception e) {
throw e;
}
}
/**
* Assign tag to Concept Set
*
* @summary Assign concept set tag
* @since v2.10.0
* @param id The concept set ID
* @param tagId The tag ID
*/
@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}/tag/")
@Transactional
@CacheEvict(cacheNames = CachingSetup.CONCEPT_SET_LIST_CACHE, allEntries = true)
public void assignTag(@PathParam("id") final Integer id, final int tagId) {
ConceptSet entity = getConceptSetRepository().findById(id);
assignTag(entity, tagId);
}
/**
* Unassign tag from Concept Set
*
* @summary Remove tag from concept set
* @since v2.10.0
* @param id The concept set ID
* @param tagId The tag ID
*/
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}/tag/{tagId}")
@Transactional
public void unassignTag(@PathParam("id") final Integer id, @PathParam("tagId") final int tagId) {
ConceptSet entity = getConceptSetRepository().findById(id);
unassignTag(entity, tagId);
}
/**
* Assign protected tag to Concept Set
*
* @summary Assign protected concept set tag
* @since v2.10.0
* @param id The concept set ID
* @param tagId The tag ID
*/
@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}/protectedtag/")
@Transactional
public void assignPermissionProtectedTag(@PathParam("id") final int id, final int tagId) {
assignTag(id, tagId);
}
/**
* Unassign protected tag from Concept Set
*
* @summary Remove protected concept set tag
* @since v2.10.0
* @param id The concept set ID
* @param tagId The tag ID
*/
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}/protectedtag/{tagId}")
@Transactional
public void unassignPermissionProtectedTag(@PathParam("id") final int id, @PathParam("tagId") final int tagId) {
unassignTag(id, tagId);
}
/**
* Checks a concept set for diagnostic problems. At this time,
* this appears to be an endpoint used to check to see which tags
* are applied to a concept set.
*
* @summary Concept set tag check
* @since v2.10.0
* @param conceptSetDTO The concept set
* @return A check result
*/
@POST
@Path("/check")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Transactional
public CheckResult runDiagnostics(ConceptSetDTO conceptSetDTO) {
return new CheckResult(checker.check(conceptSetDTO));
}
/**
* Get a list of versions of the selected concept set
*
* @summary Get concept set version list
* @since v2.10.0
* @param id The concept set ID
* @return A list of version information
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}/version/")
@Transactional
public List<VersionDTO> getVersions(@PathParam("id") final int id) {
List<VersionBase> versions = versionService.getVersions(VersionType.CONCEPT_SET, id);
return versions.stream()
.map(v -> conversionService.convert(v, VersionDTO.class))
.collect(Collectors.toList());
}
/**
* Get a specific version of a concept set
*
* @summary Get concept set by version
* @since v2.10.0
* @param id The concept set ID
* @param version The version ID
* @return The concept set for the selected version
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}/version/{version}")
@Transactional
public ConceptSetVersionFullDTO getVersion(@PathParam("id") final int id, @PathParam("version") final int version) {
checkVersion(id, version, false);
ConceptSetVersion conceptSetVersion = versionService.getById(VersionType.CONCEPT_SET, id, version);
return conversionService.convert(conceptSetVersion, ConceptSetVersionFullDTO.class);
}
/**
* Update a specific version of a selected concept set
*
* @summary Update a concept set version
* @since v2.10.0
* @param id The concept set ID
* @param version The version ID
* @param updateDTO The version update
* @return The version information
*/
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}/version/{version}")
@Transactional
public VersionDTO updateVersion(@PathParam("id") final int id, @PathParam("version") final int version,
VersionUpdateDTO updateDTO) {
checkVersion(id, version);
updateDTO.setAssetId(id);
updateDTO.setVersion(version);
ConceptSetVersion updated = versionService.update(VersionType.CONCEPT_SET, updateDTO);
return conversionService.convert(updated, VersionDTO.class);
}
/**
* Delete a version of a concept set
*
* @summary Delete a concept set version
* @since v2.10.0
* @param id The concept ID
* @param version THe version ID
*/
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}/version/{version}")
@Transactional
public void deleteVersion(@PathParam("id") final int id, @PathParam("version") final int version) {
checkVersion(id, version);
versionService.delete(VersionType.CONCEPT_SET, id, version);
}
/**
* Create a new asset from a specific version of the selected
* concept set
*
* @summary Create a concept set copy from a specific concept set version
* @since v2.10.0
* @param id The concept set ID
* @param version The version ID
* @return The concept set copy
*/
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}/version/{version}/createAsset")
@Transactional
@CacheEvict(cacheNames = CachingSetup.CONCEPT_SET_LIST_CACHE, allEntries = true)
public ConceptSetDTO copyAssetFromVersion(@PathParam("id") final int id, @PathParam("version") final int version) {
checkVersion(id, version, false);
ConceptSetVersion conceptSetVersion = versionService.getById(VersionType.CONCEPT_SET, id, version);
ConceptSetVersionFullDTO fullDTO = conversionService.convert(conceptSetVersion, ConceptSetVersionFullDTO.class);
ConceptSetDTO conceptSetDTO = fullDTO.getEntityDTO();
// Reset id so it won't be used during saving
conceptSetDTO.setId(0);
conceptSetDTO.setTags(null);
conceptSetDTO.setName(NameUtils.getNameForCopy(conceptSetDTO.getName(), this::getNamesLike, getConceptSetRepository().findByName(conceptSetDTO.getName())));
ConceptSetDTO createdDTO = createConceptSet(conceptSetDTO);
saveConceptSetItems(createdDTO.getId(), fullDTO.getItems().toArray(new ConceptSetItem[0]));
return createdDTO;
}
/**
* Get list of concept sets with their assigned tags
*
* @summary Get concept sets and tag information
* @param requestDTO The tagNameListRequest
* @return A list of concept sets with their assigned tags
*/
@POST
@Path("/byTags")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public List<ConceptSetDTO> listByTags(TagNameListRequestDTO requestDTO) {
if (requestDTO == null || requestDTO.getNames() == null || requestDTO.getNames().isEmpty()) {
return Collections.emptyList();
}
List<String> names = requestDTO.getNames().stream()
.map(name -> name.toLowerCase(Locale.ROOT))
.collect(Collectors.toList());
List<ConceptSet> entities = getConceptSetRepository().findByTags(names);
return listByTags(entities, names, ConceptSetDTO.class);
}
private void checkVersion(int id, int version) {
checkVersion(id, version, true);
}
private void checkVersion(int id, int version, boolean checkOwnerShip) {
Version conceptSetVersion = versionService.getById(VersionType.CONCEPT_SET, id, version);
ExceptionUtils.throwNotFoundExceptionIfNull(conceptSetVersion, String.format("There is no concept set version with id = %d.", version));
ConceptSet entity = getConceptSetRepository().findOne(id);
if (checkOwnerShip) {
checkOwnerOrAdminOrGranted(entity);
}
}
private ConceptSetVersion saveVersion(int id) {
ConceptSet def = getConceptSetRepository().findById(id);
ConceptSetVersion version = conversionService.convert(def, ConceptSetVersion.class);
UserEntity user = Objects.nonNull(def.getModifiedBy()) ? def.getModifiedBy() : def.getCreatedBy();
Date versionDate = Objects.nonNull(def.getModifiedDate()) ? def.getModifiedDate() : def.getCreatedDate();
version.setCreatedBy(user);
version.setCreatedDate(versionDate);
return versionService.create(VersionType.CONCEPT_SET, version);
}
/**
* Update the concept set annotation for each concept in concept set ID in the
* WebAPI database.
* <p>
* The body has two parts: 1) the elements new concept which added to the
* concept set. 2) the elements concept which remove from concept set.