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.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
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 Logger log = LoggerFactory.getLogger(AccountMgmtCommand.class);
20 private static String command = System.getProperty("command");
21 private static String indexStr = System.getProperty("index","0");
22 private static String countStr = System.getProperty("count","1");
23 private static PrintStream out = System.out;
24
25 private static EntityManagerFactory getEMF() {
26 return Persistence.createEntityManagerFactory("eSalesBO");
27 }
28
29 private static void invoke(AccountMgmt accountMgmt) throws Exception {
30 if (command == null) {
31 throw new Exception("command not supplied");
32 }
33 else if ("listAccounts".equals(command)) {
34 listAccounts(accountMgmt);
35 }
36 else {
37 throw new Exception("unknown command:" + command);
38 }
39 }
40
41 private static void listAccounts(AccountMgmt accountMgmt)
42 throws Exception {
43
44 int index = Integer.parseInt(indexStr);
45 int count = Integer.parseInt(countStr);
46
47 for (Account account : accountMgmt.getAccounts(index, count)) {
48 out.println("account:" + account);
49 }
50 }
51
52 public static void main(String args[]) {
53 try {
54 EntityManagerFactory emf = null;
55 EntityManager em = null;
56 try {
57 emf = getEMF();
58 em = emf.createEntityManager();
59 JPAAccountDAO accountDAO = new JPAAccountDAO();
60 accountDAO.setEntityManager(em);
61
62 AccountMgmtImpl accountMgmt = new AccountMgmtImpl();
63 accountMgmt.setAccountDAO(accountDAO);
64 em.getTransaction().begin();
65
66 invoke(accountMgmt);
67 }
68 finally {
69 if (em != null) {
70 EntityTransaction tx = em.getTransaction();
71 if (tx.getRollbackOnly()) { tx.rollback(); }
72 else { tx.commit(); }
73 }
74 if (emf != null) { emf.close(); }
75 }
76 }
77 catch (Exception ex) {
78 ex.printStackTrace();
79 System.exit(-1);
80 }
81 }
82 }