/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.delegate.doctor.prescription;
import beans.doctor.prescription.PrescriptionBean;
import beans.doctor.prescription.PrescriptionBeanRemote;
import clips.delegate.client.ClientLocal;
import clips.delegate.doctor.HasSerrenLocal;
import beans.doctor.prescription.PrescriptionDetails;
import clips.delegate.doctor.Iconable;
import clips.delegate.DEC.DECServiceRender;
import cli_fmw.delegate.DelegateLine2;
import cli_fmw.delegate.cache.ExtraDataManager;
import clips.delegate.service.SerRenLocal;
import cli_fmw.main.ClipsException;
import cli_fmw.delegate.lists.DataChunkList;
import cli_fmw.delegate.utils.TimeLocal;
import clips.doctor.prescription.TableModelPrescription;
import cli_fmw.main.DelegatePrintable;
import cli_fmw.report.CombinedReportCreator;
import cli_fmw.report.FormReportCreator;
import cli_fmw.report.ReporterFactory;
import cli_fmw.utils.SelectorEditable;
import clips.doctor.newEMC.init.generic.DelegateNodeLeaf;
import clips.delegate.directory.ro.DirectoryCollaboratorItem;
import clips.delegate.doctor.Signable;
import beans.doctor.prescription.PrescriptionItemDetails;
import cli_fmw.delegate.AuditListener;
import framework.beans.ModificationInfo;
import java.util.Date;
import java.util.HashMap;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.table.TableModel;
import framework.utils.Converter;
import framework.utils.DateTimeUtils;
/**
* Делегат, реализующий доступ к одной строке таблицы prescription (назначения)
* содержит в себе список список ArrayList<PrescriptionItem>, в котором
* находятся строки таблицы (prescription_item) лекарства и способы их приема
* @author ViP
*/
public class PrescriptionLocal extends
DelegateNodeLeaf<PrescriptionBeanRemote, PrescriptionDetails>
implements Iconable, DelegatePrintable, HasSerrenLocal, Signable {
// Лекарства в рецептах
private PrescriptionItemList itemList = new PrescriptionItemList(getEDM());
private DECServiceRender serviceRender = new DECServiceRender(this, "информацию об услуге, в рамках которой назначен приём лекарств", getAuditListener());
private static ImageIcon icon = null;
public PrescriptionLocal(SerRenLocal serRen) throws ClipsException {
super(serRen.getAuditListener());
getDetails().date = TimeLocal.getCurrentTime().getTime();
serviceRender.initBy(serRen);
}
public PrescriptionLocal(int id, SerRenLocal serRen, AuditListener al) throws ClipsException {
super(id, al);
serviceRender.initBy(serRen);
}
public PrescriptionLocal(PrescriptionDetails details, SerRenLocal serRen, AuditListener al) throws ClipsException {
super(details, al);
serviceRender.initBy(serRen);
}
@Override
protected PrescriptionDetails getNewDetails() {
return new PrescriptionDetails();
}
@Override
public SerRenLocal getSerrenLocal() throws ClipsException {
return serviceRender.get();
}
public Date getDate() throws ClipsException {
return getDetails().date;
}
//never use client date
@Deprecated
public void setDate(Date date) throws ClipsException {
getDetails().date = date;
fireContentStateEvent();
}
public SelectorEditable<PrescriptionItemData> getPrescriptionItemList()
throws ClipsException {
return itemList.selector();
}
@Override
public Icon getIcon() {
return icon;
}
@Override
public String toString() {
return "Назначение";
}
@Override
protected String getBeanName() {
return PrescriptionBean.class.getSimpleName();
}
public void itemPrint(PrescriptionItemData itemData) throws ClipsException {
DirectoryCollaboratorItem collab = getSerrenLocal().getSerRenDirector();
ClientLocal client = getSerrenLocal().getPolisData().getClient(getAuditListener());
FormReportCreator creater = ReporterFactory.createFormReporter(getClass(), 1);
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("recipeDate", Converter.dateToString(getDate()));
data.put("recipeClient", client.toString());
int age = DateTimeUtils.calcAge(client.getBorn(), TimeLocal.getCurrentTime().getTime());
data.put("clientAge", age != Integer.MIN_VALUE ? Integer.toString(age) : "Не определен");
data.put("clientAddress", client.getAddress().toStringShort());
data.put("clientEMC", client.getEMC().getID());
data.put("recipeCollab", collab.getTitle());
data.put("recipeVidal", itemData.getVidal().toString());
data.put("recipeTake", itemData.getTakeDrug().toString());
creater.createReport(null, null, data.entrySet(), null);
creater.finish();
}
public TableModel getItemsTableModel() throws ClipsException {
boolean canBeEdit = getSerrenLocal().canReadMedicalData();
TableModelPrescription model = new TableModelPrescription(getPrescriptionItemList(), canBeEdit);
return model;
}
@Override
public void print(CombinedReportCreator cCreator) throws ClipsException {
FormReportCreator creater = cCreator.createFormReporter(getClass(), 2);
DirectoryCollaboratorItem collab = getSerrenLocal().getSerRenDirector();
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("client", getSerrenLocal().getPolisData().getClient(getAuditListener()).toString());
data.put("date", Converter.dateToString(getDate()));
data.put("collab", collab.toString());
creater.createReport(null, null, data.entrySet(), getItemsTableModel());
creater.finish();
}
/**
*
*/
class PrescriptionItemList extends DataChunkList<PrescriptionItemData> {
public PrescriptionItemList(ExtraDataManager contaner) {
super(contaner);
}
@Override
protected void deleteDB(PrescriptionItemData details) throws Exception {
ModificationInfo mi = getBean().removePrescriptionItem(details.getDetails().getId());
auditDetailsList.addAll(mi.getAudit());
fireAuditEvent();
}
@Override
protected int saveDB(PrescriptionItemData details) throws Exception {
ModificationInfo mi = getBean().setPrescriptionItem(details.getDetails());
auditDetailsList.addAll(mi.getAudit());
fireAuditEvent();
return mi.getId();
}
@Override
protected void loadDB() throws Exception {
if (!isNewlyCreated()) {
for(PrescriptionItemDetails d: getBean().getPrescriptionsList()) {
initByDetails(new PrescriptionItemData(PrescriptionLocal.this, d));
}
}
}
}
@Override
public boolean isSignCorrect() throws ClipsException{
//STUB
return (getID() != -1 );
//END STUB
}
@Override
public String getSignMessage() throws ClipsException {
if (getID() == 0) {
return "Подпись отсутствует";
}
//STUB
String coll;
if (getSerrenLocal().getCollaboratorFunctions() != null) {
coll = getSerrenLocal().getCollaboratorFunctions().getTitle();
} else {
coll = getSerrenLocal().getSerRenDirector().getTitle();
}
String s = "Подписано " + Converter.dateToString(getDate())
+ " - " + coll;
if (isSignCorrect() ) {
return s;
} else {
return "Подпись не соответствует данным!";
}
//END STUB
}
}