public static TabularData createTable(ResultSet rs) throws Exception { ResultSetMetaData rsMetaData = rs.getMetaData(); int numCols = rsMetaData.getColumnCount(); String[] headers = new String[numCols]; OpenType[] allTypes = new OpenType[numCols]; Vector[] values = new Vector[numCols]; Object[] allValues = new Object[numCols]; for (int i = 0; i < numCols; i++) { headers[i] = rsMetaData.getColumnName(i + 1); allTypes[i] = new ArrayType(1, SimpleType.STRING); values[i] = new Vector(); } CompositeType ct = new CompositeType("column and values", "column and values", headers, headers, allTypes); TabularType tt = new TabularType("column and values", "column with values", ct, headers); TabularData td = new TabularDataSupport(tt); while (rs.next()) { for (int i = 0; i < numCols; i++) { values[i].add(rs.getString(i + 1)); } } for (int i = 0; i < numCols; i++) { allValues[i] = (String[]) values[i].toArray(new String[0]); } CompositeData entry = new CompositeDataSupport(ct, headers, allValues); td.put(entry); return td; }
/** * Sets the TabularData to the AaplicationTable * * @param data The TabularData to be set to the AaplicationTable */ public void setAlarmTable(TabularData data) throws Exception { AgentException ae = null; for (Enumeration e = data.enumerate(); e.hasMoreElements(); ) { Object[] index = (Object[]) e.nextElement(); CompositeData comp = data.getRow(index); if (table != null) entry = (AlarmEntry) Utilities.getEntryFromCompositeData(table, comp, indexNames, instrClassName); else if (vec != null) entry = (AlarmEntry) Utilities.getEntryFromCompositeData(vec, comp, indexNames, instrClassName); if (comp.getOperationType().equals(CompositeData.CREATED)) { // create new entry if (entry != null) throw new AgentException("Row already exist", CommonUtils.ROWCREATIONFAILED); // no i18n entry = new AlarmEntry(); if (table != null) table.put(index, entry); else if (vec != null) vec.addElement(entry); for (Enumeration ce = comp.enumerate(); ce.hasMoreElements(); ) { String key = (String) ce.nextElement(); try { Utilities.setField(entry, instrClassName, key, comp.getDataItem(key)); } catch (AgentException aexp) { ae = aexp; } } } else if (comp.getOperationType().equals(CompositeData.DELETED)) { if (table != null) { for (Enumeration en = table.keys(); en.hasMoreElements(); ) { Object keyObject = en.nextElement(); if (entry.equals(table.get(keyObject))) table.remove(keyObject); } } else if (vec != null) if (!vec.removeElement(entry)) throw new AgentException("Invalid Index", CommonUtils.INVALIDINDEX); // no i18n data.deleteRow(index); } else if (comp.getOperationType().equals(CompositeData.MODIFIED)) { for (Enumeration ce = comp.enumerate(); ce.hasMoreElements(); ) { String key = (String) ce.nextElement(); if (!comp.isModified(key)) continue; try { Utilities.setField(entry, instrClassName, key, comp.getDataItem(key)); } catch (AgentException aexp) { ae = aexp; } } } comp.setOperationType(CompositeData.NOCHANGES); } if (ae != null) throw ae; }