2.6. T05 Foreach Object

In this tutorial we will create a class for each object in the model.

  • Open in directory metafactory-workshop-metafactory\src\model the file called model.xml and add the highlighted content:

Eye S You can open model.xml directly from the MetaFactory Project window by double clicking it.

Listing 2.11 src/model/model.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?xml version="1.0" encoding="UTF-8"?>
<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">
  <metadata>
    <angular.webapp>
      <main.directory>src/main/webapp</main.directory>
      <root>src/main/webapp/app</root>
    </angular.webapp>
    <application>
      <backend.api.url>
        <dev>http://workshop.local:8080</dev>
        <prod>https://localhost:8080</prod>
      </backend.api.url>
      <frontend.host.dev>localhost</frontend.host.dev>
    </application>
    <backend.app.class.name>MetaFactoryworkshopApplication</backend.app.class.name>
    <main.stylesheet.filename>global.scss</main.stylesheet.filename>
  </metadata>
  <package name="domain">
    <metadata>
      <base>com.metafactory.workshop</base>
      <content.folder.name>content</content.folder.name>
      <dto>
        <demo.keyword>Demo</demo.keyword>
        <rest.controller.package>com.metafactory.workshop.rest.dto</rest.controller.package>
      </dto>
      <jhipster>
        <login.screen>modal</login.screen>
        <style>6</style>
      </jhipster>
      <jhl.base>com.metafactory.workshop</jhl.base>
      <main.stylesheet.filename>global.scss</main.stylesheet.filename>
      <markup.version>material_v1</markup.version>
      <maven.artifact>
        <id>metafactory-workshop</id>
        <name>MetaFactory workshop</name>
        <version>1.0.0-SNAPSHOT</version>
      </maven.artifact>
      <model>
        <implementation.package>com.metafactory.workshop.domain</implementation.package>
        <runtime.package>org.metafactory.model.runtime.domain</runtime.package>
      </model>
      <project.support.intelligentfiltering>true</project.support.intelligentfiltering>
      <repository.jpa>
        <class.package>com.metafactory.workshop.repository.jpa.impl</class.package>
        <dialect>
          <packagename>com.metafactory.workshop.repository.dialect</packagename>
          <version>82</version>
        </dialect>
      </repository.jpa>
      <repository>
        <jpa>
          <interface.package>com.metafactory.workshop.repository.jpa</interface.package>
        </jpa>
        <style>JPA2</style>
        <util.package>com.metafactory.workshop.repository.util</util.package>
      </repository>
      <sequencename>hibernate_sequence</sequencename>
      <theme>hoursangular</theme>
    </metadata>
    <object name="YourFirstObject">
      <attribute name="firstAttribute" type="String"></attribute>
    </object>
    <object name="YourSecondObject">
      <attribute name="secondAttribute" type="String"></attribute>
    </object>
  </package>
</model>
  • Open in directory metafactory-workshop-metafactory\src\patterns\package\feature1 the file called pattern1.xml and adapt line 7 so it has the highlighted content:

Listing 2.12 src/patterns/feature1/pattern1.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<package xmlns="http://www.firstbase.nl/xsd/personaliom/pattern"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.firstbase.nl/xsd/personaliom/pattern http://www.firstbase.nl/xsd/personaliom/pattern.xsd"
         name="com.company.workshop"
         path="src/main/java"
         package="domain">
  <class name="${object.name}" foreach="object">
    <operation name="constructor">
      <body>${fmsnippet.feature1.constructor}</body>
    </operation>
  </class>
</package>

Now:

  • Click on metafactory-workshop-metafactory in the MetaFactory Project window.

  • Click on the Play button to run the MetaFactory tool.

Once finished the MetaFactory Tool will have generated a class for each object in the model

  • Open the directory metafactory-workshop-backend\src\main\java\com\company\workshop to see the result.