Code Generation

Generate Java Code

The M2 Code Generator generates Java code and JSON from data models in M2.

First go to your home models page that lists all models you created. This is the home page that M2 will load when you log in or you can go to this page from most of other M2 pages by clicking the Models button.

_images/gencode-1.png

If you have not created any model yet, you can still copy any of the either Daprota or public models already available in M2 and generate code form it just to get an idea how it works.

Click the link representing the model you want to generate the code for. In our example it is the Atomic Operation model. Now the Atomic Operation model page is loaded:

Click the Code Generation button to load the page to specify few parameters which values M2 needs in order to generate the code:

_images/gencode-2.png

When the Code Generation page is loaded for the first time for the specific model you will have to specify the values for all parameters except the port parameter which default value is 27017. All these values except the test user password will be saved by M2 and they will be presented on the page the next time when you generate the code again.

_images/gencode-3.png

The Code Generation parameters include the follwing parameters:

Parameter Description
Package name The Java package name of the code thet will be generated (e.g., com.mycompany.mymodel)
MongoDB host The name or IP address of a machine where you run MongoDB server (e.g., localhost, or 192.123.7.456, etc.)
MongoDB port The port of the machine where you run MongoDB server (default value is 27017)
MongoDB test database This is the name of the MongoDB database where you will run your tests generated by the M2 Code Generator
Username The username of the MongoDB database user you will use to run the tests (e.g., test). Please see instructions below how to create a test database user in MongoDB.
Password The password of the MongoDB database user you will use to run the tests

This is how the Code Generation page will look like when you enter values for above parameters:

_images/gencode-4.png

As soon as you enter the values for all parameters click the Generate Code button to generate the code.

M2 will generated the code, store it in M2 repository, compress it in a distribution package, and create download links for it to be downloaded. Two versions of the compressed packages are creared: tgz for Linux/UNIX and zip for Windows. Both of the them have the same code. The only difference is the format of the compression.

_images/gencode-5.png

Now you can download the code and use it to build your software from it and test it.

This is a list of software you need on your machine in order to build and test generated code. If you already have this software please skip the download and installation instructions in the table below.

Software Download and Installation Instructions
MongoDB 3.0.x

Access MongoDB download page

to download and install MongoDB on either Linux or Windows.

MongoDB Java Driver 2.13.2 MongoDB Java driver will be loaded via Maven. If you want to load it separately you can access MongoDB Java driver download page.
Java 7/8 You can us either Java 7 or Java 8 to build and run the generated code.
Maven 3 You will use Apache Maven to build your your code and run tests.

The generated test code is based on the NoSQLUnit, an open source tool that is a JUnit extension for NoSQL unit tests.

If you do not already have a test database user, you should create one in MongoDB test database. For more information on how to create use user in MongoDB database visit the Add a User to a Database MongoDB documentation page.

For example, to create test user execute the following statements in mongo shell:

use test
db.createUser(
    {
        user: "test",
        pwd: "test",
        roles: [
            { role: "readWrite", db: "test" }
        ]
    }
)

Now you are ready to build and test generated code you downloaded from M2.

For example, let us assume that you uncompressed the Atomic Operation code in this folder:

/user/home/mongo/atomic_operation

Go to the plainjava subfolder of the atomic_operation folder first:

cd /user/home/mongo/atomic_operation/plainjava

Run Maven with install option to build APIs from the generated code and run tests:

mvn install

This is how your screen will look like if mvn install executes without errors:

[daprota@localhost plainjava]$ mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building atomic-operation 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ atomic-operation ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/daprota/daprota_code_rep/gencode/daprota/atomic_operation/plainjava/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ atomic-operation ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 13 source files to /home/daprota/daprota_code_rep/gencode/daprota/atomic_operation/plainjava/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ atomic-operation ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.0:testCompile (default-testCompile) @ atomic-operation ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to /home/daprota/daprota_code_rep/gencode/daprota/atomic_operation/plainjava/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ atomic-operation ---
[INFO] Surefire report directory: /home/daprota/daprota_code_rep/gencode/daprota/atomic_operation/plainjava/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.mycompany.atomicoperation.WhenANewBookIsCreated
22:58:13.321 [main] DEBUG c.l.n.c.CleanInsertLoadStrategyOperation - Calling Clean and Insert Load Strategy.
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.382 sec - in com.mycompany.atomicoperation.WhenANewBookIsCreated
Running com.mycompany.atomicoperation.WhenYouFindAllBook
22:58:14.143 [main] DEBUG c.l.n.c.CleanInsertLoadStrategyOperation - Calling Clean and Insert Load Strategy.
22:58:14.265 [main] DEBUG com.mongodb.MongoOptions - Dropping Collection book.
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.161 sec - in com.mycompany.atomicoperation.WhenYouFindAllBook
Running com.mycompany.atomicoperation.WhenYouFindAllPublisher
22:58:14.404 [main] DEBUG c.l.n.c.CleanInsertLoadStrategyOperation - Calling Clean and Insert Load Strategy.
22:58:14.458 [main] DEBUG com.mongodb.MongoOptions - Dropping Collection book.
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.194 sec - in com.mycompany.atomicoperation.WhenYouFindAllPublisher
Running com.mycompany.atomicoperation.WhenANewPublisherIsCreated
22:58:14.521 [main] DEBUG c.l.n.c.CleanInsertLoadStrategyOperation - Calling Clean and Insert Load Strategy.
22:58:14.606 [main] DEBUG com.mongodb.MongoOptions - Dropping Collection publisher.
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.246 sec - in com.mycompany.atomicoperation.WhenANewPublisherIsCreated

Results :

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ atomic-operation ---
[INFO] Building jar: /home/daprota/daprota_code_rep/gencode/daprota/atomic_operation/plainjava/target/atomic-operation-1.0.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ atomic-operation ---
[INFO] Installing /home/daprota/daprota_code_rep/gencode/daprota/atomic_operation/plainjava/target/atomic-operation-1.0.jar to /home/daprota/.m2/repository/com/mycompany/atomicoperation/atomic-operation/1.0/atomic-operation-1.0.jar
[INFO] Installing /home/daprota/daprota_code_rep/gencode/daprota/atomic_operation/plainjava/pom.xml to /home/daprota/.m2/repository/com/mycompany/atomicoperation/atomic-operation/1.0/atomic-operation-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.431 s
[INFO] Finished at: 2015-06-17T22:58:15-04:00
[INFO] Final Memory: 19M/185M
[INFO] ------------------------------------------------------------------------