giovedì 1 ottobre 2015

Partial mapping with JMapper

Hi guys,

with this post i will illustrate how is possible define a mapping strategy with JMapper.
The classic scenario is a mapping where value from source must be passed to destination, but there are cases where what we need is pass from source only valorized fields or.. we want that only the null fields of destination will be mapped.. this cases are handled very well by JMapper.

The following example will use these classes:

class Destination {              class Source {

   @JMap String name;               String name;
   @JMap Integer age;               Integer age;
   @JMap String company;            String company;

   // getters and setters..         // getters and setters..

}                                }

JMapper istance generation:

JMapper<Destination, Source> mapper = new JMapper<>(Destination.class, Source.class);

Ok, it's arrived the moment to see what Jmapper permits to do.
This are the filled beans:
Destination destination = new Destination("empty", null, "Anonymous");
Source source = new Source("Alessandro", 30, null);

We want to take only the valued fields of Source:

Destination result = mapper.getDestination(destination, source, MappingType.ALL_FIELDS, MappingType.ONLY_VALUED_FIELDS);
Result:
Destination[ name="Alessandro", age=30, company="Anonymous"]
We want just fill null fields of Destination:

Destination result = mapper.getDestination(destination, source, MappingType.ONLY_NULL_FIELDS, MappingType.ALL_FIELDS);

Result:
Destination[ name="empty", age=30, company="Anonymous"]

There are a lot of other combinations.. try to find yourself    :)

venerdì 1 maggio 2015

JMapper Framework philosophy

There are a lot of java mapping framework, each of them developed on a basic principle.
For example MapStruct applies loose coupling using annotations, Orika the same using API, Model Mapper is convention based etc...

JMapper Framework instead applies the DRY principle ( Don't repeat yourself ) using Annotations:
the mappings are defined on fields so that they are used as configuration, 
the loose coupling imposes to repeat this information.

However, with JMapper, you can choose to apply loose coupling using the XML or API configuration.

The second objective of JMapper is the Relational Mapping:

The relational mapping is the ability to map one bean toward many other and viceversa.

REAL CASE

Thinks you have a service that applies a method called "checkExistence" that needs of the fields: name and surname. This service takes as input a general object that can be everything, in our case takes three different bean: Consultant, Employee and Manager. This beans have in common only this two fields and we have need to map only these toward the service's bean.

SOLUTION

With JMapper you need to configure only the service's bean toward the others.

IMPLEMENTATION

@JGlobalMap(classes={Consultant.class, Employee.class, Manager.class})
class ServiceBean{

     String name;
     String surname;

     //getters and setters..
}

USAGE

class Service {

RelationalJMapper<ServiceBean> mapper = new RelationalJMapper<>(ServiceBean.class);

   public void checkExistence(Object bean){
        ServiceBean serviceBean =  mapper.manyToOne(bean);
        // some logic  
   }
}

CONCLUSION

DRY principle + relational mapping  = interesting implementations

Are you curious? have a look to the relational mapping wiki page, there a lot of complete and more complex examples.

p.s. JMapper has more other interesting features as static/dynamic conversions, enrichment, mapping type etc.., don't worry in the next articles i'll describe all of it ;)





lunedì 19 gennaio 2015

How to write a good XML schema

XML Schema is a formal description of a grammar for a markup language based on XML.
 There are a lot of guides and articles about this, i suggest to visit W3C XML Schema (XSD) as start point,
 for grammar analysis: W3C XML Schema (XSD) Validation online.

The purpose of this article is to give you some tips on how to write a better xml schema.

 It's very important to know default values of all tag, for several reasons:
 - define all parameters as maxOccurs, minOccurs, optional, use etc.. for all tag makes the xsd difficult to read and modify.
 - Alternatively define a few, ignoring the default values, leads to abnormal behavior, found only with test
Here you can found all default values for all tag.

Make your xsd more readable, for this purpose you can use:
 - complexType to externalize a attribute type, for example:

 Before:
        <xs:element name="employee">
      <xs:complextype>
        <xs:sequence>
          <xs:element name="firstname" type="xs:string"/>
          <xs:element name="lastname" type="xs:string"/>
        </xs:sequence>
      </xs:complextype>
    </xs:element>
  <xs:element name="student">
   <xs:complextype>
<xs:sequence>
         <xs:element name="firstname" type="xs:string"/>
         <xs:element name="lastname" type="xs:string"/>
  </xs:sequence>
    </xs:complextype>
  </xs:element>

 after:
<xs:element name="employee" type="personinfo">
<xs:element name="student" type="personinfo">

<xs:complextype name="personinfo">
  <xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
  </xs:sequence>
</xs:complextype>

 - import an external xsd and use it with namespace
 - add comments as markers, it permits to go fast on the interested point, you have to think that not everyone uses XML tools.

 - use extension or restriction of complexContent if your element is almost equal to an existing.

 follow an example of extention:

<xs:element name="employee" type="fullpersoninfo">

<xs:complextype name="personinfo">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
  </xs:sequence>
</xs:complextype>

<xs:complextype name="fullpersoninfo">
  <xs:complexcontent>
    <xs:extension base="personinfo">
      <xs:sequence>
        <xs:element name="address" type="xs:string"/>
        <xs:element name="city" type="xs:string"/>
        <xs:element name="country" type="xs:string"/>
      </xs:sequence>
    </xs:extension>
  </xs:complexcontent>
</xs:complextype>

an example of restriction:

<xs:complextype name="customer">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    <xs:element name="country" type="xs:string"/>
  </xs:sequence>
</xs:complextype>

<xs:complextype name="Norwegian_customer">
  <xs:complexcontent>
    <xs:restriction base="customer">
      <xs:sequence>
        <xs:element name="firstname" type="xs:string"/>
        <xs:element name="lastname" type="xs:string"/>
        <xs:element fixed="Norway" name="country" type="xs:string"/>
      </xs:sequence>
    </xs:restriction>
  </xs:complexcontent>
</xs:complextype>

 Few tips but if used makes the development, of large XSD file, sustainable.