/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.administrator.register.writer;
import cli_fmw.main.ClipsException;
import cli_fmw.utils.ErrorValue;
import clips.administrator.register.LpuFields;
import clips.dicom.utils.Pair;
import framework.utils.Converter;
import java.io.IOException;
import java.util.Date;
import javax.swing.table.AbstractTableModel;
import org.xBaseJ.micro.DBF;
import org.xBaseJ.micro.fields.DateField;
import org.xBaseJ.micro.xBaseJException;
/**
*
* @author vip
*/
public class TableModelRegister extends AbstractTableModel{
public static final int COLCOUNT = 20;
public static final int COL_0 = 0;
public static final int COL_ROW = 1;
public static final int COL_FIO = 2;
public static final int COL_3 = 3;
public static final int COL_POLIS = 4;
public static final int COL_INSURER = 5;
public static final int COL_DATE_BORN = 6;
public static final int COL_SEX = 7;
public static final int COL_ADDRESS = 8;
public static final int COL_SOCIAL = 9;
public static final int COL_DATE_VISIT = 10;
public static final int COL_FACT = 11;
public static final int COL_MES = 12;
public static final int COL_CODE_MES = 13;
public static final int COL_TARIF = 14;
public static final int COL_15 = 15;
public static final int COL_UKL = 16;
public static final int COL_MKB = 17;
public static final int COL_SUMM = 18;
public static final int COL_RESULT = 19;
private int lastRow = -1;
private DBF regLpu;
private WriterAbstract writer;
public TableModelRegister(WriterAbstract writer) {
this.writer = writer;
}
@Override
public int getRowCount() {
return writer.getRowCount();
}
@Override
public int getColumnCount() {
return COLCOUNT;
}
public Pair<Date, Date> getDates() throws xBaseJException, IOException {
return writer.getDates();
}
public String getInsurerTitle() {
return writer.getTitle();
}
public Date getDateGenerated() {
return writer.getDateGenerated();
}
@Override
public Object getValueAt(int row, int col) {
System.err.println("col = " + col);
try {
if (lastRow != row) {
regLpu = writer.read(row);
lastRow = row;
}
switch (col) {
case COL_0: return "";
case COL_ROW: return row + 1;
case COL_FIO: {
return regLpu.getField(LpuFields.FAM).get().trim()
+ " " + regLpu.getField(LpuFields.NAM).get().trim()
+ "\n" + regLpu.getField(LpuFields.FNAM).get().trim();
}
case COL_3: return "";
case COL_POLIS: return regLpu.getField(LpuFields.POL_NUM).get().trim();
case COL_INSURER: return regLpu.getField(LpuFields.INS_ORG).get().trim();
case COL_DATE_BORN: {
DateField date = (DateField) regLpu.getField(LpuFields.BDAY);
return Converter.dateToString(date.getCalendar().getTime(),"dd.MM.yy");
}
case COL_SEX: {
String pol = regLpu.getField(LpuFields.SEX).get().trim();
if (pol.equals("1")) {
return "М";
} else if (pol.equals("2")){
return "Ж";
} else {
return "?";
}
}
case COL_ADDRESS: {
String adr = "";
String str = regLpu.getField(LpuFields.AREA).get().trim();
if (!str.isEmpty()) {
adr = adr + str + ",";
}
str = regLpu.getField(LpuFields.REGION).get().trim();
if (!str.isEmpty()) {
adr = adr + " " + str + ",";
}
str = regLpu.getField(LpuFields.ITEM).get().trim();
if (!str.isEmpty()) {
adr = adr + " " + str + ",";
}
str = regLpu.getField(LpuFields.STREET).get().trim();
if (!str.isEmpty()) {
adr = adr + " " + str + ",";
}
str = regLpu.getField(LpuFields.HOUSE).get().trim();
if (!str.isEmpty()) {
adr = adr + " д." + str;
str = regLpu.getField(LpuFields.LITER).get().trim();
if (!str.isEmpty()) {
adr = adr + str;
}
}
str = regLpu.getField(LpuFields.FLAT).get().trim();
if (!str.isEmpty()) {
adr = adr + " кв." + str;
}
return adr;
}
case COL_SOCIAL: return regLpu.getField(LpuFields.STATUS).get().trim();
case COL_DATE_VISIT: {
DateField date = (DateField) regLpu.getField(LpuFields.DATE_BEGIN);
return Converter.dateToString(date.getCalendar().getTime(),"dd.MM.yy");
}
case COL_FACT: return "1";
case COL_MES: return "1";
case COL_CODE_MES: return regLpu.getField(LpuFields.MES).get().trim();
case COL_TARIF: return "";
case COL_15: return "";
case COL_UKL: return regLpu.getField(LpuFields.UKL).get().trim();
case COL_MKB: return regLpu.getField(LpuFields.MKB).get().trim();
case COL_SUMM: return "";
case COL_RESULT: return regLpu.getField(LpuFields.OUT).get().trim();
default: throw new IllegalArgumentException("Неверный номер столбца : " + col);
}
} catch (xBaseJException ex) {
return new ErrorValue(new ClipsException("Не удалось прочитать DBF", ex));
} catch (IOException ex) {
return new ErrorValue(new ClipsException("Не удалось прочитать DBF", ex));
}
}
}