package de.desy.tine.alarmUtils;
import java.io.IOException;
import java.util.Arrays;
import de.desy.tine.client.TLink;
import de.desy.tine.dataUtils.TDataType;
import de.desy.tine.definitions.TAccess;
import de.desy.tine.definitions.TErrorList;
public class TAlarmDataBase
{
private static final int nice_query_size = 1000;
private static CasAction[] actions = new CasAction[nice_query_size/10];
private static CasDb[] databases = new CasDb[nice_query_size];
private static CasAlarmSys[] systems = new CasAlarmSys[nice_query_size/10];
private static boolean isInitialized = false;
private static void initStructs()
{
if (isInitialized) return;
for (int i=0; i<nice_query_size; i++)
{
databases[i] = new CasDb();
}
for (int i=0; i<nice_query_size/10; i++)
{
actions[i] = new CasAction();
}
for (int i=0; i<nice_query_size/10; i++)
{
systems[i] = new CasAlarmSys();
}
isInitialized = true;
}
public static CasDb[] getServerList(String context) throws IOException
{
// "SERVER.DB"
if (context == null) return null;
if (!isInitialized) initStructs();
for (CasDb p : databases) p.clear();
TDataType dtdb = new TDataType(databases);
String dname = "/" + context + "/CAS";
TLink tl = new TLink(dname,"SERVER.DB",dtdb,null,TAccess.CA_READ);
if (tl.executeAndClose(1000) != 0) throw new IOException(tl.getLastError());
int len = dtdb.getCompletionLength();
CasDb[] results = new CasDb[len];
for (int i=0; i<len; i++) results[i] = new CasDb(databases[i]);
return results;
}
public static int setServerList(String context,CasDb[] dbItems) throws IOException
{
if (context == null || dbItems == null) return TErrorList.argument_list_error;
if (!isInitialized) initStructs();
TDataType dtrecs = new TDataType(dbItems);
String dname = "/" + context + "/CAS";
TLink tl = new TLink(dname,"SERVER.DB",null,dtrecs,TAccess.CA_WRITE);
int cc = tl.executeAndClose(2000);
if (cc == TErrorList.connection_timeout || cc == TErrorList.link_timeout)
throw new IOException(tl.getLastError());
return cc;
}
public static CasAction[] getActionList(String context,String server) throws IOException
{
//"ACTION.DB"
if (context == null || server == null) return null;
if (!isInitialized) initStructs();
for (CasAction p : actions) p.clear();
TDataType dtact = new TDataType(actions);
String dname = "/" + context + "/CAS/" + server;
TLink tl = new TLink(dname,"ACTION.DB",dtact,null,TAccess.CA_READ);
if (tl.executeAndClose(1000) != 0) throw new IOException(tl.getLastError());
int len = dtact.getCompletionLength();
CasAction[] results = new CasAction[len];
for (int i=0; i<len; i++) results[i] = new CasAction(actions[i]);
return results;
}
public static int setActionList(String context,String server,CasAction[] actionItems) throws IOException
{
if (context == null || actionItems == null || server == null) return TErrorList.argument_list_error;
if (!isInitialized) initStructs();
TDataType dtrecs;
if (actionItems.length == 0) {
dtrecs = new TDataType();
} else {
dtrecs = new TDataType(actionItems);
}
String dname = "/" + context + "/CAS/" + server;
TLink tl = new TLink(dname,"ACTION.DB",null,dtrecs,TAccess.CA_WRITE);
int cc = tl.executeAndClose(2000);
if (cc == TErrorList.connection_timeout || cc == TErrorList.link_timeout)
throw new IOException(tl.getLastError());
return cc;
}
public static CasAlarmSys[] getAlarmSystemList(String context) throws IOException
{
if (context == null) return null;
if (!isInitialized) initStructs();
for (CasAlarmSys p : systems) p.clear();
TDataType dtact = new TDataType(systems);
String dname = "/" + context + "/CAS";
TLink tl = new TLink(dname,"ALMSYSTEM.DB",dtact,null,TAccess.CA_READ);
if (tl.executeAndClose(1000) != 0) throw new IOException(tl.getLastError());
int len = dtact.getCompletionLength();
CasAlarmSys[] results = new CasAlarmSys[len];
for (int i=0; i<len; i++) results[i] = new CasAlarmSys(systems[i]);
return results;
}
public static int setAlarmSystemList(String context,CasAlarmSys[] dbItems) throws IOException
{
if (context == null || dbItems == null) return TErrorList.argument_list_error;
if (!isInitialized) initStructs();
TDataType dtrecs = new TDataType(dbItems);
String dname = "/" + context + "/CAS";
TLink tl = new TLink(dname,"ALMSYSTEM.DB",null,dtrecs,TAccess.CA_WRITE);
int cc = tl.executeAndClose(2000);
if (cc == TErrorList.connection_timeout || cc == TErrorList.link_timeout)
throw new IOException(tl.getLastError());
return cc;
}
public static int[] getAlarmSystemCodeList(String context,String server) throws IOException
{
if (context == null || server == null) return null;
if (!isInitialized) initStructs();
int[] results = new int[25];
TDataType dtcodes = new TDataType(results);
String dname = "/"+context+"/CAS/"+server;
TLink tl = new TLink(dname,"AlmSystem4Server",dtcodes,null,TAccess.CA_READ);
if (tl.executeAndClose(1000) != 0) throw new IOException(tl.getLastError());
int len = dtcodes.getCompletionLength();
return Arrays.copyOf(results, len);
}
public static int setAlarmSystemCodeList(String context,String server,int[] sysCodes) throws IOException
{
if (context == null || server == null || sysCodes == null) return TErrorList.argument_list_error;
if (!isInitialized) initStructs();
TDataType dtcodes = new TDataType(sysCodes);
String dname = "/"+context+"/CAS/"+server;
TLink tl = new TLink(dname,"AlmSystem4Server",null,dtcodes,TAccess.CA_WRITE);
int cc = tl.executeAndClose(1000);
if (cc == TErrorList.connection_timeout || cc == TErrorList.link_timeout)
throw new IOException(tl.getLastError());
return cc;
}
public static int reloadDataBase(String context) throws IOException
{
if (context == null) return TErrorList.argument_list_error;
if (!isInitialized) initStructs();
String dname = "/" + context + "/CAS";
TLink tl = new TLink(dname,"RESTART",null,null,TAccess.CA_WRITE);
int cc = tl.executeAndClose(3000);
if (cc == TErrorList.connection_timeout || cc == TErrorList.link_timeout)
throw new IOException(tl.getLastError());
return cc;
}
}