attributeHasMetaData (metaDataKey)

Category

metadata functions

Signature

attributeHasMetaData(String metadataKey) : boolean

Description

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

Arguments

metaDataKey: key of metadata to check for existence

Returns

  • true if the model attribute in the context has the metadata key,

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

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 that is transient for each attribute that has metadata transient.

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

Input

src/codeinstruction/examples/example-code-instruction-simple-entity-foreach-attribute-and-attribute-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="${attribute.name}"
11                       foreach="attribute"
12                       condition="${attributeHasMetaData(transient)}"
13                       transient="true"
14                       access="rw"
15                >
16                        <datatype>${attribute.type}</datatype>
17                </field>
18        </class>
19</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>
 8                                </metadata>
 9                        </attribute>
10                        <attribute name="exampleAttribute2" type="Long">
11                                <metadata>
12                                        <transient></transient>
13                                </metadata>
14                        </attribute>
15                </object>
16        </package>
17</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 Long exampleAttribute2;
7}