TrimmMongo 1.0.1 Snapshot with support for Spring Data Mongo

TrimmMongo 1.0.1 Snapshot with support for Spring Data Mongo

The first Maven snapshot of TRIMM 1.0.1 with TrimmMongo is available from our Maven Repository.

The TrimmMongo project has in this first version a Listener (dk.tigerteam.trimm.mdsd.mongo.SpringDataMongoGeneratorListener) that based on stereotypes can use Spring Data Mongo mapping annotation to save your model in MongDB instance.

There’s also an TrimmMongo example project available called TrimmMongoExample

Short introduction to TrimmMongo modeling

In this first version of the SpringDataMongoGeneratorListener there is support for following annotations for mappings of objects to documents. The listener Stereotypes and Tag values to drive the mapping metadata:

  • @Id – applied at the field level to mark the field used for identiy purpose. STEREOTYPE: DOCUMENT_ID
  • @Document – applied at the class level to indicate this class is a candidate for mapping to the database. You can specify the name of the collection where the database will be stored. STEREOTYPE: DOCUMENT
  • @DBRef – applied at the field to indicate it is to be stored using a com.mongodb.DBRef. STEREOTYPE: DBREF
  • @Indexed – applied at the field level to describe how to index the field. STEREOTYPE: INDEX & UNIQUEINDEX
  • @Transient – by default all private fields are mapped to the document, this annotation excludes the field where it is applied from being stored in the database STEREOTYPE: TRANSIENT


You can find out more about Spring Data Mongo mapping by clicking this link Spring Data Mongo Mapping

Have a look at the model from the example project to see how the Stereotype are used

TrimmMongo example model

TrimmMongo example model

Short introduction to TrimmMongo codegeneration

Code generation for a TrimmMongo annotated UML class model can be done in a classical Maven project by using the TrimmJavaMavenPlugin.
For the Mongo example you need to XMI Export the mongo-example package from Enterprise Architect (as XMI version 2.1) to the Java example projects model/ subfolder under the name model.xml (since this is how we reference it in the MongoModel.yml that I will cover in a second)
See here for information about modeling in Enterprise Architect in general and here for information about Exporting a model to XMI 2.1.

We need to add the TrimmJavaMavenPlugin to the Java example projects pom.xml:

<plugin>
	<groupId>dk.tigerteam</groupId>
	<artifactId>TrimmJavaMavenPlugin</artifactId>
	<version>1.0.1-SNAPSHOT</version>
	<executions>
		<execution>
			<id>generate</id>
			<phase>generate-sources</phase>
			<goals>
				<goal>generate</goal>
			</goals>
			<configuration>
				<yamlFile>model/MongoModel.yml</yamlFile>
			</configuration>
		</execution>
	</executions>
	<dependencies>
		<dependency>
			<groupId>dk.tigerteam</groupId>
			<artifactId>TrimmMongo</artifactId>
			<version>1.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
</plugin>

The yml configuration looks like this, remember to add SpringDataMongoGeneratorListener listener.

# Setup
xmiModelPath: model/model.xml
# EA, MagicDraw, FQCN, .groovy script
umlTool: EA

basePackageName: 
generateExtensionClasses: false
# addExtensionAndBaseClassAnnotations is only valid if generateExtensionClasses is set to true
addExtensionAndBaseClassAnnotations: false
sourceCodeFormatting:
#    implementation: dk.tigerteam.trimm.mdsd.java.generator.codewriter.JalopyFormattingCodeWriter
    # Formatting exclusions can be specified even though implementation hasn't been specified (and the default JalopyFormattingCodeWriter is used)
    excludeFormattingForPackages: 
        - dk.tigerteam.mddexample.meta
        - dk.tigerteam.mddexample.test
      
# Paths
generateBaseClassesToPath: target/generated-sources/domainmodel

# Extensions (note these listeners can also be groovy scripts containing a single class implement the GeneratorEventListener interface (or
# which extend specializations thereof) 
eventListeners:
    - listener: dk.tigerteam.trimm.mdsd.mongo.SpringDataMongoGeneratorListener
    - listener: dk.tigerteam.trimm.mdsd.java.generator.extension.SerialVersionUIDGeneratorListener
    - listener: dk.tigerteam.trimm.mdsd.java.generator.extension.PropertySugarMethodsEventListener
    - listener: dk.tigerteam.trimm.mdsd.java.generator.extension.ToStringListener
    - listener: dk.tigerteam.trimm.mdsd.java.generator.extension.HashCodeAndEqualsListener

See the TrimmMongo example project for the full configuration and setup.

Have a look in the target/generated-sources/domainmodel for an example of a generated artifact from the example model:

package dk.tigerteam.trimm.mongo.exa;

import dk.tigerteam.trimm.mongo.exa.Address;
import dk.tigerteam.trimm.mongo.exa.Customer;
import dk.tigerteam.trimm.mongo.exa.Order;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@Document
public class Customer {
    private static final long serialVersionUID = 626358653L;

    @Indexed(unique = true)
    private String username;

    private String firstName;

    private String lastName;

    @Id
    private String id;

    @DBRef
    private Set<Order> order = new HashSet<Order>();

    private Address address;

    public String getUsername() {
        return username;
    }

    public void setUsername(String parameter) {
        this.username = parameter;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String parameter) {
        this.firstName = parameter;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String parameter) {
        this.lastName = parameter;
    }

    public String getId() {
        return id;
    }

    public void setId(String parameter) {
        this.id = parameter;
    }

    public Set<Order> getOrder() {
        return order;
    }

    public void setOrder(Set<Order> parameter) {
        this.order = parameter;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address parameter) {
        this.address = parameter;
    }

    public Customer withUsername(String username) {
        setUsername(username);

        return (Customer) this;
    }

    public Customer withFirstName(String firstName) {
        setFirstName(firstName);

        return (Customer) this;
    }

    public Customer withLastName(String lastName) {
        setLastName(lastName);

        return (Customer) this;
    }

    public Customer withId(String id) {
        setId(id);

        return (Customer) this;
    }

    public Customer addToOrder(Order... values) {
        getOrder().addAll(Arrays.asList(values));

        return (Customer) this;
    }

    public Customer withAddress(Address address) {
        setAddress(address);

        return (Customer) this;
    }

    public String toString() {
        return "Customer[" + "address: " + address + ", firstName: " +
        firstName + ", id: " + id + ", lastName: " + lastName + ", username: " +
        username + "]";
    }
}

Try it out with our own model or the example one and let us know what you think.

 
Comments

No comments yet.