package com.simoncat.time;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import com.simoncat.time.RestartTask;
import com.simoncat.time.TestTask;
import java.util.Vector;
import java.util.Date;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Set;
import java.util.HashMap;
import java.util.Map;
import com.simoncat.vo.Server;
import com.simoncat.vo.Event;
public class TaskProgrammer{// extends Timer{
private Vector events = new Vector();
private HashMap eventServer = new HashMap();
private Timer clock = new Timer();
private Vector executed = new Vector();
public TaskProgrammer(){
}
public boolean scheduleEvent(Server server,Event event){
//super();
boolean ultimaTarea=false;
String eventDate = event.getDate();
String eventHour = event.getHour();
//cargar la variable date con los datos de fecha y hora del evento
int second =0;
int minute = new Integer(eventHour.substring(eventHour.indexOf(":")+1,eventHour.length())).intValue();
int hour = new Integer(eventHour.substring(0,eventHour.indexOf(":"))).intValue();
String dateParts[] = eventDate.split("-");
int year = new Integer(dateParts[0]).intValue();
int month = new Integer(dateParts[1]).intValue();
int dayOfMonth = new Integer(dateParts[2]).intValue();
Date date=getDateOf(second,minute,hour,dayOfMonth,month,year);
Date now=Calendar.getInstance().getTime();
try{
if(event.getType().equals(Event.RESTART)){
clock.schedule(new RestartTask(clock,server,event,this),date);
ultimaTarea=true;
}
if(event.getType().equals(Event.TEST)){
clock.schedule(new TestTask(clock,server,event,this),date);
ultimaTarea=true;
}
events.add(event);
eventServer.put(event,server);
return true;
}
catch(IllegalStateException ise){
//reinstanciar el timer y reprogramar eventos
clock = new Timer();
if(rescheduleEvents()){
if(!ultimaTarea)
return scheduleEvent(server,event);
else
return true;
}
return false;
/*
else{
clock.close();
clock = new Timer( );
rescheduleEvents();
}
*/
}
}
private boolean rescheduleEvents(){
try{
Set entries = eventServer.entrySet();
System.out.println("******************************************************************");
System.out.println("***********************REPROGRAMANDO EVENTOS**********************");
System.out.println("******************************************************************");
Iterator it = entries.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Event event = (Event)entry.getKey();
Server server = (Server)entry.getValue();
//Server server = (Server)eventServer.get(event);
boolean ultimaTarea=false;
String eventDate = event.getDate();
String eventHour = event.getHour();
//cargar la variable date con los datos de fecha y hora del evento
int second =0;
int minute = new Integer(eventHour.substring(eventHour.indexOf(":")+1,eventHour.length())).intValue();
int hour = new Integer(eventHour.substring(0,eventHour.indexOf(":"))).intValue();
String dateParts[] = eventDate.split("-");
int year = new Integer(dateParts[0]).intValue();
int month = new Integer(dateParts[1]).intValue();
int dayOfMonth = new Integer(dateParts[2]).intValue();
Date date=getDateOf(second,minute,hour,dayOfMonth,month,year);
Date now=Calendar.getInstance().getTime();
if(event.getType().equals(Event.RESTART)){
clock.schedule(new RestartTask(clock,server,event,this),date);
}
if(event.getType().equals(Event.TEST)){
clock.schedule(new TestTask(clock,server,event,this),date);
}
}
return true;
}
catch(Exception e){
System.out.println("******************************************************************");
System.out.println("********PROBLEMA ***REPROGRAMANDO EVENTOS**********************");
System.out.println("******************************************************************");
e.printStackTrace();
return false;
}
}
public Event[] getEventsProgrammed(){
Event[] arre = new Event[events.size()];
events.toArray(arre);
return arre;
}
public Event[] getEventsFinished(){
Event[] arre = new Event[executed.size()];
executed.toArray(arre);
return arre;
}
public static void main(String[] args) {
//for(int i=0;i<2;++i)
}
public Date getDateOf(int second, int minute, int hour,
int dayOfMonth, int month, int year) {
Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month - 1);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, second);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
public void addFinishedTask(Server server,Event event){
//TODO modificar el XML
//Insertar en el vector executed
executed.add(event);
//quitar del vector events
eventServer.remove(event);
events.remove(event);
}
}