package org.apache.ojb.tutorial2;
/* Copyright 2002-2004 The Apache Software Foundation
*
* 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.
*/
import org.odmg.Implementation;
import org.odmg.Transaction;
/**
* Use case for adding a new product to the database.
*/
public class UCEnterNewProduct extends AbstractUseCase
{
/**
* Creates a new enter-new use case instance.
*
* @param odmg The odmg implementation to use
*/
public UCEnterNewProduct(Implementation odmg)
{
super(odmg);
}
/**
* Returns a description of this use case.
*
* @return A description of the use case
*/
public String getDescription()
{
return "Enter a new product";
}
/**
* Performs this use case.
*/
public void apply()
{
// 1. this will be our new object
Product newProduct = new Product();
// 2. 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
// 3. open transaction
Transaction tx = odmg.newTransaction();
tx.begin();
// 4. acquire write lock on new object
tx.lock(newProduct, Transaction.WRITE);
// 5. commit transaction
tx.commit();
}
}