5.6.1. foreach=”attribute.all”

Context

reference/code instructions/foreach=”attributes.all”

Signature

foreach=”attribute.all”

Description

foreach=”attribute.all” instructs the CodeComposer to generate code for each attribute of the current model object, including all of the attributes of the model objects the current model object extends directly or indirectly.

Exceptions

This foreach raises an InvalidPatternException when the context does not contain a model object:

Error

InvalidPatternException: invalid pattern: trying to create a <patternType> foreach attribute but currentModelObject is null. Either specify a foreach=”object” in pattern or refer directly to a object of the model with the package=”<package name>” and object=”<object name>” statement.

Failed to produce code due to invalid pattern.

Example

CodeComposer input

Listing 5.42 codecomposer-reference-input/src/codeinstruction/examples/example-code-instruction-simple-entity-foreach-attribute-all.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}.dtos.simple"
 6              path="${pattern.property.java.main.directory}"
 7              package="domain_model">
 8  <class name="${createSimpleDtoClassName(${object.name})}"
 9         foreach="object">
10        <field name="${attribute.name}"
11               foreach="attribute.all"
12        >
13          <datatype>${attribute.type}</datatype>
14        </field>
15  </class>
16</java_package>
Listing 5.43 codecomposer-reference-input/src/codeinstruction/example-custom-functions.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<functions xmlns="https://metafactory.io/xsd/v1/codeinstruction" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://metafactory.io/xsd/v1/codeinstruction https://metafactory.io/xsd/v1/codeinstruction.xsd">
3        <function name="createSimpleDtoClassName">
4                <!--Function must be called with 1 argument:
5                        1) name of the model object
6                -->
7                <definition>${arg1}SimpleDto</definition>
8        </function>
9</functions>
Listing 5.44 codecomposer-reference-input/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="ExampleObject5">
 5                  <metadata>
 6                        <name.plural>ExampleObjects5</name.plural>
 7                        <serialVersionUID>0</serialVersionUID>
 8                  </metadata>
 9                  <attribute name="exampleObject5Attribute" type="String"></attribute>
10                </object>
11                <object name="ExampleObject6" extends="ExampleObject5">
12                  <metadata>
13                        <name.plural>ExampleObjects6</name.plural>
14                        <serialVersionUID>0</serialVersionUID>
15                  </metadata>
16                  <attribute name="exampleObject6Attribute" type="String"></attribute>
17                </object>
18                <object name="ExampleObject7" extends="ExampleObject6">
19                  <metadata>
20                        <name.plural>ExampleObjects7</name.plural>
21                        <serialVersionUID>0</serialVersionUID>
22                  </metadata>
23                  <attribute name="exampleObject7Attribute" type="String"></attribute>
24                </object>
25        </package>
26</model>

CodeComposer output

Listing 5.45 codecomposer-reference-output/src/main/java/io/metafactory/codecomposer_reference/dtos/simple/ExampleObject5SimpleDto.java
 1package io.metafactory.codecomposer_reference.dtos.simple;
 2
 3
 4
 5      
 6/**
 7* ExampleObject5SimpleDto -  Created by MetaFactory: Automation of Software Development 
 8
 9 */
10public class ExampleObject5SimpleDto
11{
12  private String exampleObject5Attribute;
13
14
15
16}
Listing 5.46 codecomposer-reference-output/src/main/java/io/metafactory/codecomposer_reference/dtos/simple/ExampleObject6SimpleDto.java
 1package io.metafactory.codecomposer_reference.dtos.simple;
 2
 3
 4
 5      
 6/**
 7* ExampleObject6SimpleDto -  Created by MetaFactory: Automation of Software Development 
 8
 9 */
10public class ExampleObject6SimpleDto
11{
12  private String exampleObject5Attribute;
13
14  private String exampleObject6Attribute;
15
16
17
18}
Listing 5.47 codecomposer-reference-output/src/main/java/io/metafactory/codecomposer_reference/dtos/simple/ExampleObject7SimpleDto.java
 1package io.metafactory.codecomposer_reference.dtos.simple;
 2
 3
 4
 5      
 6/**
 7* ExampleObject7SimpleDto -  Created by MetaFactory: Automation of Software Development 
 8
 9 */
10public class ExampleObject7SimpleDto
11{
12  private String exampleObject5Attribute;
13
14  private String exampleObject6Attribute;
15
16  private String exampleObject7Attribute;
17
18
19
20}