/**
 * 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;

import org.apache.log4j.Logger;

import eu.dnetlib.data.udm.cron.CronJobs;

// TODO: Auto-generated Javadoc
/**
 * The Class DeliverUsageData initializes delivering of records for each mdstore founded in
 * IS registry. Having ResultSet EPR, thread initializes ResultSet iterator
 * and start the ResultSet loader for each mdstore. Thread calls handelMDStoreRequestsQueue()
 * which decides if the new delivering process can be started now or maybe it have to wait
 * sometime.
 * 
 * @author <a href="mailto:marek.imialek at uni-bielefeld.de">Marek Imialek</a>
 */

public class DataCollectionCreatingThread implements Runnable {

	/** The Constant LOGGER. */
	private final static Logger LOGGER = Logger.getLogger(DataCollectionCreatingThread.class);
	
	/**
	 * The Enum COLECTION_TYPE.
	 */
	public enum COLECTION_TYPE {
			
			/** The COMPLETE. */
			COMPLETE, 
			
			/** The YESTERDAY. */
			YESTERDAY};

	/** The collection type. */
	private COLECTION_TYPE collectionType;

	/** The cron jobs. */
	private CronJobs cronJobs;

	/** The storing date. */
	private String storingDate;

	/**
	 * Instantiates a new deliver usage data.
	 * 
	 * @param storingDate the storing date
	 * @param collectionType the collection type
	 * @param cronJobs the cron jobs
	 * 
	 * @throws Exception the exception
	 */
	public DataCollectionCreatingThread (String storingDate, 
			COLECTION_TYPE collectionType , CronJobs cronJobs) 
		throws Exception {
		
		this.setStoringDate(storingDate);
		this.setCollectionType(collectionType);
		this.cronJobs = cronJobs;
		
	}
	
	
	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		
		if (COLECTION_TYPE.COMPLETE.equals(collectionType)) {
			try {
				LOGGER.debug("Starting createCompleteDataCollectionInFS");
				cronJobs.createCompleteDataCollectionInFS();
			} catch (Exception e) {
				LOGGER.error("Problem by creating complete collection:" +e);
			}
		} else if (COLECTION_TYPE.YESTERDAY.equals(collectionType)) {
			try {
				cronJobs.createYesterdaysDataCollectionInFS(storingDate);
			} catch (Exception e) {
				LOGGER.error("Problem by creating yesterdays collection:" +e);
			}
		}
		
	}


	/**
	 * Sets the collection type.
	 * 
	 * @param collectionType the collectionType to set
	 */
	public void setCollectionType(COLECTION_TYPE collectionType) {
		this.collectionType = collectionType;
	}


	/**
	 * Gets the collection type.
	 * 
	 * @return the collectionType
	 */
	public COLECTION_TYPE getCollectionType() {
		return collectionType;
	}

	/**
	 * Sets the storing date.
	 * 
	 * @param storingDate the storingDate to set
	 */
	public void setStoringDate(String storingDate) {
		this.storingDate = storingDate;
	}


	/**
	 * Gets the storing date.
	 * 
	 * @return the storingDate
	 */
	public String getStoringDate() {
		return storingDate;
	}


	/**
	 * Sets the cron jobs.
	 * 
	 * @param cronJobs the cronJobs to set
	 */
	public void setCronJobs(CronJobs cronJobs) {
		this.cronJobs = cronJobs;
	}


	/**
	 * Gets the cron jobs.
	 * 
	 * @return the cronJobs
	 */
	public CronJobs getCronJobs() {
		return cronJobs;
	}
	
	
}
