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