001package org.hl7.fhir.dstu3.model;
002
003/*-
004 * #%L
005 * org.hl7.fhir.dstu3
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024import java.io.IOException;
025import java.net.URISyntaxException;
026import java.text.ParseException;
027import java.util.UUID;
028
029import org.hl7.fhir.dstu3.model.ContactPoint.ContactPointSystem;
030import org.hl7.fhir.dstu3.model.Narrative.NarrativeStatus;
031import org.hl7.fhir.exceptions.FHIRException;
032import org.hl7.fhir.utilities.Utilities;
033import org.hl7.fhir.utilities.xhtml.XhtmlParser;
034
035/*
036Copyright (c) 2011+, HL7, Inc
037All rights reserved.
038
039Redistribution and use in source and binary forms, with or without modification, 
040are permitted provided that the following conditions are met:
041
042 * Redistributions of source code must retain the above copyright notice, this 
043   list of conditions and the following disclaimer.
044 * Redistributions in binary form must reproduce the above copyright notice, 
045   this list of conditions and the following disclaimer in the documentation 
046   and/or other materials provided with the distribution.
047 * Neither the name of HL7 nor the names of its contributors may be used to 
048   endorse or promote products derived from this software without specific 
049   prior written permission.
050
051THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
052ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
053WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
054IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
055INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
056NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
057PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
058WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
059ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
060POSSIBILITY OF SUCH DAMAGE.
061
062*/
063
064
065
066public class Factory {
067
068  public static IdType newId(String value) {
069    if (value == null)
070      return null;
071    IdType res = new IdType();
072    res.setValue(value);
073    return res;
074        }
075
076  public static StringType newString_(String value) {
077    if (value == null)
078      return null;
079    StringType res = new StringType();
080    res.setValue(value);
081    return res;
082  }
083
084  public static UriType newUri(String value) throws URISyntaxException {
085    if (value == null)
086      return null;
087    UriType res = new UriType();
088    res.setValue(value);
089    return res;
090  }
091
092  public static DateTimeType newDateTime(String value) throws ParseException {
093    if (value == null)
094      return null;
095    return new DateTimeType(value);
096  }
097
098  public static DateType newDate(String value) throws ParseException {
099    if (value == null)
100      return null;
101    return new DateType(value);
102  }
103
104  public static CodeType newCode(String value) {
105    if (value == null)
106      return null;
107    CodeType res = new CodeType();
108    res.setValue(value);
109    return res;
110  }
111
112  public static IntegerType newInteger(int value) {
113    IntegerType res = new IntegerType();
114    res.setValue(value);
115    return res;
116  }
117  
118  public static IntegerType newInteger(java.lang.Integer value) {
119    if (value == null)
120      return null;
121    IntegerType res = new IntegerType();
122    res.setValue(value);
123    return res;
124  }
125  
126  public static BooleanType newBoolean(boolean value) {
127    BooleanType res = new BooleanType();
128    res.setValue(value);
129    return res;
130  }
131  
132  public static ContactPoint newContactPoint(ContactPointSystem system, String value) {
133        ContactPoint res = new ContactPoint();
134        res.setSystem(system);
135        res.setValue(value);
136        return res;
137  }
138
139        public static Extension newExtension(String uri, Type value, boolean evenIfNull) {
140                if (!evenIfNull && (value == null || value.isEmpty()))
141                        return null;
142                Extension e = new Extension();
143                e.setUrl(uri);
144                e.setValue(value);
145          return e;
146  }
147
148        public static CodeableConcept newCodeableConcept(String code, String system, String display) {
149                CodeableConcept cc = new CodeableConcept();
150                Coding c = new Coding();
151                c.setCode(code);
152                c.setSystem(system);
153                c.setDisplay(display);
154                cc.getCoding().add(c);
155          return cc;
156  }
157
158        public static Reference makeReference(String url) {
159          Reference rr = new Reference();
160          rr.setReference(url);
161          return rr;
162        }
163
164        public static Narrative newNarrative(NarrativeStatus status, String html) throws IOException, FHIRException {
165                Narrative n = new Narrative();
166                n.setStatus(status);
167                try {
168                        n.setDiv(new XhtmlParser().parseFragment("<div>"+Utilities.escapeXml(html)+"</div>"));
169                } catch (org.hl7.fhir.exceptions.FHIRException e) {
170                        throw new FHIRException(e.getMessage(), e);
171                }
172                return n;
173        }
174
175        public static Coding makeCoding(String code) throws FHIRException {
176                String[] parts = code.split("\\|");
177                Coding c = new Coding();
178                if (parts.length == 2) {
179                        c.setSystem(parts[0]);
180                        c.setCode(parts[1]);
181                } else if (parts.length == 3) {
182                        c.setSystem(parts[0]);
183                        c.setCode(parts[1]);
184                        c.setDisplay(parts[2]);
185                } else 
186                        throw new FHIRException("Unable to understand the code '"+code+"'. Use the format system|code(|display)");
187                return c;
188        }
189
190        public static Reference makeReference(String url, String text) {
191                Reference rr = new Reference();
192                rr.setReference(url);
193                if (!Utilities.noString(text))
194                        rr.setDisplay(text);
195                return rr;
196        }
197
198  public static String createUUID() {
199    return "urn:uuid:"+UUID.randomUUID().toString().toLowerCase();
200  }
201
202  public Type create(String name) throws FHIRException {
203    if (name.equals("boolean"))
204      return new BooleanType();
205    else if (name.equals("integer"))
206      return new IntegerType();
207    else if (name.equals("decimal"))
208      return new DecimalType();
209    else if (name.equals("base64Binary"))
210      return new Base64BinaryType();
211    else if (name.equals("instant"))
212      return new InstantType();
213    else if (name.equals("string"))
214      return new StringType();
215    else if (name.equals("uri"))
216      return new UriType();
217    else if (name.equals("date"))
218      return new DateType();
219    else if (name.equals("dateTime"))
220      return new DateTimeType();
221    else if (name.equals("time"))
222      return new TimeType();
223    else if (name.equals("code"))
224      return new CodeType();
225    else if (name.equals("oid"))
226      return new OidType();
227    else if (name.equals("id"))
228      return new IdType();
229    else if (name.equals("unsignedInt"))
230      return new UnsignedIntType();
231    else if (name.equals("positiveInt"))
232      return new PositiveIntType();
233    else if (name.equals("markdown"))
234      return new MarkdownType();
235    else if (name.equals("Annotation"))
236      return new Annotation();
237    else if (name.equals("Attachment"))
238      return new Attachment();
239    else if (name.equals("Identifier"))
240      return new Identifier();
241    else if (name.equals("CodeableConcept"))
242      return new CodeableConcept();
243    else if (name.equals("Coding"))
244      return new Coding();
245    else if (name.equals("Quantity"))
246      return new Quantity();
247    else if (name.equals("Range"))
248      return new Range();
249    else if (name.equals("Period"))
250      return new Period();
251    else if (name.equals("Ratio"))
252      return new Ratio();
253    else if (name.equals("SampledData"))
254      return new SampledData();
255    else if (name.equals("Signature"))
256      return new Signature();
257    else if (name.equals("HumanName"))
258      return new HumanName();
259    else if (name.equals("Address"))
260      return new Address();
261    else if (name.equals("ContactPoint"))
262      return new ContactPoint();
263    else if (name.equals("Timing"))
264      return new Timing();
265    else if (name.equals("Reference"))
266      return new Reference();
267    else if (name.equals("Meta"))
268      return new Meta();
269    else
270      throw new FHIRException("Unknown data type name "+name);
271  }
272}