/*
 * 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.faces.facelets.component.schedule;

import com.icesoft.faces.facelets.component.schedule.common.CalendarViewModel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.faces.event.ActionEvent;
import java.io.Serializable;

/**
 * Abstract Calendar view controller class. Each of the different calendar
 * view controllers (day, week, month, months) implement this base class.
 * <b>Note:</b> the CalendarViewService is set via bean chaining in the faces
 * config.
 *
 * @author ICEfaces Technologies Inc.
 */
public abstract class AbstractCalendarViewController implements Serializable {

    protected final Log logger = LogFactory.getLog(AbstractCalendarViewController.class);

    protected CalendarViewService calendarViewService;

    protected CalendarViewModel viewModel;

    /**
     * Decrements the calendar view one unit past the current date cursor.
     *
     * @param event jsf action event.
     */
    public abstract void previousCalendar(ActionEvent event);

    /**
     * Increments the calendar view one unit past the current date cursor.
     *
     * @param event jsf action event.
     */
    public abstract void nextCalendar(ActionEvent event);

    /**
     * Initialized the respective CalendarViewController dataStructures.
     */
    public abstract void initialize();

    /**
     * Disposes the respective CalendarViewController dataStructure
     */
    public abstract void dispose();

    /**
     * Sets calendarViewService and initializes the calendar data model. This is
     * intended to be called from the faces config only.
     *
     * @param calendarViewService calendar service for fetching calendar event
     *                            data.
     */
    public void setInitializeDataModel(CalendarViewService calendarViewService) {
        setCalendarViewService(calendarViewService);
        initialize();
    }

    /**
     * Sets link to calendar service which is responsible for getting
     * calendar events for the four different view types.
     *
     * @param calendarViewService service wrapper for Spring services.
     */
    public void setCalendarViewService(CalendarViewService calendarViewService) {
        this.calendarViewService = calendarViewService;
    }

    /**
     * Uility to refresh view model.
     */
    protected void refreshModel() {
        dispose();
        initialize();
    }

    public CalendarViewModel getViewModel() {
        return viewModel;
    }

}