/*
 * $Id: ShowStatisticsForm.java,v 1.13 2006/02/15 16:54:38 jonyp Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ShowStatisticsForm.java,v $
 * Revision 1.13  2006/02/15 16:54:38  jonyp
 * *** empty log message ***
 *
 * Revision 1.12  2006/02/03 15:48:44  jonyp
 * bugfix: display correct predecessor/successor when granularity is month
 *
 * Revision 1.11  2005/09/15 14:43:32  jonyp
 * reset form to current year an dmonth if no date is given
 *
 * Revision 1.10  2005/09/14 22:33:03  jonyp
 * *** empty log message ***
 *
 * Revision 1.9  2005/09/14 22:25:07  jonyp
 * added getLinkParameters method
 *
 * Revision 1.8  2005/09/14 15:49:34  jonyp
 * added methods for year, month, day, successorLink, predessessorLink
 *
 * Revision 1.7  2004/07/19 14:42:47  jonyp
 * added versioned document statistics
 *
 * Revision 1.6  2004/07/14 11:04:52  jonyp
 * *** empty log message ***
 *
 * Revision 1.5  2004/07/13 23:25:22  jonyp
 * usable version
 *
 * Revision 1.4  2004/07/06 14:40:14  jonyp
 * preliminary
 *
 * Revision 1.3  2004/06/24 23:31:12  marbobo
 * Quellcode maintenance
 *
 * Revision 1.2  2004/06/18 15:02:55  jonyp
 * added conveninces methods
 *
 * Revision 1.1  2004/06/18 07:55:30  jonyp
 * first strike
 *
 *
 */

package vkc.statistics.form;

import java.util.GregorianCalendar;
import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import vkc.statistics.VKCSystemStatistics;
import vkc.statistics.VKCStatisticsRetriever;


public final class ShowStatisticsForm extends ActionForm {

    private String from = null;
    private String until = null;
    private Integer year = null;
    private Integer month = null;
    private Integer day = null;
    private String chartFilename = null;
    private String chartURL = null;
    private VKCSystemStatistics statisticsToday = null;
	private VKCSystemStatistics statisticsWeek = null;
	private VKCSystemStatistics statisticsMonth = null;
	private VKCSystemStatistics statisticsYear = null;
	private VKCSystemStatistics statisticsAll = null;
    private boolean cumulate = false;
 
    /**
     * Reset der Eingabefelder auf Defaultwerte
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {        
        year = null; 
        month = null;
        day = null;
        cumulate = false;
    }


    /**
     * Validierung der Eingabefelder
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
     */
    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();
        
		return errors;
    }

	public VKCSystemStatistics getStatistics4Today() throws Exception{
		
		if (statisticsToday == null) {
			statisticsToday = VKCStatisticsRetriever.retrieveSystemStatistics4Today();   
		}
		
		return statisticsToday;
	}

	public VKCSystemStatistics getStatistics4CurrentWeek() throws Exception {

		if (statisticsWeek == null) {
			statisticsWeek = VKCStatisticsRetriever.retrieveSystemStatistics4CurrentWeek();   
		}

		return statisticsWeek;
	}

	public VKCSystemStatistics getStatistics4CurrentMonth() throws Exception {

		if (statisticsMonth == null) {
			statisticsMonth = VKCStatisticsRetriever.retrieveSystemStatistics4CurrentWeek();   
		}
	
		return statisticsMonth;
	}

	public VKCSystemStatistics getStatistics4CurrentYear() throws Exception {

		if (statisticsYear == null) {
			statisticsYear = VKCStatisticsRetriever.retrieveSystemStatistics4CurrentYear();   
		}

		return statisticsYear;
	}

	public VKCSystemStatistics getStatistics4All() throws Exception {

		if (statisticsAll == null) {
			statisticsAll = VKCStatisticsRetriever.retrieveSystemStatistics4All();   
		}
		
		return statisticsAll;
	}

/*
	public void setStatistics4Today(VKCSystemStatistics statistics) {
		statisticsToday = statistics;
	}

	public void setStatistics4CurrentWeek(VKCSystemStatistics statistics) {
		statisticsWeek = statistics;
	}

	public void setStatistics4CurrentMonth(VKCSystemStatistics statistics) {
		statisticsMonth = statistics;
	}

	public void setStatistics4CurrentYear(VKCSystemStatistics statistics) {
		statisticsYear = statistics;
	}

	public void setStatistics4All(VKCSystemStatistics statistics) {
		statisticsAll = statistics;
	}
*/
    public String getFrom() {
        return from;
    }
    public void setFrom(String t) {
        from = t;
    }
    public String getUntil() {
        return until;
    }
    public void setUntil(String t) {
        until = t;
    }


    public Integer getDay() {
        return day;
    }


    public void setDay(Integer day) {
        this.day = day;
    }


    public Integer getMonth() {
        return month;
    }


    public void setMonth(Integer month) {
        this.month = month;
    }


    public Integer getYear() {
        return year;
    }


    public void setYear(Integer year) {
        this.year = year;
    }
    
    public String getSuccessorLink() {
        return "/showStatistics.do" + getLinkParameters(year, month, day, +1);
    }
    
    public String getPredecessorLink() {
        return "/showStatistics.do" + getLinkParameters(year, month, day, -1);    
    }
    
    private String getLinkParameters(Integer year, Integer month, Integer day, int delta) {
     	
    	// TODO: return "", if new date is greater than given    	
    	GregorianCalendar currentDate = new GregorianCalendar();
        String parameters = "";
        
    	if (year != null) {    		
    		if (month != null) {
    			if (day != null) {
                    currentDate.set(year.intValue(), month.intValue() - 1, day.intValue(), 0, 0, 0);
                    currentDate.add(Calendar.DAY_OF_MONTH, delta);
                    parameters = "?year=" + currentDate.get(Calendar.YEAR) + "&month=" + Integer.toString(currentDate.get(Calendar.MONTH)+1) + "&day=" + currentDate.get(Calendar.DAY_OF_MONTH);
    			} else {
    				currentDate.set(year.intValue(), month.intValue() - 1, 1, 0, 0, 0);
    				currentDate.add(Calendar.MONTH, delta);
                    parameters = "?year=" + currentDate.get(Calendar.YEAR) + "&month=" + Integer.toString(currentDate.get(Calendar.MONTH)+1);
    			}
    		} else {
    			currentDate.set(year.intValue(), 0, 1, 0, 0, 0);
    			currentDate.add(Calendar.YEAR, delta);
                parameters = "?year=" + currentDate.get(Calendar.YEAR);
    		}
    	} else {
    		// current year and month as default
            currentDate.add(Calendar.MONTH, delta);
            parameters = "?year=" + currentDate.get(Calendar.YEAR) + "&month=" + Integer.toString( currentDate.get(Calendar.MONTH)+1);
    	}
    	
    	return parameters;   
    }


    public String getChartFilename() {
        return chartFilename;
    }


    public void setChartFilename(String chartFilename) {
        this.chartFilename = chartFilename;
    }


    public String getChartURL() {
        return chartURL;
    }


    public void setChartURL(String chartURL) {
        this.chartURL = chartURL;
    }


    public boolean isCumulate() {
        return cumulate;
    }


    public void setCumulate(boolean cumulate) {
        this.cumulate = cumulate;
    }    
}