attributeHasMetaData (metaDataKey, metadataValue)

Category

metadata functions

Signature

attributeHasMetaData(String metadataKey, String metadataValue) : boolean

Description

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

Arguments

  • metadataKey: key of metadata to check for existence

  • metadataValue: value of metadata to check for existence

Returns

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

  • false if the model attribute 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 attribute in context exception when the context does not contain a model attribute.

Example

The following example shows a code instruction that generates a Java class for each object, with a Java field of type Long, for each attribute that has metadata bar with value baz.

Given the input shown in the input section below CodeComposer will generate the field only for attribute exampleAttribute3 as shown in the output section below.

Input

src/codeinstruction/examples/example-code-instruction-simple-entity-foreach-attribute-and-attribute-has-meta-data-with-value.xml
 1<java_package xmlns="https://metafactory.io/xsd/v1/java-codeinstruction"
 2              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3              xsi:schemaLocation="https://metafactory.io/xsd/v1/java-codeinstruction https://metafactory.io/xsd/v1/java-codeinstruction.xsd"
 4              name="${pattern.property.java.package.base}.entities.simple"
 5              path="${pattern.property.java.main.directory}"
 6              package="domain_model"
 7>
 8        <class  name="${createSimpleEntityClassName(${object.name})}"
 9                foreach="object"
10        >
11                <field name="${attribute.name}Baz">
12                       foreach="attribute"
13                       condition="${attributeHasMetaData(bar,baz)}"
14                >
15                        <datatype>Long</datatype>
16                </field>
17        </class>
18</java_package>
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="ExampleObject1">
 5                        <metadata></metadata>
 6                        <attribute name="exampleAttribute1" type="String">
 7                                <metadata></metadata>
 8                        </attribute>
 9                        <attribute name="exampleAttribute2" type="Long">
10                                <metadata>
11                                        <bar></bar>
12                                        <transient></transient>
13                                </metadata>
14                        </attribute>
15                        <attribute name="exampleAttribute3" type="String">
16                                <metadata>
17                                        <bar>baz</bar>
18                                </metadata>
19                        </attribute>
20                        <attribute name="exampleAttribute4" type="Long">
21                                <metadata>
22                                        <bar>zab</bar>
23                                </metadata>
24                        </attribute>
25                </object>
26        </package>
27</model>

Output

src/main/java/io/metafactory/codecomposer_reference/entities/simple/ExampleObject1SimpleEntity.java
 1package io.metafactory.codecomposer_reference.entities.simple;
 2
 3public class ExampleObject1SimpleEntity
 4{
 5        private String exampleAttribute1;
 6        private transient String exampleAttribute2;
 7        private String exampleAttribute3;
 8        private String exampleAttribute4;
 9        private Long exampleAttribute3Baz;
10}