Code Search for Developers
 
 
  

AutoRefresher.java from Compiere Monitor at Krugle


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

Compiere Monitor

This project extends the Open-Source ERP-System Compiere(R) with aspectorientation. Its using the aspectoriented language ObjectTeams/Java and was developed within a diploma-thesis at the Technical University Berlin, Germany.

Project homepage: http://sourceforge.net/projects/compieremonitor
Programming language(s): Java
License: gpl2

  AutoRefresher.java
  Startup.java