} // if
if (obx2.getValue() == null) {
if (v.getData() != null) {
if (!(v.getData() instanceof Primitive) || ((Primitive) v.getData()).getValue() != null) {
throw new HL7Exception(
"OBX-5 is valued, but OBX-2 is not. A datatype for OBX-5 must be specified using OBX-2. See JavaDoc for Varies#fixOBX5(Segment, ModelClassFactory)",
HL7Exception.REQUIRED_FIELD_MISSING);
}
}
}
else {
//set class
String version = segment.getMessage().getVersion();
String obx2Value = obx2.getValue();
Class<? extends Type> c = factory.getTypeClass(obx2Value, version);
// Class c = ca.uhn.hl7v2.parser.Parser.findClass(obx2.getValue(),
// segment.getMessage().getVersion(),
// "datatype");
if (c == null) {
String defaultOBX2Type = parserConfiguration.getInvalidObx2Type();
if (defaultOBX2Type == null) {
defaultOBX2Type = System.getProperty(INVALID_OBX2_TYPE_PROP);
}
if (defaultOBX2Type != null) {
c = factory.getTypeClass(defaultOBX2Type, version);
}
if (c == null) {
Primitive obx1 = (Primitive) segment.getField(1, 0);
HL7Exception h = new HL7Exception("\'" +
obx2.getValue() + "\' in record " +
obx1.getValue() + " is invalid for version " + version +
". See JavaDoc for Varies#fixOBX5(Segment, ModelClassFactory)",
HL7Exception.DATA_TYPE_ERROR);
h.setSegmentName("OBX");
h.setFieldPosition(2);
throw h;
}
}
Type newTypeInstance;
try {
newTypeInstance = (Type) c.getConstructor(new Class[]{Message.class}).newInstance(new Object[]{v.getMessage()});
} catch (NoSuchMethodException e) {
newTypeInstance = (Type) c.getConstructor(new Class[]{Message.class, Integer.class}).newInstance(new Object[]{v.getMessage(), 0});
}
if (newTypeInstance instanceof Primitive) {
Type[] subComponentsInFirstField = v.getFirstComponentSubcomponentsOnlyIfMoreThanOne();
if (subComponentsInFirstField != null) {
if (escapeSubcompponentDelimInPrimitive()) {
StringBuilder firstComponentValue = new StringBuilder();
for (Type type : subComponentsInFirstField) {
if (firstComponentValue.length() != 0) {
char subComponentSeparator = EncodingCharacters.getInstance(segment.getMessage()).getSubcomponentSeparator();
firstComponentValue.append(subComponentSeparator);
}
firstComponentValue.append(type.encode());
}
v.setFirstComponentPrimitiveValue(firstComponentValue.toString());
}
}
}
v.setData(newTypeInstance);
}
} // for reps
}
catch (HL7Exception e) {
throw e;
}
catch (Exception e) {
throw new HL7Exception(
e.getClass().getName() + " trying to set data type of OBX-5",
HL7Exception.APPLICATION_INTERNAL_ERROR,
e);
}
}