001package org.hl7.fhir.r5.liquid;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import org.hl7.fhir.exceptions.FHIRException;
007import org.hl7.fhir.r5.fhirpath.FHIRPathEngine;
008import org.hl7.fhir.r5.fhirpath.FHIRPathFunctionDefinition;
009import org.hl7.fhir.r5.fhirpath.FHIRPathUtilityClasses.FunctionDetails;
010import org.hl7.fhir.r5.fhirpath.TypeDetails;
011import org.hl7.fhir.r5.fhirpath.ExpressionNode.CollectionStatus;
012import org.hl7.fhir.r5.model.*;
013import org.hl7.fhir.utilities.FhirPublication;
014import org.hl7.fhir.utilities.MarkedToMoveToAdjunctPackage;
015import org.hl7.fhir.utilities.Utilities;
016
017@MarkedToMoveToAdjunctPackage
018public class GlobalObject extends Base {
019
020  private DateTimeType dt;
021  private DateType dtD;
022  private StringType pathToSpec;
023  
024  public GlobalObject(DateTimeType td, DateType dtD, StringType pathToSpec) {
025    super();
026    this.dt = td;
027    this.dtD = dtD;
028    this.pathToSpec = pathToSpec;
029  }
030
031  @Override
032  public String fhirType() {
033    return "GlobalObject";
034  }
035
036  @Override
037  public String getIdBase() {
038    return null;
039  }
040
041  @Override
042  public void setIdBase(String value) {
043    throw new Error("Read only"); 
044  }
045
046  @Override
047  public Base copy() {
048    return this;
049  }
050
051  @Override
052  public FhirPublication getFHIRPublicationVersion() {
053    return FhirPublication.R5;
054  }
055
056  public Base[] getProperty(int hash, String name, boolean checkValid) throws FHIRException {
057    if ("dateTime".equals(name)) {
058      return wrap(dt);
059    } else if ("date".equals(name)) {
060        return wrap(dtD);
061    } else if ("path".equals(name)) {
062      return wrap(pathToSpec);
063    } else {
064      return super.getProperty(hash, name, checkValid);
065    }
066  }
067
068  private Base[] wrap(Base b) {
069    Base[] l = new Base[1];
070    l[0] = b;
071    return l;
072  }
073  
074  @Override
075  public List<Base> executeFunction(FHIRPathEngine engine, Object appContext, List<Base> focus, String functionName, List<List<Base>> parameters) {
076    return null;
077  }
078
079  public static class GlobalObjectRandomFunction extends FHIRPathFunctionDefinition {
080
081    @Override
082    public String name() {
083      return "random";
084    }
085
086    @Override
087    public FunctionDetails details() {
088      return new FunctionDetails("Generate a Random Number", 1, 1);
089    }
090
091    @Override
092    public TypeDetails check(FHIRPathEngine engine, Object appContext, TypeDetails focus, List<TypeDetails> parameters) {
093      if (focus.hasType("GlobalObject")) {
094        return new TypeDetails(CollectionStatus.SINGLETON, "integer");
095      } else {
096        return null;
097      }
098    }
099
100    @Override
101    public List<Base> execute(FHIRPathEngine engine, Object appContext, List<Base> focus, List<List<Base>> parameters) {
102      List<Base> list = new ArrayList<>();
103      int scale = Utilities.parseInt(parameters.get(0).get(0).primitiveValue(), 100)+ 1;
104      list.add(new IntegerType((int)(Math.random() * scale)));
105      return list;
106    }
107    
108  }
109}