Top Banner
Binding business data to Vaadin components Best practices Peter Lehto Senior Vaadin Expert @peter_lehto
89

Binding business data to vaadin components

Apr 11, 2017

Download

Peter Lehto
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Binding business data to vaadin components

Binding business data to Vaadin components

Best practices

Peter Lehto Senior Vaadin Expert

@peter_lehto

Page 2: Binding business data to vaadin components

Application

architecture

Data transfer

Page 3: Binding business data to vaadin components

Vaadin Containers

Vaadin

Fields

Page 4: Binding business data to vaadin components

Application Architecture

Page 5: Binding business data to vaadin components

Deployment

Page 6: Binding business data to vaadin components

<<UI module>>

CustomerView ProductView

Page 7: Binding business data to vaadin components

<<UI module>>

CustomerView ProductView

<<Commons module>>

DTOService CustomerDTOCustomerDTO

Page 8: Binding business data to vaadin components

<<UI module>>

CustomerView ProductView

<<Backend module>>

<<Commons module>>

DTOService CustomerDTO

EntityService

CustomerDTO

DTOServiceBean

Page 9: Binding business data to vaadin components

Application layers

Page 10: Binding business data to vaadin components

Client Browser

Page 11: Binding business data to vaadin components

DevDayUI

Client Browser

Page 12: Binding business data to vaadin components

DevDayUI

Client Browser

CustomerView ProductView

Page 13: Binding business data to vaadin components

DevDayUI

DTOService

Client Browser

CustomerView ProductView

Page 14: Binding business data to vaadin components

DevDayUI

Client Browser

CustomerView ProductView

DTOService

Converters EntityService

Page 15: Binding business data to vaadin components

DevDayUI

Client Browser

CustomerView ProductView

PersistenceUnit

DTOService

Converters EntityService

Page 16: Binding business data to vaadin components

DevDayUI

Client Browser

CustomerView ProductView

PersistenceUnit

DTOService

Converters EntityService

Page 17: Binding business data to vaadin components

@Localpublic interface EntityService {

void storeEntity(AbstractEntity entity);

<E extends AbstractEntity> List<E> getAll(Class<E> entityType);}

Page 18: Binding business data to vaadin components

@Localpublic interface EntityService {

void storeEntity(AbstractEntity entity);

<E extends AbstractEntity> List<E> getAll(Class<E> entityType);

<E extends AbstractEntity> E getEntityById(long id, Class<E> entityType);

<E extends AbstractEntity> List<E> getPagedEntities(Class<E> entityType,int startIndex, int items, Object[] sortPropertyIds, boolean[] sortStates);

}

Page 19: Binding business data to vaadin components

@Localpublic interface DTOService {

<DTO extends AbstractDTOWithIdentity> List<DTO> getAllDtos(Class<DTO> dtoType);

}

Page 20: Binding business data to vaadin components

@Localpublic interface DTOService {

<DTO extends AbstractDTOWithIdentity> List<DTO> getAllDtos(Class<DTO> dtoType);

<DTO extends AbstractDTOWithIdentity> List<DTO> getPagedDtos(Class<DTO> dtoType, int startIndex, int items, Object[] sortPropertyIds, boolean[] sortStates);

<DTO extends AbstractDTOWithIdentity> long getCount(Class<DTO> dtoType);

}

Page 21: Binding business data to vaadin components

@Localpublic interface DTOService {

<DTO extends AbstractDTOWithIdentity> List<DTO> getAllDtos(Class<DTO> dtoType);

<DTO extends AbstractDTOWithIdentity> List<DTO> getPagedDtos(Class<DTO> dtoType, int startIndex, int items, Object[] sortPropertyIds, boolean[] sortStates);

<DTO extends AbstractDTOWithIdentity> long getCount(Class<DTO> dtoType);

<DTO extends AbstractDTOWithIdentity> DTO getDtoById(long itemId, Class<DTO> dtoType);

<DTO extends AbstractDTOWithIdentity> void storeDto(DTO dto);}

Page 22: Binding business data to vaadin components

@Statelesspublic class DTOServiceBean implements DTOService, EntityService {

@PersistenceContext(unitName = "devday")private EntityManager entityManager;

…}

Page 23: Binding business data to vaadin components

Data transfer

Page 24: Binding business data to vaadin components

DTO (DataTransferObject)

Page 25: Binding business data to vaadin components

POJO built by DTOService

DTO (DataTransferObject)

Page 26: Binding business data to vaadin components

POJO built by DTOService

Minimal ondemand data item

DTO (DataTransferObject)

Page 27: Binding business data to vaadin components

POJO built by DTOService

Minimal ondemand data item

Free from Entity model changes

DTO (DataTransferObject)

Page 28: Binding business data to vaadin components

POJO built by DTOService

Minimal ondemand data item

Free from Entity model changes

No referenced property loading concerns

DTO (DataTransferObject)

Page 29: Binding business data to vaadin components

Entity <-> DTO Conversion

Page 30: Binding business data to vaadin components

UI needs DTOs

Entity <-> DTO Conversion

Page 31: Binding business data to vaadin components

UI needs DTOs

Backend knows about entities

Entity <-> DTO Conversion

Page 32: Binding business data to vaadin components

UI needs DTOs

Backend knows about entities

Converter per DTO / usecase type

Entity <-> DTO Conversion

Page 33: Binding business data to vaadin components

public interface EntityToDTOConverter {

AbstractDTOWithIdentity convertToDTO(AbstractEntity entity);

Class<? extends AbstractEntity> getEntityType();

Class<? extends AbstractDTOWithIdentity> getDTOType();}

Page 34: Binding business data to vaadin components

public interface EntityToDTOConverter {

AbstractDTOWithIdentity convertToDTO(AbstractEntity entity);

Class<? extends AbstractEntity> getEntityType();

Class<? extends AbstractDTOWithIdentity> getDTOType();}

public interface DTOToEntityConverter {

<DTO extends AbstractDTOWithIdentity, E extends AbstractEntity> E convertToEntity(DTO dto);

Class<? extends AbstractEntity> getEntityType();

Class<? extends AbstractDTOWithIdentity> getDTOType();

}

Page 35: Binding business data to vaadin components

How to find converter?

Page 36: Binding business data to vaadin components

Runtime

Instance selection

Page 37: Binding business data to vaadin components

@Retention(RUNTIME)@Target(TYPE)@Qualifierpublic @Interface DTOType {

}

Page 38: Binding business data to vaadin components

@Retention(RUNTIME)@Target(TYPE)@Qualifierpublic @Interface DTOType {

Class<? extends AbstractDTO> value(); }

Page 39: Binding business data to vaadin components

@DTOType(CustomerListingDTO.class)public class CustomerEntityToListingDTOConverter implements EntityToDTOConverter {

@Overridepublic Class<CustomerEntity> getEntityType() {

return CustomerEntity.class;}

@Overridepublic Class<CustomerListingDTO> getDTOType() {

return CustomerListingDTO.class;}

@Overridepublic CustomerListingDTO convertToDTO(CustomerEntity e) { …

}}

Page 40: Binding business data to vaadin components

public class DTOTypeAnnotationLiteral extends AnnotationLiteral<DTOType> implements DTOType {

private Class<? extends AbstractDTO> dtoType;

public DTOTypeAnnotationLiteral(Class<? extends AbstractDTO> dtoType) {this.dtoType = dtoType;

}

@Overridepublic Class<? extends AbstractDTO> value() {

return dtoType;}

}

Page 41: Binding business data to vaadin components

protected EntityToDTOConverter findEntityToDTOConverter( Class<? extends AbstractDTO> dtoType) {

Instance<EntityToDTOConverter> converterSelection = converterInstantiator.select(new DTOTypeAnnotationLiteral(dtoType));

if (converterSelection.isAmbiguous()) {// more than one converter for same type exception…

}if (converterSelection.isUnsatisfied()) {

// no converter available exception}

return converterSelection.get();}

Page 42: Binding business data to vaadin components

DTOService + Converter

Page 43: Binding business data to vaadin components

No need to change code

DTOService + Converter

Page 44: Binding business data to vaadin components

No need to change code

Add new converters for new DTO types

DTOService + Converter

Page 45: Binding business data to vaadin components

No need to change code

Add new converters for new DTO types

Converters via bean discovery

DTOService + Converter

Page 46: Binding business data to vaadin components

No need to change code

Add new converters for new DTO types

Converters via bean discovery

UI knows and works only with DTOs

DTOService + Converter

Page 47: Binding business data to vaadin components

Reading data through

Vaadin Containers

Page 48: Binding business data to vaadin components

Data binding

ContainerItem

Property

Page 49: Binding business data to vaadin components

In memory containers

Page 50: Binding business data to vaadin components

In memory containers

IndexedContainer – Basic container that is manually built

Page 51: Binding business data to vaadin components

In memory containers

HierarchicalContainer – IndexedContainer that supports parent/child relationships

IndexedContainer – Basic container that is manually built

Page 52: Binding business data to vaadin components

In memory containers

BeanItemContainer – A container that is automatically built from a collection of Java Beans

HierarchicalContainer – IndexedContainer that supports parent/child relationships

IndexedContainer – Basic container that is manually built

Page 53: Binding business data to vaadin components

100

Browser

Page 54: Binding business data to vaadin components

Container

100

Server side UI Browser

Page 55: Binding business data to vaadin components

DB Container

1 000 000 100

Backend Server side UI Browser

Page 56: Binding business data to vaadin components

DB Container

1 000 000 100

Backend Server side UI Browser

Handled by the table component

Page 57: Binding business data to vaadin components

Reduce memory footprint with

Lazy loading

Page 58: Binding business data to vaadin components

LazyQueryContainer

Page 59: Binding business data to vaadin components

Add-on from Vaadin Directory

LazyQueryContainer

Page 60: Binding business data to vaadin components

Add-on from Vaadin Directory

Retrieve data by index

LazyQueryContainer

Page 61: Binding business data to vaadin components

Add-on from Vaadin Directory

Retrieve data by index

Will not expose backend assets

LazyQueryContainer

Page 62: Binding business data to vaadin components

Grid grid = new Grid();container = new LazyQueryContainer(queryFactory, PRIMARY_KEY_PROPERTY, BATCH_SIZE);grid.setContainerDataSource(container);

Page 63: Binding business data to vaadin components

public class LazyDTOQuery<DTO extends AbstractDTO> extends AbstractBeanQuery<DTO> {

private Class<DTO> dtoType;private DTOService dtoService;

public LazyDTOQuery(Class<DTO> dtoType, DTOService dtoService, QueryDefinition queryDefinition) {

super(…);}

@Overrideprotected List<DTO> loadBeans(int startIndex, int numItems) {

return dtoService.getPagedDtos(dtoType, startIndex, numItems, getSortPropertyIds(), getSortStates());

}

@Overridepublic int size() {

return dtoService.getCount(dtoType);}

…}

Page 64: Binding business data to vaadin components

Binding data with

Vaadin Fields

Page 65: Binding business data to vaadin components

Examples of Fields

Page 66: Binding business data to vaadin components

Vaadin Fields

Page 67: Binding business data to vaadin components

Fields are Components with databinding

Vaadin Fields

Page 68: Binding business data to vaadin components

Fields are Components with databinding

Fields can be bound to properties

Vaadin Fields

Page 69: Binding business data to vaadin components

Fields are Components with databinding

Fields can be bound to properties

Group of properties is an Item

Vaadin Fields

Page 70: Binding business data to vaadin components

public class Person {

private String name;private int age;

// Getters and setters omitted

}

Page 71: Binding business data to vaadin components

public class Person {

private String name;private int age;

// Getters and setters omitted

}

BeanItem<Person> item = new BeanItem<Person>(person);

Page 72: Binding business data to vaadin components

public class Person {

private String name;private int age;

// Getters and setters omitted

}

BeanItem<Person> item = new BeanItem<Person>(person);

Property ID Type Value

“name” String.class “John Doe”

“age” Integer.class 42

Page 73: Binding business data to vaadin components

public class Person {

private String name;private int age;

// Getters and setters omitted

}

BeanItem<Person> item = new BeanItem<Person>(person);

Property ID Type Value

“name” String.class “John Doe”

“age” Integer.class 42

Page 74: Binding business data to vaadin components

Separate Field value from Property value with

Buffering

Page 75: Binding business data to vaadin components

Buffering

Page 76: Binding business data to vaadin components

setPropertyDatasource(property);

Buffering

Page 77: Binding business data to vaadin components

setPropertyDatasource(property);

setBuffered(true);

Buffering

Page 78: Binding business data to vaadin components

setPropertyDatasource(property);

setBuffered(true);

commit();

Buffering

Page 79: Binding business data to vaadin components

setPropertyDatasource(property);

setBuffered(true);

commit();

discard();

Buffering

Page 80: Binding business data to vaadin components

Buff

eri

ng

Data source

UI Logic

AbstractField

Button

Browser

Page 81: Binding business data to vaadin components

Buff

eri

ng

Data source

UI Logic

AbstractField

Button

Browser

TextField value change

Page 82: Binding business data to vaadin components

Buff

eri

ng

Data source

UI Logic

AbstractField

Button

Browser

TextField value change

Save button pressed

Page 83: Binding business data to vaadin components

Buff

eri

ng

Data source

UI Logic

AbstractField

Button

Browser

TextField value change

Save button pressed

ClickEvent

Page 84: Binding business data to vaadin components

commit()

Buff

eri

ng

Data source

UI Logic

AbstractField

Button

Browser

TextField value change

Save button pressed

ClickEvent

Page 85: Binding business data to vaadin components

commit()

Buff

eri

ng

Data source

UI Logic

AbstractField

Button

Browser

TextField value change

Save button pressed

ClickEvent

Page 86: Binding business data to vaadin components

FieldGroup combining many Fields together

(used to be Form)

Page 87: Binding business data to vaadin components
Page 88: Binding business data to vaadin components

setItemDataSource(Item item)

Page 89: Binding business data to vaadin components

?What is the difference between these two method calls?

comboBox. setPropertyDataSource(property);

comboBox. setContainerDataSource(container);