/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.contract.info.contractService;
import cli_fmw.main.ClipsException;
import clips.delegate.contract.ContractLocal;
import clips.delegate.contract.ServisePriceData;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import clips.delegate.directory.filtered.DirectoryService;
import clips.delegate.directory.filtered.DirectoryServiceItem;
import clips.delegate.directory.complex.DirectoryServicesGroupItem;
import cli_fmw.utils.ErrorValue;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.Selector;
import clips.delegate.directory.simple.receptionType.DirectoryReceptionType;
import clips.delegate.directory.simple.receptionType.DirectoryReceptionTypeItem;
import beans.directory.service.entity.ServiceGroup;
import framework.utils.Pair;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
/**
* Модель, отображающая услуги, входящие в группу услуг
* @author ViP
*/
class TableModelServicePrice extends AbstractTableModel {
public static final int COLCOUNT = 5;
public static final int COL_INCONTRACT = 0;
public static final int COL_RECEPTION_TYPE = 1;
public static final int COL_SERVICE = 2;
public static final int COL_PRICE = 3;
public static final int COL_MONEY = 4;
private ContractLocal contract; //Договор
private ArrayList<Pair<DirectoryServiceItem, DirectoryReceptionTypeItem>> serRecTypeList
= new ArrayList<Pair<DirectoryServiceItem, DirectoryReceptionTypeItem>>();
private DirectoryServicesGroupItem servicesGroup;
private boolean isVisit = false;
/**
*
* @param serviceGroup
* @param contract
* @throws cli_fmw.delegate.exception.ClipsException
*/
public TableModelServicePrice(DirectoryServicesGroupItem serviceGroup, ContractLocal contract) throws ClipsException {
this.contract = contract;
DirectoryService svc = DirectoryLocator.getDirectory(DirectoryService.class, false);
DirectoryReceptionType dirRecType = DirectoryLocator.getDirectory(DirectoryReceptionType.class);
isVisit = (serviceGroup != null && serviceGroup.getID() == ServiceGroup.SERVICE_GROUP_VISIT);
Selector<DirectoryServiceItem> services = svc.getFilteredItems(serviceGroup);
for (DirectoryReceptionTypeItem receptionType : dirRecType.getItemsList()) {
for (int i = 0; i < services.size(); i++) {
DirectoryServiceItem service = services.get(i);
if (isVisit) {
if (receptionType.getID() != 0 && !receptionType.getExtKey().isEmpty()) {
//В посещениях отображаем только типы приемов, имеющие внешний ключ
Pair<DirectoryServiceItem, DirectoryReceptionTypeItem> pair = new Pair<DirectoryServiceItem, DirectoryReceptionTypeItem>();
pair.first = service;
pair.second = receptionType;
serRecTypeList.add(pair);
}
} else {
if (receptionType.getID() == 0) {
//В услугах отображаем только нулевые типы приемов
Pair<DirectoryServiceItem, DirectoryReceptionTypeItem> pair = new Pair<DirectoryServiceItem, DirectoryReceptionTypeItem>();
pair.first = service;
pair.second = receptionType;
serRecTypeList.add(pair);
}
}
}
}
this.servicesGroup = serviceGroup;
}
@Override
public String getColumnName(int column) {
switch (column) {
case COL_INCONTRACT: return "В договоре";
case COL_SERVICE: return isVisit ? "Посещение" : "Услуга";
case COL_RECEPTION_TYPE: return "Тип приема";
case COL_PRICE: return "Цена";
case COL_MONEY: return "По договору";
default: throw new IllegalArgumentException("Некорректный номер столбца : " + column);
}
}
@Override
public int getRowCount() {
return serRecTypeList.size();
}
@Override
public int getColumnCount() {
return COLCOUNT;
}
@Override
public Object getValueAt(int row, int col) {
try {
Pair<DirectoryServiceItem, DirectoryReceptionTypeItem> pair = serRecTypeList.get(row);
ServisePriceData contractServiceData = contract.getContractService(pair);
switch (col) {
case COL_INCONTRACT:
return (contractServiceData != null);
case COL_RECEPTION_TYPE:
return pair.second;
case COL_SERVICE:
return pair.first;
case COL_PRICE:
if (contractServiceData == null || contractServiceData.getPrice()<0) {
return "";
}
else {
return String.format("%.2f", ((float) contractServiceData.getPrice()) / 100);
}
case COL_MONEY:
if (contractServiceData == null || contractServiceData.getMoney() < 0) {
return "";
} else {
return String.format("%.2f", ((float) contractServiceData.getMoney()) / 100);
}
default: throw new IllegalArgumentException("Некорректный номер столбца : " + col);
}
} catch (ClipsException ex) {
if (getColumnClass(col) == Boolean.class) {
return false;
} else {
return new ErrorValue(ex);
}
}
}
@Override
public boolean isCellEditable(int row, int col) {
if (col == COL_SERVICE || col == COL_RECEPTION_TYPE) {
return false;
}
else if (col == COL_INCONTRACT) {
return true;
}
else {
try {
Pair<DirectoryServiceItem, DirectoryReceptionTypeItem> pair = serRecTypeList.get(row);
return contract.getContractService(pair) != null;
} catch (ClipsException ex) {
ex.printStackTrace();
return false;
}
}
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
try {
Pair<DirectoryServiceItem, DirectoryReceptionTypeItem> pair = serRecTypeList.get(rowIndex);
switch (columnIndex) {
case COL_INCONTRACT: {
//Добавить или удалить услугу из договора
if ((Boolean)value) {
ServisePriceData contractServiceData = contract.getContractService(pair);
if (contractServiceData == null) {
contractServiceData = new ServisePriceData(pair);
contract.getContractServiceList().append(contractServiceData);
}
} else {
contract.removeService(pair);//! о как надо
}
break;
}
case COL_PRICE: {
//Назначить другую цену услуги
ServisePriceData contractServiceData = contract.getContractService(pair);
try {
Number v = NumberFormat.getInstance().parse(value.toString());
contractServiceData.setPrice((int) (v.floatValue()*100));
} catch (ParseException ex) {
contractServiceData.setPrice(-1);
System.out.println("Неправильный формат числа, используем -1");
}
break;
}
case COL_MONEY: {
//Назначить другую оплату по договору услуги
ServisePriceData contractServiceData = contract.getContractService(pair);
try {
Number v = NumberFormat.getInstance().parse(value.toString());
contractServiceData.setMoney((int) (v.floatValue()*100));
} catch (ParseException ex) {
contractServiceData.setMoney(-1);
System.out.println("Неправильный формат числа, используем -1");
}
break;
}
default: throw new IllegalArgumentException("Некорректный номер столбца : " + columnIndex);
}
// Обновить всю строчку
for (int i = 0; i < getColumnCount(); i++) {
fireTableCellUpdated(rowIndex, i);
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
@Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == COL_INCONTRACT) {
return Boolean.class;
}
return super.getColumnClass(columnIndex);
}
public Pair<DirectoryServiceItem, DirectoryReceptionTypeItem> getSerRecType(int row){
return serRecTypeList.get(row);
}
}