-
Notifications
You must be signed in to change notification settings - Fork 62
/
MetadataHelper.java
187 lines (160 loc) · 4.88 KB
/
MetadataHelper.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
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
package gov.cms.qpp.conversion.api.helper;
import gov.cms.qpp.conversion.api.model.Constants;
import gov.cms.qpp.conversion.api.model.Metadata;
import gov.cms.qpp.conversion.decode.ClinicalDocumentDecoder;
import gov.cms.qpp.conversion.model.Node;
import gov.cms.qpp.conversion.model.Program;
import gov.cms.qpp.conversion.model.TemplateId;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
/**
* Utilities for working with Metadata beans
*/
public class MetadataHelper {
private static final ThreadLocalRandom RANDOM_HASH = ThreadLocalRandom.current();
/**
* No need for constructor in this utility class
*/
private MetadataHelper() {
//empty
}
/**
* Generates a {@link Metadata} object from a {@link Node}.
* This {@link Metadata} does not contain data not found in a standard {@link Node}.
*
* @param node from which to extract metadata
* @param outcome to update with metadata
* @return metadata
*/
public static Metadata generateMetadata(Node node, Outcome outcome) {
Objects.requireNonNull(outcome, "outcome");
Metadata metadata = new Metadata();
if (node != null) {
metadata.setApm(findApm(node));
metadata.setTin(findTin(node));
metadata.setNpi(findNpi(node));
metadata.setCpc(deriveCpcHash(node));
metadata.setCpcProcessed(false);
}
outcome.setStatus(metadata);
return metadata;
}
/**
* Retrieves the random hash for the Cpc field if this is a CPC+ conversion.
*
* @param node to ensure it's a CPC+ node
* @return Cpc field randomly hashed or null if this isn't a CPC+ conversion
*/
private static String deriveCpcHash(Node node) {
String cpcHash = null;
if (isCpc(node)) {
cpcHash = Constants.CPC_DYNAMO_PARTITION_START + RANDOM_HASH.nextInt(Constants.CPC_DYNAMO_PARTITIONS);
}
return cpcHash;
}
/**
* Retrieves the APM Entity Id from the given node
*
* @param node to interrogate
* @return Apm Entity ID value
*/
private static String findApm(Node node) {
return findValue(node, ClinicalDocumentDecoder.PRACTICE_ID, TemplateId.CLINICAL_DOCUMENT);
}
/**
* Retrieves the Taxpayer Identification Number from the given node
*
* @param node to interrogate
* @return TIN value
*/
private static String findTin(Node node) {
return findValue(node, ClinicalDocumentDecoder.TAX_PAYER_IDENTIFICATION_NUMBER,
TemplateId.CLINICAL_DOCUMENT);
}
/**
* Retrieves the National Provider Identifier from the given node
*
* @param node to interrogate
* @return NPI value
*/
private static String findNpi(Node node) {
return findValue(node, ClinicalDocumentDecoder.NATIONAL_PROVIDER_IDENTIFIER,
TemplateId.CLINICAL_DOCUMENT);
}
/**
* Retrieves the random hash for CPC Field
*
* @param node to interrogate
* @return Cpc field randomly hashed
*/
private static boolean isCpc(Node node) {
if (Program.isCpc(node)) {
return true;
}
Node found = findPossibleChildNode(node, ClinicalDocumentDecoder.RAW_PROGRAM_NAME,
TemplateId.CLINICAL_DOCUMENT);
return found != null && Program.isCpc(found);
}
/**
* Recursively searches a node for a value.
*
* @param node to interrogate
* @param key value name
* @param possibleLocations where the value might reside
* @return the found value or null
*/
private static String findValue(Node node, String key, TemplateId... possibleLocations) {
String value = node.getValue(key);
if (value != null) {
return value;
}
Node found = findPossibleChildNode(node, key, possibleLocations);
return found == null ? null : found.getValue(key);
}
/**
* Finds all possible children within the given node for each {@link TemplateId} given
* filtered by children with specific keys
*
* @param node Object to search through
* @param key value to filter
* @param possibleLocations areas which the child can exist
* @return A child node with the correct value or null
*/
private static Node findPossibleChildNode(Node node, String key, TemplateId... possibleLocations) {
return Arrays.stream(possibleLocations)
.distinct()
.map(node::findNode)
.flatMap(List::stream)
.filter(child -> child.hasValue(key))
.findFirst()
.orElse(null);
}
/**
* Potential states of conversion outcomes.
*/
public enum Outcome {
SUCCESS(true, true, true),
CONVERSION_ERROR(false, false, false),
VALIDATION_ERROR(false, true, false);
private boolean overall;
private boolean conversion;
private boolean validation;
Outcome(boolean overall, boolean conversion, boolean validation) {
this.overall = overall;
this.conversion = conversion;
this.validation = validation;
}
/**
* Set status of conversion on {@link Metadata} instance
*
* @param metadata to update
*/
private void setStatus(Metadata metadata) {
metadata.setOverallStatus(overall);
metadata.setConversionStatus(conversion);
metadata.setValidationStatus(validation);
}
}
}