forked from OHDSI/WebAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeAnalysisEntityToFeAnalysisDTOConverter.java
78 lines (69 loc) · 3.69 KB
/
FeAnalysisEntityToFeAnalysisDTOConverter.java
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
package org.ohdsi.webapi.feanalysis.converter;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.ohdsi.analysis.cohortcharacterization.design.FeatureAnalysisAggregate;
import org.ohdsi.webapi.feanalysis.domain.*;
import org.ohdsi.webapi.feanalysis.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.hibernate.Hibernate;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static org.ohdsi.analysis.cohortcharacterization.design.StandardFeatureAnalysisType.CRITERIA_SET;
@Component
public class FeAnalysisEntityToFeAnalysisDTOConverter extends BaseFeAnalysisEntityToFeAnalysisDTOConverter<FeAnalysisDTO> {
@Autowired
private ObjectMapper objectMapper;
@Override
public FeAnalysisDTO convert(final FeAnalysisEntity source) {
final FeAnalysisDTO dto = super.convert(source);
dto.setDesign(convertDesignToJson(source));
if (CRITERIA_SET.equals(source.getType())){
FeAnalysisWithConceptSetDTO dtoWithConceptSet = (FeAnalysisWithConceptSetDTO) dto;
FeAnalysisWithCriteriaEntity<?> sourceWithCriteria = (FeAnalysisWithCriteriaEntity) source;
dtoWithConceptSet.setConceptSets(sourceWithCriteria.getConceptSets());
}
return dto;
}
@Override
protected FeAnalysisDTO createResultObject(FeAnalysisEntity feAnalysisEntity) {
switch (feAnalysisEntity.getType()){
case CRITERIA_SET:
return new FeAnalysisWithConceptSetDTO();
default:
return new FeAnalysisDTO();
}
}
private Object convertDesignToJson(final FeAnalysisEntity source) {
switch (source.getType()) {
case CRITERIA_SET:
FeAnalysisWithCriteriaEntity<?> sourceWithCriteria = (FeAnalysisWithCriteriaEntity<?>) source;
Hibernate.initialize(sourceWithCriteria.getDesign()); // Explicitly initialize the collection
return sourceWithCriteria.getDesign()
.stream()
.map(this::convertCriteria)
.map(c -> (JsonNode)objectMapper.valueToTree(c))
.collect(Collectors.toList());
default:
return source.getDesign();
}
}
private BaseFeAnalysisCriteriaDTO convertCriteria(FeAnalysisCriteriaEntity criteriaEntity){
BaseFeAnalysisCriteriaDTO criteriaDTO;
if (criteriaEntity instanceof FeAnalysisCriteriaGroupEntity) {
FeAnalysisCriteriaGroupEntity groupEntity = (FeAnalysisCriteriaGroupEntity) criteriaEntity;
criteriaDTO = new FeAnalysisCriteriaDTO(groupEntity.getId(), groupEntity.getName(), groupEntity.getExpression());
} else if (criteriaEntity instanceof FeAnalysisWindowedCriteriaEntity) {
FeAnalysisWindowedCriteriaEntity w = (FeAnalysisWindowedCriteriaEntity) criteriaEntity;
criteriaDTO = new FeAnalysisWindowedCriteriaDTO(w.getId(), w.getName(), w.getExpression());
} else if (criteriaEntity instanceof FeAnalysisDemographicCriteriaEntity) {
FeAnalysisDemographicCriteriaEntity d = (FeAnalysisDemographicCriteriaEntity) criteriaEntity;
criteriaDTO = new FeAnalysisDemographicCriteriaDTO(d.getId(), d.getName(), d.getExpression());
} else {
throw new IllegalArgumentException(String.format("Cannot convert criteria entity, %s is not supported", criteriaEntity));
}
criteriaDTO.setAggregate(conversionService.convert(criteriaEntity.getAggregate(), FeAnalysisAggregateDTO.class));
return criteriaDTO;
}
}