referenceHasMetaData (metaDataKey, metadataValue)

Category

metadata functions

Signature

referenceHasMetaData(String metadataKey, String metadataValue) : boolean

Description

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

Arguments

  • metadataKey: key of metadata to check for existence

  • metadataValue: value of metadata to check for existence

Returns

  • true if the model reference in the context has the metadata key with the given metadata value

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

    key with a metadata value different from the given metadata value.

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 generates a comment for a Java field for each reference that has metadata bar with value baz.

Given the input in the input section below CodeComposer will generate the file(s) as shown in the output section below.

Input

src/codeinstruction/examples/example-code-instruction-simple-entity-foreach-reference-and-reference-has-meta-data-with-value.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,baz)}"
14    >
15      <apicommentline>${fmsnippet.examples.exampleCommentForReferenceThatHasMetaDataBarWithValueBaz}</apicommentline>
16    </field>
17  </class>
18</java_package>
src/snippet/examples/exampleCommentForReferenceThatHasMetaDataBarWithValueBaz.ftl
1<#if !(modelReference)??>  <#stop "modelReference not found in context" ></#if>
2
3This is an example comment for a reference with meta data bar with value baz: ${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
 3/**
 4 * This is an example comment for a reference with any multiplicity: ExampleObject1 exampleObject1.
 5 * This is an example comment for a reference with multiplicity 0..1 or 1..1: ExampleObject1 exampleObject1.
 6 */
 7private ExampleObject1SimpleEntity exampleObject1;
 8
 9
10/**
11 * This is an example comment for a reference with any multiplicity: ExampleObject2 exampleObject2.
12 * This is an example comment for a reference with multiplicity 0..1 or 1..1: ExampleObject2 exampleObject2.
13 * This is an example comment for a reference with meta data bar: ExampleObject2 exampleObject2.
14 */
15private ExampleObject2SimpleEntity exampleObject2;
16
17
18/**
19 * This is an example comment for a reference with any multiplicity: ExampleObject3 exampleObject3.
20 * This is an example comment for a reference with multiplicity 0..1 or 1..1: ExampleObject3 exampleObject3.
21 * This is an example comment for a reference with meta data bar: ExampleObject3 exampleObject3.
22 * This is an example comment for a reference with meta data bar with value baz: ExampleObject3 exampleObject3.
23 */
24private ExampleObject3SimpleEntity exampleObject3;