/* Copyright 1999-2008 Acelet.org. All rights reserved. GPL v2 license */
/** @author Wei Jiang */
package com.acelet.s.watchdog;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import java.io.*;
import javax.naming.Context;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import com.acelet.lib.*;
import com.acelet.s.*;
import com.acelet.s.scheduler.DirectTaskDatabaseConnection;
import com.acelet.s.scheduler.DirectTaskDatabaseConnectionPanel;
import com.acelet.s.scheduler.SchedulerConstants;
import com.acelet.s.scheduler.EntitlementAdminLoginPanel;
import com.acelet.s.scheduler.EntitlementTablePanel;
import com.acelet.s.scheduler.RolePanel;
import com.acelet.s.scheduler.RoleTablePanel;
import com.acelet.s.scheduler.SchedulerConstants;
public class WatchdogManagerPanel implements ActionListener, SchedulerConstants {
static boolean isFirstTime = true;
protected JMenu menu;
ImageIcon watchdogIcon1;
ImageIcon watchdogIcon2;
ImageIcon historyIcon;
ImageIcon doertalkersIcon;
ImageIcon taskDatabaseIcon;
ImageIcon preferenceIcon;
JMenuItem watchdogMenuItem;
JMenuItem taskDatabaseMenuItem;
JMenuItem preferenceMenuItem;
JMenuItem historyMenuItem;
JMenuItem doertalkersMenuItem;
JMenuItem entitlementMenu;
JMenuItem entitlementMenuItem;
JMenuItem roleMenuItem;
String watchdogDoerName;
String watchdogTalkerName;
public WatchdogManagerPanel() throws Exception {
watchdogDoerName = Phrase.get("TX_WATCHDOG") + " " + Phrase.get("TX_DOER");
watchdogTalkerName = Phrase.get("TX_WATCHDOG") + " " + Phrase.get("TX_TALKER");
if (isFirstTime) {
isFirstTime = false;
}
loadIcons();
}
public void actionPerformed(ActionEvent event) {
try {
Object source = event.getSource();
if (source == watchdogMenuItem) {
if (makeSureApiIsInitialized()) {
int entitlement = DirectTaskDatabaseConnection.entitlement;
if ((entitlement & ENTITLEMENT_DOER) == 0) {
InfoBox.exhibit(Globals.masterWindow, Phrase.get("ER_NOT_ENTITLED_TO_OPEN_DOER"),
Phrase.get("TX_ERROR"), InfoBox.OK, null,
"Errors." + "ER_NOT_ENTITLED_TO_OPEN_DOER");
return;
}
MainTabbedPane.showSuperWatchdog();
}
} else if (source == preferenceMenuItem) {
try {
makeSureApiIsInitialized();
} catch (Exception e) {
InfoBox.exhibit(Globals.masterWindow, InfoBox.OK, e);
}
preference();
} else if (source == historyMenuItem) {
if (makeSureApiIsInitialized())
history();
} else if (source == doertalkersMenuItem) {
if (makeSureApiIsInitialized())
doertalkers();
} else if (source == taskDatabaseMenuItem) {
taskDatabase();
} else if (source == entitlementMenuItem) {
if (makeSureApiIsInitialized())
entitlement();
} else if (source == roleMenuItem) {
if (makeSureApiIsInitialized())
role();
}
} catch (Throwable ex) {
InfoBox.exhibit(Globals.masterWindow, InfoBox.OK, ex);
}
}
protected boolean checkFreshInstallation() throws Exception {
boolean flag =
new WatchdogProperties().getBoolean(WatchdogProperties.IS_FRESH_INSTALLATION, true);
if (flag) {
unsetFreshInstallationFlag();
return true;
} else
return false;
}
void doertalkers() throws Exception {
String title = Phrase.get("TX_DOERTALKER_TABLE");
DoerTalkerTablePanel doerTalkerTablePanel = new DoerTalkerTablePanel();
if (doerTalkerTablePanel.getReady() == false)
return;
new CommonDlg(Globals.masterWindow, title, doerTalkerTablePanel);
}
public void entitlement() throws Exception {
Connection connection = getEntitlementConnection();
if (connection == null)
return;
EntitlementTablePanel panel = new EntitlementTablePanel(connection);
if (panel.getReady() == false)
return;
new CommonDlg(Globals.masterWindow, Phrase.get("TX_ENTITLEMENT_TABLE"), panel);
connection.close();
}
Connection getEntitlementConnection() throws Exception {
EntitlementAdminLoginPanel panel = new EntitlementAdminLoginPanel();
if (panel.getReady() == false)
return null;
new CommonDlg(Globals.masterWindow, Phrase.get("TX_ENTITIEMENT_ADMIN_LOGIN"), panel);
InfoBox.exhibit(Globals.masterWindow,
Phrase.get("TX_ENTITLEMENT_AND_ROLE_CHANGES_TAKE_EFFECT_ON_RESTART"),
Phrase.get("TX_ATTENTION"), InfoBox.OK, null,
"Messages." + "TX_ENTITLEMENT_AND_ROLE_CHANGES_TAKE_EFFECT_ON_RESTART");
return panel.connection;
}
public Object getMenuItem() throws Exception {
if (menu != null)
return menu;
menu = new JMenu();
loadIcons();
MakeMenu makeMenu = new MakeMenu();
watchdogMenuItem = makeMenu.makeMenuItem(Phrase.get("TX_SUPERWATCHDOG"), watchdogIcon1,
this, "WatchdogWindows.MenuCommands");
historyMenuItem = makeMenu.makeMenuItem(Phrase.get("TX_HISTORY"), historyIcon,
this, "WatchdogWindows.MenuCommands");
doertalkersMenuItem = makeMenu.makeMenuItem(Phrase.get("TX_DOERTALKERS"), doertalkersIcon,
this, "WatchdogWindows.MenuCommands");
taskDatabaseMenuItem = makeMenu.makeMenuItem(Phrase.get("TX_TASK_DATABASE"),
taskDatabaseIcon, this, "WatchdogWindows.MenuCommands");
preferenceMenuItem = makeMenu.makeMenuItem(Phrase.get("TX_PREFERENCE"), preferenceIcon,
this, "WatchdogWindows.MenuCommands");
entitlementMenu = makeMenu.makeMenu(Phrase.get("TX_ENTITLEMENT"), "entitlement.gif",
"SchedulerWindows.MenuCommands");
entitlementMenuItem = makeMenu.makeMenuItem(Phrase.get("TX_ENTITLEMENT"), (ImageIcon) null,
this, "SchedulerWindows.MenuCommands");
roleMenuItem = makeMenu.makeMenuItem(Phrase.get("TX_ROLE"), (ImageIcon) null,
this, "SchedulerWindows.MenuCommands");
entitlementMenu.add(entitlementMenuItem);
entitlementMenu.add(roleMenuItem);
int nn = 0;
menu.add(watchdogMenuItem, nn++);
menu.add(historyMenuItem, nn++);
menu.add(doertalkersMenuItem, nn++);
menu.add(taskDatabaseMenuItem, nn++);
menu.add(preferenceMenuItem, nn++);
menu.add(entitlementMenu, nn++);
return menu;
}
void history() throws Exception {
long from = -1;
long to = -1;
boolean activitySelected;
String caption = Phrase.get("TX_CHOOSE_HISTORY");
PeriodPanel periodPanel = new PeriodPanel();
if (periodPanel.getReady() == false)
return;
new CommonDlg(Globals.masterWindow, caption, periodPanel);
if (periodPanel.getAnswer() != CommonPanel.OK)
return;
else {
from = periodPanel.getFrom();
to = periodPanel.getTo();
if ( to > System.currentTimeMillis())
to = System.currentTimeMillis();
}
String title = Phrase.get("TX_HISTORY_TABLE");
HistoryTablePanel historyTablePanel = new HistoryTablePanel(from, to);
if (historyTablePanel.getReady() == false)
return;
new CommonDlg(Globals.masterWindow, title, historyTablePanel);
}
protected void loadIcons() {
LoadFile loadFile = new LoadFile();
watchdogIcon1 = loadFile.receiveImageIcon("watchdog.gif");
historyIcon = loadFile.receiveImageIcon("history.gif");
doertalkersIcon = loadFile.receiveImageIcon("doertalker.gif");
taskDatabaseIcon = loadFile.receiveImageIcon("database.gif");
preferenceIcon = loadFile.receiveImageIcon("preference.gif");
}
boolean makeSureApiIsInitialized() throws Exception {
while (DirectTaskDatabaseConnection.testConnection() == null) {
DirectTaskDatabaseConnection directTaskDatabaseConnection =
new DirectTaskDatabaseConnection();
WaitDialog waitDialog = new WaitDialog(null, directTaskDatabaseConnection);
waitDialog.runIt();
if (waitDialog.isCancelled())
return false;
Throwable ex = directTaskDatabaseConnection.getException();
if (ex != null) {
InfoBox.exhibit(null, Phrase.get("ER_CONNECT_DATABASE"),
Phrase.get("TX_ERROR"), InfoBox.OK, ex, "Errors." + "ER_CONNECT_DATABASE");
String title = Phrase.get("TX_DIRECT_DATABASE_CONNECTION");
DirectTaskDatabaseConnectionPanel directTaskDatabaseConnectionPanel =
new DirectTaskDatabaseConnectionPanel(false);
directTaskDatabaseConnectionPanel.getReady();
new CommonDlg(null, title, directTaskDatabaseConnectionPanel);
if (directTaskDatabaseConnectionPanel.getAnswer() != CommonPanel.OK)
return false;
}
}
if (Api.isInitialized())
return true;
InitApiRunnable initApiRunnable = new InitApiRunnable();
WaitDialog waitDialog = new WaitDialog(Globals.masterWindow, initApiRunnable);
waitDialog.runIt();
if (waitDialog.isCancelled())
return false;
Throwable exception = initApiRunnable.getException();
if (exception != null) {
InfoBox.exhibit(Globals.masterWindow,
Phrase.get("ER_CANNOT_INIT_WATCHDOG_API"),
Phrase.get("TX_ERROR"), InfoBox.OK, exception,
"Errors." + "ER_CANNOT_INIT_WATCHDOG_API");
return false;
}
return true;
}
void preference() throws Exception {
String title = Phrase.get("TX_PREFERENCE");
PreferencePanel preferencePanel = new PreferencePanel();
if (preferencePanel.getReady() == false)
return;
new CommonDlg(Globals.masterWindow, title, preferencePanel);
}
protected void prepareData() throws Exception {
}
public void role() throws Exception {
Connection connection = getEntitlementConnection();
if (connection == null)
return;
RoleTablePanel panel = new RoleTablePanel(connection);
if (panel.getReady() == false)
return;
new CommonDlg(Globals.masterWindow, Phrase.get("TX_ROLE_TABLE"), panel);
connection.close();
}
void taskDatabase() throws Exception {
InfoBox.exhibit(Globals.masterWindow,
Phrase.get("TX_YOU_NEED_TO_SHUTDOWN_ALL_SUPERSCHEDULER_AND_SUPERWATCHDOG_FOR_DATABASE_PARAMETER_CHANGE"),
Phrase.get("TX_ATTENTION"), InfoBox.OK, null,
"Messages." + "TX_YOU_NEED_TO_SHUTDOWN_ALL_SUPERSCHEDULER_AND_SUPERWATCHDOG_FOR_DATABASE_PARAMETER_CHANGE");
String title = Phrase.get("TX_DIRECT_TASK_DATABASE_CONNECTION");
DirectTaskDatabaseConnectionPanel directTaskDatabaseConnectionPanel =
new DirectTaskDatabaseConnectionPanel(false);
directTaskDatabaseConnectionPanel.getReady();
new CommonDlg(Globals.masterWindow, title, directTaskDatabaseConnectionPanel);
}
void unsetFreshInstallationFlag() throws Exception {
new WatchdogProperties().putBoolean(WatchdogProperties.IS_FRESH_INSTALLATION, false);
}
}