/*
 * CalculaMayoriaEdad.java
 *
 * Created on 21 de marzo de 2007, 12:47 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.fidufosyga.rules;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
 * 
 * @author 
 */
public class CalculaMayoriaEdad extends AbstractRule {

        @Override
	public void setParams(Object[] params) {
		super.setParams(params);
	}

        @Override
	public boolean performValidation(String field, String registro[],
			String nombreArchivo) {
		try {
			if (registro[new Integer((String) getParams()[0]).intValue()]
							.equals(getParams()[1])) {
				
					String fecha_ini = registro[new Integer((String) getParams()[2]).intValue()];
					String fecha_fin = registro[new Integer((String) getParams()[3]).intValue()];         
		
						Calendar birth = new GregorianCalendar();
						Calendar today = new GregorianCalendar();
						int age;
						int factor=0;
						java.util.Date birthDate = new SimpleDateFormat("dd/MM/yyyy").parse(fecha_ini);
						java.util.Date currentDate= new SimpleDateFormat("dd/MM/yyyy").parse(fecha_fin);
						birth.setTime(birthDate);
						today.setTime(currentDate);
						if (today.get(Calendar.MONTH) <= birth.get(Calendar.MONTH))
						{
							if (today.get(Calendar.MONTH) == birth.get(Calendar.MONTH)) 
							{
								if (today.get(Calendar.DATE) > birth.get(Calendar.DATE)) 
								{
									factor = -1;
								}
							} 
							else 
							{
								factor = -1;
							}
						}
						
						age=(today.get(Calendar.YEAR)-birth.get(Calendar.YEAR))+factor;
						
				if (age >= 18) 
				{
		 			return false;
				}
				else
				{
					return true;
				}
			}
			else
				return false;
		} catch (ParseException e) {
			e.printStackTrace();
			return true;
		}

	}

}
