/* * Copyright 2016 SIB Visions GmbH * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. * * * History * * 11.08.2016 - [JR] - creation */ package com.sibvisions.forum; import jvx.rad.application.SimpleTestLauncher; import jvx.rad.genui.UIFactoryManager; import jvx.rad.genui.container.UIPanel; import jvx.rad.genui.layout.UIBorderLayout; import jvx.rad.type.bean.IBean; import jvx.rad.ui.celleditor.ILinkedCellEditor; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import com.sibvisions.apps.components.NavigationTable; import com.sibvisions.apps.server.util.LifeCycleUtil; import com.sibvisions.rad.persist.StorageDataBook; import com.sibvisions.rad.persist.event.IStorageListener; import com.sibvisions.rad.persist.event.StorageEvent; import com.sibvisions.rad.persist.jdbc.DBAccess; import com.sibvisions.rad.persist.jdbc.DBStorage; import com.sibvisions.rad.ui.swing.impl.SwingFactory; import com.sibvisions.util.type.CommonUtil; public class TestInsteadOf extends UIPanel { /** the database access. */ private DBAccess dba; /** the contract storage. */ private DBStorage dbsContract; /** the activity storage. */ private DBStorage dbsActivity; /** * Initializes the unit test. * * @throws Exception if initialization fails */ @BeforeClass public static void beforeClass() { UIFactoryManager.getFactoryInstance(SwingFactory.class); } /** * Initializes the test case. * * @throws Exception if initialization fails */ @Before public void before() throws Exception { dba = DBAccess.getDBAccess("jdbc:oracle:thin:@localhost:1521:XE", "username", "password"); dba.open(); } /** * Cleanup the test case. */ @After public void after() { CommonUtil.close(dbsActivity, dbsContract, dba); } /** * Tests application start with a custom UI. * * @throws Exception if app start fails */ @Test public void testInsteadOf() throws Exception { DBStorage dbsActivities = new DBStorage(); dbsActivities.setAutoLinkReference(false); dbsActivities.setFromClause("V_ACTIVITIES"); dbsActivities.setWritebackTable("ACTIVITY"); //manual autolink dbsActivities.createAutomaticLinkReference(new String[] {"CONT_ID", "CONT_TITLE"}, "CONTRACT", new String[] {"ID", "TITLE"}); dbsActivities.eventInsteadOfInsert().addListener(new IStorageListener() { public void storageChanged(StorageEvent pStorageEvent) throws Throwable { boolean bAutoCommit = dba.isAutoCommit(); try { dba.setAutoCommit(false); DBStorage contract = getContract(); IBean newRecord = pStorageEvent.getNew(); //check if a new contract should be created for the activity if (newRecord.get("CONT_ID") == null) { IBean bnContract = contract.createEmptyBean(); bnContract.put("TITLE", newRecord.get("CONT_TITLE")); bnContract = contract.insert(bnContract); //we need the new id in our activity record! newRecord.put("CONT_ID", bnContract.get("ID")); } //updates newRecord, because we need the updated columns (e.g. ID) newRecord.putAll(getActivity().insert(newRecord)); dba.commit(); } catch (Throwable th) { dba.rollback(); throw th; } finally { dba.setAutoCommit(bAutoCommit); } } }); dbsActivities.setDBAccess(dba); dbsActivities.open(); StorageDataBook sdbActivities = new StorageDataBook(dbsActivities); sdbActivities.open(); ((ILinkedCellEditor)sdbActivities.getRowDefinition().getColumnDefinition("CONT_TITLE"). getDataType().getCellEditor()).setValidationEnabled(false); setLayout(new UIBorderLayout()); NavigationTable nav = new NavigationTable(); nav.setDataBook(sdbActivities); //show all columns //nav.setColumnView(new ColumnView(sdbActivities.getRowDefinition().getColumnNames())); add(nav); new SimpleTestLauncher(this); } /** * Gets the contract storage. * * @return the contract storage. * @throws Exception if creation fails */ public DBStorage getContract() throws Exception { if (dbsContract == null) { dbsContract = LifeCycleUtil.createWriteBackStorage(dba, "CONTRACT"); } return dbsContract; } /** * Gets the activity storage. * * @return the contract storage. * @throws Exception if creation fails */ public DBStorage getActivity() throws Exception { if (dbsActivity == null) { dbsActivity = LifeCycleUtil.createWriteBackStorage(dba, "ACTIVITY"); } return dbsActivity; } } // TestInsteadOf