referenceHasMetaData (metaDataKey)

Category

metadata functions

Signature

referenceHasMetaData(String metadataKey, String metadataValue) : boolean

Description

Checks if the model reference in the context has a certain metadata key.

Arguments

metaDataKey: key of metadata to check for existence

Returns

  • true if the model reference in the context has the metadata key

  • false if the model reference in the context does not have the metadata key

Exceptions

Error

This function raises a No model reference in context exception when the context does not contain a model reference.

Example

The following example shows a code instruction that generates a Java class for each object, and conditionally adds a field for each reference that has metadata bar. The field will have a comment line.

Given the input shown in the input section below CodeComposer will generate the comment for reference exampleObject2 and exampleObject3 as shown in the output section below.

Note

Multiple code instructions may generate code in the same output file. The datatype of the fields shown in the output is generated by another code instruction.

Input

src/codeinstruction/examples/example-code-instruction-simple-entity-foreach-reference-and-reference-has-meta-data.xml
 1<?xml version="1.0" encoding="UTF-8"?>
 2<java_package xmlns="https://metafactory.io/xsd/v1/java-codeinstruction"
 3              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4              xsi:schemaLocation="https://metafactory.io/xsd/v1/java-codeinstruction https://metafactory.io/xsd/v1/java-codeinstruction.xsd"
 5              name="${pattern.property.java.package.base}.entities.simple"
 6              path="${pattern.property.java.main.directory}"
 7              package="domain_model">
 8  <class  name="${createSimpleEntityClassName(${object.name})}"
 9          foreach="object">
10    <field  name="${reference.name}"
11            access="rw"
12            foreach="reference"
13            condition="${referenceHasMetaData(bar)}"
14    >
15      <apicommentline>${fmsnippet.examples.exampleCommentForReferenceThatHasMetaDataBar}</apicommentline>
16    </field>
17  </class>
18</java_package>
src/snippet/examples/exampleCommentForReferenceThatHasMetaDataBar.ftl
1<#if !(modelReference)??>  <#stop "modelReference not found in context" ></#if>
2
3This is an example comment for a reference with meta data bar: ${modelReference.type} ${modelReference.name}.
src/model/model.xml
 1<?xml version="1.0" encoding="UTF-8"?>
 2<model xmlns="https://metafactory.io/xsd/v1/model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://metafactory.io/xsd/v1/model https://metafactory.io/xsd/v1/model.xsd">
 3        <package name="domain_model">
 4                <object name="ExampleObject4">
 5                        <metadata>
 6                                <name.plural>ExampleObjects4</name.plural>
 7                                <serialVersionUID>0</serialVersionUID>
 8                        </metadata>
 9                        <reference name="exampleObject1" type="ExampleObject1" multiplicity="1..1" notnull="true">
10                                <metadata>
11                                </metadata>
12                        </reference>
13                        <reference name="exampleObject2" type="ExampleObject2" multiplicity="1..1" notnull="true">
14                                <metadata>
15                                        <bar></bar>
16                                </metadata>
17                        </reference>
18                        <reference name="exampleObject3" type="ExampleObject3" multiplicity="1..1" notnull="true">
19                                <metadata>
20                                        <bar>baz</bar>
21                                </metadata>
22                        </reference>
23                </object>
24        </package>
25</model>

Output

src/main/java/io/metafactory/codecomposer_reference/entities/simple/ExampleObject4SimpleEntity.java
 1package io.metafactory.codecomposer_reference.entities.simple;
 2
 3public class ExampleObject4SimpleEntity
 4{
 5        private ExampleObject1SimpleEntity exampleObject1;
 6
 7        /**
 8         * This is an example comment for a reference with meta data bar: ExampleObject2 exampleObject2.
 9         */
10        private ExampleObject2SimpleEntity exampleObject2;
11
12
13        /**
14         * This is an example comment for a reference with meta data bar: ExampleObject3 exampleObject3.
15         */
16        private ExampleObject3SimpleEntity exampleObject3;
17}