When I create a JPAContainer in my MVP model Project, Can I pass JPAContain

Hi, I am using a MVP(Model-View-Presenter) model to crate my project.

My understand JPAContainer is a container that allows me to pass the data from the datasource to my view components.

  1. If I create a JPAContainer in my Presenter, can I pass it to my view object?
  2. If not, what is the best way of doing it?

In the following code, I have two class. One is the Presenter Class, that I call
Create Empolyee
empolyeeJpaContainer.addEntity(empolyee); “Which I think it excetue SQL code to add Entity”

Update Empolyee
I do my update in CreateEmpolyeeView Class which use
empolyeeBeanBinder = new FieldGroup();
empolyeeBeanBinder.commit();

  1. Do I need to move the update part into Presenter to get better de-copuling?

Hope Someone with more expreience who can give me a solution.

Thanks.

Presenter Class

@Override
public void clickHappened(Button.ClickEvent event, Object bean, String activeString) {

    if (ActiveString.LOGIN.equals(activeString)) {
        if (model.authenticationUser((LoginBean) bean)) {                            
            navigator.addView(CreateItemView.ITEMVIEW, createItemView);
            navigator.addView(CreateSupplierView.SUPPLIERVIEW, createSupplierView);
            navigator.addView(CreateEmpolyeeView.EMPOLYEE,createEmpolyeeView);
            navigator.addView(CreatePositionView.POSITIONVIEW,createPositionView);
            navigator.addView(CreateMainMenuBarView.MAINMENU, createMainMenuBarView);
        }
        
        navigator.navigateTo(CreateMainMenuBarView.MAINMENU);        

    } else if (ActiveString.CREATE_EMPOLYEE.equals(activeString)) {
        
        EmpolyeeBean empolyee = (EmpolyeeBean)bean;    
        empolyeeJpaContainer.addEntity(empolyee);
        createEmpolyeeView.setJPAContainer(empolyeeJpaContainer);
        Notification.show("You empolyee "+empolyee.geteFirstNameString()+" "+empolyee.geteLastNameString()+" is Create");
        
    }else if(ActiveString.UPDATE_EMPOLYEE.equals(activeString)){
        
        
    }

}

CreateEmpolyeeView Class

@Override
public void buttonClick(ClickEvent event) {
    
    
    
    if(event.getButton()==createButton){
        
        EmpolyeeBean empolyeeBean = new EmpolyeeBean();
        empolyeeBean.seteDateOfBirth(eDateOfBirthField.getValue());
        empolyeeBean.seteFirstNameString(eFirstNameTextField.getValue());
        empolyeeBean.seteLastNameString(eLastNameTextField.getValue());
        empolyeeBean.setePasswordString(ePasswordField.getValue());
        empolyeeBean.setePositionBean(empolyeePositionBean);
        empolyeeBean.seteSocialSecurityIDString(eSocialSecurityIDTextField.getValue());
        empolyeeBean.seteStartDate(eStartDateField.getValue());    
        empolyeeBean.seteStatusString(selectedStatusString);
        empolyeeBean.seteUserNameString(eUserNameTextField.getValue());

        clickedListener.clickHappened(event,empolyeeBean,ActiveString.CREATE_EMPOLYEE);

    }else if(event.getButton()==updateButton){
    
            try {
                empolyeeBeanBinder.commit();
                Notification.show("You Empolyee: "+eFirstNameTextField.getValue()+" is updated");
                
            } catch (CommitException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        
    }else if(event.getButton()==deleteButton){
        
        empolyeeContainer.removeItem(empolyee.getItemId());
        cleanEmpolyeeForm();
        
    }
    
}

@Override
public void setButtonClickPOSListener(ClickedListener b) {
    clickedListener =b;
    
}

@SuppressWarnings("unchecked")
@Override
public void setJPAContainer(JPAContainer<?> JpaContainer) {

    cleanEmpolyeeForm();
    
    empolyeeContainer = (JPAContainer<EmpolyeeBean>) JpaContainer;
    empolyeeTable.setContainerDataSource(empolyeeContainer);
    Object column={"eFirstNameString","eLastNameString","ePositionBean.positionString","eStatusString"};
    empolyeeTable.setVisibleColumns(column);
    empolyeeTable.setColumnHeader("eFirstNameString","First Name");
    empolyeeTable.setColumnHeader("eLastNameString", "Last Name");
    empolyeeTable.setColumnHeader("ePositionBean.positionString", "Position");
    empolyeeTable.setColumnHeader("eStatusString", "Status");    
    empolyeeTable.setSelectable(true);
    empolyeeTable.setNullSelectionAllowed(false);
    empolyeeTable.setImmediate(true);
    
    
}


private void cleanEmpolyeeForm() {
    
    eIDLabel.setValue("");
    eDateOfBirthField.setValue(new Date());
    eFirstNameTextField.setValue("");
    eLastNameTextField.setValue("");
    ePasswordField.setValue("");
    eSocialSecurityIDTextField.setValue("");
    eStartDateField.setValue(new Date());
    eUserNameTextField.setValue("");
    
}

@Override
public void valueChange(ValueChangeEvent event) {

    if (event.getProperty() == eStatusComBoBox) {

        selectedStatusString = (String) event.getProperty().getValue();

    } else if (event.getProperty() == ePositionComBoBox) {

        if (event.getProperty().getValue() != null) {

            valueChangedListener.valueChangedHappend(event);

        }

    } else if (event.getProperty() == empolyeeTable) {

        if (event.getProperty().getValue() != null) {
            empolyee = empolyeeContainer.getItem(event.getProperty()
                    .getValue());
            empolyeeBeanBinder = new FieldGroup();
            empolyeeBeanBinder.setItemDataSource(empolyee);

            selectedIDString = String.valueOf(empolyee.getEntity().getId());
            eIDLabel.setValue(selectedIDString);
            eIDLabel.setImmediate(true);

            empolyeeBeanBinder.bind(eDateOfBirthField, "eDateOfBirth");
            empolyeeBeanBinder
                    .bind(eFirstNameTextField, "eFirstNameString");
            empolyeeBeanBinder.bind(eLastNameTextField, "eLastNameString");
            empolyeeBeanBinder.bind(eUserNameTextField, "eUserNameString");
            empolyeeBeanBinder.bind(ePasswordField, "ePasswordString");

            empolyeeBeanBinder.bind(ePositionComBoBox, "ePositionBean");
            empolyeeBeanBinder.bind(eSocialSecurityIDTextField,
                    "eSocialSecurityIDString");
            empolyeeBeanBinder.bind(eStartDateField, "eStartDate");
            empolyeeBeanBinder.bind(eStatusComBoBox, "eStatusString");
        }

    } else {
        return;
    }
    
}

Interface

package com.example.view;

import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.ui.Button.ClickEvent;

public interface ButtonClickedPOSListener {

public void setButtonClickPOSListener(ClickedListener b);

interface ClickedListener{

    public void clickHappened(ClickEvent event, Object bean, String activeString);

    public void clickHappened(ClickEvent event);

    public void clickHappened(ClickEvent event, String activeString);
}

public void setJPAContainer(JPAContainer<?> JpaContainer);

}

Hi,

Container, and JPAContainer, is just an old interface that some Vaadin components use to bind data into them. I practically never use any kind of container, even less JPAContainer, but try to just use basic Java collections to do databinding. This way you presenter can become less coupled with Vaadin code.

See Viritin add-on that that has lots of helpers for databinding using plain Java collections. If you are using plain Vaadin, you can wrap collections of your JPA entities using BeanItemContainer.

cheers,
matti

Hi Matti,

Thank you so much for your reply, I read your blog about using JPAContainer. Looks like you are not a big fan of it. I know you have developed a add-on call Viritin. Are there more examples or turtoial that I can find of how to use Viritin?

If I do not like write SQL code in my project, can Viritn do the work as JPAContainer? or there is another addon that I can use?

Thanks,
James

Hi,

There are couple of example projects in my
GitHub repositories
and there is also a rather extensive set of usage examples in the projects integration test codes.

To avoid writing boilerplate code with JPA, I strongly suggest to use and learn either
Spring Data
or
DeltaSpike Data
. Those are superb libraries and you’ll want to use those also in Java projects that don’t use Vaadin. Highly recommended!

cheers,
matti