Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.openmrs.module.pixpdq;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Set;
- import javax.sound.midi.Patch;
- import org.hibernate.collection.PersistentList;
- import org.openmrs.Patient;
- import org.openmrs.PatientIdentifier;
- import org.openmrs.PersonAddress;
- import org.openmrs.PersonAttributeType;
- import ca.uhn.hl7v2.HL7Exception;
- import ca.uhn.hl7v2.model.DataTypeException;
- import ca.uhn.hl7v2.model.Type;
- import ca.uhn.hl7v2.model.v24.datatype.HD;
- import ca.uhn.hl7v2.model.v24.group.RSP_K22_QUERY_RESPONSE;
- import ca.uhn.hl7v2.model.v24.message.RSP_K22;
- import ca.uhn.hl7v2.model.v24.segment.DSC;
- import ca.uhn.hl7v2.model.v24.segment.MSA;
- import ca.uhn.hl7v2.model.v24.segment.MSH;
- import ca.uhn.hl7v2.model.v24.segment.PID;
- import ca.uhn.hl7v2.model.v24.segment.QAK;
- import ca.uhn.hl7v2.model.v24.segment.QPD;
- import ca.uhn.hl7v2.util.Terser;
- public class Segments {
- private static final SimpleDateFormat formatter = new SimpleDateFormat(
- "yyyyMMddHHmmssZ");
- public static final String birhtdateFormat = "yyyyMMdd";
- /***
- * This method sets the MSH segment and returns MSH segment
- *
- * @param header
- * @param msh
- * @return
- * @throws DataTypeException
- */
- public static MSH populateReplyMSH(HL7MessageHeader header, MSH msh)
- throws DataTypeException {
- // MSH-1
- msh.getFieldSeparator().setValue("|");
- // MSH-2
- msh.getEncodingCharacters().setValue("^~\\&");
- // MSH-3
- HD hd = msh.getSendingApplication();
- hd.getNamespaceID().setValue(
- header.getReceivingApplicationNameSpaceId());
- hd.getUniversalID().setValue(
- header.getReceivingApplicationUniversalId());
- hd.getUniversalIDType().setValue(
- header.getReceivingApplicationUniversalTypeId());
- // MSH-4
- hd = msh.getSendingFacility();
- hd.getNamespaceID().setValue(header.getReceivingFacilityNameSpaceId());
- hd.getUniversalID().setValue(header.getReceivingFacilityUniversalId());
- hd.getUniversalIDType().setValue(
- header.getReceivingApplicationUniversalTypeId());
- // MSH-5
- hd = msh.getReceivingApplication();
- hd.getNamespaceID().setValue(header.getSendingApplicationNameSpaceId());
- hd.getUniversalID().setValue(header.getSendingApplicationUniversalId());
- hd.getUniversalIDType().setValue(
- header.getSendingApplicationUniversalTypeId());
- // MSH-6
- hd = msh.getReceivingFacility();
- hd.getNamespaceID().setValue(header.getSendingFacilityNameSpaceId());
- hd.getUniversalID().setValue(header.getSendingFacilityUniversalId());
- hd.getUniversalIDType().setValue(
- header.getSendingFacilityUniversalTypeId());
- // MSH-7
- msh.getDateTimeOfMessage().getTimeOfAnEvent()
- .setValue(formatter.format(new Date()));
- // MSH-9
- msh.getMessageType().getMessageType().setValue("RSP");
- msh.getMessageType().getTriggerEvent().setValue("K22");
- msh.getMessageType().getMessageStructure().setValue("RSP_K22");
- // MSH-10
- msh.getMessageControlID().setValue(header.getMessageControlId());
- // MSH-11
- msh.getProcessingID().getProcessingID().setValue("P");
- // MSH-12
- msh.getVersionID().getVersionID().setValue("2.5");
- return msh;
- }
- /***
- * This method sets the MSA segment of response and returns the MSA segment
- *
- * @param msa
- * @param acknowledgmentCode
- * @param messageControlId
- * @return
- * @throws DataTypeException
- */
- public static MSA buildMSA(
- MSA msa, String acknowledgmentCode,
- String messageControlId) throws DataTypeException {
- // MSA-1
- msa.getAcknowledgementCode().setValue(acknowledgmentCode);
- // MSA-2
- msa.getMessageControlID().setValue(messageControlId);
- return msa;
- }
- /***
- * This method sets the QAK segment of response and returns the qak segment
- *
- * @param qak
- * @param queryTag
- * @param responseStatus
- * @param qpd
- * @param hitCount
- * @param reply
- * @return
- * @throws HL7Exception
- */
- public static QAK buildQAK(QAK qak, String queryTag, String responseStatus,
- QPD qpd, String hitCount, RSP_K22 reply, String present,
- String remainigp) throws HL7Exception {
- // QAK-1
- qak.getQueryTag().setValue(queryTag);
- // QAK-2
- qak.getQueryResponseStatus().setValue(responseStatus);
- // QAK-3
- Terser t = new Terser(reply);
- t.set(qak, 1, 0, 1, 1, qpd.getMessageQueryName().getIdentifier()
- .getValue().toString());
- t.set(qak, 1, 0, 2, 1, qpd.getMessageQueryName().getText().getValue()
- .toString());
- t.set(qak, 1, 0, 3, 1, qpd.getMessageQueryName()
- .getNameOfCodingSystem().getValue().toString());
- // QAK-4
- qak.getHitCountTotal().setValue(hitCount);
- // QAK-5
- t.set(qak, 5, 0, 1, 1, present);
- // QAK-6
- qak.getHitsRemaining().setValue(remainigp);
- return qak;
- }
- /**
- * This Method echoes the 'qpd' segment using terser
- *
- * @param out
- * @param in
- * @param reply
- * @return
- * @throws HL7Exception
- */
- public static QPD replicateQPD(Terser out, Terser in, RSP_K22 reply)
- throws HL7Exception {
- QPD inputQPD = (QPD) in.getSegment("/.QPD");
- QPD outputQPD = (QPD) out.getSegment("/.QPD");
- int numFields = inputQPD.numFields();
- for (int d = 1; d <= numFields; d++) {
- Type[] reps = inputQPD.getField(d);
- for (int a = 0; a < reps.length; a++) {
- for (int b = 1; b <= 5; b++) {
- for (int c = 1; c <= 5; c++) {
- out.set(outputQPD, d, a, b, c,
- in.get(inputQPD, d, a, b, c));
- }
- }
- }
- }
- return outputQPD;
- }
- public static void populatePID(Terser out, List<Patient> finalPatients,
- List<Patient> totPatients, RSP_K22 reply, int k, int j)
- throws HL7Exception {
- int sequenceNumber = j;
- List<Integer> sn = new ArrayList<Integer>();
- List<Integer> sn1 = new ArrayList<Integer>();
- String patientIdentifier = "";
- String assigningAuthority = "";
- sn.clear();
- sn1.clear();
- int reqno = k;
- while (k > 0) {
- sn.add(sequenceNumber);
- sequenceNumber--;
- k--;
- }
- for (int i = sn.size() - 1; i >= 0; i--) {
- Integer obj = sn.get(i);
- sn1.add(obj);
- }
- List<PatientIdentifier> listOfId = new ArrayList<PatientIdentifier>();
- for (int i = 0; i < finalPatients.size(); i++) {
- Patient p = finalPatients.get(i);
- RSP_K22_QUERY_RESPONSE rsp = reply.getQUERY_RESPONSE(i);
- PID pid = rsp.getPID();
- // u got 2 now u suh print 1 & 2+
- out.set(pid, 1, 0, 1, 1, sn1.get(i) + "");
- // Set PID-3
- int listi = 0;
- System.out.println(listi);
- if (p != null) {
- Set<PatientIdentifier> h = null;
- h = p.getIdentifiers();
- listOfId.addAll(h);
- for (int l = 0; l < listOfId.size(); l++) {
- patientIdentifier = listOfId.get(l).getIdentifier();
- assigningAuthority = listOfId.get(l).getIdentifierType()
- .getName();
- if (assigningAuthority
- .equals("OpenMRS Identification Number")) {
- out.set(pid, 3, listi, 1, 1, patientIdentifier);
- out.set(pid, 3, listi, 5, 1, "MR");
- listi++;
- } else {
- out.set(pid, 3, listi, 1, 1, patientIdentifier);
- out.set(pid, 3, listi, 5, 1, "U");
- listi++;
- }
- }
- h.clear();
- listOfId.clear();
- }
- pid.getAdministrativeSex().setValue(p.getGender());
- out.set(pid, 5, 0, 1, 1, p.getFamilyName());
- out.set(pid, 5, 0, 2, 1, p.getGivenName());
- if (p.getAttribute(new PersonAttributeType(4)) != null) {
- out.set(pid, 6, 0, 1, 1,
- p.getAttribute(new PersonAttributeType(4)) + "");
- }
- // setting race
- if (p.getAttribute(new PersonAttributeType(1)) != null) {
- out.set(pid, 10, 0, 1, 1,
- p.getAttribute(new PersonAttributeType(1)) + "");
- }
- Set<PersonAddress> addrs = p.getAddresses();
- PersonAddress addr = (PersonAddress) addrs.toArray()[0];
- out.set(pid, 11, 0, 1, 1, addr.getSubregion());
- out.set(pid, 11, 0, 2, 1, addr.getCityVillage());
- out.set(pid, 11, 0, 3, 1, addr.getStateProvince());
- out.set(pid, 11, 0, 5, 1, addr.getPostalCode());
- out.set(pid, 12, 0, 1, 1, addr.getCountyDistrict());// county
- // code
- // mother's name
- if (p.getAttribute(new PersonAttributeType(5)) != null) {
- out.set(pid, 16, 0, 1, 1,
- p.getAttribute(new PersonAttributeType(5)) + "");
- }
- // birthplace
- if (p.getAttribute(new PersonAttributeType(2)) != null) {
- out.set(pid, 23, 0, 1, 1,
- p.getAttribute(new PersonAttributeType(2)) + "");
- }
- // citizenship
- if (p.getAttribute(new PersonAttributeType(3)) != null) {
- out.set(pid, 26, 0, 1, 1,
- p.getAttribute(new PersonAttributeType(3)) + "");
- }
- }
- }
- /**
- * Populate DSC segments
- *
- * @param dsc
- * @param pointer
- * @return
- * @throws DataTypeException
- */
- public static DSC populateDSC(DSC dsc, String pointer)
- throws DataTypeException {
- // DSC-1
- dsc.getContinuationPointer().setValue(pointer);
- // DSC-2
- dsc.getContinuationStyle().setValue("I");
- return dsc;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement