1 | package gov.hhs.fha.nhinc.transform.document;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 | import java.util.Iterator;
|
---|
5 |
|
---|
6 | import javax.xml.bind.JAXBContext;
|
---|
7 | import javax.xml.bind.Marshaller;
|
---|
8 |
|
---|
9 | import gov.hhs.fha.nhinc.nhinclib.NullChecker;
|
---|
10 | import gov.hhs.fha.nhinc.util.format.PatientIdFormatUtil;
|
---|
11 |
|
---|
12 | import java.io.StringWriter;
|
---|
13 | import oasis.names.tc.ebxml_regrep.xsd.query._3.AdhocQueryRequest;
|
---|
14 | import oasis.names.tc.ebxml_regrep.xsd.rim._3.AdhocQueryType;
|
---|
15 | import oasis.names.tc.ebxml_regrep.xsd.rim._3.SlotType1;
|
---|
16 | import oasis.names.tc.ebxml_regrep.xsd.rim._3.ValueListType;
|
---|
17 |
|
---|
18 | import org.apache.commons.logging.Log;
|
---|
19 | import org.apache.commons.logging.LogFactory;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Class to perform transform operations for document query messages.
|
---|
23 | *
|
---|
24 | * @author Neil Webb
|
---|
25 | */
|
---|
26 | public class DocumentQueryTransform
|
---|
27 | {
|
---|
28 | private static Log log = LogFactory.getLog(DocumentQueryTransform.class);
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Replace the patient identifier information in an AdhocQuery message with the information
|
---|
32 | * provided.
|
---|
33 | *
|
---|
34 | * @param sourceQuery Original AdhocQuery message
|
---|
35 | * @param homeCommuinty Home community identifier
|
---|
36 | * @param assigningAuthority Assigning authority
|
---|
37 | * @param patientId Patient identifier
|
---|
38 | * @return Altered AdhocQuery Message
|
---|
39 | */
|
---|
40 | public AdhocQueryRequest replaceAdhocQueryPatientId(AdhocQueryRequest sourceQuery, String homeCommuinty, String assigningAuthority, String patientId)
|
---|
41 | {
|
---|
42 | log.debug("DocumentQueryTransform.replaceAdhocQueryPatientId() -- Begin");
|
---|
43 | AdhocQueryRequest adhocQueryRequest = null;
|
---|
44 |
|
---|
45 | if (sourceQuery != null)
|
---|
46 | {
|
---|
47 | adhocQueryRequest = sourceQuery;
|
---|
48 |
|
---|
49 | // Home community ID
|
---|
50 | //-------------------
|
---|
51 | if (NullChecker.isNotNullish(homeCommuinty))
|
---|
52 | {
|
---|
53 | if (adhocQueryRequest.getAdhocQuery() == null)
|
---|
54 | {
|
---|
55 | adhocQueryRequest.setAdhocQuery(new AdhocQueryType());
|
---|
56 | }
|
---|
57 | adhocQueryRequest.getAdhocQuery().setHome(homeCommuinty);
|
---|
58 | }
|
---|
59 |
|
---|
60 | // Patient ID
|
---|
61 | //-------------
|
---|
62 | if (NullChecker.isNotNullish(patientId) && NullChecker.isNotNullish(assigningAuthority))
|
---|
63 | {
|
---|
64 | if (adhocQueryRequest.getAdhocQuery() == null)
|
---|
65 | {
|
---|
66 | adhocQueryRequest.setAdhocQuery(new AdhocQueryType());
|
---|
67 | }
|
---|
68 |
|
---|
69 | String formattedPatientId = PatientIdFormatUtil.hl7EncodePatientId(patientId, assigningAuthority);
|
---|
70 |
|
---|
71 | // Look for the entries in the slot that contain the patient ID and fix them. If none were found, create one.
|
---|
72 | //-------------------------------------------------------------------------------------------------------------
|
---|
73 | boolean foundEntry = false;
|
---|
74 | List<SlotType1> slotType1 = adhocQueryRequest.getAdhocQuery().getSlot();
|
---|
75 | Iterator<SlotType1> iterSlotType1 = slotType1.iterator();
|
---|
76 | while (iterSlotType1.hasNext())
|
---|
77 | {
|
---|
78 | SlotType1 slot = iterSlotType1.next();
|
---|
79 | if ((slot.getName() != null) &&
|
---|
80 | (slot.getName().equals(DocumentTransformConstants.EBXML_DOCENTRY_PATIENT_ID)))
|
---|
81 | {
|
---|
82 | ValueListType slotValueList = new ValueListType();
|
---|
83 | slot.setValueList(slotValueList);
|
---|
84 | List<String> slotValues = null; // Handle to a list of strings
|
---|
85 | slotValues = slotValueList.getValue();
|
---|
86 | slotValues.add(formattedPatientId);
|
---|
87 | foundEntry = true;
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | // If we did not replace an entry - then we need to create one...
|
---|
92 | //-----------------------------------------------------------------
|
---|
93 | if (!foundEntry)
|
---|
94 | {
|
---|
95 | SlotType1 slot = new SlotType1();
|
---|
96 | slot.setName(DocumentTransformConstants.EBXML_DOCENTRY_PATIENT_ID);
|
---|
97 | ValueListType slotValueList = new ValueListType();
|
---|
98 | slot.setValueList(slotValueList);
|
---|
99 | List<String> slotValues = null; // Handle to a list of strings
|
---|
100 | slotValues = slotValueList.getValue();
|
---|
101 | slotValues.add(formattedPatientId);
|
---|
102 | slotType1.add(slot);
|
---|
103 | } // if (!bFoundEntry)
|
---|
104 | } // if ((oInsertDocQueryPatIds.getQualifiedSubjectId() != null) &&
|
---|
105 | } // if ((oInsertDocQueryPatIds != null) &&
|
---|
106 |
|
---|
107 | if(log.isDebugEnabled())
|
---|
108 | {
|
---|
109 | log.debug("The result as it should be: ");
|
---|
110 | outputAdhocQueryRequest(adhocQueryRequest);
|
---|
111 | log.debug("----------------------------");
|
---|
112 | }
|
---|
113 |
|
---|
114 | log.debug("DocumentQueryTransform.replaceAdhocQueryPatientId() -- End");
|
---|
115 | return adhocQueryRequest;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Output the contents of the adhoc query request.
|
---|
120 | *
|
---|
121 | * @param oAdhocQueryRequest The object to be printed out.
|
---|
122 | */
|
---|
123 | public static void outputAdhocQueryRequest(AdhocQueryRequest oAdhocQueryRequest)
|
---|
124 | {
|
---|
125 | log.debug("DocumentQueryTransform.outputAdhocQueryRequest() -- Begin");
|
---|
126 | try
|
---|
127 | {
|
---|
128 | JAXBContext jc = JAXBContext.newInstance("oasis.names.tc.ebxml_regrep.xsd.query._3");
|
---|
129 | Marshaller marshaller = jc.createMarshaller();
|
---|
130 | StringWriter oXML = new StringWriter();
|
---|
131 | marshaller.marshal(oAdhocQueryRequest, oXML);
|
---|
132 |
|
---|
133 | log.debug("");
|
---|
134 | log.debug(oXML);
|
---|
135 | log.debug("");
|
---|
136 | }
|
---|
137 | catch (Exception e)
|
---|
138 | {
|
---|
139 | log.error("Unexpected exception: " + e.getMessage());
|
---|
140 | e.printStackTrace();
|
---|
141 | }
|
---|
142 | log.debug("DocumentQueryTransform.outputAdhocQueryRequest() -- End");
|
---|
143 | }
|
---|
144 |
|
---|
145 | }
|
---|