getMetaDataFromObject (metaDataKey, defaultValue)

Category

metadata functions

Signature

getMetaDataFromObject(String metadataKey, String defaultValue) : String

Description

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

Arguments

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

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

Returns

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

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

Note

When the model object in the context does not have the metadata key, CodeComposer will add the metadata key to the model object with the default value.

Exceptions

Error

This function raises a No model object in context exception when the context does not contain a model object.

Example

The following example shows parts of a code instruction that generates a Java class for each object, with a Java field named serialVersionUID.

Given the input shown in the input section below CodeComposer will generate the field with value 1 for ExampleObject1 and value 0 (default value) for ExampleObject2 as shown in the output section below.

Input

src/codeinstruction/examples/example-code-instruction-simple-entity-foreach-object-and-get-meta-data-from-object.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        >
11        <field name="serialVersionUID">
12                        <datatype>Long</datatype>
13                        <body>${getMetaDataFromObject(serialVersionUID,0)}L</body>
14                </field>
15        </class>
16</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                <model>
 5                        <package name="domain_model">
 6                                <object name="ExampleObject1">
 7                                        <metadata>
 8                                                <serialVersionUID>1</serialVersionUID>
 9                                        </metadata>
10                                </object>
11                                <object name="ExampleObject2">
12                                        <metadata>
13                                                <serialVersionUID>2</serialVersionUID>
14                                        </metadata>
15                                </object>
16                                <object name="ExampleObject3">
17                                        <metadata>
18                                        </metadata>
19                                </object>
20                        </package>
21                </model>
22        </package>
23</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 Long serialVersionUID = 1L;
6}
src/main/java/io/metafactory/codecomposer_reference/entities/simple/ExampleObject2SimpleEntity.java
1package io.metafactory.codecomposer_reference.entities.simple;
2
3public class ExampleObject2SimpleEntity
4{
5        private Long serialVersionUID = 2L;
6}
src/main/java/io/metafactory/codecomposer_reference/entities/simple/ExampleObject3SimpleEntity.java
1package io.metafactory.codecomposer_reference.entities.simple;
2
3public class ExampleObject3SimpleEntity
4{
5        private Long serialVersionUID = 0L;
6}