1 package ejava.projects.esales.blimpl; 2 3 import java.io.PrintStream; 4 5 import javax.persistence.EntityManager; 6 import javax.persistence.EntityManagerFactory; 7 import javax.persistence.EntityTransaction; 8 import javax.persistence.Persistence; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 13 import ejava.projects.esales.bl.AccountMgmt; 14 import ejava.projects.esales.bo.Account; 15 import ejava.projects.esales.jpa.JPAAccountDAO; 16 17 public class AccountMgmtCommand extends ESalesIngestCommand { 18 @SuppressWarnings("unused") 19 private static final Log log = 20 LogFactory.getLog(AccountMgmtCommand.class); 21 private static String command = System.getProperty("command"); 22 private static String indexStr = System.getProperty("index","0"); 23 private static String countStr = System.getProperty("count","1"); 24 private static PrintStream out = System.out; 25 26 private static EntityManagerFactory getEMF() { 27 return Persistence.createEntityManagerFactory("eSalesBO"); 28 } 29 30 private static void invoke(AccountMgmt accountMgmt) throws Exception { 31 if (command == null) { 32 throw new Exception("command not supplied"); 33 } 34 else if ("listAccounts".equals(command)) { 35 listAccounts(accountMgmt); 36 } 37 else { 38 throw new Exception("unknown command:" + command); 39 } 40 } 41 42 private static void listAccounts(AccountMgmt accountMgmt) 43 throws Exception { 44 45 int index = Integer.parseInt(indexStr); 46 int count = Integer.parseInt(countStr); 47 48 for (Account account : accountMgmt.getAccounts(index, count)) { 49 out.println("account:" + account); 50 } 51 } 52 53 public static void main(String args[]) { 54 try { 55 EntityManagerFactory emf = null; 56 EntityManager em = null; 57 try { 58 emf = getEMF(); 59 em = emf.createEntityManager(); 60 JPAAccountDAO accountDAO = new JPAAccountDAO(); 61 accountDAO.setEntityManager(em); 62 63 AccountMgmtImpl accountMgmt = new AccountMgmtImpl(); 64 accountMgmt.setAccountDAO(accountDAO); 65 em.getTransaction().begin(); 66 67 invoke(accountMgmt); 68 } 69 finally { 70 if (em != null) { 71 EntityTransaction tx = em.getTransaction(); 72 if (tx.getRollbackOnly()) { tx.rollback(); } 73 else { tx.commit(); } 74 } 75 if (emf != null) { emf.close(); } 76 } 77 } 78 catch (Exception ex) { 79 ex.printStackTrace(); 80 System.exit(-1); 81 } 82 } 83 }