/**
 * Copyright 2009-2012 OpenAIRE PROJECT (Bielefeld University)
 * Original author: Marek Imialek <marek.imialek at uni-bielefeld.de>
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.dnetlib.data.udm.cron;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import eu.dnetlib.data.udm.collector.FSUsageDataCollector;
import eu.dnetlib.data.udm.collector.GeneralUsageDataCollector.STORING_TYPE;

/**
 * The Class CronJobs.
 * 
 * @author <a href="mailto:marek.imialek at uni-bielefeld.de">Marek Imialek</a>
 */
public class CronJobs {

	/** The Constant LOGGER. */
	private final static Logger LOGGER = Logger.getLogger(CronJobs.class);
			
	/** The fs usage data collector. */
	private FSUsageDataCollector fsUsageDataCollector;
	
	/** The break processing after. */
	private int breakProcessingAfter;
	
	/**
	 * Creates the complete data collection in fs.
	 * 
	 * @throws Exception the exception
	 */
	public void createCompleteDataCollectionInFS() throws Exception {
		createCompleteDataCollectionInFS(null);
	}
	
	/**
	 * Creates the complete data collection in fs.
	 * 
	 * @param storingDate the storing date
	 * 
	 * @throws Exception the exception
	 */
	public void createCompleteDataCollectionInFS(String storingDate) throws Exception {
		
		LOGGER.info("Creating file containing all usage data: "
				+ storingDate);
		
		if (storingDate == null)
			storingDate = new SimpleDateFormat(
					fsUsageDataCollector.getDateFormatPattern()).format(new Date());
		
		fsUsageDataCollector.collectAndStoreDataS(STORING_TYPE.REFRESH, 
				storingDate, breakProcessingAfter);
	}
	
	/**
	 * Creates the yesterdays data collection in fs.
	 * 
	 * @throws Exception the exception
	 */
	public void createYesterdaysDataCollectionInFS() throws Exception {
		createYesterdaysDataCollectionInFS(null);
	}
	
	/**
	 * Creates the yesterdays data collection in fs.
	 * 
	 * @param storingDate the storing date
	 * 
	 * @throws Exception the exception
	 */
	public void createYesterdaysDataCollectionInFS(String storingDate) throws Exception {
		
		LOGGER.info("Creating file containing yesterdays usage data: "
				+ storingDate);
		
		if (storingDate == null)
			storingDate = new SimpleDateFormat(
					fsUsageDataCollector.getDateFormatPattern()).format(new Date());
		
		fsUsageDataCollector.collectAndStoreDataS(STORING_TYPE.INCREMENTAL, 
				storingDate, breakProcessingAfter);
		
	}

	/**
	 * Sets the fs usage data collector.
	 * 
	 * @param fsUsageDataCollector the fsUsageDataCollector to set
	 */
	public void setFsUsageDataCollector(FSUsageDataCollector fsUsageDataCollector) {
		this.fsUsageDataCollector = fsUsageDataCollector;
	}

	/**
	 * Gets the fs usage data collector.
	 * 
	 * @return the fsUsageDataCollector
	 */
	public FSUsageDataCollector getFsUsageDataCollector() {
		return fsUsageDataCollector;
	}

	/**
	 * @param breakProcessingAfter the breakProcessingAfter to set
	 */
	public void setBreakProcessingAfter(int breakProcessingAfter) {
		this.breakProcessingAfter = breakProcessingAfter;
	}

	/**
	 * @return the breakProcessingAfter
	 */
	public int getBreakProcessingAfter() {
		return breakProcessingAfter;
	}
}
