View Javadoc

1   package org.minetti.astrodevice.server.hibernate.dao;
2   
3   import javax.persistence.EntityManagerFactory;
4   import org.minetti.astrodevice.server.core.dao.DaoSession;
5   import org.minetti.astrodevice.server.core.dao.DaoTransaction;
6   import org.minetti.astrodevice.server.core.dao.DeviceSpecificationDao;
7   import org.minetti.astrodevice.server.core.dao.ObservingSiteDao;
8   
9   /**
10   * DAO factory used by the {@link DaoModuleImpl} class.
11   * @author Jean-Philippe MINETTI
12   */
13  public final class DaoFactory {
14  
15  	/**
16  	 * Entity manager factory.
17  	 */
18  	private EntityManagerFactory factory = null;
19  
20  	/**
21  	 * Constructor.
22  	 * @param factory Entity manager factory.
23  	 */
24  	public DaoFactory (final EntityManagerFactory factory) {
25  		super();
26  		this.factory = factory;
27  	}
28  
29  	/**
30  	 * Returns the session used for the DAO.
31  	 * @return Session used for the DAO.
32  	 */
33  	public DaoSession newDaoSession () {
34  		return new DaoSessionImpl(this.factory.createEntityManager());
35  	}
36  
37  	/**
38  	 * Creates and returns a new instance of transaction used to group multiple changing by DAO.
39  	 * @param session Session used for the DAO.
40  	 * @return Transaction used to group multiple changing by DAO.
41  	 */
42  	public DaoTransaction newDaoTransaction (final DaoSession session) {
43  		return new DaoTransactionImpl(((DaoSessionImpl) session).getEntityManager().getTransaction());
44  	}
45  
46  	/**
47  	 * Creates and returns a new instance of DAO for observing sites.
48  	 * @param session Session used for the DAO.
49  	 * @return DAO for observing sites.
50  	 */
51  	public ObservingSiteDao newObservingSiteDao (final DaoSession session) {
52  		return new ObservingSiteDaoImpl(((DaoSessionImpl) session).getEntityManager());
53  	}
54  
55  	/**
56  	 * Creates and returns a new instance of DAO for specifications on devices.
57  	 * @param session Session used for the DAO.
58  	 * @return DAO for specifications on devices.
59  	 */
60  	public DeviceSpecificationDao newDeviceSpecificationDao (final DaoSession session) {
61  		return new DeviceSpecificationDaoImpl(((DaoSessionImpl) session).getEntityManager());
62  	}
63  
64  	/**
65  	 * Closes the factory.
66  	 */
67  	public void close () {
68  		this.factory.close();
69  	}
70  
71  	/*
72  	 * (non-Javadoc)
73  	 * @see java.lang.Object#toString()
74  	 */
75  	@Override
76  	public String toString () {
77  		return "DaoFactory[" + this.factory + "]";
78  	}
79  
80  }