1 | package gov.hhs.fha.nhinc.util.format;
|
---|
2 |
|
---|
3 | import org.apache.commons.logging.Log;
|
---|
4 | import org.apache.commons.logging.LogFactory;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * Format utility for patient identifiers.
|
---|
8 | *
|
---|
9 | * @author Neil Webb
|
---|
10 | */
|
---|
11 | public class PatientIdFormatUtil
|
---|
12 | {
|
---|
13 | private static Log log = LogFactory.getLog(PatientIdFormatUtil.class);
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Parse an optionally HL7 encoded patient identifier. If the patient
|
---|
17 | * identifier is not HL7 encoded, the original id will be returned.
|
---|
18 | * The format of an HL7 encoded patient id is
|
---|
19 | * "<id>^^^&<home coummunity id>&ISO"
|
---|
20 | *
|
---|
21 | * @param receivedPatientId Optionally HL7 encoded patient identifier
|
---|
22 | * @return Parsed patient id
|
---|
23 | */
|
---|
24 | public static String parsePatientId(String receivedPatientId)
|
---|
25 | {
|
---|
26 | log.debug("Parsing patient id: " + receivedPatientId);
|
---|
27 | String patientId = receivedPatientId;
|
---|
28 | if ((patientId != null) && (patientId.length() > 0))
|
---|
29 | {
|
---|
30 | // In some cases we see a quote - in others we do not. So lets strip them off if we see them.
|
---|
31 | //---------------------------------------------------------------------------------------------
|
---|
32 | if ((patientId.startsWith("'")) && (patientId.length() > 1))
|
---|
33 | {
|
---|
34 | StringBuffer sbPatientId = new StringBuffer(patientId);
|
---|
35 | if (patientId.endsWith("'"))
|
---|
36 | {
|
---|
37 | sbPatientId.deleteCharAt(sbPatientId.length() - 1); // strip off the ending quote
|
---|
38 | }
|
---|
39 | sbPatientId.deleteCharAt(0); // strip off hte first char quote
|
---|
40 |
|
---|
41 | patientId = sbPatientId.toString();
|
---|
42 | }
|
---|
43 |
|
---|
44 | int componentIndex = patientId.indexOf("^");
|
---|
45 | log.debug("Index: " + componentIndex);
|
---|
46 | if (componentIndex != -1)
|
---|
47 | {
|
---|
48 | patientId = patientId.substring(0, componentIndex);
|
---|
49 | log.debug("Parsed patient id: " + patientId);
|
---|
50 | }
|
---|
51 | }
|
---|
52 | return patientId;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Parse an optionally HL7 encoded community id. If the patient
|
---|
57 | * identifier is not HL7 encoded, null will be returned.
|
---|
58 | * The format of an HL7 encoded patient id is
|
---|
59 | * "<id>^^^&<home coummunity id>&ISO"
|
---|
60 | *
|
---|
61 | * @param encodedPatientId Optionally HL7 encoded patient identifier
|
---|
62 | * @return Parsed community id
|
---|
63 | */
|
---|
64 | public static String parseCommunityId(String encodedPatientId)
|
---|
65 | {
|
---|
66 | log.debug("Parsing community id: " + encodedPatientId);
|
---|
67 | String communityId = null;
|
---|
68 | if ((encodedPatientId != null) && (encodedPatientId.length() > 0))
|
---|
69 | {
|
---|
70 | String workingCommunityId = encodedPatientId;
|
---|
71 | // In some cases we see a quote - in others we do not. So lets strip them off if we see them.
|
---|
72 | //---------------------------------------------------------------------------------------------
|
---|
73 | if ((workingCommunityId.startsWith("'")) && (workingCommunityId.length() > 1))
|
---|
74 | {
|
---|
75 | StringBuffer sbCommunityId = new StringBuffer(workingCommunityId);
|
---|
76 | if (workingCommunityId.endsWith("'"))
|
---|
77 | {
|
---|
78 | sbCommunityId.deleteCharAt(sbCommunityId.length() - 1); // strip off the ending quote
|
---|
79 | }
|
---|
80 | sbCommunityId.deleteCharAt(0); // strip off hte first char quote
|
---|
81 |
|
---|
82 | workingCommunityId = sbCommunityId.toString();
|
---|
83 | }
|
---|
84 |
|
---|
85 | // First remove the first components
|
---|
86 | int componentIndex = workingCommunityId.lastIndexOf("^");
|
---|
87 | log.debug("Index: " + componentIndex);
|
---|
88 | if ((componentIndex != -1) && (workingCommunityId.length() > (componentIndex + 1)))
|
---|
89 | {
|
---|
90 | workingCommunityId = workingCommunityId.substring(componentIndex + 1);
|
---|
91 | log.debug("Working community id after first components removed: " + workingCommunityId);
|
---|
92 |
|
---|
93 | if(workingCommunityId.startsWith("&"))
|
---|
94 | {
|
---|
95 | workingCommunityId = workingCommunityId.substring(1);
|
---|
96 | }
|
---|
97 | int subComponentIndex = workingCommunityId.indexOf("&");
|
---|
98 | if(subComponentIndex != -1)
|
---|
99 | {
|
---|
100 | workingCommunityId = workingCommunityId.substring(0, subComponentIndex);
|
---|
101 | }
|
---|
102 | communityId = workingCommunityId;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | return communityId;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * HL7 encode a patient identifier. The resulting format will be:
|
---|
110 | * "<id>^^^&<home coummunity id>&ISO"
|
---|
111 | *
|
---|
112 | * @param patientId Patient identifier
|
---|
113 | * @param homeCommunityId Home community id
|
---|
114 | * @return HL7 encoded patient id
|
---|
115 | */
|
---|
116 | public static String hl7EncodePatientId(String patientId, String homeCommunityId)
|
---|
117 | {
|
---|
118 | // Sometimes the homeCommunityId is prepended with "urn:oid:" for various reasons. We do not
|
---|
119 | // want that included when putting together the Patient ID. If it is there, we need to
|
---|
120 | // strip it off.
|
---|
121 | //---------------------------------------------------------------------------------------------
|
---|
122 | String sLocalHomeCommunityId = homeCommunityId;
|
---|
123 | if (homeCommunityId.startsWith("urn:oid:"))
|
---|
124 | {
|
---|
125 | sLocalHomeCommunityId = sLocalHomeCommunityId.substring("urn:oid:".length());
|
---|
126 | }
|
---|
127 | String encodedPatientId = null;
|
---|
128 | log.debug("Creating HL7 encoded patient id for patient id: " + patientId + ", home community id: " + sLocalHomeCommunityId);
|
---|
129 | if (patientId != null)
|
---|
130 | {
|
---|
131 | encodedPatientId = "'" + patientId + "^^^&" + sLocalHomeCommunityId + "&ISO" + "'";
|
---|
132 | log.debug("HL7 encoded patient id: " + encodedPatientId);
|
---|
133 | }
|
---|
134 | return encodedPatientId;
|
---|
135 | }
|
---|
136 | }
|
---|