1 package org.apache.ojb.tutorial5; 2 3 17 18 import java.util.Collection ; 19 20 import javax.jdo.PersistenceManagerFactory; 21 import javax.jdo.PersistenceManager; 22 import javax.jdo.Query; 23 24 27 public class UCEditProduct extends AbstractUseCase 28 { 29 34 public UCEditProduct(PersistenceManagerFactory factory) 35 { 36 super(factory); 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:"); 56 int id = Integer.parseInt(in); 57 PersistenceManager manager = null; 58 59 try 60 { 61 64 manager = factory.getPersistenceManager(); 66 manager.currentTransaction().begin(); 67 68 Query query = manager.newQuery(Product.class, "id == " + id); 70 71 Collection result = (Collection )query.execute(); 73 Product toBeEdited = (Product)result.iterator().next(); 74 75 if (toBeEdited == null) 76 { 77 System.out.println("did not find a matching instance..."); 78 manager.currentTransaction().rollback(); 79 return; 80 } 81 82 System.out.println("please edit the product entry"); 84 in = readLineWithMessage("enter name (was " + toBeEdited.getName() + "):"); 85 toBeEdited.setName(in); 86 in = readLineWithMessage("enter price (was " + toBeEdited.getPrice() + "):"); 87 toBeEdited.setPrice(Double.parseDouble(in)); 88 in = readLineWithMessage("enter available stock (was "+ toBeEdited.getStock()+ "):"); 89 toBeEdited.setStock(Integer.parseInt(in)); 90 91 manager.currentTransaction().commit(); 93 } 94 catch (Throwable t) 95 { 96 manager.currentTransaction().rollback(); 98 t.printStackTrace(); 99 } 100 finally 101 { 102 manager.close(); 103 } 104 } 105 } 106 | Popular Tags |