getMetaDataFromAttribute (metadataKey, defaultValue)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. list-table::
:widths: 20 80
:header-rows: 0
:class: tight-table
* - 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**
.. code-block:: xml
:caption: src/codeinstruction/examples/example-code-instruction-simple-entity-foreach-attribute-and-get-meta-data-from-attribute.xml
:name: GetMetaDataFromAttribute_CodeInstruction
:linenos:
:emphasize-lines: 15
${attribute.type}
${getMetaDataFromAttribute(initialValue,0)}L
.. code-block:: xml
:caption: src/model/model.xml
:name: GetMetaDataFromAttribute_Model
:linenos:
:emphasize-lines: 12, 13, 18, 22, 23
**Output**
.. code-block:: java
:caption: src/main/java/io/metafactory/codecomposer_reference/entities/simple/ExampleObject1SimpleEntity.java
:name: AttributeHasMetaData_Output1
:linenos:
:emphasize-lines: 5, 7, 9
package io.metafactory.codecomposer_reference.entities.simple;
public class ExampleObject2SimpleEntity
{
private Long exampleCounter1 = 1L;
private Long exampleCounter2 = 0L;
private Long exampleCounter3;
}
.. 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).
.. code-block:: xml
:caption: src/model/model.xml
:name: GetMetaDataFromAttribute_Model
:linenos:
:emphasize-lines: 18