Changes between Version 4 and Version 5 of MappingDecorator


Ignore:
Timestamp:
2011-02-14T10:24:19+01:00 (13 years ago)
Author:
jvelde
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • MappingDecorator

    v4 v5  
    22You can customize the way entities are added, updated or removed by the database logic by adding decorator classes. Start by adding a decorator your your entity:
    33
    4 `<entity name="Example" decorator="decorators.ExampleDecorator">`[[BR]][[BR]]This will put a template decorator '!ExampleDecorator.java' in a package 'decorators' in your Java source folder. If you open this file, you will notice three functions:[[BR]][[BR]]`public int add(List<E> entities)public int update(List<E> entities)public int remove(List<E> entities)`[[BR]][[BR]]Each of these functions can be customized with new pre-, post- and database action behaviour, in a sequential layout:[[BR]][[BR]]`// add your pre-processing here// here we call the standard 'add'int count = super.add(entities);// add your post-processing here// if you throw an exception the previous add will be rolled back`
     4{{{
     5<entity name="Example" decorator="decorators.ExampleDecorator">
     6}}}
     7
     8This will put a template decorator '!ExampleDecorator.java' in a package 'decorators' in your Java source folder. If you open this file, you will notice three functions:
     9
     10{{{
     11public int add(List<E> entities)
     12public int update(List<E> entities)
     13public int remove(List<E> entities)
     14}}}
     15
     16Each of these functions can be customized with new pre-, post- and database action behaviour, in a sequential layout:
     17
     18{{{
     19// add your pre-processing here
     20
     21// here we call the standard 'add'
     22int count = super.add(entities);
     23
     24// add your post-processing here
     25// if you throw an exception the previous add will be rolled back
     26}}}
    527
    628== Example ==
    7 For example, change the value of the field 'name' by turning it into uppercase before adding:[[BR]][[BR]]`public int add(List<E> entities) throws DatabaseException{    try{        for (Entity e : entities){                    e.set("name", e.get("name").toString().toUpperCase());            }    } catch (ParseException pe) {        throw new DatabaseException(pe);    }        int count = super.add(entities);        return count;}`
     29For example, change the value of the field 'name' by turning it into uppercase before adding:
     30
     31{{{
     32public int add(List<E> entities) throws DatabaseException{
     33    try{
     34        for (Entity e : entities){
     35            e.set("name", e.get("name").toString().toUpperCase());
     36        }
     37    } catch (ParseException pe) {
     38        throw new DatabaseException(pe);
     39    }
     40    int count = super.add(entities);
     41    return count;
     42}
     43}}}
    844
    945Remember the functions get a list of entities which can be adressed in the most low-level form (org.molgenis.util.Entity) as shown in the example. However, usually you want to use the generated class for this decorator to use its field functions, so the previous example becomes:
    1046
    11 ` for (``Example`` e : entities){``    ``e.setName(e.getName().toUpperCase());``}`
     47{{{
     48for (Example e : entities){
     49    e.setName(e.getName().toUpperCase());
     50}
     51}}}
    1252
    1353Which makes more sense!