package com.gitrekt.resort.model.services;

import com.gitrekt.resort.hibernate.HibernateUtil;
import com.gitrekt.resort.model.entities.Guest;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

public class GuestService {

    @PersistenceContext
    private final EntityManager entityManager;

    public GuestService() {
        this.entityManager = HibernateUtil.getEntityManager();
    }

    @Override
    public void finalize() throws Throwable {
        super.finalize();
        this.entityManager.close();
    }
    
    /**
     * Forcefully closes the entityManager for this service.
     */
    public void cleanup() {
        entityManager.close();
    }

    public List<Guest> getCurrentlyCheckedInGuests() {
        String query = "FROM Guest WHERE isCheckedIn = :param";
        Query q = entityManager.createQuery(query);
        q.setParameter("param", true);
        return q.getResultList();
    }

    public Guest getGuestById(Long id) throws EntityNotFoundException {
        Guest guest = entityManager.getReference(Guest.class, id);
        return guest;
    }

    public Guest getGuestByEmailAddress(String emailAddress)
            throws EntityNotFoundException {
        String query = "FROM Guest WHERE emailAddress = :emailAddress";
        Query q = entityManager.createQuery(query);
        q.setParameter("emailAddress", emailAddress);
        return (Guest) q.getSingleResult();
    }

    public void createNewGuest(Guest guest) {
        try {
            entityManager.getTransaction().begin();
            entityManager.persist(guest);
            entityManager.getTransaction().commit();
        } catch (PersistenceException e) {
            entityManager.getTransaction().rollback();
            // TODO: Log rollback or notify user somewhere, possibly in e.
            throw e;
        }
    }

    public void updateGuest(Guest guest) {
        try {
            entityManager.getTransaction().begin();
            entityManager.merge(guest);
            entityManager.getTransaction().commit();
        } catch (PersistenceException e) {
            entityManager.getTransaction().rollback();
            // TODO: Log rollback or notify user somewhere, possibly in e.
            throw e;
        }
    }
}

@@ -129,21 +129,175 @@
 ntities.
-Guest
+Booking;%0Aimport com.gitrekt.resort.model.entities.Guest;%0Aimport java.time.LocalDate;%0Aimport java.time.ZoneId;%0Aimport java.util.ArrayList;%0Aimport java.util.Date
 ;%0Aimport
@@ -2587,10 +2587,794 @@
 %7D%0A    %7D%0A
+    %0A    public List%3CGuest%3E getDailyGuestRegistry()%7B%0A        LocalDate today = LocalDate.now();%0A        Date date = Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant());%0A        BookingService b = new BookingService();%0A        List%3CGuest%3E guests = new ArrayList();%0A        for (Booking booking : b.getBookingsBetweenDates(date, date))%7B%0A            guests.add(booking.getGuest());%0A        %7D%0A        %0A        String queryString = %22FROM Guest WHERE isCheckedIn = true%22;%0A        Query query = entityManager.createQuery(queryString);%0A        List%3CGuest%3E checkedInGuests = query.getResultList();%0A        for(Guest g : checkedInGuests) %7B%0A            if(!guests.contains(g)) %7B%0A                guests.add(g);%0A            %7D%0A        %7D%0A        %0A        return guests;%0A        %0A    %7D%0A
 %7D%0A

Add getDailyGuestRegistry