29
29
import com .fasterxml .jackson .core .JsonProcessingException ;
30
30
import com .fasterxml .jackson .databind .ObjectMapper ;
31
31
32
+ import org .apache .commons .collections4 .CollectionUtils ;
32
33
import org .apache .shiro .authz .UnauthorizedException ;
33
34
import org .ohdsi .circe .vocabulary .ConceptSetExpression ;
34
35
import org .ohdsi .vocabulary .Concept ;
43
44
import org .ohdsi .webapi .conceptset .annotation .ConceptSetAnnotation ;
44
45
import org .ohdsi .webapi .exception .ConceptNotExistException ;
45
46
import org .ohdsi .webapi .security .PermissionService ;
47
+ import org .ohdsi .webapi .service .annotations .SearchDataTransformer ;
46
48
import org .ohdsi .webapi .service .dto .AnnotationDetailsDTO ;
47
49
import org .ohdsi .webapi .service .dto .ConceptSetDTO ;
48
- import org .ohdsi .webapi .service .dto .ConceptSetAnnotationDTO ;
50
+ import org .ohdsi .webapi .service .dto .SaveConceptSetAnnotationsRequest ;
49
51
import org .ohdsi .webapi .service .dto .AnnotationDTO ;
50
52
import org .ohdsi .webapi .service .dto .CopyAnnotationsRequest ;
51
53
import org .ohdsi .webapi .shiro .Entities .UserEntity ;
@@ -114,6 +116,12 @@ public class ConceptSetService extends AbstractDaoService implements HasTags<Int
114
116
@ Autowired
115
117
private VersionService <ConceptSetVersion > versionService ;
116
118
119
+ @ Autowired
120
+ private SearchDataTransformer searchDataTransformer ;
121
+
122
+ @ Autowired
123
+ private ObjectMapper mapper ;
124
+
117
125
118
126
@ Value ("${security.defaultGlobalReadPermissions}" )
119
127
private boolean defaultGlobalReadPermissions ;
@@ -875,28 +883,21 @@ private ConceptSetVersion saveVersion(int id) {
875
883
* The body has two parts: 1) the elements new concept which added to the
876
884
* concept set. 2) the elements concept which remove from concept set.
877
885
*
878
- * @param id The concept set ID
879
- * @param dto An object of 2 Array new annotation and remove annotation
886
+ * @param conceptSetId The concept set ID
887
+ * @param request An object of 2 Array new annotation and remove annotation
880
888
* @return Boolean: true if the save is successful
881
889
* @summary Create new or delete concept set annotation items
882
890
*/
883
891
@ PUT
884
892
@ Path ("/{id}/annotation" )
885
893
@ Produces (MediaType .APPLICATION_JSON )
886
894
@ Transactional
887
- public boolean saveConceptSetAnnotation (@ PathParam ("id" ) final int id , ConceptSetAnnotationDTO dto ) {
888
- if (dto .getRemoveAnnotation () != null && !dto .getRemoveAnnotation ().isEmpty ()) {
889
- for (AnnotationDTO annotationDTO : dto .getRemoveAnnotation ()) {
890
- this .getConceptSetAnnotationRepository ().deleteAnnotationByConceptSetIdAndConceptId (id , annotationDTO .getConceptId ());
891
- }
892
- // getConceptSetAnnotationRepository().deleteAnnotationByConceptSetIdAndInConceptId(id,
893
- // dto.getRemoveAnnotation().stream().map(AnnotationDTO::getConceptId).collect(Collectors.toList()));
894
- }
895
- ObjectMapper mapper = new ObjectMapper ();
896
- if (dto .getNewAnnotation () != null && !dto .getNewAnnotation ().isEmpty ()) {
897
- List <ConceptSetAnnotation > annotationList = dto .getNewAnnotation ().stream ().map (m -> {
895
+ public boolean saveConceptSetAnnotation (@ PathParam ("id" ) final int conceptSetId , SaveConceptSetAnnotationsRequest request ) {
896
+ removeAnnotations (conceptSetId , request );
897
+ if (CollectionUtils .isNotEmpty (request .getNewAnnotation ())) {
898
+ List <ConceptSetAnnotation > annotationList = request .getNewAnnotation ().stream ().map (m -> {
898
899
ConceptSetAnnotation conceptSetAnnotation = new ConceptSetAnnotation ();
899
- conceptSetAnnotation .setConceptSetId (id );
900
+ conceptSetAnnotation .setConceptSetId (conceptSetId );
900
901
try {
901
902
AnnotationDetailsDTO annotationDetailsDTO = mapper .readValue (mapper .writeValueAsString (m ), AnnotationDetailsDTO .class );
902
903
conceptSetAnnotation .setAnnotationDetails (mapper .writeValueAsString (annotationDetailsDTO ));
@@ -916,7 +917,13 @@ public boolean saveConceptSetAnnotation(@PathParam("id") final int id, ConceptSe
916
917
917
918
return true ;
918
919
}
919
-
920
+ private void removeAnnotations (int id , SaveConceptSetAnnotationsRequest request ){
921
+ if (CollectionUtils .isNotEmpty (request .getRemoveAnnotation ())) {
922
+ for (AnnotationDTO annotationDTO : request .getRemoveAnnotation ()) {
923
+ this .getConceptSetAnnotationRepository ().deleteAnnotationByConceptSetIdAndConceptId (id , annotationDTO .getConceptId ());
924
+ }
925
+ }
926
+ }
920
927
private ConceptSetAnnotation copyAnnotation (ConceptSetAnnotation sourceConceptSetAnnotation , int targetConceptSetId ){
921
928
ConceptSetAnnotation targetConceptSetAnnotation = new ConceptSetAnnotation ();
922
929
targetConceptSetAnnotation .setConceptSetId (targetConceptSetId );
@@ -947,19 +954,22 @@ public void copyAnnotations(CopyAnnotationsRequest copyAnnotationsRequest ) {
947
954
@ Path ("/{id}/annotation" )
948
955
@ Produces (MediaType .APPLICATION_JSON )
949
956
public List <AnnotationDTO > getConceptSetAnnotation (@ PathParam ("id" ) final int id ) {
950
- ObjectMapper mapper = new ObjectMapper ();
951
957
List <ConceptSetAnnotation > annotationList = getConceptSetAnnotationRepository ().findByConceptSetId (id );
952
958
List <AnnotationDTO > annotationDTOList = new ArrayList <>();
953
959
for (ConceptSetAnnotation conceptSetAnnotation : annotationList ) {
954
- AnnotationDTO annotationDTO = null ;
960
+ AnnotationDTO annotationDTO ;
955
961
try {
956
962
annotationDTO = mapper .readValue (conceptSetAnnotation .getAnnotationDetails (), AnnotationDTO .class );
957
963
annotationDTO .setId (conceptSetAnnotation .getId ());
958
964
annotationDTO .setVocabularyVersion (conceptSetAnnotation .getVocabularyVersion ());
959
965
annotationDTO .setConceptSetVersion (conceptSetAnnotation .getConceptSetVersion ());
960
966
annotationDTO .setCreatedBy (conceptSetAnnotation .getCreatedBy () != null ? conceptSetAnnotation .getCreatedBy ().getName () : null );
961
967
annotationDTO .setCreatedDate (conceptSetAnnotation .getCreatedDate () != null ? conceptSetAnnotation .getCreatedDate ().toString () : null );
962
- annotationDTOList .add (annotationDTO );
968
+
969
+ String searchDataJSON = annotationDTO .getSearchData ();
970
+ String humanReadableData = searchDataTransformer .convertJsonToReadableFormat (searchDataJSON );
971
+ annotationDTO .setSearchData (humanReadableData );
972
+ annotationDTOList .add (annotationDTO );
963
973
} catch (IOException e ) {
964
974
throw new RuntimeException (e );
965
975
}
@@ -982,7 +992,6 @@ public Response deleteConceptSetAnnotation(@PathParam("id") final int id) {
982
992
@ Path ("/update/{id}/annotation" )
983
993
@ Produces (MediaType .APPLICATION_JSON )
984
994
public AnnotationDTO updateConceptSetAnnotation (@ PathParam ("id" ) final int id , AnnotationDTO annotationDTO ) throws IOException {
985
- ObjectMapper mapper = new ObjectMapper ();
986
995
ConceptSetAnnotation conceptSetAnnotation = getConceptSetAnnotationRepository ()
987
996
.findConceptSetAnnotationByConceptIdAndConceptId (id , annotationDTO .getConceptId ())
988
997
.orElseThrow (() -> new RuntimeException ("Concept set annotation not found" ));
0 commit comments