package garden.delights.earthly.imageserver.persistence;

import static net.aequologica.neo.geppaequo.persistence.DbUtils.getEntityManagerFactory;

import java.io.Closeable;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.servlet.ServletException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Persistor implements Closeable {

    @SuppressWarnings("unused")
    private final static Logger log = LoggerFactory.getLogger(Persistor.class);
    
    final private EntityManagerFactory emf;
    
    public Persistor() {
        this.emf = getEntityManagerFactory("buildhubPersist");
        
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void close() throws IOException {
        if (emf != null && emf.isOpen()) {
            this.emf.close();
        }
    }

    public List<Point> get() throws SQLException, IOException {
        EntityManager em = emf.createEntityManager();
        try {
            @SuppressWarnings("unchecked")
            List<Point> resultList = em.createNamedQuery("AllPoints").getResultList();
            return resultList;
        } finally {
            em.close();
        }
    }

    public long getCount() {
        EntityManager em = emf.createEntityManager();
        try {
            Object pointCount = em.createNamedQuery("PointsCount").getSingleResult();
            if (pointCount instanceof Number) {
                return ((Number)pointCount).longValue();
            } else {
                return Long.valueOf(pointCount.toString());
            }
        } finally {
            em.close();
        }
    }
    
    public void store(long x, long y) throws ServletException, IOException, SQLException {
        EntityManager em = emf.createEntityManager();
        try {
            Point person = new Point();
            person.setX(x);
            person.setY(y);
            em.getTransaction().begin();
            em.persist(person);
            em.getTransaction().commit();
        } finally {
            em.close();
        }
    }
    
    public int deleteAll() throws SQLException, IOException {
        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            int theNumberOfEntitiesDeleted = em.createNamedQuery("DeleteAllPoints").executeUpdate();
            em.getTransaction().commit();
            return theNumberOfEntitiesDeleted;
        } finally {
            em.close();
        }
    }

    
    
}

@@ -739,307 +739,8 @@
 %22);%0A
-        %0A        Runtime.getRuntime().addShutdownHook(new Thread() %7B%0A            @Override%0A            public void run() %7B%0A                try %7B%0A                    close();%0A                %7D catch (Exception e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A        %7D);%0A
     

remove this hook