Shortening up OBX segments #6235
-
So I'm trying to clean up the OBX segments in what I'm being sent by our reports software. Unfortunately it often sends whole paragraphs in a single OBX segment, which when received by the receiver, it mimics the formatting, which is a problem. // All credit to pacmano1 for this code btw - shown in the post but wanted to make that clear.
function splitIntoChunks(str, chunkSize) {
const chunks = [];
var i = 0;
while (i < str.length) {
var end = Math.min(i + chunkSize, str.length);
if (end < str.length && str[end] !== ' ') {
// Find the last space in the substring
const lastSpace = str.lastIndexOf(' ', end);
if (lastSpace > i) {
end = lastSpace;
}
}
chunks.push(str.slice(i, end));
i = end + (str[end] === ' ' ? 1 : 0); // Skip the space if it's the end character
}
return chunks;
}
var sometext = ''
for each (obx in msg.OBX) {
sometext += obx['OBX.5'][0]['OBX.5.1'].toString() + ' '; // Ensure spaces between concatenated parts
}
msg.OBX = [] // clear current OBXs
var split = splitIntoChunks(sometext.trim(), 35)
var new_obx;
for (var i = 0; i < split.length; i++) {
if (i == 0) {
new_obx = createSegmentAfter('OBX', msg.OBR);
} else {
new_obx = createSegmentAfter('OBX', new_obx);
}
new_obx['OBX.1']['OBX.1.1'] = i + 1;
new_obx['OBX.5']['OBX.5.1'] = split[i];
} So an example section of an HL7 message being processed looks like this:
So to get more into my questions: var sometext = ''
for each (obx in msg.OBX) {
sometext += obx['OBX.5'][0]['OBX.5.1'].toString() + ' '; // Ensure spaces between concatenated parts
}
msg.OBX = [] // clear current OBXs So this cycles through all of the OBX segments, taking OBX segment 5, the actual data and adding it into the variable sometext. Effectively a super long string of all the text in the OBX segment. So at this point , in theory the message goes to the OBR, and all of the OBX segments are gone. var split = splitIntoChunks(sometext.trim(), 35)
function splitIntoChunks(str, chunkSize) {
const chunks = [];
var i = 0;
while (i < str.length) {
var end = Math.min(i + chunkSize, str.length);
if (end < str.length && str[end] !== ' ') {
// Find the last space in the substring
const lastSpace = str.lastIndexOf(' ', end);
if (lastSpace > i) {
end = lastSpace;
}
}
chunks.push(str.slice(i, end));
i = end + (str[end] === ' ' ? 1 : 0); // Skip the space if it's the end character
}
return chunks;
} So this part, as far as I am following is creating an array of text from "sometext". Basically it's getting the first 35 characters, checking if there is a space, then returning the array. var new_obx;
for (var i = 0; i < split.length; i++) {
if (i == 0) {
new_obx = createSegmentAfter('OBX', msg.OBR);
} else {
new_obx = createSegmentAfter('OBX', new_obx);
}
new_obx['OBX.1']['OBX.1.1'] = i + 1;
new_obx['OBX.5']['OBX.5.1'] = split[i];
} Finally, it cycles through the array "split", creates an initial OBX segment after the OBR segment, then subsequent OBX segments after that, renumbering them as it goes. All good stuff. But I'm obviously missing something as that part of the transformer doesn't appear to be doing anything to the message. That said, I had a couple of thoughts:
for each (obx in msg.OBX) works, so maybe that strange starting count throws it off? Thank you for your patience. I am trying to learn. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 15 replies
-
Read the user manual starting at https://docs.nextgen.com/bundle/Mirth_User_Guide_40/page/connect/connect/topics/c_mirth_connect_debugger_connect.html. I think you missed a few things. That's a long post (and who knows if my code works, lol). Post a before and after message please. It's often faster (for me) to just see what you are expecting. |
Beta Was this translation helpful? Give feedback.
-
I would not use my chunker function anymore. @tonygermano pointed out that Apache WordUtils is a better idea. Not fully QAed, but: var WordUtils = Packages.org.apache.commons.lang3.text.WordUtils;
var largeString = msg['OBX']['OBX.5']['OBX.5.1'].toString()
var obx_id = msg['OBX']['OBX.3']['OBX.3.1'].toString()
delete msg.OBX
var maxLength = 30;
var wrappedString = WordUtils.wrap(largeString, maxLength);
var brokenStringsArray = wrappedString.split("\n");
// Output the result
var new_obx;
for (var i = 0; i < brokenStringsArray.length; i++) {
if (i == 0) {
new_obx = createSegmentAfter('OBX', msg.OBR);
} else {
new_obx = createSegmentAfter('OBX', new_obx);
}
new_obx['OBX.1']['OBX.1.1'] = i + 1;
new_obx['OBX.3']['OBX.3.1'] = obx_id
new_obx['OBX.4']['OBX.4.1'] = new_obx['OBX.1']['OBX.1.1']
new_obx['OBX.5']['OBX.5.1'] = brokenStringsArray[i];
} |
Beta Was this translation helpful? Give feedback.
I would not use my chunker function anymore. @tonygermano pointed out that Apache WordUtils is a better idea.
Not fully QAed, but: