1 package org.apache.ojb.tutorial2; 2 3 17 18 import java.util.List ; 19 20 import org.odmg.Implementation; 21 import org.odmg.Transaction; 22 import org.odmg.OQLQuery; 23 24 27 public class UCEditProduct extends AbstractUseCase 28 { 29 34 public UCEditProduct(Implementation odmg) 35 { 36 super(odmg); 37 } 38 39 44 public String getDescription() 45 { 46 return "Edit a product entry"; 47 } 48 49 52 public void apply() 53 { 54 String in = readLineWithMessage("Edit Product with id:"); 55 int id = Integer.parseInt(in); 56 57 60 String oqlQuery = "select del from " + Product.class.getName() + " where id = " + id; 62 Transaction tx = null; 63 64 try 65 { 66 tx = odmg.newTransaction(); 68 tx.begin(); 69 70 OQLQuery query = odmg.newOQLQuery(); 72 73 query.create(oqlQuery); 74 75 List result = (List )query.execute(); 76 Product toBeEdited = (Product)result.get(0); 77 78 tx.lock(toBeEdited, Transaction.WRITE); 80 81 System.out.println("please edit existing product"); 83 in = readLineWithMessage("enter name (was " + toBeEdited.getName() + "):"); 84 toBeEdited.setName(in); 85 in = readLineWithMessage("enter price (was " + toBeEdited.getPrice() + "):"); 86 toBeEdited.setPrice(Double.parseDouble(in)); 87 in = readLineWithMessage("enter available stock (was " + toBeEdited.getStock() + "):"); 88 toBeEdited.setStock(Integer.parseInt(in)); 89 90 tx.commit(); 92 } 93 catch (Throwable t) 94 { 95 tx.abort(); 97 t.printStackTrace(); 98 } 99 } 100 } 101 | Popular Tags |