001package org.hl7.fhir.r5.renderers.utils;
002
003import java.io.IOException;
004import java.io.UnsupportedEncodingException;
005import java.util.ArrayList;
006import java.util.List;
007
008import org.hl7.fhir.exceptions.FHIRException;
009import org.hl7.fhir.exceptions.FHIRFormatError;
010import org.hl7.fhir.r5.model.Base;
011import org.hl7.fhir.r5.model.ElementDefinition;
012import org.hl7.fhir.r5.model.StructureDefinition;
013import org.hl7.fhir.r5.model.Narrative.NarrativeStatus;
014import org.hl7.fhir.r5.model.Resource;
015import org.hl7.fhir.r5.renderers.ResourceRenderer;
016import org.hl7.fhir.utilities.xhtml.XhtmlNode;
017
018public class BaseWrappers {
019  
020  public interface RendererWrapper {
021    public RenderingContext getContext();
022  }
023
024  public interface PropertyWrapper extends RendererWrapper {
025    public String getName();
026    public boolean hasValues();
027    public List<BaseWrapper> getValues();
028    public String getTypeCode();
029    public String getDefinition();
030    public int getMinCardinality();
031    public int getMaxCardinality();
032    public StructureDefinition getStructure();
033    public ElementDefinition getElementDefinition();
034    public BaseWrapper value();
035    public ResourceWrapper getAsResource();
036    public String fhirType();
037  }
038
039  public interface WrapperBase  extends RendererWrapper {
040    public boolean has(String name);
041    public Base get(String name) throws UnsupportedEncodingException, FHIRException, IOException;
042    public List<BaseWrapper> children(String name) throws UnsupportedEncodingException, FHIRException, IOException;
043    public List<PropertyWrapper> children();
044    public String fhirType();
045  }
046
047  public interface ResourceWrapper extends WrapperBase {
048    public List<ResourceWrapper> getContained();
049    public String getId();
050    public XhtmlNode getNarrative() throws FHIRFormatError, IOException, FHIRException;
051    public Base getBase();
052    public String getName();
053    public void describe(XhtmlNode x) throws UnsupportedEncodingException, IOException;
054    public void injectNarrative(ResourceRenderer renderer, XhtmlNode x, NarrativeStatus status) throws IOException;
055    public BaseWrapper root();
056    public PropertyWrapper getChildByName(String tail);
057    public StructureDefinition getDefinition();
058    public boolean hasNarrative();
059    public String getNameFromResource();
060    public Resource getResource(); // if there is one
061  }
062
063  public interface BaseWrapper extends WrapperBase {
064    public Base getBase() throws UnsupportedEncodingException, IOException, FHIRException;
065    public ResourceWrapper getResource() throws UnsupportedEncodingException, IOException, FHIRException; // for contained, etc
066    public PropertyWrapper getChildByName(String tail);
067    public String fhirType();
068  }
069
070  public static abstract class RendererWrapperImpl implements RendererWrapper {
071    protected RenderingContext context;
072
073    public RendererWrapperImpl(RenderingContext context) {
074      super();
075      this.context = context;
076    }
077
078    public RenderingContext getContext() {
079      return context;
080    }
081    
082    protected String tail(String path) {
083      return path.substring(path.lastIndexOf(".")+1);
084    }
085
086  }
087  
088  public static abstract class WrapperBaseImpl extends RendererWrapperImpl implements WrapperBase {
089    
090    public WrapperBaseImpl(RenderingContext context) {
091      super(context);
092    }
093
094    @Override
095    public boolean has(String name) {
096      for (PropertyWrapper p : children()) {
097        if (p.getName().equals(name) || p.getName().equals(name+"[x]") ) {
098          return p.hasValues();
099        }
100      }
101      return false;
102    }
103
104    @Override
105    public Base get(String name) throws UnsupportedEncodingException, FHIRException, IOException {
106      for (PropertyWrapper p : children()) {
107        if (p.getName().equals(name) || p.getName().equals(name+"[x]")) {
108          if (p.hasValues()) {
109            return p.getValues().get(0).getBase();
110          } else {
111            return null;
112          }
113        }
114      }
115      return null;
116    }
117
118    @Override
119    public List<BaseWrapper> children(String name) throws UnsupportedEncodingException, FHIRException, IOException {
120      for (PropertyWrapper p : children()) {
121        if (p.getName().equals(name) || p.getName().equals(name+"[x]")) {
122          List<BaseWrapper> res = new ArrayList<>();
123          for (BaseWrapper b : p.getValues()) {
124            res.add(b);
125          }
126          return res;
127        }
128      }
129      return null;
130    }
131  }
132}