View Javadoc

1   /*
2    * HibernatePersistenceFactory.java
3    *
4    * Created on June 15, 2005, 4:42 PM
5    *
6    * To change this template, choose Tools | Options and locate the template under
7    * the Source Creation and Management node. Right-click the template and choose
8    * Open. You can then make changes to the template in the Source Editor.
9    */
10  
11  package gov.bnl.gums.persistence;
12  import gov.bnl.gums.GridUser;
13  import gov.bnl.gums.configuration.Configuration;
14  import gov.bnl.gums.configuration.ConfigurationToolkit;
15  import gov.bnl.gums.userGroup.ManualUserGroup;
16  
17  import java.util.Collection;
18  import java.util.Properties;
19  
20  import javax.persistence.EntityManager;
21  import javax.persistence.EntityManagerFactory;
22  import javax.persistence.FlushModeType;
23  import javax.persistence.NoResultException;
24  import javax.persistence.Persistence;
25  
26  import org.apache.log4j.Logger;
27  import org.hibernate.*;
28  
29  
30  /**
31   *
32   * @author Gabriele Carcassi, Jay Packard
33   */
34  public class HibernatePersistenceEngine implements PersistenceEngine {
35  	private Logger log = Logger.getLogger(HibernatePersistenceEngine.class);
36  	protected EntityManagerFactory entityManagerFactory;
37  	protected ThreadLocal<EntityManager> entityManager = new ThreadLocal<EntityManager>();
38  	protected SessionFactory sessions;
39  	protected Properties properties;
40  
41  	/**
42  	 * Create a new hibernate persistence factory.  This empty constructor is needed by the XML Digester.
43  	 */
44  	public HibernatePersistenceEngine(Properties properties) {
45  		this.properties = properties;
46  		log.trace("HibernatePersistenceFactory instanciated");
47  		entityManagerFactory = Persistence.createEntityManagerFactory("gums", properties);
48  	}
49  
50  	public boolean contains(Object obj) {
51  		return getEntityManager().contains(obj);
52  	}
53  
54  	public void finalize() {
55  		try {
56  			if (entityManager!=null) {
57  				log.trace("Closing entity manager...");
58  				getEntityManager().close();
59  				log.trace("Closed entity manager.");
60  				entityManager = null;
61  			}
62  		} catch (HibernateException e) {
63  			log.error("Couldn't close hibernate sessions: " + e.getMessage());
64  		}
65  	}
66  
67  	public void beginTransaction() {
68  		if (!getEntityManager().getTransaction().isActive()) {
69  			getEntityManager().getTransaction().begin();
70  		}
71  	}
72  
73  	public void commit() {
74  		getEntityManager().getTransaction().commit();
75  	}
76  	
77  	public void rollback() {
78  		getEntityManager().getTransaction().rollback();
79  	}
80  
81  	public void removeBackupConfiguration(String name) {
82  		getEntityManager().createQuery("removeBackupConfiguration").setParameter("name", name);
83  	}
84  
85  	@SuppressWarnings("unchecked")
86  	public Collection<String> retrieveBackupConfigurationNames() {
87  		javax.persistence.Query q = getEntityManager().createQuery("select c.name from Configuration as c where c.name = null");
88  		return (Collection<String>)q.getResultList();
89  	}
90  
91  	public Configuration retrieveCurrentConfiguration() {
92  		EntityManager entityManager = getEntityManager();
93  		javax.persistence.Query q = entityManager.createQuery("from Configuration c where c.name = null");
94  		Configuration conf;
95  		try {
96  			conf = (Configuration)q.getSingleResult();
97  		} catch (NoResultException e) {
98  			conf = new Configuration();
99  			
100 	    	// add admins usergroup if needed
101 	    	if (conf.getUserGroup("admins")==null) {
102 	    		ManualUserGroup userGroup = new ManualUserGroup(conf, "admins");
103 	    		userGroup.setAccess("write");
104 	    		// TODO: remove line below
105 	    		userGroup.addMember(new GridUser("/DC=org/DC=doegrids/OU=People/CN=Jay Packard 335585"));
106 	    	}
107 
108 			// Add test user and test configuration
109 	    	if (conf.getGroupToAccountMapping("_test")==null)
110 	    		ConfigurationToolkit.insertTest(conf);
111 			
112 			entityManager.persist(conf);
113 		}
114 		return conf;
115 	}
116 
117 	public Configuration restoreConfiguration(String name) {
118 		EntityManager entityManager = getEntityManager();
119 
120 		// retrieve backup configuration
121 		javax.persistence.Query q = entityManager.createQuery("from Configuration where name = :name").setParameter("name", name);
122 		Configuration conf = (Configuration)q.getSingleResult();
123 		
124 		// clone
125 		conf = (Configuration)conf.clone();
126 		
127 		// remove current configuration
128 		entityManager.remove(retrieveCurrentConfiguration());
129 
130 		// set new configuration
131 		conf.setCurrent();
132 		entityManager.persist(conf);
133 		
134 		return conf;
135 	}
136 
137 	public void setConfiguration(Configuration conf) {
138 		Configuration currentConfig = retrieveCurrentConfiguration();
139 		if (currentConfig != null)
140 			getEntityManager().remove(currentConfig);
141 		getEntityManager().persist(conf);
142 	}
143 
144 	protected EntityManager getEntityManager() {
145 		if (entityManager.get() == null) {
146 			EntityManager em = entityManagerFactory.createEntityManager();
147 			em.setFlushMode(FlushModeType.COMMIT);
148 			entityManager.set(em);
149 		}
150 
151 		return entityManager.get();
152 	}
153 
154 }