/**
*  Copyright (C) 2014,2015 Tomasz Bosak.
**/
package pl.tobo.ISS.utils;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;

import pl.tobo.ISS.dao.ContentDao;
import pl.tobo.ISS.dao.ScreenDao;
import pl.tobo.ISS.dao.SettingDao;
import pl.tobo.ISS.dao.TagDao;
import pl.tobo.ISS.dao.UserDao;
import pl.tobo.ISS.entities.GlobalSetting;

@WebListener
public class DBInitializer implements ServletRequestListener {
	
	Logger logger = Logger.getLogger("pl.tobo.ISS");
	
	@Override
	public void requestDestroyed(ServletRequestEvent arg0) {

	}

	@Override
	public void requestInitialized(ServletRequestEvent arg0) {
		EntityManager em = DBConfig.getEntityManager();
		UserDao userDAO = new UserDao(em);
		TagDao tagDAO = new TagDao(em);
		ScreenDao screenDAO = new ScreenDao(em);
		ContentDao contentDAO = new ContentDao(em);
		SettingDao settingDAO = new SettingDao(em);

		ServletRequest req = arg0.getServletRequest();
		req.setAttribute(StringConstants.REQUEST_ATTR_USER_DAO, userDAO);
		req.setAttribute(StringConstants.REQUEST_ATTR_TAG_DAO, tagDAO);
		req.setAttribute(StringConstants.REQUEST_ATTR_SCREEN_DAO, screenDAO);
		req.setAttribute(StringConstants.REQUEST_ATTR_CONTENT_DAO, contentDAO);
		req.setAttribute(StringConstants.REQUEST_ATTR_SETTING_DAO, settingDAO);

		// TODO: move this into session (?) performance issues
		if(req.getAttribute("registrationDisabled") == null && settingDAO != null){
			GlobalSetting registrationDisabled = settingDAO.getGlobalSettingByKey(StringConstants.SETTING_REGISTRATION_DISABLED);
			if (registrationDisabled != null){
				
				logger.log(Level.INFO, "Registration disabled: "+registrationDisabled.getValue());
				if("TRUE".equals(registrationDisabled.getValue())){
					req.setAttribute("registrationDisabled", "TRUE");
				}
			}
		}

	}
	
}

@@ -784,16 +784,218 @@
 rg0) %7B%0D%0A
+%09%09ServletRequest req = arg0.getServletRequest();%0D%0A%09%09EntityManager em = (EntityManager) req.getAttribute(StringConstants.REQUEST_ATTR_ENTITY_MANAGER);%0D%0A%09%09if(em!=null && em.isOpen())%7B%0D%0A%09%09%09em.close();%0D%0A%09%09%7D
 %0D%0A%09%7D%0D%0A%0D%0A
@@ -1744,531 +1744,76 @@
 );%0D%0A
-%0D%0A%09%09// TODO: move this into session (?) performance issues%0D%0A%09%09if(req.getAttribute(%22registrationDisabled%22) == null && settingDAO != null)%7B%0D%0A%09%09%09GlobalSetting registrationDisabled = settingDAO.getGlobalSettingByKey(StringConstants.SETTING_REGISTRATION_DISABLED);%0D%0A%09%09%09if (registrationDisabled != null)%7B%0D%0A%09%09%09%09%0D%0A%09%09%09%09logger.log(Level.INFO, %22Registration disabled: %22+registrationDisabled.getValue());%0D%0A%09%09%09%09if(%22TRUE%22.equals(registrationDisabled.getValue()))%7B%0D%0A%09%09%09%09%09req.setAttribute(%22registrationDisabled%22, %22TRUE%22);%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%09%09%7D
+%09%09req.setAttribute(StringConstants.REQUEST_ATTR_ENTITY_MANAGER, em);
 %0D%0A%0D%0A

add closing of Entity Manager