package com.acme.model.domain.sc.sessionbean;


import com.acme.model.domain.sc.dao.SCDao;
import com.acme.model.domain.sc.entity.Section;
import com.acme.model.domain.sc.entity.Service;
import com.acme.structure.GridData;
import com.acme.structure.PerformanceMonitor;
import com.acme.structure.ProfileInterceptor;
import com.acme.structure.ServerResponse;
import com.acme.structure.dao.MainDao;

import java.util.Collection;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import javax.interceptor.Interceptors;

import javax.persistence.EntityManager;
import javax.persistence.FlushModeType;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;


@Stateless(name = "SCManager", mappedName = "ejb/SCManager/remote")
@TransactionManagement(TransactionManagementType.CONTAINER)
@Interceptors(value = { PerformanceMonitor.class, ProfileInterceptor.class })
@Remote(SCManager.class)
public class SCManagerBean implements SCManager, SCManagerLocal
{
    @PersistenceContext(unitName = "restmatic")
    private EntityManager manager;

    @Resource
    SessionContext ctx;

    private Logger logger = Logger.getLogger(this.getClass());

    MainDao dao = null;
    SCDao scDao = null;

    @PostConstruct
    public void initialized()
    {
        dao = new MainDao();
        scDao = new SCDao();

        dao.setEJBContext(ctx);
    }

    public Collection findServicesForList()
    {
        manager.setFlushMode(FlushModeType.COMMIT);
        return scDao.findServicesForList(manager);
    }

    public Collection findSections()
    {
        manager.setFlushMode(FlushModeType.COMMIT);
        return dao.getEntityList(manager, new Section());
    }

    public ServerResponse synchronizeSections(GridData gridData) throws Exception
    {
        manager.setFlushMode(FlushModeType.COMMIT);
        ServerResponse response = null;

        try
        {
            response = dao.synchronizeEntities(manager, gridData, false);
        }
        catch (Exception exc)
        {
            ctx.setRollbackOnly();
            throw exc;
        }

        return response;
    }

    public ServerResponse synchronizeServiceGroups(GridData gridData) throws Exception
    {
        manager.setFlushMode(FlushModeType.COMMIT);
        ServerResponse response = null;

        try
        {
            response = dao.synchronizeEntities(manager, gridData, false);
        }
        catch (Exception exc)
        {
            ctx.setRollbackOnly();
            throw exc;
        }

        return response;
    }

    public Collection findServiceGroups()
    {
        manager.setFlushMode(FlushModeType.COMMIT);
        return dao.getEntityList(manager, new Section());
    }

    public Collection findServices()
    {
        manager.setFlushMode(FlushModeType.COMMIT);
        return dao.getEntityList(manager, new Service());
    }

    public ServerResponse synchronizeServices(GridData gridData) throws Exception
    {
        manager.setFlushMode(FlushModeType.COMMIT);
        ServerResponse response = null;

        try
        {
            response = dao.synchronizeEntities(manager, gridData, false);
        }
        catch (Exception exc)
        {
            ctx.setRollbackOnly();
            throw exc;
        }

        return response;
    }

    public Collection findServiceGroupsForList()
    {
        manager.setFlushMode(FlushModeType.COMMIT);
        return scDao.findServiceGroupsForList(manager);
    }

    public Collection findTablesForList()
    {
        manager.setFlushMode(FlushModeType.COMMIT);
        return scDao.findTablesForList(manager);
    }
}
