javaee with vaadin - workshop

69
Vaadin with Java EE 7

Upload: peter-lehto

Post on 17-Jul-2015

180 views

Category:

Internet


6 download

TRANSCRIPT

Page 1: JavaEE with Vaadin - Workshop

Vaadin with Java EE 7

Page 2: JavaEE with Vaadin - Workshop

Java EE 7

Vaadin CDI

Addon

Page 3: JavaEE with Vaadin - Workshop

Application

architecture

Hack

Hack

Hack

Page 4: JavaEE with Vaadin - Workshop

Java Enterprise Edition 7

Page 5: JavaEE with Vaadin - Workshop

Collection of Java Specification Requests (JSRs)

Page 6: JavaEE with Vaadin - Workshop

Collection of Java Specification Requests (JSRs)

Implemented by app servers

Page 7: JavaEE with Vaadin - Workshop
Page 8: JavaEE with Vaadin - Workshop
Page 9: JavaEE with Vaadin - Workshop

Do you know some Java EE specs?

Page 10: JavaEE with Vaadin - Workshop

Java Persistence API 2.1 (JPA)

(JSR-338)

Page 11: JavaEE with Vaadin - Workshop

Java Persistence API 2.1 (JPA)

(JSR-338)

Enterprise Java Beans 3.2 (EJB)

(JSR-345)

Page 12: JavaEE with Vaadin - Workshop

Java Persistence API 2.1 (JPA)

(JSR-338)

Enterprise Java Beans 3.2 (EJB)

(JSR-345)Java Servlet 3.1

(JSR-340)

Page 13: JavaEE with Vaadin - Workshop

Java Persistence API 2.1 (JPA)

(JSR-338)

Enterprise Java Beans 3.2 (EJB)

(JSR-345)Java Servlet 3.1

(JSR-340)

Context and Dependency

Injection 1.2(CDI)(JSR-340)

Page 14: JavaEE with Vaadin - Workshop

Java Persistence API 2.1 (JPA)

(JSR-338)

Enterprise Java Beans 3.2 (EJB)

(JSR-345)Java Servlet 3.1

(JSR-340)

Context and Dependency

Injection 1.2(CDI)(JSR-340)

Interceptors 1.2(JSR-318)

Page 15: JavaEE with Vaadin - Workshop

Java Persistence API 2.1 (JPA)

(JSR-338)

Enterprise Java Beans 3.2 (EJB)

(JSR-345)Java Servlet 3.1

(JSR-340)

Context and Dependency

Injection 1.2(CDI)(JSR-340)

Interceptors 1.2(JSR-318)

Java Transaction API 1.2 (JTA)

(JSR-907)

Page 16: JavaEE with Vaadin - Workshop

Java Persistence API 2.1 (JPA)

Page 17: JavaEE with Vaadin - Workshop

Customer

@Entity

______________________________@Id@AutoGeneratedLong id;

@Column(nullable = false)String name;

Date birthdate;

Page 18: JavaEE with Vaadin - Workshop

Customer

@Entity

______________________________

Customer

Id name birthdate

1 Alex 07.02.1984

2 John 18.2.1992

@Id@AutoGeneratedLong id;

@Column(nullable = false)String name;

Date birthdate;

Page 19: JavaEE with Vaadin - Workshop

Customer@Id@AutoGeneratedLong id;

@Column(nullable = false)String name;

Date birthdate;

@OneToMany(mappedBy=“customer”)List<Invoice> invoices;

@Entity

______________________________

Customer

Id name birthdate

1 Alex 07.02.1984

2 John 18.2.1992

Invoice

Id customer number

1 1 123

2 1 124

Page 20: JavaEE with Vaadin - Workshop

Enterprise Java Beans 3.2 (EJB)

Page 21: JavaEE with Vaadin - Workshop

Business layer services

Enterprise Java Beans

Page 22: JavaEE with Vaadin - Workshop

Business layer services

@local and @remote

Enterprise Java Beans

Page 23: JavaEE with Vaadin - Workshop

Business layer services

@local and @remote

Enterprise Java Beans

Transaction boundaries

Page 24: JavaEE with Vaadin - Workshop

(UI) CustomerView

Page 25: JavaEE with Vaadin - Workshop

(@Remote) CustomerService

(UI) CustomerView

@Remote @Local

Page 26: JavaEE with Vaadin - Workshop

(@Remote) CustomerService

(UI) CustomerView

(@Stateless) CustomerService

Bean

@Remote @Local

@Stateless @Stateful @Singleton

(@Stateless) CustomerService

Bean

(@Stateless) CustomerService

Bean

Page 27: JavaEE with Vaadin - Workshop

(DB) CustomerDatabase

Page 28: JavaEE with Vaadin - Workshop

@Localpublic interface CustomerService {

void storeCustomers(Collection<Customer> customers);

void removeCustomers(Collection<Customer> customers);

Collection<Customer> getAllCustomers()

Optional<Customer> getCustomerByName(String name);}

Page 29: JavaEE with Vaadin - Workshop

@Statelesspublic class CustomerServiceBean implements CustomerService {

@PersistenceContextprivate EntityManager em;

public void storeCustomers(Collection<Customer> cu) {cu.forEach(c -> storeCustomer(c));

}

public void storeCustomer(Customer c) {em.persist(c);

}}

Page 30: JavaEE with Vaadin - Workshop

Context and Dependency

Injection 1.2 (CDI)

Page 31: JavaEE with Vaadin - Workshop

Instead of saying new say @Inject

Context and Dependency Injection

Page 32: JavaEE with Vaadin - Workshop

Instead of saying new say @Inject

Decouples code and lets container manage dependencies

Context and Dependency Injection

Page 33: JavaEE with Vaadin - Workshop

Object references by scopes

Context and Dependency Injection

Page 34: JavaEE with Vaadin - Workshop

@ApplicationScoped @SessionScoped @RequestScoped

Context and Dependency Injection

Object references by scopes

Page 35: JavaEE with Vaadin - Workshop

@UIScoped @ViewScoped

Context and Dependency Injection

@ApplicationScoped @SessionScoped @RequestScoped

Object references by scopes

Page 36: JavaEE with Vaadin - Workshop

@Stateless CustomerService

_________________@UIScoped

AppUI_________________

@EJBCustomerService service;

Page 37: JavaEE with Vaadin - Workshop

@Stateless InvoiceService

@Stateless CustomerService

_________________

@EJBInvoiceService invoices;

@UIScoped AppUI

_________________

@EJBCustomerService service;

Page 38: JavaEE with Vaadin - Workshop

@Stateless CustomerService

_________________

@EJBInvoiceService invoices;

@UIScoped AppUI

_________________

@EJBCustomerService service;

@InjectMainMenu mainMenu;

@InjectUser currentUser;

@UIScoped MainMenu

_________________@InjectEvent<MenuEvent> menuEventSource;

@Stateless InvoiceService

Page 39: JavaEE with Vaadin - Workshop

@UIScoped AppUI

_____________________________

@Injectprivate MainMenu menu;

@Injectprivate ViewManager viewMgr;

@Injectprivate User loggedInUser;

<<UIScope>>

MainMenuViewManager

<<SessionScope>>

User

Page 40: JavaEE with Vaadin - Workshop

<<UIScope>>

MenuBarFooter

ViewManager

<<SessionScope>>

User

@UIScoped AppUI

_____________________________

@Injectprivate MenuBar menu;

@Injectprivate ViewManager viewMgr;

@Injectprivate User loggedInUser;

<<UIScope>>

MenuBarFooter

ViewManager

<<UIScope>>

MenuBarFooter

ViewManager

<<UIScope>>

MenuBarViewManager

Page 41: JavaEE with Vaadin - Workshop

Integration to EE through

Vaadin CDI

Page 42: JavaEE with Vaadin - Workshop

Managed UI with @CDIUI

Page 43: JavaEE with Vaadin - Workshop

Managed UI with @CDIUI

Allows injection with @Inject and @EJB

Page 44: JavaEE with Vaadin - Workshop

Easily reference EE objects

Allows injection with @Inject and @EJB

Managed UI with @CDIUI

Page 45: JavaEE with Vaadin - Workshop

@CDIUI(“”)public class AppUI extends UI {

}

Page 46: JavaEE with Vaadin - Workshop

@CDIUI(“”)public class AppUI extends UI {

@Injectprivate MainMenu mainMenu;

@Inject private User currentUser;

@Injectprivate ViewManager viewManager;

public void init(VaadinRequest request) {VerticalLayout layout = new VerticalLayout();layout.addComponent(mainMenu);

setContent(layout);}

}

Page 47: JavaEE with Vaadin - Workshop

@UIScoped

Page 48: JavaEE with Vaadin - Workshop

@UIScoped

UI specific bean references

Page 49: JavaEE with Vaadin - Workshop

@UIScoped

UI specific bean references

CDI context for mapping beans per UI

Page 50: JavaEE with Vaadin - Workshop

@UIScoped

UI specific bean references

CDI context for mapping beans per UI

@UIScoped

Page 51: JavaEE with Vaadin - Workshop

@UIScopedpublic class MainMenu extends CustomComponent {

}

Page 52: JavaEE with Vaadin - Workshop

@UIScopedpublic class MainMenu extends CustomComponent {

@Injectprivate Event<NavigationEvent> eventSource;

protected void onMenuItemClicked(MenuItem item) {eventSource.fireEvent(new NavigationEvent(item));

}}

Page 53: JavaEE with Vaadin - Workshop

@CDIUI(“”)public class AppUI extends UI {

protected void onNavigationEvent(@ObservesNavigationEvent event) {

viewMgr.navigateTo(event.getView());}

}

Page 54: JavaEE with Vaadin - Workshop

Application Architecture

Page 55: JavaEE with Vaadin - Workshop

Client Browser

View<<EJB>>

Business Logic

Server-side-UI

Presenter<<JPA>>

Persistency

Business Persistency

Page 56: JavaEE with Vaadin - Workshop

Client Browser

View

Server-side-UI

Page 57: JavaEE with Vaadin - Workshop

public interface CustomerView extends ApplicationView<CustomerViewPresenter> {

}

Page 58: JavaEE with Vaadin - Workshop

public interface CustomerView extends ApplicationView<CustomerViewPresenter> {

void populateCustomers(Collection<Customer> customers);

void openEditorFor(Customer customer); void closeEditor(); void removeTableSelection();}

Page 59: JavaEE with Vaadin - Workshop

Client Browser

View

Server-side-UI

Presenter

Page 60: JavaEE with Vaadin - Workshop

@ViewScopedpublic class CustomerViewPresenter extends AbstractPresenter<CustomerView> {

}

Page 61: JavaEE with Vaadin - Workshop

@ViewScopedpublic class CustomerViewPresenter extends AbstractPresenter<CustomerView> {

@EJB private CustomerService customerService;

@Override protected void onViewEnter() { getView().populateCustomers(customerService.getAllCustomers()); }}

Page 62: JavaEE with Vaadin - Workshop

@ViewScopedpublic class CustomerViewPresenter extends AbstractPresenter<CustomerView> {

@EJB private CustomerService customerService;

@Override protected void onViewEnter() { getView().populateCustomers(customerService.getAllCustomers()); }

public void onCustomerSaved(@Observes CustomerSavedEvent event) { … }

public void onCustomerRemoved(@Observes CustomerRemovedEvent event) { … }

public void onCustomerSelected(@Observes CustomerSelectedEvent event) { … }}

Page 63: JavaEE with Vaadin - Workshop

Client Browser

View<<EJB>>

Business Logic

Server-side-UI

Presenter

Business

Page 64: JavaEE with Vaadin - Workshop

@Localpublic interface CustomerService {

void storeCustomers(Collection<Customer> customers);

void removeCustomers(Collection<Customer> customers);

Collection<Customer> getAllCustomers();

Optional<Customer> getCustomerByUsername(String username);}

Page 65: JavaEE with Vaadin - Workshop

Application

Architecture

@Stateless@TransactionAttribute(TransactionAttributeType.REQUIRED)public class CustomerServiceBean implements CustomerService {

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

@Overridepublic void storeCustomers(Collection<Customer> customers) {

customers.forEach(cu -> entityManager.merge(cu));}

@Overridepublic Collection<Customer> getAllCustomers() {

return entityManager.createQuery(query).getResultList();}

…}

Page 66: JavaEE with Vaadin - Workshop

Client Browser

View<<EJB>>

Business Logic

Server-side-UI

Presenter<<JPA>>

Persistency

Business Persistency

Page 67: JavaEE with Vaadin - Workshop

Application

Architecture

@Entitypublic class Customer {

@Id@AutoGeneratedprivate Long id;

private String name;

@Temporal(DATE)private Date birthDate;

public boolean isPersisted() {return id != null;

}

…}

Page 68: JavaEE with Vaadin - Workshop

<persistence-unit name="appUnit" transaction-type="JTA"> <jta-data-source>jdbc/app-backend</jta-data-source> <class>org.vaadin.example.backend.entity.Customer</class>

<properties> <property name="…" … /> </properties></persistence-unit>

Page 69: JavaEE with Vaadin - Workshop

github.com/peterl1084/customermanager