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