Mapping composition into flat structure

61 views
Skip to first unread message

Lelum Polelum

unread,
Jan 21, 2014, 1:06:02 PM1/21/14
to geda-generic-dto-asse...@googlegroups.com


Having 

BigDto { 

       SmallDto {

              String field;
       }}

how can I map it to FlatEntity { String field; } ? withField (..) does not support chaining, and I couldn't find any hints in documentation. Thanks for answer!

Peter

Denis Pavlov

unread,
Jan 21, 2014, 2:00:30 PM1/21/14
to geda-generic-dto-asse...@googlegroups.com
Hi Lelum,

I assume you have the following DTO:

public class BigDto {


   
private SmallDto small;
   
private String otherField;


   
public SmallDto getSmall() {
       
return small;
   
}


   
public void setSmall(final SmallDto small) {
       
this.small = small;
   
}


   
public String getOtherField() {
       
return otherField;
   
}


   
public void setOtherField(final String otherField) {
       
this.otherField = otherField;
   
}
}


public class SmallDto {


   
private String field;


   
public String getField() {
       
return field;
   
}


   
public void setField(final String field) {
       
this.field = field;
   
}
}


and Entity:

public class FlatEntity {


   
private String field;
   
private String otherField;


   
public String getField() {
       
return field;
   
}


   
public void setField(final String field) {
       
this.field = field;
   
}


   
public String getOtherField() {
       
return otherField;
   
}


   
public void setOtherField(final String otherField) {
       
this.otherField = otherField;
   
}
}


There are two options available to you:

1. Map SmallDto to FlatEntity and then assemble from both BigDto and SmallDto
2. Create a converter for SmallDto to field

Here is the snippet for both approaches working side by side (registryOption1 for option 1 and registryOption2 for option 2)


        final ExtensibleBeanFactory bf = new SimpleMapExtensibleBeanFactory();
        bf
.registerDto("big", BigDto.class.getCanonicalName());
        bf
.registerDto("small", SmallDto.class.getCanonicalName());
        bf
.registerEntity("flatEntity", FlatEntity.class.getCanonicalName(), FlatEntity.class.getCanonicalName());




       
// our fat DTO
       
final BigDto dto = new BigDto();
        dto
.setSmall(new SmallDto());
        dto
.setOtherField("of");
        dto
.getSmall().setField("f");




       
final com.inspiresoftware.lib.dto.geda.dsl.Registry registryOption1 = Registries.registry(bf);


        registryOption1
.dto("small").forEntityGeneric().withField("field");
        registryOption1
.dto("big").forEntityGeneric().withField("otherField");




       
final FlatEntity entity1 = new FlatEntity();


       
// copy big dto fields
       
DTOAssembler.newAssembler(BigDto.class, entity1.getClass(), registryOption1).assembleEntity(dto, entity1, null, bf);
       
// copy small dto fields
       
DTOAssembler.newAssembler(SmallDto.class, entity1.getClass(), registryOption1).assembleEntity(dto.getSmall(), entity1, null, bf);


        assertEquals
(entity1.getField(), "f");
        assertEquals
(entity1.getOtherField(), "of");




       
final com.inspiresoftware.lib.dto.geda.dsl.Registry registryOption2 = Registries.registry(bf);


        registryOption2
.dto("big").forEntityGeneric()
               
.withField("otherField").and()
               
.withField("small").forField("field").converter("SmallToField");


       
final Map<String, Object> conv = new HashMap<String, Object>();
        conv
.put("SmallToField", new ValueConverter() {
           
public Object convertToDto(final Object object, final BeanFactory beanFactory) {
               
final String field1 = (String) object;
               
final SmallDto dto = new SmallDto();
                dto
.setField(field1);
               
return dto;
           
}


           
public Object convertToEntity(final Object object, final Object oldEntity, final BeanFactory beanFactory) {
               
if (object instanceof SmallDto) {
                   
return ((SmallDto) object).getField();
               
}
               
return null;
           
}
       
});




       
final FlatEntity entity2 = new FlatEntity();


       
// copy big dto fields
       
DTOAssembler.newAssembler(BigDto.class, entity2.getClass(), registryOption2).assembleEntity(dto, entity2, conv, bf);




        assertEquals
(entity2.getField(), "f");
        assertEquals
(entity2.getOtherField(), "of");


This is fully functional unit test - hope this helps.

You have quite an unusual case where your entity is flat - usually it happens other way round, so that your DTO is flat and Entity is "graphy" (so the exact opposite of your case). This way all you would need is use dot notation in like: .withField("small.field").
Reply all
Reply to author
Forward
0 new messages