/* Copyright 1999-2008 Acelet.org. All rights reserved. GPL v2 license */
/** @author Wei Jiang */
package com.acelet.s.watchdog;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
import javax.naming.*;
import com.acelet.lib.Common;
import com.acelet.lib.Externals;
import com.acelet.lib.Kit;
import com.acelet.lib.LogAgent;
import com.acelet.lib.LoggingConstants;
import com.acelet.lib.NotSupportedException;
import com.acelet.lib.Phrase;
import com.acelet.lib.SendEmail;
import com.acelet.lib.SuperProperties;
import com.acelet.s.MailServerData;
import com.acelet.s.chore.CandidateChore;
import com.acelet.s.chore.Chore;
import com.acelet.s.chore.ChoreProcess;
import com.acelet.s.chore.WatchdogDoerTalker;
import com.acelet.s.chore.WorkingChore;
import com.acelet.s.scheduler.DirectTaskDatabaseConnection;
/**
* The <code>Api</code> is a utility class for SuperWatchdog.
* It is the API to SuperWatchdog.
* <br>
* @see com.acelet.s.job.AbstractTaskConstants
* @see com.acelet.s.chore.Chore
* @see com.acelet.s.chore.ChoreConstants
* @see com.acelet.s.chore.WorkingChore
* <br>
*/
public class Api extends ApiBase {
protected Api() throws Exception {
}
/**
* <code>changeChoreStatus</code> changes the status for a Chore.
* <br>
* @param choreName the name of the Chore.
* @param newStatus the new status. It can be
* <code>STATUS_ACTIVE</code> or <code>STATUS_SUSPENDED</code>
* @return the number of rows changed. It is 1, if successful, 0 otherwise.
* @exception Exception.
*/
public static int changeChoreStatus(String choreName, int newStatus) throws Exception {
Connection connection = null;
try {
connection = DirectTaskDatabaseConnection.getNewConnection();
return new ChoreProcess(connection).changeChoreStatus(choreName, newStatus);
} finally {
if (connection != null)
connection.close();
}
}
/**
* <code>changeChoreStatus</code> changes the status for a Chore.
* <br>
* @param connection the connection will be used for this operation. It will not be closed
* automatically.
* @param choreName the name of the Chore.
* @param newStatus the new status. It can be
* <code>STATUS_ACTIVE</code> or <code>STATUS_SUSPENDED</code>
* @return the number of rows changed. It is 1, if successful, 0 otherwise.
* @exception Exception.
*/
public static int changeChoreStatus(Connection connection, String choreName, int newStatus)
throws Exception {
return new ChoreProcess(connection).changeChoreStatus(choreName, newStatus);
}
/**
* <code>deleteChore</code> deletes the Chore.
* <br>
* @param id the id of the Chore.
* @return the number of rows deleted. It is 1, if successful, 0 otherwise.
* @exception Exception.
*/
public static int deleteChore(long id) throws Exception {
Connection connection = null;
try {
connection = DirectTaskDatabaseConnection.getNewConnection();
return new ChoreProcess(connection).deleteChore(id);
} finally {
if (connection != null)
connection.close();
}
}
/**
* <code>deleteChore</code> deletes the Chore.
* <br>
* @param connection the connection will be used for this operation. It will not be closed
* automatically.
* @param id the id of the Chore.
* @return the number of rows deleted. It is 1, if successful, 0 otherwise.
* @exception Exception.
*/
public static int deleteChore(Connection connection, long id) throws Exception {
return new ChoreProcess(connection).deleteChore(id);
}
/**
* <code>deleteChore</code> deletes the Chore.
* <br>
* @param name the name of the Chore.
* @return the number of rows deleted. It is 1, if successful, 0 otherwise.
* @exception Exception.
*/
public static int deleteChore(String name) throws Exception {
Connection connection = null;
try {
connection = DirectTaskDatabaseConnection.getNewConnection();
return new ChoreProcess(connection).deleteChore(name);
} finally {
if (connection != null)
connection.close();
}
}
/**
* <code>deleteChore</code> deletes the Chore.
* <br>
* @param connection the connection will be used for this operation. It will not be closed
* automatically.
* @param name the name of the Chore.
* @return the number of rows deleted. It is 1, if successful, 0 otherwise.
* @exception Exception.
*/
public static int deleteChore(Connection connection, String name) throws Exception {
return new ChoreProcess(connection).deleteChore(name);
}
public static void init() throws Exception {
Connection connection = null;
try {
connection = DirectTaskDatabaseConnection.getNewConnection();
new ChoreProcess(connection).setLogAgent();
} finally {
if (connection != null)
connection.close();
}
initialized = true;
}
/**
* <code>init</code> init this Api. It must be called before Api can be used.
* <br>
* @param logAgentName a LogAgentName object to use for logging. It can be null
* if you do not want log.
* @exception Exception
*/
public static void init(String logAgentName) throws Exception {
ChoreProcess.setLogAgent(logAgentName, Watchdogging.logAgentTimeout);
initialized = true;
}
/**
* <code>insertCandidateChore</code> requests SuperWatchdog to run the chore.
* A Doer will take the request and run the chore.
* @param choreName the name of the chore.
* @exception Exception.
*/
public static int insertCandidateChore(String choreName) throws Exception {
Connection connection = null;
try {
connection = DirectTaskDatabaseConnection.getNewConnection();
return new ChoreProcess(connection).insertCandidateChore(choreName);
} finally {
if (connection != null)
connection.close();
}
}
/**
* <code>insertCandidateChore</code> requests SuperWatchdog to run the chore.
* A Doer will take the request and run the chore.
* @param connection the connection will be used for this operation. It will not be closed
* automatically.
* @param choreName the name of the chore.
* @exception Exception.
*/
public static int insertCandidateChore(Connection connection, String choreName) throws Exception {
return new ChoreProcess(connection).insertCandidateChore(choreName);
}
/**
* <code>insertChore</code> insert the Chore
* <br>
* @param chore the Chore to be inserted.
* @return the number of rows changed. It is 1, if successful, 0 otherwise.
* @see com.acelet.s.chore.Chore
* @see com.acelet.s.chore.ChoreConstants
*/
public static int insertChore(Chore chore) throws Exception {
Connection connection = null;
try {
connection = DirectTaskDatabaseConnection.getNewConnection();
return new ChoreProcess(connection).insertChore(chore);
} finally {
if (connection != null)
connection.close();
}
}
/**
* <code>insertChore</code> insert the Chore
* <br>
* @param connection the connection will be used for this operation. It will not be closed
* automatically.
* @param chore the Chore to be inserted.
* @return the number of rows changed. It is 1, if successful, 0 otherwise.
* @see com.acelet.s.chore.Chore
* @see com.acelet.s.chore.ChoreConstants
*/
public static int insertChore(Connection connection, Chore chore) throws Exception {
return new ChoreProcess(connection).insertChore(chore);
}
public static boolean isInitialized() {
return initialized;
}
/**
* <code>modifyChore</code> modify the Chore.
* <br>
* @param chore the Chore to be modified.
* @param newModifiedAt the time this chore is updated.
* It should be from java.System.currentTimeMillis().
* @return the number of rows changed. It is 1, if successful, 0 otherwise.
* @see com.acelet.s.chore.Chore
* @see com.acelet.s.chore.ChoreConstants
* @see com.acelet.s.job.AbstractTaskConstants
*/
public static int modifyChore(Chore chore, long newModifiedAt) throws Exception {
Connection connection = null;
try {
connection = DirectTaskDatabaseConnection.getNewConnection();
return new ChoreProcess(connection).modifyChore(chore, newModifiedAt);
} finally {
if (connection != null)
connection.close();
}
}
/**
* <code>modifyChore</code> modify the Chore.
* <br>
* @param connection the connection will be used for this operation. It will not be closed
* automatically.
* @param chore the Chore to be modified.
* @param newModifiedAt the time this chore is updated.
* It should be from java.System.currentTimeMillis().
* @return the number of rows changed. It is 1, if successful, 0 otherwise.
* @see com.acelet.s.chore.Chore
* @see com.acelet.s.chore.ChoreConstants
* @see com.acelet.s.job.AbstractTaskConstants
*/
public static int modifyChore(Connection connection, Chore chore, long newModifiedAt)
throws Exception {
return new ChoreProcess(connection).modifyChore(chore, newModifiedAt);
}
/**
* <code>selectAllChores</code> select all Chore.
* <br>
* @return Vector of Chore
* @see com.acelet.s.chore.Chore
*/
public static Vector selectAllChores() throws Exception {
Connection connection = null;
try {
connection = DirectTaskDatabaseConnection.getNewConnection();
return new ChoreProcess(connection).selectAllChores();
} finally {
if (connection != null)
connection.close();
}
}
/**
* <code>selectAllChores</code> select all Chore.
* <br>
* @param connection the connection will be used for this operation. It will not be closed
* automatically.
* @return Vector of Chore
* @see com.acelet.s.chore.Chore
*/
public static Vector selectAllChores(Connection connection) throws Exception {
return new ChoreProcess(connection).selectAllChores();
}
/**
* <code>selectAllWorkingChores</code> select all WorkingChore.
* <br>
* @param from the start time.
* @param to the end time.
* @return Vector of WorkingChore
* @see com.acelet.s.chore.WorkingChore
*/
public static Vector selectAllWorkingChores(long from, long to) throws Exception {
Connection connection = null;
try {
connection = DirectTaskDatabaseConnection.getNewConnection();
return new ChoreProcess(connection).selectAllWorkingChores(from, to);
} finally {
if (connection != null)
connection.close();
}
}
/**
* <code>selectAllWorkingChores</code> select all WorkingChore.
* <br>
* @param connection the connection will be used for this operation. It will not be closed
* automatically.
* @param from the start time.
* @param to the end time.
* @return Vector of WorkingChore
* @see com.acelet.s.chore.WorkingChore
*/
public static Vector selectAllWorkingChores(Connection connection, long from, long to)
throws Exception {
return new ChoreProcess(connection).selectAllWorkingChores(from, to);
}
/**
* <code>selectAllWorkingChores</code> select all WorkingChore.
* <br>
* @param from the start time.
* @param to the end time.
* @param rows the number of rows to select.
* @return Vector of WorkingChore
* @see com.acelet.s.chore.WorkingChore
*/
public static Vector selectAllWorkingChores(long from, long to, int rows) throws Exception {
Connection connection = null;
try {
connection = DirectTaskDatabaseConnection.getNewConnection();
return new ChoreProcess(connection).selectAllWorkingChores(from, to, rows);
} finally {
if (connection != null)
connection.close();
}
}
/**
* <code>selectAllWorkingChores</code> select all WorkingChore.
* <br>
* @param connection the connection will be used for this operation. It will not be closed
* automatically.
* @param from the start time.
* @param to the end time.
* @param rows the number of rows to select.
* @return Vector of WorkingChore
* @see com.acelet.s.chore.WorkingChore
*/
public static Vector selectAllWorkingChores(Connection connection, long from, long to, int rows)
throws Exception {
return new ChoreProcess(connection).selectAllWorkingChores(from, to, rows);
}
/**
* <code>selectChore</code> select the Chore.
* <br>
* @param id the id of the Chore.
* @return Chore
* @see com.acelet.s.chore.Chore
* @see #selectAllChores
*/
public static Chore selectChore(long id) throws Exception {
Connection connection = null;
try {
connection = DirectTaskDatabaseConnection.getNewConnection();
return new ChoreProcess(connection).selectChore(id);
} finally {
if (connection != null)
connection.close();
}
}
/**
* <code>selectChore</code> select the Chore.
* <br>
* @param connection the connection will be used for this operation. It will not be closed
* automatically.
* @param id the id of the Chore.
* @return Chore
* @see com.acelet.s.chore.Chore
* @see #selectAllChores
*/
public static Chore selectChore(Connection connection, long id) throws Exception {
return new ChoreProcess(connection).selectChore(id);
}
/**
* <code>selectChore</code> select the Chore.
* <br>
* @param name the name of the Chore.
* @return Chore
* @see com.acelet.s.chore.Chore
* @see com.acelet.s.chore.ChoreConstants
*/
public static Chore selectChore(String name) throws Exception {
Connection connection = null;
try {
connection = DirectTaskDatabaseConnection.getNewConnection();
return new ChoreProcess(connection).selectChore(name);
} finally {
if (connection != null)
connection.close();
}
}
/**
* <code>selectChore</code> select the Chore.
* <br>
* @param connection the connection will be used for this operation. It will not be closed
* automatically.
* @param name the name of the Chore.
* @return Chore
* @see com.acelet.s.chore.Chore
* @see com.acelet.s.chore.ChoreConstants
*/
public static Chore selectChore(Connection connection, String name) throws Exception {
return new ChoreProcess(connection).selectChore(name);
}
/**
* <code>selectWorkingChore</code> select the WorkingChore.
* <br>
* @param id the id of the WorkingChore.
* @return WorkingChore
* @see com.acelet.s.chore.WorkingChore
* @see #selectAllWorkingChores
*/
public static WorkingChore selectWorkingChore(long id) throws Exception {
Connection connection = null;
try {
connection = DirectTaskDatabaseConnection.getNewConnection();
return new ChoreProcess(connection).selectWorkingChore(id);
} finally {
if (connection != null)
connection.close();
}
}
/**
* <code>selectWorkingChore</code> select the WorkingChore.
* <br>
* @param connection the connection will be used for this operation. It will not be closed
* automatically.
* @param id the id of the WorkingChore.
* @return WorkingChore
* @see com.acelet.s.chore.WorkingChore
* @see #selectAllWorkingChores
*/
public static WorkingChore selectWorkingChore(Connection connection, long id) throws Exception {
return new ChoreProcess(connection).selectWorkingChore(id);
}
/**
* <code>updateChore</code> update the Chore.
* <br>
* @param chore the Chore to be updated.
* @param newModifiedAt the time this chore is updated.
* It should be from java.System.currentTimeMillis().
* @return Chore
* @see com.acelet.s.chore.Chore
*/
public static int updateChore(Chore chore, long newModifiedAt) throws Exception {
Connection connection = null;
try {
connection = DirectTaskDatabaseConnection.getNewConnection();
return new ChoreProcess(connection).updateChore(chore, newModifiedAt);
} finally {
if (connection != null)
connection.close();
}
}
/**
* <code>updateChore</code> update the Chore.
* <br>
* @param connection the connection will be used for this operation. It will not be closed
* automatically.
* @param chore the Chore to be updated.
* @param newModifiedAt the time this chore is updated.
* It should be from java.System.currentTimeMillis().
* @return Chore
* @see com.acelet.s.chore.Chore
*/
public static int updateChore(Connection connection, Chore chore, long newModifiedAt)
throws Exception {
return new ChoreProcess(connection).updateChore(chore, newModifiedAt);
}
/**
* <code>updateWorkingChore</code> update the WorkingChore.
* <br>
* @param workingChore the WorkingChore to be updated.
* @return the number of rows changed. It is 1, if successful, 0 otherwise.
* @see com.acelet.s.chore.WorkingChore
*/
public static int updateWorkingChore(WorkingChore workingChore) throws Exception {
Connection connection = null;
try {
connection = DirectTaskDatabaseConnection.getNewConnection();
return new ChoreProcess(connection).updateWorkingChore(workingChore);
} finally {
if (connection != null)
connection.close();
}
}
/**
* <code>updateWorkingChore</code> update the WorkingChore.
* <br>
* @param connection the connection will be used for this operation. It will not be closed
* automatically.
* @param workingChore the WorkingChore to be updated.
* @return the number of rows changed. It is 1, if successful, 0 otherwise.
* @see com.acelet.s.chore.WorkingChore
*/
public static int updateWorkingChore(Connection connection, WorkingChore workingChore)
throws Exception {
return new ChoreProcess(connection).updateWorkingChore(workingChore);
}
}