getMetaDataFromAttribute (metadataKey, defaultValue)

Category

metadata functions

Signature

getMetaDataFromAttribute(String metadataKey, String defaultValue) : String

Description

Gets metadata from the model attribute in the context with a certain metadata key.

Arguments

  • metaDataKey: key of metadata to get from the model attribute

  • defaultValue: value to return when the model attribute does not have metadata with metadata key

Returns

  • metadata value of the model attribute in the context if it has the metadata key

  • the provided default value if the model attribute in the context does not have the metadata key

Note

When the model attribute in the context does not have the metadata key, CodeComposer will add the metadata key to the model attribute with the default 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 parts of a code instruction that generates a Java class for each object, with a Java field for each attribute with metadata setInitialValue. The actual initial value is specified with metadata initialValue. When the attribute has no metadata initialValue, the code instruction states the default value is 0L.

Given the input shown in the input section below CodeComposer will generate the field with value 1L for attribute exampleCounter1 and value 0L (default value) for attribute exampleCounter2 and no value for attribute exampleCounter3 as shown in the output section below.

Input

src/codeinstruction/examples/example-code-instruction-simple-entity-foreach-attribute-and-get-meta-data-from-attribute.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(setInitialValue)}"
13           access="rw">
14      <datatype>${attribute.type}</datatype>
15      <body>${getMetaDataFromAttribute(initialValue,0)}L</body>
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="ExampleObject2">
 5                        <metadata>
 6                                <createSimpleService>false</createSimpleService>
 7                                <createSpecialService>false</createSpecialService>
 8                                <name.plural>ExampleObjects2</name.plural>
 9                        </metadata>
10                        <attribute name="exampleCounter1" type="Long">
11                                <metadata>
12                                        <initialValue>1</initialValue>
13                                        <setInitialValue></setInitialValue>
14                                </metadata>
15                        </attribute>
16                        <attribute name="exampleCounter2" type="Long">
17                                <metadata>
18                                        <setInitialValue></setInitialValue>
19                                </metadata>
20                        </attribute>
21                        <attribute name="exampleCounter3" type="Long">
22                                <metadata>
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 ExampleObject2SimpleEntity
 4{
 5        private Long exampleCounter1 = 1L;
 6
 7        private Long exampleCounter2 = 0L;
 8
 9        private Long exampleCounter3;
10}

Note

Because the model attribute exampleCounter2 does not have the metadata key initialValue, CodeComposer has added the metadata key to the model attribute with the default value 0 (See highlighted line 18).

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="ExampleObject2">
 5                        <metadata>
 6                                <createSimpleService>false</createSimpleService>
 7                                <createSpecialService>false</createSpecialService>
 8                                <name.plural>ExampleObjects2</name.plural>
 9                        </metadata>
10                        <attribute name="exampleCounter1" type="Long">
11                                <metadata>
12                                        <initialValue>1</initialValue>
13                                        <setInitialValue></setInitialValue>
14                                </metadata>
15                        </attribute>
16                        <attribute name="exampleCounter2" type="Long">
17                                <metadata>
18                                        <initialValue>0</initialValue>
19                                        <setInitialValue></setInitialValue>
20                                </metadata>
21                        </attribute>
22                        <attribute name="exampleCounter3" type="Long">
23                                <metadata>
24                                </metadata>
25                        </attribute>
26                </object>
27        </package>
28</model>