Pojo inside the pojo and BeanItem

Hi!

As a newbie I have plenty of easy problems, not easy for me though :slight_smile:

I implemented pojo which includes pojos like CompanyPojo-AddressPojo etc. I couldn’t get it working because Beanitem couldn’t handle (??) nested pojos. I implemented workaround like CompanyHelperForm which includes every fields in form and getters and setters. But I am still having big problems and it’s not working, could you tell me why CompanyItem is always null?

public class BasicInfoForm extends Form{

private Company company;
private BeanItem<CompanyHelperForm> companyItem;
private String companyName;
private String address1;
private String address2;
private String zip;
private String city;
private String phoneNumber;
private String email;



public BasicInfoForm(Company company){

	try {
		company = new Company();
		BeanItem<CompanyHelperForm>companyItem = new BeanItem<CompanyHelperForm>(new CompanyHelperForm(company)); 

this.setWriteThrough(false); // we want explicit 'apply'
this.setInvalidCommitted(false); // no invalid values in datamodel

// FieldFactory for customizing the fields and adding validators
this.setFormFieldFactory(new CompanyFieldFactory());
this.setItemDataSource(companyItem); // bind to POJO via BeanItem

// Determines which properties are shown, and in which order:
this.setVisibleItemProperties(Arrays.asList(new String[] {
        "companyName", "companyId", "address1, address2, zip, city" }));
System.out.println("companyItem" +companyItem);---------> Always NULL!

public class CompanyHelperForm {

private String companyName;
private String address1;
private String address2;
private String zip;
private String city;
private String phoneNumber;
private String email;
private Company company;



/**
 * 
 */
public CompanyHelperForm(Company company) {
	this.company = company;
}

public String getCompanyName() {
	return companyName;
}

If you have some nice solutions to avoid copying fields from POJO to CompanyHelperForm, I would be very thankful as well.

Your example is not quite clear to me, but in fact, there is support for nested properties in Vaadin 6.6: you can add a NestedMethodProperty to a BeanItem.

If you are using BeanContainer/BeanItemContainer, simply call e.g. addNestedContainerProperty(“company.manager.secretary.address.street”). If you are working directly with BeanItems, use e.g. item.addItemProperty(“streetAddressOfSecretary”, new NestedMethodProperty(item.getBean(), “company.manager.secretary.address.street”)).

Note that nulls in the intermediate parts of the path are not supported.

Hi and thanks for helping me!

So, I just tried to use directly BeanItem as is used in your example: http://demo.vaadin.com/sampler#FormBasic

I have a object model where is Company, there is list of Addresses, list of phone numbers, emailPOJO etc. I was just trying to get those fields straight to the form like in that example:

public FormPojoExample() {

    person = new Person(); // a person POJO
    BeanItem<Person> personItem = new BeanItem<Person>(person); // item from
                                                                // POJO

    // Create the Form
    final Form personForm = new Form();
    personForm.setCaption("Personal details");
    personForm.setWriteThrough(false); // we want explicit 'apply'
    personForm.setInvalidCommitted(false); // no invalid values in datamodel

    // FieldFactory for customizing the fields and adding validators
    personForm.setFormFieldFactory(new PersonFieldFactory());
    personForm.setItemDataSource(personItem); // bind to POJO via BeanItem

    // Determines which properties are shown, and in which order:
    personForm.setVisibleItemProperties(Arrays.asList(new String[] {
            "firstName", "lastName", "countryCode", "password",
            "birthdate", "shoesize", "uuid" }));

    // Add form to layout
    addComponent(personForm);

Is it even possible because there are lists? Do I always have to add items first, Vaadin is not doing it automatically like in your person-example? What is the wisest and maybe easiest way to handle that, your way?

Cheers,
Sami

For handling the lists, you should use a field factory that creates a suitable sub-form/table/other editor for each of the collection type fields. The
CustomField add-on
should help you there - each sub-editor can be a CustomField.

What exactly that sub-editor is like depends on what you want - is it selection from existing entities, something that allows you to create new sub-entities or something else. There are many possible variations, but maybe you can reuse an implementation for multiple cases.

I’m afraid I don’t have an example of exactly what you want at hand, though.