package market.util;

import java.util.*;
import java.util.Calendar;
import market.*;

/**
 * This class contein all fields for base Source <br>
 * Creation date: (12/16/99 11:31:06 AM)
 * @author: tin
 */
public class SrcElement extends Object implements Cloneable{
	public long srcKey;
	public String srcName;
	public int langKey;
	public int timeBase;
	public int frequence;
	public long srcCheckCount;
	public java.sql.Date regDate;
	public boolean keepInPath;
	public boolean keepInDomain;
	public java.sql.Date lastChecked;

	public byte grabLevels;
	public String srcString;
	public boolean active;
	public boolean processing;
	public boolean error;
	public String userAgent;

	public long procTime;
public SrcElement() {
	super();
}
public SrcElement( String srcValue, long srcKey, int langKey, int timeBase, int frequence, 
							boolean keepInPath, boolean keepInDomain, int grabLevel, String userAgent ) {
	super();
	active = false;
	this.srcString = srcValue;
	this.srcKey = srcKey;
	this.langKey = langKey;
	this.timeBase = timeBase;
	this.frequence = frequence;
	this.keepInPath = keepInPath;
	this.keepInDomain= keepInDomain;
	this.grabLevels = (byte) grabLevel;
	this.userAgent = userAgent;
	procTime = 0;
	processing = false;
	error = false;
}
/**
 * Insert the method's description here.
 * Creation date: (12/27/99 5:12:29 PM)
 * @return java.lang.Object
 * @exception java.lang.CloneNotSupportedException The exception description.
 */
public Object clone() throws CloneNotSupportedException {
	return super.clone();
}
/**
 * Return url status (active/not active).
 * Creation date: (01.3.2000 “. 18:19:05)
 * @return boolean
 */
public boolean isActive() {
	return active;
}
/**
 * Check is .
 * Creation date: (10.2.2000 “. 16:10:12)
 * @return boolean
 * @param elem market.util.SrcElement
 */
public boolean isGreatest(SrcElement elem) {
	return false;
}
/**
 * Convert string to object.
 * Creation date: (13.7.2000 “. 19:24:18)
 * @return market.util.SrcElement
 * @param line java.lang.String
 */
public static SrcElement makeFromString(String line) throws NumberFormatException {
	StringTokenizer stoken = new StringTokenizer(line, String.valueOf( Const.http_delim) );
	String temp_str;
	SrcElement srcElm = new SrcElement();
	temp_str = stoken.nextToken();
	// Get srcKey		
	srcElm.srcKey = Integer.valueOf(temp_str).intValue();

	// Get timeBase		
	temp_str = stoken.nextToken();
	srcElm.timeBase = Integer.valueOf(temp_str).intValue();

	// Get frequence		
	temp_str = stoken.nextToken();
	srcElm.frequence = Integer.valueOf(temp_str).intValue();

	// Get keepInPath		
	temp_str = stoken.nextToken();
	srcElm.keepInPath = "1".equals(temp_str);

	// Get keepInDomain
	temp_str = stoken.nextToken();
	srcElm.keepInDomain = "1".equals(temp_str);

	// Get lastChecked ( Data )
	temp_str = stoken.nextToken();
	if (temp_str.equals("null")) {
		srcElm.lastChecked = new java.sql.Date((long) 0);
	} else {
		srcElm.lastChecked = java.sql.Date.valueOf(temp_str);
	}

	// Get grabLevel
	temp_str = stoken.nextToken();
	srcElm.grabLevels = Byte.valueOf(temp_str).byteValue();

	// Get langKey		
	temp_str = stoken.nextToken();
	srcElm.langKey = Integer.valueOf(temp_str).intValue();

	// Get Activate
	temp_str = stoken.nextToken();
	srcElm.active = "1".equals(temp_str);

	// Get User-Agent
	srcElm.userAgent = stoken.nextToken();
	srcElm.processing = false;
	srcElm.error = false;
	return srcElm;
}
/**
 * Make source active.
 * Creation date: (01.3.2000 “. 18:19:05)
 * @param newActive boolean
 */
public void setActive(boolean newActive) {
	synchronized (this){
		active = newActive;
	}
}
/**
 * Check: run today this source ?
 * Creation date: (1/9/00 5:18:07 PM)
 * @return boolean
 */
public boolean StartToday() {

	if ( !isActive() ) return false;	

	Calendar rightNow = Calendar.getInstance();
	Calendar srcDay = Calendar.getInstance();
	if ( null == this.lastChecked ) {
		// first day in list
		return true;
	}	
	java.util.Date dat2 = (java.util.Date) this.lastChecked;
	if ( dat2.before( new java.util.Date( (long)0 ) ) )
		return true;	

	srcDay.setTime( dat2 );
	srcDay.add( Calendar.DATE, this.frequence );

	if ( (srcDay.get(Calendar.DATE) <= rightNow.get(Calendar.DATE)) || 
		 (srcDay.get(Calendar.MONTH) <= rightNow.get(Calendar.MONTH)) ||
		 (srcDay.get(Calendar.YEAR) <= rightNow.get(Calendar.YEAR)) ){
			 
		return true;
	}

	return false;	 
}
}

