1 package org.apache.ojb.tutorial1; 2 3 17 18 import org.apache.ojb.broker.PersistenceBroker; 19 import org.apache.ojb.broker.query.Query; 20 import org.apache.ojb.broker.query.QueryByIdentity; 21 22 25 public class UCEditProduct extends AbstractUseCase 26 { 27 32 public UCEditProduct(PersistenceBroker broker) 33 { 34 super(broker); 35 } 36 37 42 public String getDescription() 43 { 44 return "Edit a product entry"; 45 } 46 47 50 public void apply() 51 { 52 String in = readLineWithMessage("Edit Product with id:"); 53 int id = Integer.parseInt(in); 54 55 59 Product example = new Product(); 61 62 example.setId(id); 63 64 Query query = new QueryByIdentity(example); 66 67 try 68 { 69 broker.beginTransaction(); 71 72 Product toBeEdited = (Product) broker.getObjectByQuery(query); 74 75 System.out.println("please edit the product entry"); 77 in = readLineWithMessage("enter name (was " + toBeEdited.getName() + "):"); 78 toBeEdited.setName(in); 79 in = readLineWithMessage("enter price (was " + toBeEdited.getPrice() + "):"); 80 toBeEdited.setPrice(Double.parseDouble(in)); 81 in = readLineWithMessage("enter available stock (was " + toBeEdited.getStock() + "):"); 82 toBeEdited.setStock(Integer.parseInt(in)); 83 84 broker.store(toBeEdited); 86 broker.commitTransaction(); 88 } 89 catch (Throwable t) 90 { 91 broker.abortTransaction(); 93 t.printStackTrace(); 94 } 95 } 96 } 97 | Popular Tags |