/** * Edit an existing company. * * @param entity entity to be edited. * @param newEntity true if entity to be edited is new. */ public void edit(final Company entity, final boolean newEntity) { this.entity = entity; companyEditor.setItem(new BeanItem<Company>(entity), newEntity); invoicingAddressEditor.setItem( new BeanItem<PostalAddress>(entity.getInvoicingAddress()), newEntity); deliveryAddressEditor.setItem( new BeanItem<PostalAddress>(entity.getDeliveryAddress()), newEntity); }
@Override public void initialize() { entityManager = getSite().getSiteContext().getObject(EntityManager.class); final GridLayout gridLayout = new GridLayout(3, 2); gridLayout.setSizeFull(); gridLayout.setMargin(false); gridLayout.setSpacing(true); gridLayout.setRowExpandRatio(1, 1f); setViewContent(gridLayout); companyEditor = new ValidatingEditor(SiteFields.getFieldDescriptors(Company.class)); companyEditor.setCaption("Site"); companyEditor.addListener((ValidatingEditorStateListener) this); gridLayout.addComponent(companyEditor, 0, 0); invoicingAddressEditor = new ValidatingEditor(SiteFields.getFieldDescriptors(PostalAddress.class)); invoicingAddressEditor.setCaption("Invoicing Address"); invoicingAddressEditor.addListener((ValidatingEditorStateListener) this); gridLayout.addComponent(invoicingAddressEditor, 1, 0); deliveryAddressEditor = new ValidatingEditor(SiteFields.getFieldDescriptors(PostalAddress.class)); deliveryAddressEditor.setCaption("Delivery Address"); deliveryAddressEditor.addListener((ValidatingEditorStateListener) this); gridLayout.addComponent(deliveryAddressEditor, 2, 0); final HorizontalLayout buttonLayout = new HorizontalLayout(); buttonLayout.setSpacing(true); gridLayout.addComponent(buttonLayout, 0, 1); saveButton = new Button("Save"); saveButton.setImmediate(true); buttonLayout.addComponent(saveButton); saveButton.addListener( new ClickListener() { /** Serial version UID. */ private static final long serialVersionUID = 1L; @Override public void buttonClick(final ClickEvent event) { companyEditor.commit(); invoicingAddressEditor.commit(); deliveryAddressEditor.commit(); entityManager.getTransaction().begin(); try { entity = entityManager.merge(entity); entityManager.persist(entity); entityManager.getTransaction().commit(); entityManager.detach(entity); } catch (final Throwable t) { if (entityManager.getTransaction().isActive()) { entityManager.getTransaction().rollback(); } throw new RuntimeException("Failed to save entity: " + entity, t); } } }); discardButton = new Button("Discard"); discardButton.setImmediate(true); buttonLayout.addComponent(discardButton); discardButton.addListener( new ClickListener() { /** Serial version UID. */ private static final long serialVersionUID = 1L; @Override public void buttonClick(final ClickEvent event) { companyEditor.discard(); invoicingAddressEditor.discard(); deliveryAddressEditor.discard(); } }); }
@Override public boolean isValid() { return companyEditor.isValid() && invoicingAddressEditor.isValid() && deliveryAddressEditor.isValid(); }
@Override public boolean isDirty() { return companyEditor.isModified() || invoicingAddressEditor.isModified() || deliveryAddressEditor.isModified(); }