Advertisement
rahul0611

error

Jul 21st, 2011
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. package org.openmrs.module.pixpdq;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.Set;
  9.  
  10. import javax.sound.midi.Patch;
  11.  
  12. import org.hibernate.collection.PersistentList;
  13. import org.openmrs.Patient;
  14. import org.openmrs.PatientIdentifier;
  15. import org.openmrs.PersonAddress;
  16. import org.openmrs.PersonAttributeType;
  17.  
  18. import ca.uhn.hl7v2.HL7Exception;
  19. import ca.uhn.hl7v2.model.DataTypeException;
  20. import ca.uhn.hl7v2.model.Type;
  21. import ca.uhn.hl7v2.model.v24.datatype.HD;
  22. import ca.uhn.hl7v2.model.v24.group.RSP_K22_QUERY_RESPONSE;
  23. import ca.uhn.hl7v2.model.v24.message.RSP_K22;
  24. import ca.uhn.hl7v2.model.v24.segment.DSC;
  25. import ca.uhn.hl7v2.model.v24.segment.MSA;
  26. import ca.uhn.hl7v2.model.v24.segment.MSH;
  27. import ca.uhn.hl7v2.model.v24.segment.PID;
  28. import ca.uhn.hl7v2.model.v24.segment.QAK;
  29. import ca.uhn.hl7v2.model.v24.segment.QPD;
  30. import ca.uhn.hl7v2.util.Terser;
  31.  
  32. public class Segments {
  33.  
  34. private static final SimpleDateFormat formatter = new SimpleDateFormat(
  35. "yyyyMMddHHmmssZ");
  36.  
  37. public static final String birhtdateFormat = "yyyyMMdd";
  38.  
  39. /***
  40. * This method sets the MSH segment and returns MSH segment
  41. *
  42. * @param header
  43. * @param msh
  44. * @return
  45. * @throws DataTypeException
  46. */
  47.  
  48. public static MSH populateReplyMSH(HL7MessageHeader header, MSH msh)
  49. throws DataTypeException {
  50.  
  51. // MSH-1
  52. msh.getFieldSeparator().setValue("|");
  53.  
  54. // MSH-2
  55. msh.getEncodingCharacters().setValue("^~\\&");
  56.  
  57. // MSH-3
  58. HD hd = msh.getSendingApplication();
  59. hd.getNamespaceID().setValue(
  60. header.getReceivingApplicationNameSpaceId());
  61. hd.getUniversalID().setValue(
  62. header.getReceivingApplicationUniversalId());
  63. hd.getUniversalIDType().setValue(
  64. header.getReceivingApplicationUniversalTypeId());
  65.  
  66. // MSH-4
  67. hd = msh.getSendingFacility();
  68. hd.getNamespaceID().setValue(header.getReceivingFacilityNameSpaceId());
  69. hd.getUniversalID().setValue(header.getReceivingFacilityUniversalId());
  70. hd.getUniversalIDType().setValue(
  71. header.getReceivingApplicationUniversalTypeId());
  72.  
  73. // MSH-5
  74. hd = msh.getReceivingApplication();
  75. hd.getNamespaceID().setValue(header.getSendingApplicationNameSpaceId());
  76. hd.getUniversalID().setValue(header.getSendingApplicationUniversalId());
  77. hd.getUniversalIDType().setValue(
  78. header.getSendingApplicationUniversalTypeId());
  79.  
  80. // MSH-6
  81. hd = msh.getReceivingFacility();
  82. hd.getNamespaceID().setValue(header.getSendingFacilityNameSpaceId());
  83. hd.getUniversalID().setValue(header.getSendingFacilityUniversalId());
  84. hd.getUniversalIDType().setValue(
  85. header.getSendingFacilityUniversalTypeId());
  86.  
  87. // MSH-7
  88. msh.getDateTimeOfMessage().getTimeOfAnEvent()
  89. .setValue(formatter.format(new Date()));
  90.  
  91. // MSH-9
  92. msh.getMessageType().getMessageType().setValue("RSP");
  93.  
  94. msh.getMessageType().getTriggerEvent().setValue("K22");
  95.  
  96. msh.getMessageType().getMessageStructure().setValue("RSP_K22");
  97.  
  98. // MSH-10
  99. msh.getMessageControlID().setValue(header.getMessageControlId());
  100. // MSH-11
  101. msh.getProcessingID().getProcessingID().setValue("P");
  102. // MSH-12
  103. msh.getVersionID().getVersionID().setValue("2.5");
  104.  
  105. return msh;
  106. }
  107.  
  108. /***
  109. * This method sets the MSA segment of response and returns the MSA segment
  110. *
  111. * @param msa
  112. * @param acknowledgmentCode
  113. * @param messageControlId
  114. * @return
  115. * @throws DataTypeException
  116. */
  117. public static MSA buildMSA(
  118.  
  119. MSA msa, String acknowledgmentCode,
  120.  
  121. String messageControlId) throws DataTypeException {
  122. // MSA-1
  123. msa.getAcknowledgementCode().setValue(acknowledgmentCode);
  124. // MSA-2
  125. msa.getMessageControlID().setValue(messageControlId);
  126.  
  127. return msa;
  128. }
  129.  
  130. /***
  131. * This method sets the QAK segment of response and returns the qak segment
  132. *
  133. * @param qak
  134. * @param queryTag
  135. * @param responseStatus
  136. * @param qpd
  137. * @param hitCount
  138. * @param reply
  139. * @return
  140. * @throws HL7Exception
  141. */
  142. public static QAK buildQAK(QAK qak, String queryTag, String responseStatus,
  143. QPD qpd, String hitCount, RSP_K22 reply, String present,
  144. String remainigp) throws HL7Exception {
  145. // QAK-1
  146. qak.getQueryTag().setValue(queryTag);
  147. // QAK-2
  148. qak.getQueryResponseStatus().setValue(responseStatus);
  149. // QAK-3
  150. Terser t = new Terser(reply);
  151.  
  152. t.set(qak, 1, 0, 1, 1, qpd.getMessageQueryName().getIdentifier()
  153. .getValue().toString());
  154.  
  155. t.set(qak, 1, 0, 2, 1, qpd.getMessageQueryName().getText().getValue()
  156. .toString());
  157.  
  158. t.set(qak, 1, 0, 3, 1, qpd.getMessageQueryName()
  159. .getNameOfCodingSystem().getValue().toString());
  160.  
  161. // QAK-4
  162. qak.getHitCountTotal().setValue(hitCount);
  163. // QAK-5
  164. t.set(qak, 5, 0, 1, 1, present);
  165. // QAK-6
  166. qak.getHitsRemaining().setValue(remainigp);
  167.  
  168. return qak;
  169. }
  170.  
  171. /**
  172. * This Method echoes the 'qpd' segment using terser
  173. *
  174. * @param out
  175. * @param in
  176. * @param reply
  177. * @return
  178. * @throws HL7Exception
  179. */
  180.  
  181. public static QPD replicateQPD(Terser out, Terser in, RSP_K22 reply)
  182. throws HL7Exception {
  183.  
  184. QPD inputQPD = (QPD) in.getSegment("/.QPD");
  185.  
  186. QPD outputQPD = (QPD) out.getSegment("/.QPD");
  187.  
  188. int numFields = inputQPD.numFields();
  189.  
  190. for (int d = 1; d <= numFields; d++) {
  191.  
  192. Type[] reps = inputQPD.getField(d);
  193.  
  194. for (int a = 0; a < reps.length; a++) {
  195.  
  196. for (int b = 1; b <= 5; b++) {
  197.  
  198. for (int c = 1; c <= 5; c++) {
  199.  
  200. out.set(outputQPD, d, a, b, c,
  201. in.get(inputQPD, d, a, b, c));
  202. }
  203. }
  204. }
  205. }
  206. return outputQPD;
  207. }
  208.  
  209. public static void populatePID(Terser out, List<Patient> finalPatients,
  210. List<Patient> totPatients, RSP_K22 reply, int k, int j)
  211. throws HL7Exception {
  212.  
  213. int sequenceNumber = j;
  214.  
  215. List<Integer> sn = new ArrayList<Integer>();
  216.  
  217. List<Integer> sn1 = new ArrayList<Integer>();
  218.  
  219. String patientIdentifier = "";
  220.  
  221. String assigningAuthority = "";
  222. sn.clear();
  223.  
  224. sn1.clear();
  225.  
  226. int reqno = k;
  227.  
  228. while (k > 0) {
  229.  
  230. sn.add(sequenceNumber);
  231.  
  232. sequenceNumber--;
  233.  
  234. k--;
  235.  
  236. }
  237. for (int i = sn.size() - 1; i >= 0; i--) {
  238.  
  239. Integer obj = sn.get(i);
  240.  
  241. sn1.add(obj);
  242. }
  243.  
  244. List<PatientIdentifier> listOfId = new ArrayList<PatientIdentifier>();
  245.  
  246. for (int i = 0; i < finalPatients.size(); i++) {
  247.  
  248. Patient p = finalPatients.get(i);
  249.  
  250. RSP_K22_QUERY_RESPONSE rsp = reply.getQUERY_RESPONSE(i);
  251.  
  252. PID pid = rsp.getPID();
  253.  
  254. // u got 2 now u suh print 1 & 2+
  255.  
  256. out.set(pid, 1, 0, 1, 1, sn1.get(i) + "");
  257.  
  258. // Set PID-3
  259. int listi = 0;
  260.  
  261. System.out.println(listi);
  262.  
  263. if (p != null) {
  264.  
  265. Set<PatientIdentifier> h = null;
  266.  
  267. h = p.getIdentifiers();
  268.  
  269. listOfId.addAll(h);
  270.  
  271. for (int l = 0; l < listOfId.size(); l++) {
  272.  
  273. patientIdentifier = listOfId.get(l).getIdentifier();
  274.  
  275. assigningAuthority = listOfId.get(l).getIdentifierType()
  276. .getName();
  277.  
  278. if (assigningAuthority
  279. .equals("OpenMRS Identification Number")) {
  280. out.set(pid, 3, listi, 1, 1, patientIdentifier);
  281. out.set(pid, 3, listi, 5, 1, "MR");
  282. listi++;
  283. } else {
  284. out.set(pid, 3, listi, 1, 1, patientIdentifier);
  285. out.set(pid, 3, listi, 5, 1, "U");
  286. listi++;
  287. }
  288. }
  289. h.clear();
  290. listOfId.clear();
  291.  
  292. }
  293.  
  294. pid.getAdministrativeSex().setValue(p.getGender());
  295.  
  296. out.set(pid, 5, 0, 1, 1, p.getFamilyName());
  297.  
  298. out.set(pid, 5, 0, 2, 1, p.getGivenName());
  299.  
  300. if (p.getAttribute(new PersonAttributeType(4)) != null) {
  301.  
  302. out.set(pid, 6, 0, 1, 1,
  303. p.getAttribute(new PersonAttributeType(4)) + "");
  304.  
  305. }
  306. // setting race
  307.  
  308. if (p.getAttribute(new PersonAttributeType(1)) != null) {
  309. out.set(pid, 10, 0, 1, 1,
  310. p.getAttribute(new PersonAttributeType(1)) + "");
  311. }
  312.  
  313. Set<PersonAddress> addrs = p.getAddresses();
  314.  
  315. PersonAddress addr = (PersonAddress) addrs.toArray()[0];
  316.  
  317. out.set(pid, 11, 0, 1, 1, addr.getSubregion());
  318.  
  319. out.set(pid, 11, 0, 2, 1, addr.getCityVillage());
  320.  
  321. out.set(pid, 11, 0, 3, 1, addr.getStateProvince());
  322.  
  323. out.set(pid, 11, 0, 5, 1, addr.getPostalCode());
  324.  
  325. out.set(pid, 12, 0, 1, 1, addr.getCountyDistrict());// county
  326. // code
  327.  
  328. // mother's name
  329.  
  330. if (p.getAttribute(new PersonAttributeType(5)) != null) {
  331.  
  332. out.set(pid, 16, 0, 1, 1,
  333. p.getAttribute(new PersonAttributeType(5)) + "");
  334. }
  335. // birthplace
  336.  
  337. if (p.getAttribute(new PersonAttributeType(2)) != null) {
  338.  
  339. out.set(pid, 23, 0, 1, 1,
  340. p.getAttribute(new PersonAttributeType(2)) + "");
  341. }
  342. // citizenship
  343.  
  344. if (p.getAttribute(new PersonAttributeType(3)) != null) {
  345.  
  346. out.set(pid, 26, 0, 1, 1,
  347. p.getAttribute(new PersonAttributeType(3)) + "");
  348.  
  349. }
  350. }
  351.  
  352. }
  353.  
  354. /**
  355. * Populate DSC segments
  356. *
  357. * @param dsc
  358. * @param pointer
  359. * @return
  360. * @throws DataTypeException
  361. */
  362.  
  363. public static DSC populateDSC(DSC dsc, String pointer)
  364. throws DataTypeException {
  365. // DSC-1
  366. dsc.getContinuationPointer().setValue(pointer);
  367. // DSC-2
  368. dsc.getContinuationStyle().setValue("I");
  369. return dsc;
  370. }
  371.  
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement