001/*-
002 * #%L
003 * Smile CDR - CDR
004 * %%
005 * Copyright (C) 2016 - 2024 Smile CDR, Inc.
006 * %%
007 * All rights reserved.
008 * #L%
009 */
010package ca.cdr.api.pub.hl7v2.common.abstraction;
011
012import ca.uhn.hl7v2.HL7Exception;
013import ca.uhn.hl7v2.model.AbstractMessage;
014import ca.uhn.hl7v2.model.v25.group.MDM_T02_OBXNTE;
015import ca.uhn.hl7v2.model.v25.message.MDM_T01;
016import ca.uhn.hl7v2.model.v25.message.MDM_T02;
017import ca.uhn.hl7v2.model.v25.segment.MSH;
018
019import java.util.List;
020
021import static ca.cdr.api.pub.hl7v2.common.abstraction.IDocumentManagementMessage.*;
022
023public class DocumentManagementMessageFactory {
024
025        public IDocumentManagementMessage asDocumentManagementMessage(AbstractMessage theMsg) throws HL7Exception {
026                MdmTrigger trigger = getMdmTrigger(theMsg);
027                DocumentManagementMessage message = new DocumentManagementMessage();
028                message.setTrigger(trigger);
029
030                if (theMsg instanceof MDM_T02 mdmT02) {
031                        List<MDM_T02_OBXNTE> allObxNtes = mdmT02.getOBXNTEAll();
032                        allObxNtes.stream().map(MDM_T02_OBXNTE::getOBX).forEach(message::addObx);
033                        message.setTXA(mdmT02.getTXA());
034                } else if (theMsg instanceof MDM_T01 mdmT01) {
035                        message.setTXA(mdmT01.getTXA());
036                }
037
038                return message;
039        }
040
041        public static MdmTrigger getMdmTrigger(AbstractMessage theMsg) throws HL7Exception {
042                MSH msh = (MSH) theMsg.get("MSH");
043                String triggerEvent = msh.getMsh9_MessageType().getMsg2_TriggerEvent().getValueOrEmpty();
044                return MdmTrigger.valueOf(triggerEvent);
045        }
046}