/*
 * FechaRangoMinimaMaximaFActualDependiente.java
 *
 * Created on 23 de marzo de 2007, 08:48 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.fidufosyga.rules;

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

import com.fidufosyga.rules.util.ValidarFecha;

/**
 * 
 * @author jlozanop
 */
public class FechaRangoMinimaMaximaDependiente extends AbstractRule {

	String DATE_FORMAT = "dd/MM/yyyy";

	SimpleDateFormat sdf;

	// Create object of SimpleDateFormat and pass the desired date format.

	/** Creates a new instance of CampoVacio */
	public FechaRangoMinimaMaximaDependiente() {
		sdf = new SimpleDateFormat(DATE_FORMAT);
		sdf.setLenient(false);
	}

	public boolean performValidation(String field, String registro[],
			String nombreArchivo) {
		String DatoCompara = registro[new Integer((String) getParams()[0])
				.intValue()];
		Date dateMinima = null;
		Date dateMaxima = null;
		if (DatoCompara.equals((String) getParams()[1])) {

			Date dateCampo = null;

			if (!ValidarFecha.esFecha(field))
				return false;

			try {
				dateCampo = sdf.parse(field);
				dateMinima = sdf.parse((String) getParams()[2]);
				
				if(sdf.parse((String) getParams()[3]).equals("GETDATE()"))				
					dateMaxima = Calendar.getInstance().getTime();
				else
					dateMaxima = sdf.parse((String) getParams()[3]);
				
			} catch (Exception e) {
				return false;
			}

			if (dateCampo.after(dateMaxima) || dateCampo.before(dateMinima)) {
				return true;
			}
		}
		return false;
	}
}
