/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */

package com.icesoft.sample.accessible.dataTable;

import com.icesoft.sample.accessible.service.Employee;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;
import javax.faces.event.PhaseListener;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.context.ExternalContext;
import com.icesoft.components.accessible.datapaginator.DataPaginator ;
import javax.faces.context.FacesContext;
/**
 *
 */
@ManagedBean
@ApplicationScoped
public class SortHeaderModel extends DataTableBean implements PhaseListener {

    private static final Log logger =
            LogFactory.getLog(SortHeaderModel.class);

    private boolean descending = true;
    private boolean oldDescending = descending;
    private String columnName = Employee.DEPARTMENT_NAME_COLUMN;
    private String oldColumnName = columnName;
    private DataPaginator dataPaginatorBinding;

	public DataPaginator getDataPaginatorBinding() { return this.dataPaginatorBinding; }
	public void setDataPaginatorBinding(DataPaginator dataPaginatorBinding) { this.dataPaginatorBinding = dataPaginatorBinding; }

    /**
     * <p>Handle a notification that the processing for a particular
     * phase of the request processing lifecycle is about to begin.</p>
     * <p>Checks if data model flags are dirty and call service layer to
     * refresh data if necessary.</p>
     */
    public void beforePhase(PhaseEvent event) {
        FacesContext fc = FacesContext.getCurrentInstance();
        ELContext elc = fc.getELContext();
        ExpressionFactory ef = fc.getApplication().getExpressionFactory();
        ValueExpression ve = ef.createValueExpression(elc, "#{sortHeaderModel}", Object.class);
        SortHeaderModel sortHeaderModel = (SortHeaderModel)ve.getValue(elc);
        if ((sortHeaderModel.descending != sortHeaderModel.oldDescending) ||
                (!sortHeaderModel.columnName.equals(sortHeaderModel.oldColumnName))){
            sortHeaderModel.init();
            logger.debug("SortHeaderModel - dataRefresh ");
            // reset dirty flags.
            sortHeaderModel.oldDescending = sortHeaderModel.descending;
            sortHeaderModel.oldColumnName = sortHeaderModel.columnName;
        }
    }

    /**
     * <p>Handle a notification that the processing for a particular
     * phase has just been completed.</p>
     */
    public void afterPhase(PhaseEvent event) {}

    /**
     * <p>Return the identifier of the request processing phase during
     * which this listener is interested in processing {@link javax.faces.event.PhaseEvent}
     * events.  Legal values are the singleton instances defined by the
     * {@link javax.faces.event.PhaseId} class, including <code>PhaseId.ANY_PHASE</code>
     * to indicate an interest in being notified for all standard phases.</p>
     */
    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }

    protected void init() {
        // build employee list form employee service.
        employees = employeeService.getEmployees(58, descending,
                columnName);
    }

    public boolean isDescending() {
        return descending;
    }

    public void setDescending(boolean descending) {
        oldDescending = this.descending;
        this.descending = descending;
    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        oldColumnName = this.columnName;
        this.columnName = columnName;
    }
}