View Javadoc
1   package ejava.projects.edmv.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 ejava.projects.edmv.bl.PersonMgmt;
11  import ejava.projects.edmv.bl.VehicleMgmt;
12  import ejava.projects.edmv.bo.Person;
13  import ejava.projects.edmv.bo.VehicleRegistration;
14  import ejava.projects.edmv.jpa.JPAPersonDAO;
15  import ejava.projects.edmv.jpa.JPAVehicleDAO;
16  
17  /**
18   * This class provides an example wrapper of the business logic to show
19   * how the business logic cab be wrapped in a main to be run outside
20   * of JUnit.
21   */
22  public class EDmvMgmtCommand extends EDmvIngestCommand {
23  	private static String command = System.getProperty("command");
24  	private static String indexStr = System.getProperty("index","0");
25  	private static String countStr = System.getProperty("count","1");
26  	private static PrintStream out = System.out;
27  	
28  	private static EntityManagerFactory getEMF() {
29  		return Persistence.createEntityManagerFactory("eDmvBO-test");
30  	}
31  	
32  	private static void invoke(
33  	        PersonMgmt personMgmt, VehicleMgmt vehicleMgmt) throws Exception {
34  		if (command == null) {
35  			throw new Exception("command not supplied");
36  		}
37  		else if ("listPeople".equals(command)) {
38  			listPeople(personMgmt);
39  		}
40          else if ("listRegistrations".equals(command)) {
41              listRegistrations(vehicleMgmt);
42          }
43  		else {
44  			throw new Exception("unknown command:" + command);
45  		}
46  	}
47  	
48  	private static void listPeople(PersonMgmt personMgmt) 
49  	    throws Exception {
50  		
51  		int index = Integer.parseInt(indexStr);
52  		int count = Integer.parseInt(countStr);
53  		
54  		for (Person person : personMgmt.getPeople(index, count)) {
55  	        out.println("person:" + person);	    	
56  		}
57  	}
58  	
59      private static void listRegistrations(VehicleMgmt vehicleMgmt) 
60          throws Exception {
61          
62          int index = Integer.parseInt(indexStr);
63          int count = Integer.parseInt(countStr);
64          
65          for (VehicleRegistration reg : 
66              vehicleMgmt.getRegistrations(index, count)) {
67              out.println("registration:" + reg);            
68          }
69      }
70  
71  	public static void main(String args[]) {		
72  		try {
73  			EntityManagerFactory emf = null;
74  			EntityManager em = null;
75  			try {
76  				emf = getEMF();
77  				em = emf.createEntityManager();
78  				JPAPersonDAO personDAO = new JPAPersonDAO();
79  				personDAO.setEntityManager(em);
80  				JPAVehicleDAO vehicleDAO = new JPAVehicleDAO();
81  				vehicleDAO.setEntityManager(em);
82  				
83  			    EDmvMgmtImpl dmvMgmt = new EDmvMgmtImpl();
84  			    dmvMgmt.setPersonDAO(personDAO);
85  			    dmvMgmt.setVehicleDAO(vehicleDAO);
86  			    em.getTransaction().begin();
87  			    
88  			    invoke(dmvMgmt, dmvMgmt); //same object implements both ifaces
89  			}
90  			finally {
91  				if (em != null) {
92  					EntityTransaction tx = em.getTransaction();
93  					if (tx.getRollbackOnly()) { tx.rollback(); }
94  					else                      { tx.commit(); }
95   				}
96  				if (emf != null) { emf.close(); }				
97  			}
98  		}
99  		catch (Exception ex) {
100 			ex.printStackTrace();
101 			System.exit(-1);
102 		}
103 	}
104 }