To start working with Nomin it's necessary to download the Nomin distribution package, unpack and put nomin.jar with dependencies into the classpath of your application. If you use Maven to build an application just add Nomin as a dependency into your pom.xml.
<dependency> <groupId>net.sf.nomin</groupId> <artifactId>nomin</artifactId> <version>${nomin.version}</version> </dependency>
Suppose we have an integration task to feed an external system with some data, so we have two domain models, ours and theirs. Despite the transport layer both of the domains are represented by sets of POJOs. The following diagram shows all classes and its relations.
To perform mappings between the domains it needs to create mappings rules. Just create these files and put them into classpath.
// person2employee.groovy import org.nomin.entity.* mappingFor a: Person, b: Employee a.name = b.name a.lastName = b.last a.birthDate = b.details.birth a.children = b.details.kids a.strDate = b.details.birth dateFormat "dd-MM-yyyy" a.gender = b.details.sex simple ([Gender.MALE, true], [Gender.FEMALE, false]) a.snn = b.employeeId convert to_a: { eId -> repositry.findByEmployeeId(eId) }, to_b: { snn -> repository.findBySnn(snn) }
// child2kid.groovy import org.nomin.entity.* mappingFor a: Child, b: Kid a.name = b.kidName
Well, it's not looking too complicated. The last question that remains is how to make it work? The next code snippet shows that.
// MappingTest.java public class MappingTest { NominMapper nomin = new Nomin("person2employee.groovy", "child2kid.groovy"); Person person; Employee employee; @Before public void before() { /* create and initialize a person and an employee instances */ } @Test public void test() { Employee e = nomin.map(person, Employee.class); // here should be assertions to ensure that everything is ok Person p = nomin.map(employee, Person.class); // assertions again } }
Let's look more deeply at the mappings. There are only a couple of things I should clarify because they are not so obvious as others.
a.gender = b.details.sex simple ([Gender.MALE, true], [Gender.FEMALE, false])
The simple conversion defines pairs of corresponding values for both sides.
a.snn = b.employeeId convert to_a: { eId -> service.convertEmployeeId2snn(eId) }, to_b: { snn -> service.convertSnn2EmployeeId(snn) }
We have string properties on both sides, but they can be converted to each other only using an external component or service. Nomin provides access to a particular context to get services which are able to perform conversions to and from. How to configure Nomin to use required context during mapping will be shown in the rest of this guide.
Now you have all necessary knowledges to start working with Nomin.