Skip to content

Commit

Permalink
Implement MFE4 data type fix method. Added options to ParserConfigura…
Browse files Browse the repository at this point in the history
…tion to allow invalid/default datatype to be configured for MFE-5. Follows same convention as used for OBX-2 invalid/default configuration.
  • Loading branch information
kbalthaser committed Jun 8, 2018
1 parent 1755815 commit 6d6da03
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ private FixFieldDataType() {}
*/
public static final String INVALID_OBX2_TYPE_PROP = "ca.uhn.hl7v2.model.varies.invalid_obx2_type";

/**
* System property key: The value may be set to provide a default
* datatype ("ST", "NM", etc) for an MFE segment with a missing
* MFE-5 value.
*/
public static final String DEFAULT_MFE5_TYPE_PROP = "ca.uhn.hl7v2.model.varies.default_mfe5_type";

/**
* System property key: The value may be set to provide a default
* datatype ("ST", "NM", etc) for an MFE segment with an invalid
* MFE-5 value type. In other words, if MFE-5 has a value of "ZYZYZ",
* which is not a valid value, but this property is set to "ST", then
* MFE-4 will be parsed as an ST.
*/
public static final String INVALID_MFE5_TYPE_PROP = "ca.uhn.hl7v2.model.varies.invalid_mfe5_type";


/**
* <p>
* System property key: If this is not set, or set to "true", and a subcomponent delimiter is found within the
Expand Down Expand Up @@ -123,13 +140,41 @@ public static void fixOBX5(Segment segment, ModelClassFactory factory, ParserCon
fix(segment, 2, 5, defaultOBX2Type, invalidOBX2Type, factory, parserConfiguration);
}

/**
* <p>
* Sets the data type of field 4 in the given MFE segment to the value of MFE-5. The argument
* is a Segment as opposed to a particular MFE because it is meant to work with any version.
* </p>
* <p>
* Note that if no value is present in MFE-5, or an invalid value is present in
* MFE-5, this method will throw an error. This behaviour can be corrected by using the
* following system properties: {@link #DEFAULT_MFE5_TYPE_PROP} and {@link #INVALID_MFE5_TYPE_PROP}
* or by using configuration in {@link ParserConfiguration}
* </p>
*
* @param segment MFE segment instance to be modified
* @param factory ModelClassFactory to be used
* @param parserConfiguration configuration that influences setting MFE-5
* @throws ca.uhn.hl7v2.HL7Exception if the operation fails
*/
public static void fixMFE4(Segment segment, ModelClassFactory factory, ParserConfiguration parserConfiguration)
throws HL7Exception {
if (!(segment.getName().contains("MFE")) &&
Version.versionOf(segment.getMessage().getVersion()).isGreaterThan(Version.V23)) {
throw new IllegalArgumentException("Expected MFE segment, but was: " + segment.getName());
}
fix(segment, 5, 4, null, null, factory, parserConfiguration);

String defaultMFE5Type = parserConfiguration.getDefaultMfe5Type();
if (defaultMFE5Type == null) {
defaultMFE5Type = System.getProperty(DEFAULT_MFE5_TYPE_PROP);
}

String invalidMFE5Type = parserConfiguration.getInvalidMfe5Type();
if (invalidMFE5Type == null) {
invalidMFE5Type = System.getProperty(INVALID_MFE5_TYPE_PROP);
}

fix(segment, 5, 4, defaultMFE5Type, invalidMFE5Type, factory, parserConfiguration);
}

/**
Expand Down
104 changes: 104 additions & 0 deletions hapi-base/src/main/java/ca/uhn/hl7v2/parser/ParserConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class ParserConfiguration {
private Escaping escaping = new DefaultEscaping();
private boolean xmlDisableWhitespaceTrimmingOnAllNodes = false;
private Set<String> xmlDisableWhitespaceTrimmingOnNodeNames = Collections.emptySet();
private String myInvalidMfe5Type;
private String myDefaultMfe5Type;

/**
* <p>
Expand Down Expand Up @@ -157,6 +159,18 @@ public String getDefaultObx2Type() {
return myDefaultObx2Type;
}

/**
* Returns the default datatype ("ST", "NM", etc) for an MFE segment with a
* missing MFE-5 value
*
* @return Returns the default datatype ("ST", "NM", etc) for an OBX segment
* with a missing MFE-5 value
* @see #setDefaultMfe5Type(String)
*/
public String getDefaultMfe5Type() {
return myDefaultMfe5Type;
}

/**
* @return Returns the forced encode strings added by
* {@link #addForcedEncode(String)}
Expand Down Expand Up @@ -187,6 +201,19 @@ public String getInvalidObx2Type() {
return myInvalidObx2Type;
}

/**
* Returns the value provides a default datatype ("ST", "NM", etc) for an
* MFE segment with an invalid MFE-5 value.
*
* @return Returns the value provides a default datatype ("ST", "NM", etc)
* for an MFE segment with an invalid MFE-5 value.
* @see #setInvalidMfe5Type(String)
*/
public String getInvalidMfe5Type() {
return myInvalidMfe5Type;
}


/**
* Returns the behaviour to use when parsing a message and a nonstandard
* segment is found. Default is
Expand Down Expand Up @@ -346,6 +373,44 @@ public void setDefaultObx2Type(String theDefaultObx2Type) {
myDefaultObx2Type = theDefaultObx2Type;
}

/**
* <p>
* If this property is set, the value provides a default datatype ("ST",
* "NM", etc) for an MFE segment with a missing MFE-5 value. This is useful
* when parsing messages from systems which do not correctly populate MFE-5.
* </p>
* <p>
* For example, if this property is set to "ST", and the following MFE
* segment is encountered:
*
* <pre>
* MFE||||This is a value
* </pre>
*
* It will be parsed as though it had read:
*
* <pre>
* MFE||||This is a value|ST
* </pre>
*
* </p>
* <p>
* Note that this configuration can also be set globally using the system
* property {@link FixFieldDataType#DEFAULT_MFE5_TYPE_PROP}, but any value provided to
* {@link ParserConfiguration} takes priority over the system property.
* </p>
*
* @param theDefaultMfe5Type
* If this property is set, the value provides a default datatype
* ("ST", "NM", etc) for an MFE segment with a missing MFE-5
* value
* @see #setInvalidMfe5Type(String)
* @see FixFieldDataType#DEFAULT_MFE5_TYPE_PROP
*/
public void setDefaultMfe5Type(String theDefaultMfe5Type) {
myDefaultMfe5Type = theDefaultMfe5Type;
}

/**
* <p>
* If set to <code>true</code> (default is <code>true</code>), when encoding
Expand Down Expand Up @@ -450,6 +515,45 @@ public void setInvalidObx2Type(String theInvalidObx2Type) {
myInvalidObx2Type = theInvalidObx2Type;
}

/**
* <p>
* If this property is set, the value provides a default datatype ("ST",
* "NM", etc) for an MFE segment with an invalid MFE-5 value. This is useful
* when parsing messages from systems which do not correctly populate MFE-5.
* </p>
* <p>
* For example, if this property is set to "ST", and the following MFE
* segment is encountered:
*
* <pre>
* MFE||||This is a value|INVALID
* </pre>
*
* It will be parsed as though it had read:
*
* <pre>
* MFE||||This is a value|ST
* </pre>
*
* </p>
* <p>
* Note that this configuration can also be set globally using the system
* property {@link FixFieldDataType#INVALID_MFE5_TYPE_PROP}, but any value provided to
* {@link ParserConfiguration} takes priority over the system property.
* </p>
*
* @param theInvalidMfe5Type
* If this property is set, the value provides a default datatype
* ("ST", "NM", etc) for an MFE segment with an invalid MFE-5
* value. This is useful when parsing messages from systems which
* do not correctly populate MFE-5.
* @see ParserConfiguration#setDefaultMfe5Type(String)
* @see FixFieldDataType#INVALID_MFE5_TYPE_PROP
*/
public void setInvalidMfe5Type(String theInvalidMfe5Type) {
myInvalidMfe5Type = theInvalidMfe5Type;
}

/**
* If set to <code>true</code> (default is <code>false</code>), pipe parser will be
* put in non-greedy mode. This setting applies only to {@link PipeParser Pipe Parsers} and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v25.message.ORU_R01;

public class ParserConfigurationTest {
Expand Down Expand Up @@ -46,6 +48,101 @@ public void testAddForcedEncodeValidatesInput() {
assertIllegalArgument(pc, "AAA-123-");

}


/**
* <p>
* Test for {@link ParserConfiguration#setDefaultMfe5Type(String)}. Attempt to parse a message with a missing MFE-5 value.
* </p>
* <p>
* Setup: Set default MFE-5 type to 'ST' with {@link ParserConfiguration#setDefaultMfe5Type(String)}. Attempt to parse sample message.
* </p>
* <p>
* Expected Result: Message will parse successfully
* </p>
*/
@Test
public void test_setDefaultMfe5Type() throws Exception {

String message = "MSH|^~\\&|Send App|Send Fac|Rec App|Rec Fac|20070504141816||MFN^M02||P|2.6\r" +
"MFE||||VALUE\r";

Parser p = new PipeParser();
p.getParserConfiguration().setDefaultMfe5Type("ST");

Message m = p.parse(message);

Assert.assertNotNull(m);
}

/**
* <p>
* Test for {@link ParserConfiguration#setDefaultMfe5Type(String)}. Attempt to parse a message with a missing MFE-5 value.
* </p>
* <p>
* Setup: Attempt to parse sample message, not adjusting {@link ParserConfiguration}
* </p>
* <p>
* Expected Result: Message will fail to parse
* </p>
*/
@Test(expected = HL7Exception.class)
public void test_setDefaultMfe5Type_systemDefault() throws Exception {

String message = "MSH|^~\\&|Send App|Send Fac|Rec App|Rec Fac|20070504141816||MFN^M02||P|2.6\r" +
"MFE||||VALUE\r";

Parser p = new PipeParser();

p.parse(message);
}

/**
* <p>
* Test for {@link ParserConfiguration#setInvalidMfe5Type(String)}. Attempt to parse a message with an invalid MFE-5 value.
* </p>
* <p>
* Setup: Set default MFE-5 type to 'ST' with {@link ParserConfiguration#setDefaultMfe5Type(String)}. Attempt to parse sample message.
* </p>
* <p>
* Expected Result: Message will parse successfully
* </p>
*/
@Test
public void test_setInvalidMfe5Type() throws Exception {

String message = "MSH|^~\\&|Send App|Send Fac|Rec App|Rec Fac|20070504141816||MFN^M02||P|2.6\r" +
"MFE||||VALUE|INVALID\r";

Parser p = new PipeParser();
p.getParserConfiguration().setInvalidMfe5Type("ST");

Message m = p.parse(message);

Assert.assertNotNull(m);
}

/**
* <p>
* Test for {@link ParserConfiguration#setInvalidMfe5Type(String)}. Attempt to parse a message with an invalid MFE-5 value.
* </p>
* <p>
* Setup: Attempt to parse sample message, not adjusting {@link ParserConfiguration}
* </p>
* <p>
* Expected Result: Message will fail to parse
* </p>
*/
@Test(expected = HL7Exception.class)
public void test_setInvalidMfe5Type_systemDefault() throws Exception {

String message = "MSH|^~\\&|Send App|Send Fac|Rec App|Rec Fac|20070504141816||MFN^M02||P|2.6\r" +
"MFE||||VALUE|INVALID\r";

Parser p = new PipeParser();

p.parse(message);
}

private void assertIllegalArgument(ParserConfiguration pc, String forcedEncode) {
try {
Expand Down

0 comments on commit 6d6da03

Please sign in to comment.