001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.model.dataformat;
018
019import java.util.List;
020import java.util.Set;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlTransient;
023import org.apache.camel.CamelContext;
024import org.apache.camel.model.DataFormatDefinition;
025import org.apache.camel.spi.DataFormat;
026import org.apache.camel.spi.Metadata;
027import org.apache.camel.util.ObjectHelper;
028
029public abstract class FhirDataformat extends DataFormatDefinition {
030    @XmlTransient
031    @Metadata(label = "advanced")
032    private Object fhirContext;
033
034    @XmlAttribute
035    @Metadata(enums = "DSTU2,DSTU2_HL7ORG,DSTU2_1,DSTU3,R4", defaultValue = "DSTU3")
036    private String fhirVersion;
037
038    @XmlAttribute
039    private Boolean prettyPrint;
040
041    @XmlTransient
042    @Metadata(label = "advanced")
043    private Object parserErrorHandler;
044
045    @XmlTransient
046    @Metadata(label = "advanced")
047    private Object parserOptions;
048
049    @XmlTransient
050    @Metadata(label = "advanced")
051    private Object preferTypes;
052
053    @XmlTransient
054    @Metadata(label = "advanced")
055    private Object forceResourceId;
056
057    @XmlAttribute
058    @Metadata(label = "advanced")
059    private String serverBaseUrl;
060
061    @XmlAttribute
062    @Metadata(label = "advanced")
063    private Boolean omitResourceId;
064
065    @XmlAttribute
066    @Metadata(label = "advanced")
067    private Set<String> encodeElementsAppliesToResourceTypes;
068
069    @XmlAttribute
070    @Metadata(label = "advanced")
071    private Boolean encodeElementsAppliesToChildResourcesOnly;
072
073    @XmlAttribute
074    @Metadata(label = "advanced")
075    private Set<String> encodeElements;
076
077    @XmlAttribute
078    @Metadata(label = "advanced")
079    private Set<String> dontEncodeElements;
080
081    @XmlAttribute
082    @Metadata(label = "advanced")
083    private Boolean stripVersionsFromReferences;
084
085    @XmlAttribute
086    @Metadata(label = "advanced")
087    private Boolean overrideResourceIdWithBundleEntryFullUrl;
088
089    @XmlAttribute
090    @Metadata(label = "advanced")
091    private Boolean summaryMode;
092
093    @XmlAttribute
094    @Metadata(label = "advanced")
095    private Boolean suppressNarratives;
096
097    @XmlAttribute
098    @Metadata(label = "advanced")
099    private List<String> dontStripVersionsFromReferencesAtPaths;
100
101    protected FhirDataformat(String dataFormatName) {
102        super(dataFormatName);
103    }
104
105    protected FhirDataformat() {
106        // This constructor is needed by jaxb for schema generation
107    }
108
109
110
111    @Override
112    protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
113        if (getContentTypeHeader() != null) {
114            setProperty(camelContext, dataFormat, "contentTypeHeader", getContentTypeHeader());
115        }
116        if (getFhirContext() != null) {
117            setProperty(camelContext, dataFormat, "fhirContext", getFhirContext());
118        }
119        if (getFhirVersion() != null) {
120            setProperty(camelContext, dataFormat, "fhirVersion", getFhirVersion());
121        }
122        if (ObjectHelper.isNotEmpty(getDontStripVersionsFromReferencesAtPaths())) {
123            setProperty(camelContext, dataFormat, "dontStripVersionsFromReferencesAtPaths", getDontStripVersionsFromReferencesAtPaths());
124        }
125        if (ObjectHelper.isNotEmpty(getDontEncodeElements())) {
126            setProperty(camelContext, dataFormat, "dontEncodeElements", getDontEncodeElements());
127        }
128        if (ObjectHelper.isNotEmpty(getEncodeElements())) {
129            setProperty(camelContext, dataFormat, "encodeElements", getEncodeElements());
130        }
131        if (ObjectHelper.isNotEmpty(getEncodeElementsAppliesToResourceTypes())) {
132            setProperty(camelContext, dataFormat, "encodeElementsAppliesToResourceTypes", getEncodeElementsAppliesToResourceTypes());
133        }
134        if (ObjectHelper.isNotEmpty(getServerBaseUrl())) {
135            setProperty(camelContext, dataFormat, "serverBaseUrl", getServerBaseUrl());
136        }
137        if (ObjectHelper.isNotEmpty(getForceResourceId())) {
138            setProperty(camelContext, dataFormat, "forceResourceId", getForceResourceId());
139        }
140        if (ObjectHelper.isNotEmpty(getPreferTypes())) {
141            setProperty(camelContext, dataFormat, "preferTypes", getPreferTypes());
142        }
143        if (ObjectHelper.isNotEmpty(getParserOptions())) {
144            setProperty(camelContext, dataFormat, "parserOptions", getParserOptions());
145        }
146        if (ObjectHelper.isNotEmpty(getParserErrorHandler())) {
147            setProperty(camelContext, dataFormat, "parserErrorHandler", getParserErrorHandler());
148        }
149
150        Boolean answer = ObjectHelper.toBoolean(isEncodeElementsAppliesToChildResourcesOnly());
151        if (answer != null) {
152            setProperty(camelContext, dataFormat, "encodeElementsAppliesToChildResourcesOnly", answer);
153        }
154        answer = ObjectHelper.toBoolean(isOmitResourceId());
155        if (answer != null) {
156            setProperty(camelContext, dataFormat, "omitResourceId", answer);
157        }
158        answer = ObjectHelper.toBoolean(isPrettyPrint());
159        if (answer != null) {
160            setProperty(camelContext, dataFormat, "prettyPrint", answer);
161        }
162        answer = ObjectHelper.toBoolean(isSuppressNarratives());
163        if (answer != null) {
164            setProperty(camelContext, dataFormat, "suppressNarratives", answer);
165        }
166        answer = ObjectHelper.toBoolean(isSummaryMode());
167        if (answer != null) {
168            setProperty(camelContext, dataFormat, "summaryMode", answer);
169        }
170        answer = ObjectHelper.toBoolean(getOverrideResourceIdWithBundleEntryFullUrl());
171        if (answer != null) {
172            setProperty(camelContext, dataFormat, "overrideResourceIdWithBundleEntryFullUrl", answer);
173        }
174        answer = ObjectHelper.toBoolean(getStripVersionsFromReferences());
175        if (answer != null) {
176            setProperty(camelContext, dataFormat, "stripVersionsFromReferences", answer);
177        }
178    }
179
180    public Object getFhirContext() {
181        return fhirContext;
182    }
183
184    public void setFhirContext(Object fhirContext) {
185        this.fhirContext = fhirContext;
186    }
187
188    public String getFhirVersion() {
189        return fhirVersion;
190    }
191
192    /**
193     * The version of FHIR to use. Possible values are: DSTU2,DSTU2_HL7ORG,DSTU2_1,DSTU3,R4
194     */
195    public void setFhirVersion(String fhirVersion) {
196        this.fhirVersion = fhirVersion;
197    }
198
199    public Boolean isPrettyPrint() {
200        return prettyPrint;
201    }
202
203    /**
204     * Sets the "pretty print" flag, meaning that the parser will encode resources with human-readable spacing and
205     * newlines between elements instead of condensing output as much as possible.
206     *
207     * @param prettyPrint The flag
208     */
209    public void setPrettyPrint(Boolean prettyPrint) {
210        this.prettyPrint = prettyPrint;
211    }
212
213    public Object getParserErrorHandler() {
214        return parserErrorHandler;
215    }
216
217    /**
218     * Registers an error handler which will be invoked when any parse errors are found
219     *
220     * @param parserErrorHandler The error handler to set. Must not be null.
221     */
222    public void setParserErrorHandler(Object parserErrorHandler) {
223        this.parserErrorHandler = parserErrorHandler;
224    }
225
226    public Object getParserOptions() {
227        return parserOptions;
228    }
229
230    /**
231     * Sets the parser options object which will be used to supply default
232     * options to newly created parsers.
233     *
234     * @param parserOptions The parser options object
235     */
236    public void setParserOptions(Object parserOptions) {
237        this.parserOptions = parserOptions;
238    }
239
240    public Object getPreferTypes() {
241        return preferTypes;
242    }
243
244    /**
245     * If set, when parsing resources the parser will try to use the given types when possible, in
246     * the order that they are provided (from highest to lowest priority). For example, if a custom
247     * type which declares to implement the Patient resource is passed in here, and the
248     * parser is parsing a Bundle containing a Patient resource, the parser will use the given
249     * custom type.
250     *
251     * @param preferTypes The preferred types, or <code>null</code>
252     */
253    public void setPreferTypes(Object preferTypes) {
254        this.preferTypes = preferTypes;
255    }
256
257    public Object getForceResourceId() {
258        return forceResourceId;
259    }
260
261    /**
262     * When encoding, force this resource ID to be encoded as the resource ID
263     */
264    public void setForceResourceId(Object forceResourceId) {
265        this.forceResourceId = forceResourceId;
266    }
267
268    public String getServerBaseUrl() {
269        return serverBaseUrl;
270    }
271
272    /**
273     * Sets the server's base URL used by this parser. If a value is set, resource references will be turned into
274     * relative references if they are provided as absolute URLs but have a base matching the given base.
275     *
276     * @param serverBaseUrl The base URL, e.g. "http://example.com/base"
277     */
278    public void setServerBaseUrl(String serverBaseUrl) {
279        this.serverBaseUrl = serverBaseUrl;
280    }
281
282    public Boolean isOmitResourceId() {
283        return omitResourceId;
284    }
285
286    /**
287     * If set to <code>true</code> (default is <code>false</code>) the ID of any resources being encoded will not be
288     * included in the output. Note that this does not apply to contained resources, only to root resources. In other
289     * words, if this is set to <code>true</code>, contained resources will still have local IDs but the outer/containing
290     * ID will not have an ID.
291     *
292     * @param omitResourceId Should resource IDs be omitted
293     */
294    public void setOmitResourceId(Boolean omitResourceId) {
295        this.omitResourceId = omitResourceId;
296    }
297
298    public Set<String> getEncodeElementsAppliesToResourceTypes() {
299        return encodeElementsAppliesToResourceTypes;
300    }
301
302    /**
303     * If provided, tells the parse which resource types to apply {@link #setEncodeElements(Set) encode elements} to. Any
304     * resource types not specified here will be encoded completely, with no elements excluded.
305     *
306     * @param encodeElementsAppliesToResourceTypes resouce types
307     */
308    public void setEncodeElementsAppliesToResourceTypes(Set<String> encodeElementsAppliesToResourceTypes) {
309        this.encodeElementsAppliesToResourceTypes = encodeElementsAppliesToResourceTypes;
310    }
311
312    public Boolean isEncodeElementsAppliesToChildResourcesOnly() {
313        return encodeElementsAppliesToChildResourcesOnly;
314    }
315
316    /**
317     * If set to <code>true</code> (default is false), the values supplied
318     * to {@link #setEncodeElements(Set)} will not be applied to the root
319     * resource (typically a Bundle), but will be applied to any sub-resources
320     * contained within it (i.e. search result resources in that bundle)
321     */
322    public void setEncodeElementsAppliesToChildResourcesOnly(Boolean encodeElementsAppliesToChildResourcesOnly) {
323        this.encodeElementsAppliesToChildResourcesOnly = encodeElementsAppliesToChildResourcesOnly;
324    }
325
326    public Set<String> getEncodeElements() {
327        return encodeElements;
328    }
329
330    /**
331     * If provided, specifies the elements which should be encoded, to the exclusion of all others. Valid values for this
332     * field would include:
333     * <ul>
334     * <li><b>Patient</b> - Encode patient and all its children</li>
335     * <li><b>Patient.name</b> - Encode only the patient's name</li>
336     * <li><b>Patient.name.family</b> - Encode only the patient's family name</li>
337     * <li><b>*.text</b> - Encode the text element on any resource (only the very first position may contain a
338     * wildcard)</li>
339     * <li><b>*.(mandatory)</b> - This is a special case which causes any mandatory fields (min > 0) to be encoded</li>
340     * </ul>
341     *
342     * @param encodeElements The elements to encode
343     * @see #setDontEncodeElements(Set)
344     */
345    public void setEncodeElements(Set<String> encodeElements) {
346        this.encodeElements = encodeElements;
347    }
348
349    public Set<String> getDontEncodeElements() {
350        return dontEncodeElements;
351    }
352
353    /**
354     * If provided, specifies the elements which should NOT be encoded. Valid values for this
355     * field would include:
356     * <ul>
357     * <li><b>Patient</b> - Don't encode patient and all its children</li>
358     * <li><b>Patient.name</b> - Don't encode the patient's name</li>
359     * <li><b>Patient.name.family</b> - Don't encode the patient's family name</li>
360     * <li><b>*.text</b> - Don't encode the text element on any resource (only the very first position may contain a
361     * wildcard)</li>
362     * </ul>
363     * <p>
364     * DSTU2 note: Note that values including meta, such as <code>Patient.meta</code>
365     * will work for DSTU2 parsers, but values with subelements on meta such
366     * as <code>Patient.meta.lastUpdated</code> will only work in
367     * DSTU3+ mode.
368     * </p>
369     *
370     * @param dontEncodeElements The elements to encode
371     * @see #setEncodeElements(Set)
372     */
373    public void setDontEncodeElements(Set<String> dontEncodeElements) {
374        this.dontEncodeElements = dontEncodeElements;
375    }
376
377    public Boolean getStripVersionsFromReferences() {
378        return stripVersionsFromReferences;
379    }
380
381    /**
382     * If set to <code>true<code> (which is the default), resource references containing a version
383     * will have the version removed when the resource is encoded. This is generally good behaviour because
384     * in most situations, references from one resource to another should be to the resource by ID, not
385     * by ID and version. In some cases though, it may be desirable to preserve the version in resource
386     * links. In that case, this value should be set to <code>false</code>.
387     * <p>
388     * This method provides the ability to globally disable reference encoding. If finer-grained
389     * control is needed, use {@link #setDontStripVersionsFromReferencesAtPaths(List)}
390     * </p>
391     *
392     * @param stripVersionsFromReferences Set this to <code>false<code> to prevent the parser from removing resource versions
393     *                                    from references (or <code>null</code> to apply the default setting from the {@link #setParserOptions(Object)}
394     * @see #setDontStripVersionsFromReferencesAtPaths(List)
395     */
396    public void setStripVersionsFromReferences(Boolean stripVersionsFromReferences) {
397        this.stripVersionsFromReferences = stripVersionsFromReferences;
398    }
399
400    public Boolean getOverrideResourceIdWithBundleEntryFullUrl() {
401        return overrideResourceIdWithBundleEntryFullUrl;
402    }
403
404    /**
405     * If set to <code>true</code> (which is the default), the Bundle.entry.fullUrl will override the Bundle.entry.resource's
406     * resource id if the fullUrl is defined. This behavior happens when parsing the source data into a Bundle object. Set this
407     * to <code>false</code> if this is not the desired behavior (e.g. the client code wishes to perform additional
408     * validation checks between the fullUrl and the resource id).
409     *
410     * @param overrideResourceIdWithBundleEntryFullUrl
411     *           Set this to <code>false</code> to prevent the parser from overriding resource ids with the
412     *           Bundle.entry.fullUrl (or <code>null</code> to apply the default setting from the {@link #setParserOptions(Object)})
413     */
414    public void setOverrideResourceIdWithBundleEntryFullUrl(Boolean overrideResourceIdWithBundleEntryFullUrl) {
415        this.overrideResourceIdWithBundleEntryFullUrl = overrideResourceIdWithBundleEntryFullUrl;
416    }
417
418    public Boolean isSummaryMode() {
419        return summaryMode;
420    }
421
422    /**
423     * If set to <code>true</code> (default is <code>false</code>) only elements marked by the FHIR specification as
424     * being "summary elements" will be included.
425     */
426    public void setSummaryMode(Boolean summaryMode) {
427        this.summaryMode = summaryMode;
428    }
429
430    public Boolean isSuppressNarratives() {
431        return suppressNarratives;
432    }
433
434    /**
435     * If set to <code>true</code> (default is <code>false</code>), narratives will not be included in the encoded
436     * values.
437     */
438    public void setSuppressNarratives(Boolean suppressNarratives) {
439        this.suppressNarratives = suppressNarratives;
440    }
441
442    public List<String> getDontStripVersionsFromReferencesAtPaths() {
443        return dontStripVersionsFromReferencesAtPaths;
444    }
445
446    /**
447     * If supplied value(s), any resource references at the specified paths will have their
448     * resource versions encoded instead of being automatically stripped during the encoding
449     * process. This setting has no effect on the parsing process.
450     * <p>
451     * This method provides a finer-grained level of control than {@link #setStripVersionsFromReferences(Boolean)}
452     * and any paths specified by this method will be encoded even if {@link #setStripVersionsFromReferences(Boolean)}
453     * has been set to <code>true</code> (which is the default)
454     * </p>
455     *
456     * @param dontStripVersionsFromReferencesAtPaths
457     *           A collection of paths for which the resource versions will not be removed automatically
458     *           when serializing, e.g. "Patient.managingOrganization" or "AuditEvent.object.reference". Note that
459     *           only resource name and field names with dots separating is allowed here (no repetition
460     *           indicators, FluentPath expressions, etc.). Set to <code>null</code> to use the value
461     *           set in the {@link #setParserOptions(Object)}
462     * @see #setStripVersionsFromReferences(Boolean)
463     */
464    public void setDontStripVersionsFromReferencesAtPaths(List<String> dontStripVersionsFromReferencesAtPaths) {
465        this.dontStripVersionsFromReferencesAtPaths = dontStripVersionsFromReferencesAtPaths;
466    }
467
468}