package com.acme.vcutil.bean;


import com.acme.model.domain.ejf.filter.LogFilter;
import com.acme.model.domain.ejf.sessionbean.EJFManager;
import com.acme.model.domain.ejf.vo.LogFilterTemplate;
import com.acme.structure.BaseEntity;
import com.acme.structure.GridData;
import com.acme.structure.ServiceLocator;
import com.acme.structure.util.ConversionUtil;
import com.acme.util.ui.UIHelper;
import com.acme.util.ui.WindowManager;
import com.acme.util.ui.bean.ToolbarMBean;
import com.acme.util.utilities.JSFUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Vector;

import javax.ejb.EJB;

import javax.faces.event.ActionEvent;

import oracle.adf.view.rich.component.rich.data.RichTable;


public class LogViewMBean extends ToolbarMBean
{
    @EJB
    EJFManager managerBean;
    private GridData data = new GridData();
    private Vector baseRowCollection = new Vector();
    private LogFilterTemplate filterTemplate;
    private RichTable richTable;
    private static String LOGTABLECOMPONENTID = "entityT";

    public LogViewMBean()
    {
        try
        {
            managerBean = (EJFManager)ServiceLocator.getInstance().getResource("ejb/EJFManager/remote", EJFManager.class);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void init()
    {
        filterTemplate = new LogFilterTemplate();
    }

    private void populateWindowWithData(Collection<BaseEntity> dataCollection)
    {
        Collections.sort((List<BaseEntity>)dataCollection);
        data.initializeGridData(dataCollection);
        baseRowCollection.clear();
        baseRowCollection.addAll(ConversionUtil.getTransformedBaseRowCollection(data.getDataCollection()));
        JSFUtils.refreshComponent(LOGTABLECOMPONENTID);
    }

    public void searchButtonPressed(ActionEvent event)
    {
        Collection dataCollection = new ArrayList();
        try
        {
            dataCollection = managerBean.findLogs(new LogFilter(filterTemplate));
        }
        catch (Exception exc)
        {
            UIHelper.showInformationMessages(WindowManager.getInstance().getHandler().handleException(exc));
        }
        populateWindowWithData(dataCollection);
    }

    public void setGridData(GridData data)
    {
        this.data = data;
    }

    public GridData getGridData()
    {
        return data;
    }

    public void setBaseRowCollection(Vector baseRowCollection)
    {
        this.baseRowCollection = baseRowCollection;
    }

    public Vector getBaseRowCollection()
    {
        return baseRowCollection;
    }

    public void setFilterTemplate(LogFilterTemplate filterTemplate)
    {
        this.filterTemplate = filterTemplate;
    }

    public LogFilterTemplate getFilterTemplate()
    {
        return filterTemplate;
    }

    public void setRichTable(RichTable richTable)
    {
        this.richTable = richTable;
    }

    public RichTable getRichTable()
    {
        return richTable;
    }
}
