Package org.apache.tuscany.das.rdb

Examples of org.apache.tuscany.das.rdb.Command


    }

    public float deposit(String account, float ammount) throws RemoteException {

        try {
            Command select = Command.FACTORY.createCommand("SELECT accountNumber, balance FROM accounts where accountNumber = :accountNumber",
                    createConfigStream());
            Connection conn = getConnection();
            select.setConnection(conn);
            select.setParameterValue("accountNumber", account);
            TypeHelper helper = TypeHelper.INSTANCE;
            select.setDataObjectModel(helper.getType(DataGraphRoot.class));
            DataGraphRoot root = (DataGraphRoot) select.executeQuery();
            Collection accounts = root.getAccountSummaries();
            AccountSummary accountData = (AccountSummary) accounts.iterator().next();
            float newbalance = accountData.getBalance() + ammount;
            accountData.setBalance(newbalance);
            // update department set companyid = ? where department.name = ?
            CommandGroup commandGroup = CommandGroup.FACTORY.createCommandGroup(createConfigStream());
            commandGroup.setConnection(conn);
            Command update = commandGroup.getCommand("update balance");
            update.setParameterValue("BALANCE", new Float(newbalance));
            update.setParameterValue("ACCOUNTNUMBER", account);
            update.execute();
            conn.close();
            return newbalance;
        } catch (Exception e) {
            throw new RemoteException(e.getClass().getName(), e);
        }
View Full Code Here


    public StockSummary sellStock(int purchaseLotNumber, int quantity) throws RemoteException {
        try {
            CommandGroup commandGroup = CommandGroup.FACTORY.createCommandGroup(createConfigStream());
            commandGroup.setConnection(getConnection());

            Command read = commandGroup.getCommand("stockbylotSelect");
            TypeHelper helper = TypeHelper.INSTANCE;
            read.setDataObjectModel(helper.getType(DataGraphRoot.class));
            read.setParameterValue("PURCHASELOTNUMBER", purchaseLotNumber);// autoboxing :-)
            DataGraphRoot root = (DataGraphRoot) read.executeQuery();
            List stocks = root.getStockSummaries();
            if (null != stocks && !stocks.isEmpty()) {
                StockSummary stock = (StockSummary) stocks.get(0);
                int newQuatity = Math.max(stock.getQuantity() - quantity, 0);
                if (newQuatity < 1) {

                    Command delete = Command.FACTORY.createCommand("DELETE FROM STOCKS WHERE PURCHASELOTNUMBER = ?");
                    delete.setParameterValue(1, purchaseLotNumber);
                    delete.setConnection(getConnection());
                    delete.execute();

                } else {

                    Command update = commandGroup.getCommand("stockbylot");

                    update.setParameterValue("QUANTITY", newQuatity);
                    update.setParameterValue("PURCHASELOTNUMBER", purchaseLotNumber);
                    update.execute();

                    stock.setQuantity(newQuatity);
                }

            }
View Full Code Here

    public StockSummary purchaseStock(int id, StockSummary stock) throws RemoteException {

        try {

            Command insert = Command.FACTORY
                    .createCommand("insert into stocks (id, symbol, quantity, purchasePrice, purchaseDate) values (?,?,?,?,?)");
            insert.setParameterValue(1, new Integer(id));
            insert.setParameterValue(2, stock.getSymbol());
            insert.setParameterValue(3, stock.getQuantity());
            insert.setParameterValue(4, stock.getPurchasePrice());
            insert.setParameterValue(5, DateConverter.INSTANCE.getColumnValue(stock.getPurchaseDate()));

            insert.setConnection(getConnection());
            insert.execute();

            return stock;
        } catch (Exception e) {
            if (e instanceof RemoteException)
                throw (RemoteException) e;
View Full Code Here

  }

  public void testPaging() throws SQLException  {

    //Build command to read all customers
    Command custCommand = Command.FACTORY.createCommand("select * from CUSTOMER order by ID");
    custCommand.setConnection(getConnection());

    //Create a pager with the command
    Pager pager = new PagerImpl(custCommand, 2);

    //Get and work with first page
View Full Code Here

  }
 
 
  public void testRandomPage() throws SQLException {
  //Build the select command
    Command select = Command.FACTORY
        .createCommand("select * from CUSTOMER order by ID");

    //Parameterize the command
    select.setConnection(getConnection());

    //Create a pager
    Pager pager = new PagerImpl(select, 2);

    //Get the first page
View Full Code Here

     * Dilton's bug for adding new child data object
     */
    public void testAddNewOrder() throws Exception {

        // Read some customers and related orders
        Command select = Command.FACTORY.createCommand(
                "SELECT * FROM CUSTOMER LEFT JOIN ANORDER ON CUSTOMER.ID = ANORDER.CUSTOMER_ID",
                getConfig("CustomersOrdersConfig.xml"));
        select.setConnection(getConnection());

        DataObject root = select.executeQuery();

        DataObject cust = root.getDataObject("CUSTOMER[1]");

        // Save ID and Order Count
        int custID = cust.getInt("ID");
        int custOrderCount = cust.getList("orders").size();

        // Create a new Order and add to customer1
        DataObject order = root.createDataObject("ANORDER");

        order.set("ID", new Integer(99));
        order.set("PRODUCT", "The 99th product");
        order.set("QUANTITY", new Integer(99));
        cust.getList("orders").add(order);

        assertEquals(custOrderCount + 1, cust.getList("orders").size());
        // Build apply changes command
        ApplyChangesCommand apply = Command.FACTORY.createApplyChangesCommand(getConfig("CustomersOrdersConfig.xml"));
        apply.setConnection(getConnection());

        // Flush changes
        apply.execute(root);

        // verify cust1 relationship updates
        select = Command.FACTORY
                .createCommand("SELECT * FROM CUSTOMER LEFT JOIN ANORDER ON CUSTOMER.ID = ANORDER.CUSTOMER_ID where CUSTOMER.ID = :ID",getConfig("CustomersOrdersConfig.xml"));
        select.setConnection(getConnection());
        select.setParameterValue("ID", new Integer(custID));
        root = select.executeQuery();

        assertEquals(custOrderCount + 1, root.getList("CUSTOMER[1]/orders").size());

    }
View Full Code Here

        // 19:29:52.636')";
        // String sql = "insert into conmgt.serverstatus (managedserverid,
        // timestamp) values (316405209, '2005-11-23 19:29:52.636')";
        String sql = "insert into conmgt.serverstatus (managedserverid, timestamp) values (:serverid, :timestamp)";

        Command insert = Command.FACTORY.createCommand(sql);
        insert.setParameterValue("serverid", new Integer(316405209));
        insert.setParameterValue("timestamp", "2005-11-23 19:29:52.636");
        insert.setConnection(getConnection());
        insert.execute();

        // Verify
        Command select = Command.FACTORY.createCommand("Select * from conmgt.serverstatus");
        select.setConnection(getConnection());
        DataObject root = select.executeQuery();
        assertEquals(1, root.getList("SERVERSTATUS").size());

    }
View Full Code Here

        CommandGroup commandGroup = CommandGroup.FACTORY
                .createCommandGroup(getConfig("CustomersOrdersConfig.xml"));
        commandGroup.setConnection(getConnection());

        // Read all customers and add one
        Command read = commandGroup.getCommand("all customers");
        DataObject root = read.executeQuery();
        int numCustomers = root.getList("CUSTOMER").size();

        DataObject newCust = root.createDataObject("CUSTOMER");
        newCust.set("ID", new Integer(100));
        newCust.set("ADDRESS", "5528 Wells Fargo Drive");
        newCust.set("LASTNAME", "Gerkin");

        // Now delete this new customer
        newCust.delete();

        ApplyChangesCommand apply = commandGroup.getApplyChangesCommand();
        apply.execute(root);

        // Verify
        root = read.executeQuery();
        assertEquals(numCustomers, root.getList("CUSTOMER").size());

    }
View Full Code Here

     * Should be able to explicitly set a parameter to null. But, should require
     * that the parameter type is set.
     */
    public void testDiltonsNullParameterBug1() throws Exception {

        Command insert = Command.FACTORY.createCommand("insert into CUSTOMER values (:ID, :LASTNAME, :ADDRESS)");
        insert.setConnection(getConnection());
        insert.setParameterValue("ID", new Integer(10));
        insert.setParameterValue("LASTNAME", null);
        insert.setParameterType("LASTNAME", SDODataTypes.STRING);
        insert.setParameterValue("ADDRESS", "5528 Wells Fargo Dr");
        insert.execute();

        // Verify
        Command select = Command.FACTORY.createCommand("Select * from CUSTOMER where ID = 10");
        select.setConnection(getConnection());
        DataObject root = select.executeQuery();
        assertEquals(1, root.getList("CUSTOMER").size());
        assertEquals("5528 Wells Fargo Dr", root.get("CUSTOMER[1]/ADDRESS"));

    }
View Full Code Here

    /**
     * Error by not setting a parameter
     */
    public void testDiltonsNullParameterBug2() throws Exception {

        Command insert = Command.FACTORY.createCommand("insert into CUSTOMER values (:ID, :LASTNAME, :ADDRESS)");
        insert.setConnection(getConnection());
        insert.setParameterValue("ID", new Integer(10));
        // insert.setParameterValue("LASTNAME", null);
        insert.setParameterValue("ADDRESS", "5528 Wells Fargo Dr");

        try {
            insert.execute();
            fail();
        } catch (RuntimeException e) {
            // Expected since "LASTNAME" parameter not set
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.tuscany.das.rdb.Command

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.