| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| * |
| */ |
| package org.eclipse.osbp.runtime.tests.dtoandui; |
| |
| import static org.junit.Assert.assertEquals; |
| import static org.junit.Assert.assertNotNull; |
| import static org.junit.Assert.assertNull; |
| import static org.junit.Assert.fail; |
| |
| import java.util.Date; |
| |
| import org.eclipse.osbp.dsl.dto.lib.impl.DtoServiceAccess; |
| import org.eclipse.osbp.ecview.core.common.beans.ISlot; |
| import org.eclipse.osbp.ecview.core.common.context.ContextException; |
| import org.eclipse.osbp.ecview.core.common.context.IViewContext; |
| import org.eclipse.osbp.ecview.core.common.editpart.DelegatingEditPartManager; |
| import org.eclipse.osbp.ecview.core.common.model.binding.YBindingSet; |
| import org.eclipse.osbp.ecview.core.common.model.core.YBeanSlot; |
| import org.eclipse.osbp.ecview.core.common.model.core.YBeanSlotListBindingEndpoint; |
| import org.eclipse.osbp.ecview.core.common.model.core.YEmbeddableCollectionEndpoint; |
| import org.eclipse.osbp.ecview.core.common.model.core.YView; |
| import org.eclipse.osbp.ecview.core.common.presentation.IFieldPresentation; |
| import org.eclipse.osbp.ecview.core.extension.model.extension.YGridLayout; |
| import org.eclipse.osbp.ecview.core.extension.model.extension.YTable; |
| import org.eclipse.osbp.ecview.core.extension.model.extension.util.SimpleExtensionModelFactory; |
| import org.eclipse.osbp.ecview.core.ui.core.editparts.extension.ITableEditpart; |
| import org.eclipse.osbp.runtime.common.filter.IDTOService; |
| import org.eclipse.osbp.runtime.tests.AbstractJPATest; |
| import org.eclipse.osbp.runtime.tests.DefaultUI; |
| import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.VaadinRenderer; |
| import org.eclipse.osbp.runtime.web.vaadin.databinding.VaadinObservables; |
| import org.junit.Test; |
| import org.osbp.tests.dtos.CashPositionDto; |
| import org.osbp.tests.dtos.CashRegisterDto; |
| import org.osbp.tests.dtos.CashSlipDto; |
| import org.osbp.tests.dtos.McustomerDto; |
| |
| import com.vaadin.data.Container.Indexed; |
| import com.vaadin.ui.CssLayout; |
| import com.vaadin.ui.Table; |
| import com.vaadin.ui.UI; |
| |
| @SuppressWarnings("restriction") |
| public class BoundCashTests extends AbstractJPATest { |
| |
| private IDTOService<McustomerDto> customerService; |
| private IDTOService<CashRegisterDto> cashRegisterService; |
| private IDTOService<CashSlipDto> cashSlipService; |
| private IDTOService<CashPositionDto> cashSlipPosService; |
| |
| private SimpleExtensionModelFactory factory = new SimpleExtensionModelFactory(); |
| |
| private UI ui = new DefaultUI(); |
| private CssLayout rootLayout = new CssLayout(); |
| private YTable yTable; |
| private VaadinRenderer renderer; |
| private ITableEditpart table1Editpart; |
| private IViewContext viewContext; |
| private IFieldPresentation<Table> tablePresentation; |
| private Table table; |
| private Indexed indexedDs; |
| private YBindingSet yBindingSet; |
| private McustomerDto cust; |
| private CashSlipDto slip; |
| private CashPositionDto pos1; |
| private CashPositionDto pos2; |
| private YBeanSlot yBeanSlot; |
| private ISlot beanSlot; |
| |
| @Override |
| public void setUp() throws Exception { |
| super.setUp(); |
| |
| customerService = DtoServiceAccess.getService(McustomerDto.class); |
| cashRegisterService = DtoServiceAccess.getService(CashRegisterDto.class); |
| cashSlipService = DtoServiceAccess.getService(CashSlipDto.class); |
| cashSlipPosService = DtoServiceAccess.getService(CashPositionDto.class); |
| |
| setupView(); |
| createDatamodel(); |
| |
| // bind the slip |
| beanSlot.setValue(slip); |
| } |
| |
| private void setupView() throws ContextException { |
| |
| ui.setContent(rootLayout); |
| VaadinObservables.activateRealm(ui); |
| |
| // create a view |
| // |
| YView yView = factory.createView(); |
| YGridLayout yLayout = factory.createGridLayout(); |
| yView.setContent(yLayout); |
| yTable = factory.createTable(); |
| yTable.setType(CashPositionDto.class); |
| yLayout.getElements().add(yTable); |
| |
| yBindingSet = yView.getOrCreateBindingSet(); |
| yBeanSlot = yView.addBeanSlot("ds", CashSlipDto.class); |
| |
| YBeanSlotListBindingEndpoint ySlotListBinding = factory.createBeanSlotListBindingEndpoint(); |
| ySlotListBinding.setBeanSlot(yBeanSlot); |
| ySlotListBinding.setCollectionType(CashPositionDto.class); |
| ySlotListBinding.setAttributePath("positions"); |
| |
| YEmbeddableCollectionEndpoint yTableEndpoint = yTable.createCollectionEndpoint(); |
| yBindingSet.addBinding(ySlotListBinding, yTableEndpoint); |
| |
| renderer = new VaadinRenderer(); |
| viewContext = renderer.render(rootLayout, yView, null); |
| |
| table1Editpart = DelegatingEditPartManager.getInstance().getEditpart(viewContext, yTable); |
| tablePresentation = table1Editpart.getPresentation(); |
| table = (Table) tablePresentation.getWidget(); |
| |
| indexedDs = (Indexed) table.getContainerDataSource(); |
| beanSlot = viewContext.getBeanSlot("ds"); |
| |
| } |
| |
| private void createDatamodel() { |
| |
| int custId = 10; |
| |
| // create customer |
| cust = new McustomerDto(); |
| cust.setId(custId); |
| cust.setFullname("customer1"); |
| customerService.update(cust); |
| cust = customerService.get(custId); |
| |
| slip = new CashSlipDto(); |
| slip.setCashier("Jörg"); |
| slip.setCustomer(cust); |
| slip.setNow(new Date()); |
| slip.setCurrentDay(new Date().toString()); |
| slip.setSerial(10l); |
| // slip.setRegister(cashRegister); |
| slip.setTotal(100d); |
| cashSlipService.update(slip); |
| slip = cashSlipService.get(slip.getId()); |
| |
| pos1 = new CashPositionDto(); |
| pos1.setPrice(1000d); |
| pos1.setQuantity(10d); |
| slip.addToPositions(pos1); |
| cashSlipPosService.update(pos1); |
| |
| pos2 = new CashPositionDto(); |
| pos2.setPrice(2000d); |
| pos2.setQuantity(20d); |
| slip.addToPositions(pos2); |
| cashSlipPosService.update(pos2); |
| } |
| |
| @Test |
| public void testBindCashPosition() throws Exception { |
| setUp(); |
| |
| assertEquals(2, slip.getPositions().size()); |
| assertEquals(2, indexedDs.size()); |
| |
| CashPositionDto pos3 = new CashPositionDto(); |
| pos3.setPrice(3000d); |
| pos3.setQuantity(30d); |
| slip.addToPositions(pos3); |
| |
| assertEquals(3, slip.getPositions().size()); |
| assertEquals(3, indexedDs.size()); |
| |
| CashPositionDto pos4 = new CashPositionDto(); |
| pos4.setPrice(4000d); |
| pos4.setQuantity(40d); |
| slip.addToPositions(pos4); |
| |
| assertEquals(4, slip.getPositions().size()); |
| assertEquals(4, indexedDs.size()); |
| |
| slip.removeFromPositions(pos3); |
| |
| assertEquals(3, slip.getPositions().size()); |
| assertEquals(3, indexedDs.size()); |
| } |
| |
| } |