You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by th...@apache.org on 2003/09/13 23:27:07 UTC
cvs commit: db-ojb/src/prevayler/org/apache/ojb/broker/prevayler/demo UCQuitApplication.java UCEnterNewProduct.java UCEditProduct.java AbstractUseCase.java Application.java UCListAllProducts.java UseCase.java UCDeleteProduct.java
thma 2003/09/13 14:27:07
Added: src/prevayler/org/apache/ojb/broker/prevayler
CommandDelete.java Database.java
PBPrevaylerImpl.java CommandStore.java
src/prevayler/org/apache/ojb/broker/prevayler/demo
UCQuitApplication.java UCEnterNewProduct.java
UCEditProduct.java AbstractUseCase.java
Application.java UCListAllProducts.java
UseCase.java UCDeleteProduct.java
Log:
add first draft of a prevayler based PB implementation
Revision Changes Path
1.1 db-ojb/src/prevayler/org/apache/ojb/broker/prevayler/CommandDelete.java
Index: CommandDelete.java
===================================================================
package org.apache.ojb.broker.prevayler;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache ObjectRelationalBridge" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache ObjectRelationalBridge", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.Serializable;
import org.prevayler.Command;
import org.prevayler.PrevalentSystem;
/**
* @author Thomas Mahler
* Command to delete an object in the Database
*/
public class CommandDelete implements Command
{
private final Object objectToDelete;
/**
* Constructor for CommandDelete.
*/
public CommandDelete(Object obj)
{
objectToDelete = obj;
}
/**
* @see org.prevayler.Command#execute(PrevalentSystem)
*/
public Serializable execute(PrevalentSystem system) throws Exception
{
((Database) system).remove(objectToDelete);
return null;
}
}
1.1 db-ojb/src/prevayler/org/apache/ojb/broker/prevayler/Database.java
Index: Database.java
===================================================================
package org.apache.ojb.broker.prevayler;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache ObjectRelationalBridge" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache ObjectRelationalBridge", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.Serializable;
import java.util.Hashtable;
import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.PersistenceBroker;
import org.prevayler.implementation.AbstractPrevalentSystem;
/**
* This class represents the persistent store OJB works against.
* It is implement as an PrevalentSystem.
* All Commands executed this DB are tracked by Prevayler and written to disk
* as command-logs.
* If the system is halted, crashes or rebooted for what reason so ever, Prevayler
* will establish the state of the Database from the command-logs written to disk.
* @author Thomas Mahler
*
*/
public class Database extends AbstractPrevalentSystem
{
private final Hashtable table = new Hashtable();
private transient PersistenceBroker broker;
public void store(Object obj)
{
Identity oid = new Identity(obj,broker);
this.getTable().put(oid.toString(), obj);
}
public void remove(Object obj)
{
Identity oid = new Identity(obj,broker);
this.getTable().remove(oid.toString());
}
public Serializable lookupObjectByIdentity(Identity oid)
{
return (Serializable) this.getTable().get(oid.toString());
}
/**
* Returns the table.
* @return Hashtable
*/
public Hashtable getTable()
{
return table;
}
/**
* Returns the broker.
* @return PersistenceBroker
*/
public PersistenceBroker getBroker()
{
return broker;
}
/**
* Sets the broker.
* @param broker The broker to set
*/
public void setBroker(PersistenceBroker broker)
{
this.broker = broker;
}
}
1.1 db-ojb/src/prevayler/org/apache/ojb/broker/prevayler/PBPrevaylerImpl.java
Index: PBPrevaylerImpl.java
===================================================================
package org.apache.ojb.broker.prevayler;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache ObjectRelationalBridge" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache ObjectRelationalBridge", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.ManageableCollection;
import org.apache.ojb.broker.MtoNImplementor;
import org.apache.ojb.broker.PBKey;
import org.apache.ojb.broker.PersistenceBrokerException;
import org.apache.ojb.broker.PersistenceBrokerFactory;
import org.apache.ojb.broker.TransactionAbortedException;
import org.apache.ojb.broker.TransactionInProgressException;
import org.apache.ojb.broker.TransactionNotInProgressException;
import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF;
import org.apache.ojb.broker.core.PersistenceBrokerImpl;
import org.apache.ojb.broker.query.Query;
import org.apache.ojb.broker.query.QueryByIdentity;
import org.apache.ojb.broker.util.BrokerHelper;
import org.apache.ojb.broker.util.ObjectModification;
import org.prevayler.Command;
import org.prevayler.Prevayler;
import org.prevayler.implementation.SnapshotPrevayler;
/**
* An OJB PersistenBroker implementation working against a datastore
* that is persisted by Prevayler.
* So in other word, this is an OODBMS with a PB API.
* Of course you can use OJB/ODMG or OJB/JDO on top of it.
*
* important note: this implementation is not finished.
* Do not try to use it in production environments.
*
* @author Thomas Mahler
*/
public class PBPrevaylerImpl extends PersistenceBrokerImpl
{
private transient Database db;
private Prevayler prevayler;
private ArrayList commandLog = new ArrayList(100);
private boolean inTransaction = false;
/**
* Constructor for PBPrevaylerImpl.
* @param key
* @param pbf
*/
public PBPrevaylerImpl(PBKey key, PersistenceBrokerFactoryIF pbf)
{
refresh();
if(key == null) throw new PersistenceBrokerException("Could not instantiate broker with PBKey 'null'");
this.pbf = pbf;
this.setPBKey(key);
brokerHelper = new BrokerHelper(this);
//connectionManager = ConnectionManagerFactory.getInstance().createConnectionManager(this);
//objectCache = ObjectCacheFactory.getInstance().createObjectCache(this);
//sequenceManager = SequenceManagerFactory.getSequenceManager(this);
//dbAccess = JdbcAccessFactory.getInstance().createJdbcAccess(this);
//statementManager = StatementManagerFactory.getInstance().createStatementManager(this);
//sqlGenerator = SqlGeneratorFactory.getInstance().createSqlGenerator(connectionManager.getSupportedPlatform());
//markedForDelete = new ArrayList();
try
{
prevayler = new SnapshotPrevayler(new Database(), "PrevalenceBase" + File.separator + "Database");
db = (Database) prevayler.system();
db.setBroker(this);
}
catch (Exception e)
{
}
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#abortTransaction()
*/
public void abortTransaction() throws TransactionNotInProgressException
{
if (! isInTransaction())
{
throw new TransactionNotInProgressException();
}
inTransaction = false;
commandLog.clear();
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#beginTransaction()
*/
public void beginTransaction()
throws TransactionInProgressException, TransactionAbortedException
{
if (this.isInTransaction())
{
throw new TransactionInProgressException();
}
inTransaction = true;
commandLog.clear();
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#commitTransaction()
*/
public void commitTransaction()
throws TransactionNotInProgressException, TransactionAbortedException
{
if (!isInTransaction())
{
throw new TransactionNotInProgressException();
}
Iterator iter = commandLog.iterator();
try
{
while (iter.hasNext())
{
Command cmd = (Command) iter.next();
prevayler.executeCommand(cmd);
}
}
catch (Exception e)
{
this.abortTransaction();
}
inTransaction = false;
commandLog.clear();
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#isInTransaction()
*/
public boolean isInTransaction() throws PersistenceBrokerException
{
return inTransaction;
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#close()
*/
public boolean close()
{
if (isInTransaction())
{
abortTransaction();
}
try
{
((SnapshotPrevayler)prevayler).takeSnapshot();
}
catch (IOException e)
{
}
setClosed(true);
return true;
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#clearCache()
*/
public void clearCache() throws PersistenceBrokerException
{
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#removeFromCache(Object)
*/
public void removeFromCache(Object obj) throws PersistenceBrokerException
{
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#store(Object, ObjectModification)
*/
public void store(Object obj, ObjectModification modification)
throws PersistenceBrokerException
{
this.store(obj);
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#store(Object)
*/
public void store(Object obj) throws PersistenceBrokerException
{
if (! (obj instanceof Serializable))
{
throw new PersistenceBrokerException(obj.getClass().getName() + "does not implement java.io.Serializable.");
}
CommandStore cmd = new CommandStore(obj);
commandLog.add(cmd);
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#delete(Object)
*/
public void delete(Object obj) throws PersistenceBrokerException
{
Command cmd = new CommandDelete(obj);
commandLog.add(cmd);
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#deleteMtoNImplementor(MtoNImplementor)
*/
public void deleteMtoNImplementor(MtoNImplementor m2nImpl)
throws PersistenceBrokerException
{
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#addMtoNImplementor(MtoNImplementor)
*/
public void addMtoNImplementor(MtoNImplementor m2nImpl)
throws PersistenceBrokerException
{
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#deleteByQuery(Query)
*/
public void deleteByQuery(Query query) throws PersistenceBrokerException
{
throw new PersistenceBrokerException("not yet implemented");
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#retrieveAllReferences(Object)
*/
public void retrieveAllReferences(Object pInstance)
throws PersistenceBrokerException
{
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#retrieveReference(Object, String)
*/
public void retrieveReference(Object pInstance, String pAttributeName)
throws PersistenceBrokerException
{
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#getCount(Query)
*/
public int getCount(Query query) throws PersistenceBrokerException
{
throw new PersistenceBrokerException("not yet implemented");
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#getCollectionByQuery(Query)
*/
public Collection getCollectionByQuery(Query query)
throws PersistenceBrokerException
{
// needs some more work ;-)
return db.getTable().values();
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#getCollectionByQuery(Class, Query)
*/
public ManageableCollection getCollectionByQuery(
Class collectionClass,
Query query)
throws PersistenceBrokerException
{
throw new PersistenceBrokerException("not yet implemented");
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#getIteratorByQuery(Query)
*/
public Iterator getIteratorByQuery(Query query)
throws PersistenceBrokerException
{
throw new PersistenceBrokerException("not yet implemented");
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#getReportQueryIteratorByQuery(Query)
*/
public Iterator getReportQueryIteratorByQuery(Query query)
throws PersistenceBrokerException
{
throw new PersistenceBrokerException("not yet implemented");
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#getObjectByIdentity(Identity)
*/
public Object getObjectByIdentity(Identity id)
throws PersistenceBrokerException
{
return db.lookupObjectByIdentity(id);
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#getObjectByQuery(Query)
*/
public Object getObjectByQuery(Query query)
throws PersistenceBrokerException
{
if (query instanceof QueryByIdentity)
{
Object id = ((QueryByIdentity) query).getExampleObject();
if (! (id instanceof Identity))
{
id = new Identity(id,PersistenceBrokerFactory.defaultPersistenceBroker());
}
Identity oid = (Identity) id;
return db.lookupObjectByIdentity(oid);
}
else
{
throw new PersistenceBrokerException("not yet implemented");
}
}
/**
* @see org.apache.ojb.broker.PersistenceBroker#getPKEnumerationByQuery(Class, Query)
*/
public Enumeration getPKEnumerationByQuery(
Class PrimaryKeyClass,
Query query)
throws PersistenceBrokerException
{
throw new PersistenceBrokerException("not yet implemented");
}
}
1.1 db-ojb/src/prevayler/org/apache/ojb/broker/prevayler/CommandStore.java
Index: CommandStore.java
===================================================================
package org.apache.ojb.broker.prevayler;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache ObjectRelationalBridge" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache ObjectRelationalBridge", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.Serializable;
import org.prevayler.Command;
import org.prevayler.PrevalentSystem;
/**
* @author Thomas Mahler
* Command to store an object in the Database.
*/
public class CommandStore implements Command
{
private final Object objectToStore;
public CommandStore(Object toStore)
{
this.objectToStore = toStore;
}
/**
* @see org.prevayler.Command#execute(PrevalentSystem)
*/
public Serializable execute(PrevalentSystem ps) throws Exception
{
((Database) ps).store(objectToStore);
return null;
}
}
1.1 db-ojb/src/prevayler/org/apache/ojb/broker/prevayler/demo/UCQuitApplication.java
Index: UCQuitApplication.java
===================================================================
package org.apache.ojb.broker.prevayler.demo;
/**
* implements Use Case "Quit Application".
* @author Thomas Mahler
*/
public class UCQuitApplication extends AbstractUseCase
{
/**
* UCQuitApplication constructor comment.
*/
public UCQuitApplication(org.apache.ojb.broker.PersistenceBroker b)
{
super(b);
}
/**
* apply method comment.
*/
public void apply()
{
// release the broker in use
broker.close();
// no OJB API for quitting the application ;-)
System.out.println("bye...");
System.exit(0);
}
/**
* getDescription method comment.
*/
public String getDescription()
{
return "Quit Application";
}
}
1.1 db-ojb/src/prevayler/org/apache/ojb/broker/prevayler/demo/UCEnterNewProduct.java
Index: UCEnterNewProduct.java
===================================================================
package org.apache.ojb.broker.prevayler.demo;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.PersistenceBrokerException;
import org.apache.ojb.tutorial1.Product;
/**
* Insert the type's description here.
* Creation date: (04.03.2001 10:34:15)
* @author Thomas Mahler
*/
public class UCEnterNewProduct extends AbstractUseCase
{
/**
* UCEnterNewProduct constructor comment.
*/
public UCEnterNewProduct(PersistenceBroker broker)
{
super(broker);
}
/** perform this use case*/
public void apply()
{
// this will be our new object
Product newProduct = new Product();
// thma: attention, no sequence numbers yet for ojb/prevalyer
newProduct.setId((int)System.currentTimeMillis());
// now read in all relevant information and fill the new object:
System.out.println("please enter a new product");
String in = readLineWithMessage("enter name:");
newProduct.setName(in);
in = readLineWithMessage("enter price:");
newProduct.setPrice(Double.parseDouble(in));
in = readLineWithMessage("enter available stock:");
newProduct.setStock(Integer.parseInt(in));
// now perform persistence operations
try
{
// 1. open transaction
broker.beginTransaction();
// 2. make the new object persistent
broker.store(newProduct);
broker.commitTransaction();
}
catch (PersistenceBrokerException ex)
{
// if something went wrong: rollback
broker.abortTransaction();
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
/** get descriptive information on use case*/
public String getDescription()
{
return "Enter a new product";
}
}
1.1 db-ojb/src/prevayler/org/apache/ojb/broker/prevayler/demo/UCEditProduct.java
Index: UCEditProduct.java
===================================================================
package org.apache.ojb.broker.prevayler.demo;
import org.apache.ojb.broker.query.Query;
import org.apache.ojb.broker.query.QueryByIdentity;
import org.apache.ojb.tutorial1.Product;
/**
* Insert the type's description here.
* Creation date: (04.03.2001 10:34:15)
* @author Thomas Mahler
*/
public class UCEditProduct extends AbstractUseCase
{
/**
* UCEditProduct constructor comment.
*/
public UCEditProduct(org.apache.ojb.broker.PersistenceBroker b)
{
super(b);
}
/** perform this use case*/
public void apply()
{
String in = readLineWithMessage("Edit Product with id:");
int id = Integer.parseInt(in);
// We don't have a reference to the selected Product.
// So first we have to lookup the object,
// we do this by a query by example (QBE):
// 1. build an example object with matching primary key values:
Product example = new Product();
example.setId(id);
// 2. build a QueryByIdentity from this sample instance:
Query query = new QueryByIdentity(example);
try
{
// 3. start broker transaction
broker.beginTransaction();
// 4. lookup the product specified by the QBE
Product toBeEdited = (Product) broker.getObjectByQuery(query);
// 5. edit the existing entry
System.out.println("please edit the product entry");
in = readLineWithMessage("enter name (was " + toBeEdited.getName() + "):");
toBeEdited.setName(in);
in = readLineWithMessage("enter price (was " + toBeEdited.getPrice() + "):");
toBeEdited.setPrice(Double.parseDouble(in));
in = readLineWithMessage("enter available stock (was " + toBeEdited.getStock() + "):");
toBeEdited.setStock(Integer.parseInt(in));
// 6. now ask broker to store the edited object
broker.store(toBeEdited);
// 7. commit transaction
broker.commitTransaction();
}
catch (Throwable t)
{
// rollback in case of errors
broker.abortTransaction();
t.printStackTrace();
}
}
/** get descriptive information on use case*/
public String getDescription()
{
return "Edit a product entry";
}
}
1.1 db-ojb/src/prevayler/org/apache/ojb/broker/prevayler/demo/AbstractUseCase.java
Index: AbstractUseCase.java
===================================================================
package org.apache.ojb.broker.prevayler.demo;
import org.apache.ojb.broker.PersistenceBroker;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* Insert the type's description here.
* Creation date: (04.03.2001 11:31:41)
* @author Thomas Mahler
*/
public abstract class AbstractUseCase implements UseCase
{
protected PersistenceBroker broker;
/**
* AbstractUseCase constructor comment.
*/
public AbstractUseCase(PersistenceBroker broker)
{
this.broker = broker;
}
/** perform this use case*/
public abstract void apply();
/** get descriptive information on use case*/
public abstract String getDescription();
/**
* read a single line from stdin and return as String
*/
protected String readLineWithMessage(String message)
{
System.out.print(message + " ");
try
{
BufferedReader rin = new BufferedReader(new InputStreamReader(System.in));
return rin.readLine();
}
catch (Exception e)
{
return "";
}
}
}
1.1 db-ojb/src/prevayler/org/apache/ojb/broker/prevayler/demo/Application.java
Index: Application.java
===================================================================
package org.apache.ojb.broker.prevayler.demo;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.PersistenceBrokerFactory;
import org.apache.ojb.broker.util.ui.AsciiSplash;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Vector;
/**
* The tutorial application.
* @author Thomas Mahler
*/
public class Application
{
private Vector useCases;
private PersistenceBroker broker;
/**
* Application constructor comment.
*/
public Application()
{
broker = null;
try
{
broker = PersistenceBrokerFactory.defaultPersistenceBroker();
}
catch (Throwable t)
{
t.printStackTrace();
}
useCases = new Vector();
useCases.add(new UCListAllProducts(broker));
useCases.add(new UCEnterNewProduct(broker));
useCases.add(new UCEditProduct(broker));
useCases.add(new UCDeleteProduct(broker));
useCases.add(new UCQuitApplication(broker));
}
/**
* Disply available use cases.
*/
public void displayUseCases()
{
System.out.println();
for (int i = 0; i < useCases.size(); i++)
{
System.out.println("[" + i + "] " + ((UseCase) useCases.get(i)).getDescription());
}
}
/**
* Insert the method's description here.
* Creation date: (04.03.2001 10:40:25)
* @param args java.lang.String[]
*/
public static void main(String[] args)
{
Application app = new Application();
app.run();
}
/**
* read a single line from stdin and return as String
*/
private String readLine()
{
try
{
BufferedReader rin = new BufferedReader(new InputStreamReader(System.in));
return rin.readLine();
}
catch (Exception e)
{
return "";
}
}
/**
* the applications main loop.
*/
public void run()
{
System.out.println(AsciiSplash.getSplashArt());
System.out.println("Welcome to the OJB PB tutorial application");
System.out.println();
// never stop (there is a special use case to quit the application)
while (true)
{
try
{
// select a use case and perform it
UseCase uc = selectUseCase();
uc.apply();
}
catch (Throwable t)
{
broker.close();
System.out.println(t.getMessage());
}
}
}
/**
* select a use case.
*/
public UseCase selectUseCase()
{
displayUseCases();
System.out.println("type in number to select a use case");
String in = readLine();
int index = Integer.parseInt(in);
return (UseCase) useCases.get(index);
}
}
1.1 db-ojb/src/prevayler/org/apache/ojb/broker/prevayler/demo/UCListAllProducts.java
Index: UCListAllProducts.java
===================================================================
package org.apache.ojb.broker.prevayler.demo;
import org.apache.ojb.broker.query.Query;
import org.apache.ojb.broker.query.QueryByCriteria;
import org.apache.ojb.tutorial1.Product;
import java.util.Collection;
/**
* Insert the type's description here.
* Creation date: (04.03.2001 10:34:15)
* @author Thomas Mahler
*/
public class UCListAllProducts extends AbstractUseCase
{
/**
* UCEnterNewProduct constructor comment.
*/
public UCListAllProducts(org.apache.ojb.broker.PersistenceBroker b)
{
super(b);
}
/** perform this use case*/
public void apply()
{
System.out.println("The list of available products:");
// build a query that select all objects of Class Product, without any further criteria
// according to ODMG the Collection containing all instances of a persistent class is called "Extent"
Query query = new QueryByCriteria(Product.class, null);
try
{
// ask the broker to retrieve the Extent collection
Collection allProducts = broker.getCollectionByQuery(query);
// now iterate over the result to print each product
java.util.Iterator iter = allProducts.iterator();
while (iter.hasNext())
{
System.out.println(iter.next());
}
}
catch (Throwable t)
{
t.printStackTrace();
}
}
/** get descriptive information on use case*/
public String getDescription()
{
return "List all product entries";
}
}
1.1 db-ojb/src/prevayler/org/apache/ojb/broker/prevayler/demo/UseCase.java
Index: UseCase.java
===================================================================
package org.apache.ojb.broker.prevayler.demo;
/**
* Interface for application use cases.
* @author Thomas Mahler
*/
public interface UseCase
{
/** perform this use case*/
public abstract void apply();
/** get descriptive information on use case*/
public abstract String getDescription();
}
1.1 db-ojb/src/prevayler/org/apache/ojb/broker/prevayler/demo/UCDeleteProduct.java
Index: UCDeleteProduct.java
===================================================================
package org.apache.ojb.broker.prevayler.demo;
import org.apache.ojb.broker.query.Query;
import org.apache.ojb.broker.query.QueryByIdentity;
import org.apache.ojb.tutorial1.Product;
/**
* Insert the type's description here.
* Creation date: (04.03.2001 10:34:15)
* @author Thomas Mahler
*/
public class UCDeleteProduct extends AbstractUseCase
{
/**
* UCEnterNewProduct constructor comment.
*/
public UCDeleteProduct(org.apache.ojb.broker.PersistenceBroker b)
{
super(b);
}
/** perform this use case*/
public void apply()
{
String in = readLineWithMessage("Delete Product with id:");
int id = Integer.parseInt(in);
// We don't have a reference to the selected Product.
// So first we have to lookup the object,
// we do this by a query by example (QBE):
// 1. build an example object with matching primary key values:
Product example = new Product();
example.setId(id);
// 2. build a QueryByIdentity from this sample instance:
Query query = new QueryByIdentity(example);
try
{
// start broker transaction
broker.beginTransaction();
// lookup the product specified by the QBE
Product toBeDeleted = (Product) broker.getObjectByQuery(query);
// now ask broker to delete the object
broker.delete(toBeDeleted);
// commit transaction
broker.commitTransaction();
}
catch (Throwable t)
{
// rollback in case of errors
broker.abortTransaction();
t.printStackTrace();
}
}
/** get descriptive information on use case*/
public String getDescription()
{
return "Delete a product entry";
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org