001package org.hl7.fhir.r5.utils;
002
003import java.util.List;
004
005import org.hl7.fhir.r5.elementmodel.Element;
006import org.hl7.fhir.r5.extensions.ExtensionDefinitions;
007import org.hl7.fhir.r5.extensions.ExtensionUtilities;
008import org.hl7.fhir.r5.model.CanonicalResource;
009import org.hl7.fhir.r5.model.CodeType;
010import org.hl7.fhir.r5.model.CompartmentDefinition;
011import org.hl7.fhir.r5.model.Constants;
012import org.hl7.fhir.r5.model.ContactDetail;
013import org.hl7.fhir.r5.model.ContactPoint;
014import org.hl7.fhir.r5.model.ContactPoint.ContactPointSystem;
015import org.hl7.fhir.utilities.HL7WorkGroups;
016import org.hl7.fhir.utilities.Utilities;
017import org.hl7.fhir.utilities.VersionUtilities;
018import org.hl7.fhir.utilities.xml.XMLUtil;
019
020public class CanonicalResourceUtilities {
021
022  public static void setHl7WG(CanonicalResource cr, String wgc) {
023    var wg = HL7WorkGroups.find(wgc);
024    if (wg == null) {
025      throw new Error("Unknown WG "+wgc);
026    }
027    ExtensionUtilities.setCodeExtension(cr, ExtensionDefinitions.EXT_WORKGROUP, wg.getCode());
028    cr.setPublisher("HL7 International / "+wg.getName());
029    boolean found = false;
030    for (ContactDetail c : cr.getContact()) {
031      for (ContactPoint t : c.getTelecom()) {
032        if ((t.getSystem() == ContactPointSystem.URL) && wg.getLink().equals(t.getValue())) {
033          found = true;
034        }
035      }
036    }
037    if (!found) {
038      cr.addContact().addTelecom().setSystem(ContactPointSystem.URL).setValue(wg.getLink());
039    }
040  }
041
042  public static void setHl7WG(CanonicalResource cr) {
043    String wgc = ExtensionUtilities.readStringExtension(cr, ExtensionDefinitions.EXT_WORKGROUP);
044    if (wgc == null) {
045      wgc = "fhir";      
046    }
047    var wg = HL7WorkGroups.find(wgc);
048    if (wg == null) {
049      throw new Error("Unknown WG '"+wgc+"' in "+cr.fhirType()+"/"+cr.getIdBase());
050    }
051    ExtensionUtilities.setCodeExtension(cr, ExtensionDefinitions.EXT_WORKGROUP, wg.getCode());
052    cr.setPublisher("HL7 International / "+wg.getName());
053    boolean found = false;
054    for (ContactDetail c : cr.getContact()) {
055      for (ContactPoint t : c.getTelecom()) {
056        if ((t.getSystem() == ContactPointSystem.URL) && wg.getLink().equals(t.getValue())) {
057          found = true;
058        }
059      }
060    }
061    if (!found) {
062      cr.addContact().addTelecom().setSystem(ContactPointSystem.URL).setValue(wg.getLink());
063    }
064  }
065
066  public static void setHl7WG(Element res, String code) {
067    if (VersionUtilities.getExtendedCanonicalResourceNames(res.getFHIRPublicationVersion().toCode()).contains(res.fhirType())) {
068      var wg = HL7WorkGroups.find(code);
069      if (wg == null) {
070        throw new Error("Unknown WG "+code);
071      }
072      
073      Element ext = res.getExtension(ExtensionDefinitions.EXT_WORKGROUP);
074      if (ext == null) {
075        ext = res.addElement("extension");
076        ext.setChildValue("url", ExtensionDefinitions.EXT_WORKGROUP);
077      } 
078      ext.setChildValue("value[x]",  new CodeType(code));
079      if (!Utilities.existsInList(res.fhirType(), "ClinicalUseDefinition")) {
080        res.setChildValue("publisher", "HL7 International / "+wg.getName());
081        while (res.hasChildren("contact")) {
082          res.removeChild("contact");
083        }
084        Element c = res.addElement("contact");
085        Element t = c.addElement("telecom");
086        t.setChildValue("system", "url");
087        t.setChildValue("value", wg.getLink());
088      }
089    }    
090  }
091//
092//  /**
093//   * for use in the core build where the context is not fully populated. Only known safe for R6 resources
094//   * 
095//   * @param res
096//   * @param code
097//   */
098//  public static void setHl7WG(org.w3c.dom.Element res, String code) {
099//    String rt = res.getNodeName();
100//    if (VersionUtilities.getExtendedCanonicalResourceNames("5.0.0").contains(rt)) {
101//      var wg = HL7WorkGroups.find(code);
102//      if (wg == null) {
103//        throw new Error("Unknown WG "+code);
104//      }
105//      
106//      List<org.w3c.dom.Element> extensions = XMLUtil.getNamedChildren(res, "extension");
107//      org.w3c.dom.Element wgext = null;
108//      for (org.w3c.dom.Element ext : extensions) {
109//        String url = ext.getAttribute("url");
110//        if (ExtensionDefinitions.EXT_WORKGROUP.equals(url)) {
111//          wgext = ext;
112//        }
113//      }
114//      if (wgext == null) {
115//        wgext = res.getOwnerDocument().createElementNS(Constants.NS_FHIR_ROOT, "extension");
116//        wgext.setAttribute("url", ExtensionDefinitions.EXT_WORKGROUP);
117//        org.w3c.dom.Element after = XMLUtil.getLastChild(res, "id", "meta", "text", "implicitRules", "language", "text", "contained");
118//        if (after != null) {
119//          after = XMLUtil.getNextSibling(after);
120//        }
121//        res.insertBefore(wgext, after);
122//        res.insertBefore(res.getOwnerDocument().createTextNode("\n  "), after);
123//      }
124//      XMLUtil.clearChildren(wgext);
125//      org.w3c.dom.Element valueCode = res.getOwnerDocument().createElementNS(Constants.NS_FHIR_ROOT, "valueCode"); 
126//      wgext.appendChild(valueCode);
127//      valueCode.setAttribute("value", code);
128//      
129//      org.w3c.dom.Element pub = XMLUtil.getNamedChild(res, "publisher");
130//      if (pub == null) {
131//        pub = res.getOwnerDocument().createElementNS(Constants.NS_FHIR_ROOT, "publisher");
132//        org.w3c.dom.Element after = XMLUtil.getLastChild(res, "id", "meta", "text", "implicitRules", "language", "text", "contained", "extension", "modifierExtension", 
133//            "url", "identifier", "version", "versionAlgorithmString", "versionAlgorithmCoding", "name", "title", "status", "experimental", "date", ("EvidenceReport".equals(rt) ? "subject" : "xx"));
134//        if (after != null) {
135//          after = XMLUtil.getNextSibling(after);
136//        }
137//        res.insertBefore(pub, after);
138//        res.insertBefore(res.getOwnerDocument().createTextNode("\n  "), after);
139//      }
140//      pub.setAttribute("value", "HL7 International / "+wg.getName());
141//
142//      org.w3c.dom.Element contact = XMLUtil.getNamedChild(res, "contact");
143//      if (contact == null) {
144//        contact = res.getOwnerDocument().createElementNS(Constants.NS_FHIR_ROOT, "contact");
145//        res.insertBefore(contact, XMLUtil.getNextSibling(pub));
146//        res.insertBefore(res.getOwnerDocument().createTextNode("\n  "), contact.getNextSibling());
147//      }
148//
149//      org.w3c.dom.Element telecom = XMLUtil.getNamedChild(contact, "telecom");
150//      if (telecom == null) {
151//        contact.appendChild(res.getOwnerDocument().createTextNode("\n    "));
152//        telecom = res.getOwnerDocument().createElementNS(Constants.NS_FHIR_ROOT, "telecom");
153//        contact.appendChild(telecom);
154//        contact.appendChild(res.getOwnerDocument().createTextNode("\n  "));
155//      }
156//      
157//      org.w3c.dom.Element system = XMLUtil.getNamedChild(telecom, "system");
158//      if (system == null) {
159//        system = res.getOwnerDocument().createElementNS(Constants.NS_FHIR_ROOT, "system");
160//        org.w3c.dom.Element after = XMLUtil.getLastChild(telecom, "id", "extension");
161//        if (after != null) {
162//          after = XMLUtil.getNextSibling(after);
163//        }
164//        telecom.insertBefore(system, after);
165//        telecom.insertBefore(res.getOwnerDocument().createTextNode("\n      "), after);
166//      }
167//      system.setAttribute("value", "url");
168//    
169//
170//      org.w3c.dom.Element value = XMLUtil.getNamedChild(telecom, "value");
171//      if (value == null) {
172//        value = res.getOwnerDocument().createElementNS(Constants.NS_FHIR_ROOT, "value");
173//        org.w3c.dom.Element after = XMLUtil.getLastChild(telecom, "id", "extension", "system");
174//        if (after != null) {
175//          after = XMLUtil.getNextSibling(after);
176//        }
177//        telecom.insertBefore(system, after);
178//        telecom.insertBefore(res.getOwnerDocument().createTextNode("\n      "), after);
179//      }
180//      value.setAttribute("value", wg.getLink());
181//    }
182//  }
183}