package com.commander4j.tablemodel;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import javax.swing.table.AbstractTableModel;
import com.commander4j.db.JDBAutoLabeller;
import com.commander4j.db.JDBControl;
import com.commander4j.sys.Common;
/**
*/
public class JDBAutoLabellerTableModel extends AbstractTableModel
{
private static final long serialVersionUID = 1;
public static final int Line_Col = 0;
public static final int Description_Col = 1;
public static final int Unique_ID_Col = 2;
public static final int Use_SSCC_Range_Col = 3;
public static final int SSCC_Prefix_Col = 4;
public static final int SSCC_Range_Col = 5;
private String ssccPrefix = "";
private String hostID;
private String sessionID;
private JDBControl ctrl;
private String[] mcolNames = { "Line","Description", "Unique ID","Use","SSCC Prefix","Sequence No" };
private ResultSet mResultSet;
private int prowCount = -1;
private HashMap<Integer,JDBAutoLabeller> cache = new HashMap<Integer,JDBAutoLabeller>();
public JDBAutoLabellerTableModel(String host,String session)
{
setHostID(host);
setSessionID(session);
ctrl = new JDBControl(getHostID(),getSessionID());
ssccPrefix = ctrl.getKeyValue("SSCC PREFIX");
}
private String getSessionID() {
return sessionID;
}
private String getHostID() {
return hostID;
}
private void setHostID(String host) {
hostID = host;
}
private void setSessionID(String session) {
sessionID = session;
}
public void setResultSet(ResultSet rs)
{
try
{
cache.clear();
rs.setFetchSize(20);
}
catch (SQLException e)
{
e.printStackTrace();
}
prowCount = -1;
mResultSet = rs;
}
public int getColumnCount() {
return mcolNames.length;
}
public int getRowCount() {
try
{
if (prowCount <= 0)
{
mResultSet.last();
prowCount = mResultSet.getRow();
mResultSet.beforeFirst();
}
return prowCount;
}
catch (Exception e)
{
return 0;
}
}
public void setValueAt(Object value, int row, int col) {
}
public String getColumnName(int col) {
return mcolNames[col];
}
public Object getValueAt(int row, int col) {
try
{
if (cache.containsKey(row)==false)
{
mResultSet.absolute(row + 1);
final JDBAutoLabeller prow = new JDBAutoLabeller(Common.selectedHostID, Common.sessionID);
prow.getPropertiesfromResultSet(mResultSet);
cache.put(row, prow);
}
switch (col)
{
case Line_Col:
return cache.get(row).getLine();
case Description_Col:
return cache.get(row).getDescription();
case Unique_ID_Col:
return cache.get(row).getUniqueID();
case SSCC_Prefix_Col:
return ssccPrefix;
case Use_SSCC_Range_Col:
return cache.get(row).isSSCCRangeEnabled();
case SSCC_Range_Col:
return cache.get(row).getSSCCSequence().toString();
}
}
catch (Exception ex)
{
return "Error";
}
return new String();
}
}