Skip to content

Commit

Permalink
Syntactic sugar for HL7v2 parsing in XDS
Browse files Browse the repository at this point in the history
  • Loading branch information
unixoid committed Feb 6, 2025
1 parent 609ffb9 commit ee124dd
Show file tree
Hide file tree
Showing 40 changed files with 175 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public Address(XAD xad) {
super(xad);
}

/**
* Parses an HL7v2 string into an Address object.
*
* @param hl7String HL7v2 representation of an Address object.
* @return parsed Address object.
*/
public static Address parse(String hl7String) {
return Hl7v2Based.parse(hl7String, Address.class);
}

/**
* @return the street address (XAD.1).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* <p>
* This class is based on the HL7 HD data type.
* <p>
* Note that most assigning authorities used in XSD only allow the definition of the
* Note that most assigning authorities used in XDS only allow the definition of the
* universal ID. The ID type must be {@code ISO} and the namespace ID has to be empty.
* The constructor {@link #AssigningAuthority(String)} can be used to create such
* authorities.
Expand Down Expand Up @@ -99,6 +99,16 @@ public AssigningAuthority(Oid universalId) {
setUniversalIdType(UNIVERSAL_ID_TYPE_OID);
}

/**
* Parses an HL7v2 string into an AssigningAuthority object.
*
* @param hl7String HL7v2 representation of an AssigningAuthority object.
* @return parsed AssigningAuthority object.
*/
public static AssigningAuthority parse(String hl7String) {
return Hl7v2Based.parse(hl7String, AssigningAuthority.class);
}

/**
* @return the universal ID (HD.2).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public CXiAssigningAuthority(String namespaceId, String universalId, String univ
setNamespaceId(namespaceId);
}

/**
* Parses an HL7v2 string into a CXiAssigningAuthority object.
*
* @param hl7String HL7v2 representation of a CXiAssigningAuthority object.
* @return parsed CXiAssigningAuthority object.
*/
public static CXiAssigningAuthority parse(String hl7String) {
return Hl7v2Based.parse(hl7String, CXiAssigningAuthority.class);
}

/**
* @return the namespace ID (HD.1).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ public Code(String code, LocalizedString displayName, String schemeName) {
setSchemeName(schemeName);
}

/**
* Parses an HL7v2 string into a Code object.
*
* @param hl7String HL7v2 representation of a Code object.
* @return parsed Code object.
*/
public static Code parse(String hl7String) {
return Hl7v2Based.parse(hl7String, Code.class);
}

/**
* @return the value of this code.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected String render() {
* HL7 v2 representation of the given object, or <code>null</code>
* when the given object is <code>null</code> or empty.
*/
public static String render(Hl7v2Based xdsModelObject) {
public static String render(Hl7v2Based<?> xdsModelObject) {
return (xdsModelObject != null) ? StringUtils.trimToNull(xdsModelObject.render()) : null;
}

Expand All @@ -142,7 +142,7 @@ public static String render(Hl7v2Based xdsModelObject) {
* HL7 v2 representation of the given object, or an empty string
* when the given object is <code>null</code> or empty.
*/
public static String rawRender(Hl7v2Based xdsModelObject) {
public static String rawRender(Hl7v2Based<?> xdsModelObject) {
return (xdsModelObject != null)
? PipeParser.encode(xdsModelObject.getHapiObject(), XdsHl7v2Renderer.ENCODING_CHARACTERS)
: "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,17 @@ public Identifiable(String id, AssigningAuthority assigningAuthority) {
public Identifiable(String id, Oid assigningAuthorityOid) {
this(id, new AssigningAuthority(assigningAuthorityOid));
}


/**
* Parses an HL7v2 string into an Identifiable object.
*
* @param hl7String HL7v2 representation of an Identifiable object.
* @return parsed Identifiable object.
*/
public static Identifiable parse(String hl7String) {
return Hl7v2Based.parse(hl7String, Identifiable.class);
}

/**
* @return person ID (CX.1) / Code.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ public Organization(String organizationName, String idNumber, AssigningAuthority
setAssigningAuthority(assigningAuthority);
}

/**
* Parses an HL7v2 string into an Organization object.
*
* @param hl7String HL7v2 representation of an Organization object.
* @return parsed Organization object.
*/
public static Organization parse(String hl7String) {
return Hl7v2Based.parse(hl7String, Organization.class);
}

/**
* @return the assigning authority (XON.6).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,16 @@ public void set(String s) {
getIterator().set(s);
switch (fieldId) {
case "PID-3":
getOtherIterator().set(Hl7v2Based.parse(s, Identifiable.class));
getOtherIterator().set(Identifiable.parse(s));
break;
case "PID-5":
getOtherIterator().set(Hl7v2Based.parse(s, XpnName.class));
getOtherIterator().set(XpnName.parse(s));
break;
case "PID-7":
case "PID-8":
break;
case "PID-11":
getOtherIterator().set(Hl7v2Based.parse(s, Address.class));
getOtherIterator().set(Address.parse(s));
break;
default:
throw new IllegalStateException("This line shall be not reachable, please report a bug");
Expand All @@ -147,16 +147,16 @@ public void add(String s) {
getIterator().add(s);
switch (fieldId) {
case "PID-3":
getOtherIterator().add(Hl7v2Based.parse(s, Identifiable.class));
getOtherIterator().add(Identifiable.parse(s));
break;
case "PID-5":
getOtherIterator().add(Hl7v2Based.parse(s, XpnName.class));
getOtherIterator().add(XpnName.parse(s));
break;
case "PID-7":
case "PID-8":
break;
case "PID-11":
getOtherIterator().add(Hl7v2Based.parse(s, Address.class));
getOtherIterator().add(Address.parse(s));
break;
default:
throw new IllegalStateException("This line shall be not reachable, please report a bug");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ public Person(Identifiable id, Name name) {
setName(name);
}

/**
* Parses an HL7v2 string into a Person object.
*
* @param hl7String HL7v2 representation of a Person object.
* @return parsed Person object.
*/
public static Person parse(String hl7String) {
return Hl7v2Based.parse(hl7String, Person.class);
}

/**
* @return the id of the person (XCN.1 and XCN.9).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ public ReferenceId(String id, CXiAssigningAuthority assigningAuthority, String i
setIdTypeCode(idTypeCode);
}

/**
* Parses an HL7v2 string into a ReferenceId object.
*
* @param hl7String HL7v2 representation of a ReferenceId object.
* @return parsed ReferenceId object.
*/
public static ReferenceId parse(String hl7String) {
return Hl7v2Based.parse(hl7String, ReferenceId.class);
}

/**
* @return the value of the id (CX.1).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ public Telecom(Long countryCode, Long areaCityCode, Long localNumber, Long exten
setExtension(extension);
}

/**
* Parses an HL7v2 string into a Telecom object.
*
* @param hl7String HL7v2 representation of a Telecom object.
* @return parsed Telecom object.
*/
public static Telecom parse(String hl7String) {
return Hl7v2Based.parse(hl7String, Telecom.class);
}

/**
* @return telecom use code (XTN-2) according to HL7 v.2.5 Table 0201.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public XcnName(String familyName, String givenName, String secondAndFurtherGiven
setDegree(degree);
}

/**
* Parses an HL7v2 string into an XcnName object.
*
* @param hl7String HL7v2 representation of an XcnName object.
* @return parsed XcnName object.
*/
public static XcnName parse(String hl7String) {
return Hl7v2Based.parse(hl7String, XcnName.class);
}

@Override
public String getFamilyName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public XpnName(String familyName, String givenName, String secondAndFurtherGiven
setDegree(degree);
}

/**
* Parses an HL7v2 string into an XpnName object.
*
* @param hl7String HL7v2 representation of an XpnName object.
* @return parsed XpnName object.
*/
public static XpnName parse(String hl7String) {
return Hl7v2Based.parse(hl7String, XpnName.class);
}

@Override
public String getFamilyName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void setTypedAuthorPerson(Person authorPerson) {
* @return a {@link Person} object.
*/
public Person getTypedAuthorPerson() {
return Hl7v2Based.parse(this.authorPerson, Person.class);
return Person.parse(this.authorPerson);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public Author fromEbXML(EbXMLClassification classification) {

var persons = classification.getSlotValues(SLOT_NAME_AUTHOR_PERSON);
if (!persons.isEmpty()) {
var person = Hl7v2Based.parse(persons.get(0), Person.class);
var person = Person.parse(persons.get(0));
author.setAuthorPerson(person);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,16 @@ protected void addSlotsFromEbXML(DocumentEntry docEntry, EbXMLExtrinsicObject ex
docEntry.setSize(size != null ? Long.parseLong(size) : null);

var hl7LegalAuthenticator = extrinsic.getSingleSlotValue(SLOT_NAME_LEGAL_AUTHENTICATOR);
docEntry.setLegalAuthenticator(Hl7v2Based.parse(hl7LegalAuthenticator, Person.class));
docEntry.setLegalAuthenticator(Person.parse(hl7LegalAuthenticator));

var sourcePatient = extrinsic.getSingleSlotValue(SLOT_NAME_SOURCE_PATIENT_ID);
docEntry.setSourcePatientId(Hl7v2Based.parse(sourcePatient, Identifiable.class));
docEntry.setSourcePatientId(Identifiable.parse(sourcePatient));

var slotValues = extrinsic.getSlotValues(SLOT_NAME_SOURCE_PATIENT_INFO);
docEntry.setSourcePatientInfo(patientInfoTransformer.fromHL7(slotValues));

for (var referenceIdValue : extrinsic.getSlotValues(SLOT_NAME_REFERENCE_ID_LIST)) {
docEntry.getReferenceIdList().add(Hl7v2Based.parse(referenceIdValue, ReferenceId.class));
docEntry.getReferenceIdList().add(ReferenceId.parse(referenceIdValue));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ public Recipient fromEbXML(String slotValue) {
var recipient = new Recipient();

var parts = slotValue.split("\\|");
recipient.setOrganization(Hl7v2Based.parse(parts[0], Organization.class));
recipient.setOrganization(Organization.parse(parts[0]));
if (parts.length > 1) {
recipient.setPerson(Hl7v2Based.parse(parts[1], Person.class));
recipient.setPerson(Person.parse(parts[1]));
}
if (parts.length > 2) {
recipient.setTelecom(Hl7v2Based.parse(parts[2], Telecom.class));
recipient.setTelecom(Telecom.parse(parts[2]));
}

return recipient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ protected void addExternalIdentifiers(C metaData, E ebXML, EbXMLObjectLibrary ob
*/
protected void addExternalIdentifiersFromEbXML(C metaData, E ebXML) {
var patientID = ebXML.getExternalIdentifierValue(patientIdExternalId);
metaData.setPatientId(Hl7v2Based.parse(patientID, Identifiable.class));
metaData.setPatientId(Identifiable.parse(patientID));
metaData.setUniqueId(ebXML.getExternalIdentifierValue(uniqueIdExternalId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected void toEbXML(T query, QuerySlotHelper slots) {
protected void fromEbXML(T query, QuerySlotHelper slots) {
super.fromEbXML(query, slots);
var patientId = slots.toString(DOC_ENTRY_PATIENT_ID);
query.setPatientId(Hl7v2Based.parse(patientId, Identifiable.class));
query.setPatientId(Identifiable.parse(patientId));
query.setDocumentEntryTypes(slots.toDocumentEntryType(DOC_ENTRY_TYPE));
query.setStatus(slots.toStatus(DOC_ENTRY_STATUS));
query.setDocumentAvailability(slots.toDocumentAvailability(DOC_ENTRY_DOCUMENT_AVAILABILITY));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected void toEbXML(FetchQuery query, QuerySlotHelper slots) {
protected void fromEbXML(FetchQuery query, QuerySlotHelper slots) {
super.fromEbXML(query, slots);
var patientId = slots.toString(DOC_ENTRY_PATIENT_ID);
query.setPatientId(Hl7v2Based.parse(patientId, Identifiable.class));
query.setPatientId(Identifiable.parse(patientId));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected void toEbXML(FindFoldersQuery query, QuerySlotHelper slots) {
protected void fromEbXML(FindFoldersQuery query, QuerySlotHelper slots) {
super.fromEbXML(query, slots);
var patientId = slots.toString(FOLDER_PATIENT_ID);
query.setPatientId(Hl7v2Based.parse(patientId, Identifiable.class));
query.setPatientId(Identifiable.parse(patientId));
query.setCodes(slots.toCodeQueryList(FOLDER_CODES, FOLDER_CODES_SCHEME));
query.getLastUpdateTime().setFrom(slots.toTimestamp(FOLDER_LAST_UPDATE_TIME_FROM));
query.getLastUpdateTime().setTo(slots.toTimestamp(FOLDER_LAST_UPDATE_TIME_TO));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected void toEbXML(FindSubmissionSetsQuery query, QuerySlotHelper slots) {
protected void fromEbXML(FindSubmissionSetsQuery query, QuerySlotHelper slots) {
super.fromEbXML(query, slots);
var patientId = slots.toString(SUBMISSION_SET_PATIENT_ID);
query.setPatientId(Hl7v2Based.parse(patientId, Identifiable.class));
query.setPatientId(Identifiable.parse(patientId));
query.setSourceIds(slots.toStringList(SUBMISSION_SET_SOURCE_ID));
query.getSubmissionTime().setFrom(slots.toTimestamp(SUBMISSION_SET_SUBMISSION_TIME_FROM));
query.getSubmissionTime().setTo(slots.toTimestamp(SUBMISSION_SET_SUBMISSION_TIME_TO));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected void toEbXML(GetAllQuery query, QuerySlotHelper slots) {
protected void fromEbXML(GetAllQuery query, QuerySlotHelper slots) {
super.fromEbXML(query, slots);
var patientId = slots.toString(PATIENT_ID);
query.setPatientId(Hl7v2Based.parse(patientId, Identifiable.class));
query.setPatientId(Identifiable.parse(patientId));
query.setStatusDocuments(slots.toStatus(DOC_ENTRY_STATUS));
query.setStatusFolders(slots.toStatus(FOLDER_STATUS));
query.setStatusSubmissionSets(slots.toStatus(SUBMISSION_SET_STATUS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected void toEbXML(T query, QuerySlotHelper slots) {
protected void fromEbXML(T query, QuerySlotHelper slots) {
super.fromEbXML(query, slots);
query.setMetadataLevel(slots.toInteger(METADATA_LEVEL));
query.setPatientId(Hl7v2Based.parse(slots.toString(DOC_ENTRY_PATIENT_ID), Identifiable.class));
query.setPatientId(Identifiable.parse(slots.toString(DOC_ENTRY_PATIENT_ID)));
query.setStatus(slots.toStatus(DOC_ENTRY_STATUS));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public List<Identifiable> toPatientIdList(QueryParameter param) {
return null;
}
return values.stream()
.map(value -> Hl7v2Based.parse(value, Identifiable.class))
.map(Identifiable::parse)
.toList();
}

Expand Down Expand Up @@ -419,7 +419,7 @@ public static List<Code> toCode(List<String> slotValues) {
var codes = new ArrayList<Code>();
slotValues.forEach(slotValue ->
decodeStringList(slotValue).forEach(hl7CE -> {
var code = Hl7v2Based.parse(hl7CE, Code.class);
var code = Code.parse(hl7CE);
if (code == null || isEmpty(code.getCode()) || isEmpty(code.getSchemeName())) {
throw new XDSMetaDataException(ValidationMessage.INVALID_QUERY_PARAMETER_VALUE, hl7CE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected void toEbXML(SubscriptionForDocumentEntryQuery query, QuerySlotHelper
protected void fromEbXML(SubscriptionForDocumentEntryQuery query, QuerySlotHelper slots) {
super.fromEbXML(query, slots);
var patientId = slots.toString(DOC_ENTRY_PATIENT_ID);
query.setPatientId(Hl7v2Based.parse(patientId, Identifiable.class));
query.setPatientId(Identifiable.parse(patientId));
query.setReferenceIds(slots.toStringQueryList(DOC_ENTRY_REFERENCE_IDS));
}
}
Loading

0 comments on commit ee124dd

Please sign in to comment.