package lemming.api.data;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class EntityManagerListener implements ServletContextListener {

    private static EntityManagerFactory entityManagerFactory;

    public void contextInitialized(ServletContextEvent event) {
        entityManagerFactory = Persistence.createEntityManagerFactory("persistence-unit");
    }

    public void contextDestroyed(ServletContextEvent event) {
        entityManagerFactory.close();
    }

    public static EntityManager createEntityManager() {
        if (entityManagerFactory == null) {
            throw new IllegalStateException("Context is not yet initialized.");
        }

        return entityManagerFactory.createEntityManager();
    }
}

@@ -278,16 +278,110 @@
 tener;%0A%0A
+/**%0A * Listener for receiving notification events about ServletContext lifecycle changes.%0A */%0A
 @WebList
@@ -456,16 +456,66 @@
 tener %7B%0A
+    /**%0A     * The entity manager factory.%0A     */
 %0A    pri
@@ -570,16 +570,182 @@
 ctory;%0A%0A
+    /**%0A     * Initializes the entity manager factory.%0A     *%0A     * @param servletContextEvent event containing the ServletContext that is being initialized%0A     */%0A
     publ
@@ -783,33 +783,47 @@
 letContextEvent 
-e
+servletContextE
 vent) %7B%0A        
@@ -904,32 +904,191 @@
 -unit%22);%0A    %7D%0A%0A
+    /**%0A     * Closes the entity manager factory.%0A     *%0A     * @param servletContextEvent event containing the ServletContext that is being destroyed%0A     */%0A
     public void 
@@ -1120,25 +1120,39 @@
 ontextEvent 
-e
+servletContextE
 vent) %7B%0A    
@@ -1188,24 +1188,115 @@
 e();%0A    %7D%0A%0A
+    /**%0A     * Creates an entity manager.%0A     *%0A     * @return An entity manager.%0A     */%0A
     public s

Add comments to EntityManagerListener.