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(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 PropertyWrapper getChildByName(String tail);
066    public String fhirType();
067  }
068
069  public static abstract class RendererWrapperImpl implements RendererWrapper {
070    protected RenderingContext context;
071
072    public RendererWrapperImpl(RenderingContext context) {
073      super();
074      this.context = context;
075    }
076
077    public RenderingContext getContext() {
078      return context;
079    }
080    
081    protected String tail(String path) {
082      return path.substring(path.lastIndexOf(".")+1);
083    }
084
085  }
086  
087  public static abstract class WrapperBaseImpl extends RendererWrapperImpl implements WrapperBase {
088    
089    public WrapperBaseImpl(RenderingContext context) {
090      super(context);
091    }
092
093    @Override
094    public boolean has(String name) {
095      for (PropertyWrapper p : children()) {
096        if (p.getName().equals(name) || p.getName().equals(name+"[x]") ) {
097          return p.hasValues();
098        }
099      }
100      return false;
101    }
102
103    @Override
104    public Base get(String name) throws UnsupportedEncodingException, FHIRException, IOException {
105      for (PropertyWrapper p : children()) {
106        if (p.getName().equals(name) || p.getName().equals(name+"[x]")) {
107          if (p.hasValues()) {
108            return p.getValues().get(0).getBase();
109          } else {
110            return null;
111          }
112        }
113      }
114      return null;
115    }
116
117    @Override
118    public List<BaseWrapper> children(String name) throws UnsupportedEncodingException, FHIRException, IOException {
119      for (PropertyWrapper p : children()) {
120        if (p.getName().equals(name) || p.getName().equals(name+"[x]")) {
121          List<BaseWrapper> res = new ArrayList<>();
122          for (BaseWrapper b : p.getValues()) {
123            res.add(b);
124          }
125          return res;
126        }
127      }
128      return null;
129    }
130  }
131}