/**
 * SearchYearDataProvider.java
 * Created on Sep 15, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.web.schedule.search;

import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.ResourceBundle;

import com.tandbergtv.workflow.web.page.ISearchFieldDataProvider;

/**
 * This class returns a map of past and future years ([YYYY]->[YYYY]).
 * The number of past and future years are read from search.properties
 * 
 * @author spuranik
 * 
 */
public class YearDataProvider implements ISearchFieldDataProvider {

	private static String SEARCH_PROPERTIES_FILE = "com.tandbergtv.watchpoint.pmm.web.schedule.search.search";

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tandbergtv.workflow.web.page.ISearchFieldDataProvider#getData()
	 */
	public Map<String, String> getData() {
		Map<String, String> years = new LinkedHashMap<String, String>();
		ResourceBundle properties = ResourceBundle
				.getBundle(SEARCH_PROPERTIES_FILE);

		// add an empty entry, so the user always has to select something
		years.put("", "");

		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		int currentYear = c.get(Calendar.YEAR);
		
		// populate next n years in the map
		int futureYearCount = Integer.parseInt(properties
				.getString(SearchProperties.FUTURE_YEAR_COUNT));
		for (int i = futureYearCount; i > 0; i--) {
			String nextYear = String.valueOf(currentYear + i);
			years.put(nextYear, nextYear);
		}

		// add current year
		years.put(String.valueOf(currentYear), String.valueOf(currentYear));

		// populate previous n years in the map
		int previousYearCount = Integer.parseInt(properties
				.getString(SearchProperties.PREVIOUS_YEAR_COUNT));
		for (int i = 1; i <= previousYearCount; i++) {
			String prevYear = String.valueOf(currentYear - i);
			years.put(prevYear, prevYear);
		}

		return years;
	}

}
