package de.desy.tine.addrUtils;
import java.util.ArrayList;
import de.desy.tine.csvUtils.csv;
import de.desy.tine.startup.*;
public class DBQuery
{
private static csv srvFile;
static TInitializer initializer = TInitializerFactory.getInstance().getInitializer();
public static String[] getContextsFromFileCache(String srvName)
{
String cntName;
ArrayList<String> ctxNames = new ArrayList<String>(10);
if (srvName != null && srvName.length() == 0) srvName = null;
String[] lst = null;
srvFile = new csv(initializer.getEquipmentResource());
try
{
String s, hdr;
int nam_col = -1,ctxt_col = -1;
boolean done = false;
while((s=srvFile.readLine()) != null)
{
if (s.length() == 0) continue;
if (s.startsWith("#") || s.startsWith(";") || s.startsWith("%")) continue;
if (!done)
{
hdr = s;
if ((nam_col=srvFile.findcol(hdr,"NAME")) < 0) throw new NoSuchFieldException();
if ((ctxt_col=srvFile.findcol(hdr,"CONTEXT")) < 0) throw new NoSuchFieldException();
done = true;
continue;
}
cntName = srvFile.colptr(ctxt_col,s);
if (srvName != null && srvFile.namcmp(srvName,s,nam_col) != 0)
continue;
boolean found = false;
for (String c : ctxNames.toArray(new String[0]))
{
if (c.compareToIgnoreCase(cntName) == 0) found = true;
}
if (!found) ctxNames.add(cntName);
}
if (ctxNames.size() > 0) lst = ctxNames.toArray(new String[0]);
}
catch (Exception e)
{ // database corrupt or not found
}
finally
{
if (srvFile != null) srvFile.close();
}
return lst;
}
public static String[] getSubsystemsFromFileCache(String ctxName)
{
String subName;
ArrayList<String> subNames = new ArrayList<String>(10);
if (ctxName != null)
{
if (ctxName.length() == 0) ctxName = null;
else if (ctxName.compareToIgnoreCase("DEFAULT") == 0) ctxName = null;
}
String[] lst = null;
srvFile = new csv(initializer.getEquipmentResource());
try
{
String s, hdr;
int sub_col = -1,ctxt_col = -1;
boolean done = false;
while((s=srvFile.readLine()) != null)
{
if (s.length() == 0) continue;
if (s.startsWith("#") || s.startsWith(";") || s.startsWith("%")) continue;
if (!done)
{
hdr = s;
if ((sub_col=srvFile.findcol(hdr,"SUBSYSTEM")) < 0) throw new NoSuchFieldException();
if ((ctxt_col=srvFile.findcol(hdr,"CONTEXT")) < 0) throw new NoSuchFieldException();
done = true;
continue;
}
subName = srvFile.colptr(sub_col,s);
if (ctxName != null && srvFile.namcmp(ctxName,s,ctxt_col) != 0)
continue;
boolean found = false;
for (String c : subNames.toArray(new String[0]))
{
if (c.compareToIgnoreCase(subName) == 0) found = true;
}
if (!found) subNames.add(subName);
}
if (subNames.size() > 0) lst = subNames.toArray(new String[0]);
}
catch (Exception e)
{ // database corrupt or not found
}
finally
{
if (srvFile != null) srvFile.close();
}
return lst;
}
static public String[] getServersFromFileCache(String ctxName,String subName)
{
String srvName;
ArrayList<String> srvNames = new ArrayList<String>(100);
if (ctxName != null)
{
if (ctxName.length() == 0) ctxName = null;
else if (ctxName.compareToIgnoreCase("DEFAULT") == 0) ctxName = null;
}
if (subName != null)
{
if (subName.length() == 0) subName = null;
else if (subName.compareToIgnoreCase("ALL") == 0) subName = null;
}
String[] lst = null;
srvFile = new csv(initializer.getEquipmentResource());
try
{
String s, hdr;
int nam_col = -1,sub_col=-1,ctxt_col = -1;
boolean done = false;
while((s=srvFile.readLine()) != null)
{
if (s.length() == 0) continue;
if (s.startsWith("#") || s.startsWith(";") || s.startsWith("%")) continue;
if (!done)
{
hdr = s;
if ((nam_col=srvFile.findcol(hdr,"NAME")) < 0) throw new NoSuchFieldException();
if ((ctxt_col=srvFile.findcol(hdr,"CONTEXT")) < 0) throw new NoSuchFieldException();
sub_col = srvFile.findcol(hdr, "SUBSYSTEM");
done = true;
continue;
}
srvName = srvFile.colptr(nam_col,s);
if (ctxName != null && srvFile.namcmp(ctxName,s,ctxt_col) != 0)
continue;
if (subName != null && srvFile.namcmp(subName, s, sub_col) != 0)
continue;
boolean found = false;
for (String c : srvNames.toArray(new String[0]))
{
if (c.compareToIgnoreCase(srvName) == 0) found = true;
}
if (!found) srvNames.add(srvName);
}
if (srvNames.size() > 0) lst = srvNames.toArray(new String[0]);
}
catch (Exception e)
{ // database corrupt or not found
}
finally
{
if (srvFile != null) srvFile.close();
}
return lst;
}
}