-
Notifications
You must be signed in to change notification settings - Fork 62
/
PiSectionEncoder.java
125 lines (109 loc) · 4.25 KB
/
PiSectionEncoder.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
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
package gov.cms.qpp.conversion.encode;
import gov.cms.qpp.conversion.Context;
import gov.cms.qpp.conversion.decode.ClinicalDocumentDecoder;
import gov.cms.qpp.conversion.decode.ReportingParametersActDecoder;
import gov.cms.qpp.conversion.model.Encoder;
import gov.cms.qpp.conversion.model.Node;
import gov.cms.qpp.conversion.model.TemplateId;
import gov.cms.qpp.conversion.model.error.Detail;
import gov.cms.qpp.conversion.model.error.ErrorCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Optional;
/**
* Encoder to serialize PI Section and it's measures
*/
@Encoder(TemplateId.PI_SECTION)
public class PiSectionEncoder extends QppOutputEncoder {
private static final Logger DEV_LOG = LoggerFactory.getLogger(PiSectionEncoder.class);
public static final String SUBMISSION_METHOD = "submissionMethod";
public PiSectionEncoder(Context context) {
super(context);
}
/**
* Encodes an PI Section into the QPP format
*
* @param wrapper JsonWrapper that will represent the PI Section
* @param node Node that represents the PI Section
* @throws EncodeException If an error occurs during encoding
*/
@Override
public void internalEncode(JsonWrapper wrapper, Node node) {
encodeTopLevelValues(wrapper, node);
List<Node> children = node.getChildNodes();
JsonWrapper measurementsWrapper = new JsonWrapper();
encodeChildren(children, measurementsWrapper);
wrapper.putObject("measurements", measurementsWrapper);
Optional.ofNullable(node.getParent()).ifPresent(parent -> pilferParent(wrapper, parent));
encodeReportingParameter(wrapper, node);
}
private void encodeTopLevelValues(JsonWrapper wrapper, Node node) {
wrapper.putString("category", node.getValue("category"));
wrapper.putString(SUBMISSION_METHOD, "electronicHealthRecord");
}
/**
* Encodes the children of the given section
*
* @param children child nodes of the given section
* @param measurementsWrapper wrapper that holds the measurements of a section
*/
private void encodeChildren(List<Node> children, JsonWrapper measurementsWrapper) {
JsonWrapper childWrapper;
for (Node currentChild : children) {
childWrapper = new JsonWrapper();
TemplateId templateId = currentChild.getType();
if (TemplateId.REPORTING_PARAMETERS_ACT != templateId) {
JsonOutputEncoder childEncoder = encoders.get(templateId);
if (childEncoder != null) {
childEncoder.encode(childWrapper, currentChild);
measurementsWrapper.putObject(childWrapper);
} else {
addValidationError(Detail.forErrorAndNode(ErrorCode.ENCODER_MISSING, currentChild));
}
}
}
}
/**
* Mine information from section parent.
*
* @param wrapper section's wrapper
* @param parent the clinical document node
*/
private void pilferParent(JsonWrapper wrapper, Node parent) {
wrapper.putString(ClinicalDocumentDecoder.PROGRAM_NAME,
parent.getValue(ClinicalDocumentDecoder.PROGRAM_NAME));
maintainContinuity(wrapper, parent, ClinicalDocumentDecoder.PROGRAM_NAME);
encodeEntityId(wrapper, parent);
}
/**
* This will add the entityId from the Clinical Document Node
*
* @param wrapper will hold the json format of nodes
* @param parent holds the decoded node sections of clinical document
*/
private void encodeEntityId(JsonWrapper wrapper, Node parent) {
String entityId = parent.getValue(ClinicalDocumentDecoder.PRACTICE_ID);
if (!StringUtils.isEmpty(entityId)) {
wrapper.putString(ClinicalDocumentDecoder.PRACTICE_ID, entityId);
}
}
/**
* Encodes the reporting parameter section
*
* @param wrapper wrapper that holds the section
* @param node PI Section Node
*/
private void encodeReportingParameter(JsonWrapper wrapper, Node node) {
JsonOutputEncoder reportingParamEncoder = encoders.get(TemplateId.REPORTING_PARAMETERS_ACT);
Node reportingChild = node.findFirstNode(TemplateId.REPORTING_PARAMETERS_ACT);
if (reportingChild == null) {
DEV_LOG.error("Missing Reporting Parameters from PI Section");
return;
}
reportingParamEncoder.encode(wrapper, reportingChild, false);
maintainContinuity(wrapper, reportingChild, ReportingParametersActDecoder.PERFORMANCE_END);
maintainContinuity(wrapper, reportingChild, ReportingParametersActDecoder.PERFORMANCE_START);
}
}