Show AutoRefresher.java syntax highlighted
package startup;
import java.util.Hashtable;
import java.util.Timer;
import java.util.TimerTask;
import org.objectteams.Team;
import de.edu.tuberlin.compiereMonitor.exception.CMException;
import de.edu.tuberlin.compiereMonitor.exception.CMExceptionTable;
import de.edu.tuberlin.compiereMonitor.exception.CMExceptionTableRowValueNotExists;
import de.edu.tuberlin.compiereMonitor.exception.CMExceptionTeamActivationFailed;
import de.edu.tuberlin.compiereMonitor.model.MAspectSchedule;
import de.edu.tuberlin.compiereMonitor.model.MAspectScheduleTimeplan;
import de.edu.tuberlin.compiereMonitor.model.MAspectTeamClass;
import de.edu.tuberlin.compiereMonitor.model.MCompiereAspectCWFNode;
import de.edu.tuberlin.compiereMonitor.model.MCompiereAspectCWFSCriteria;
import de.edu.tuberlin.compiereMonitor.model.MCompiereAspectCWFSDef;
import de.edu.tuberlin.compiereMonitor.model.MCompiereAspectCWFSDefForm;
import de.edu.tuberlin.compiereMonitor.model.MCompiereAspectCWFSDefProcess;
import de.edu.tuberlin.compiereMonitor.model.MCompiereAspectCWFSDefWin;
import de.edu.tuberlin.compiereMonitor.model.MCompiereAspectLogActivity;
import de.edu.tuberlin.compiereMonitor.model.MCompiereAspectLogProfile;
import de.edu.tuberlin.compiereMonitor.model.MConfigScheduleDW;
import de.edu.tuberlin.compiereMonitor.model.MConfigScheduleDayplan;
import de.edu.tuberlin.compiereMonitor.model.MConfigSettings;
import de.edu.tuberlin.compiereMonitor.model.Table.Row;
import de.edu.tuberlin.compiereMonitor.tools.GUITools;
public class AutoRefresher extends TimerTask {
/**
* Hashtable with all teams
* Stored as: ( Integer(teamClassPKey), Team)
*/
private static Hashtable teams;
/**
* The activator-thread
*/
private static Timer timer;
/**
* The last loaded interval for the timer
* Default: 60 (sec)
*/
private static int interval = 60;
/**
* Initializes the team hashtable
*/
private static void initialize(){
if(teams != null){
return;
}
teams = new Hashtable();
Row[] teamRows = MAspectTeamClass.getTable().getAllRows();
String teamClassName;
for(int i=0; i<teamRows.length; i++){
try {
teamClassName = MAspectTeamClass.getName(teamRows[i].getPrimaryKey());
try {
Class teamC = Class.forName(teamClassName);
Object obj = teamC.newInstance();
if(obj instanceof Team){
System.out.println("Creating teamclass <" + obj.getClass() + ">");
teams.put(new Integer(teamRows[i].getPrimaryKey()), (Team)obj);
}
} catch (Exception e1) {
CMException e = new CMExceptionTeamActivationFailed(teamClassName);
e.addContextInfo(e1.getMessage());
GUITools.showException(e);
}
} catch (CMExceptionTable e) {
e.addContextInfo("Unable to activate teamclass <"+teamRows[i].getPrimaryKey()+">.");
GUITools.showException(e);
}
}
}
/**
* Starts the team-activator-thread
*/
public static void startActivator(){
if(timer == null){
//initialize();
try {
interval = MConfigSettings.getRefreshInterval();
} catch (CMExceptionTableRowValueNotExists e) {}
timer = new Timer();
timer.schedule(new AutoRefresher(), 0, interval*1000);
}
}
/**
* The task-run-method
*/
public void run(){
//refresh settings
MConfigSettings.getTable().refresh();
//check if the interval-value from the settings-table changed
try {
int newInt = MConfigSettings.getRefreshInterval();
if(newInt != interval && interval > 0){
//new interval different to actual one -> recreate timer
System.out.println("new interval : " + newInt);
interval = newInt;
this.cancel();
timer.purge();
timer.schedule(new AutoRefresher(), 0, interval*1000);
return;
}
} catch (CMExceptionTableRowValueNotExists e) {}
//refresh the schedules for the team-activation-guards
MAspectScheduleTimeplan.getTable().refresh();
MAspectSchedule.getTable().refresh();
MConfigScheduleDayplan.getTable().refresh();
MConfigScheduleDW.getTable().refresh();
if(MConfigSettings.isAutorefreshEnabled()){
//if autorefresh is enabled, refresh the other tables which might have changed
MCompiereAspectLogProfile.getTable().refresh();
MCompiereAspectLogActivity.getTable().refresh();
MCompiereAspectCWFNode.getTable().refresh();
MCompiereAspectCWFSCriteria.getTable().refresh();
MCompiereAspectCWFSDef.getTable().refresh();
MCompiereAspectCWFSDefForm.getTable().refresh();
MCompiereAspectCWFSDefProcess.getTable().refresh();
MCompiereAspectCWFSDefWin.getTable().refresh();
}
/*
long time = new Date().getTime();
Enumeration keyEnum = teams.keys();
while(keyEnum.hasMoreElements()){
Integer pkey = (Integer)keyEnum.nextElement();
Row[] schedules = MAspectSchedule.getAllScheduleRows(pkey.intValue());
boolean isActive = false;
for(int i=0; i<schedules.length; i++){
if(MAspectSchedule.isActive(schedules[i].getPrimaryKey(), time)){
isActive = true;
break;
}
}
if(schedules.length == 0){
isActive = true;
}
Team theTeam = (Team)teams.get(pkey);
if(isActive && ! theTeam.isActive()){
theTeam.activate(Team.ALL_THREADS);
}else if(!isActive && theTeam.isActive()){
theTeam.deactivate(Team.ALL_THREADS);
}
System.out.println(theTeam.getClass() + " isActive: " + theTeam.isActive());
}
*/
}
}
See more files for this project here