/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.delegate.doctor.followup;
import beans.followup.FollowupBeanRemote;
import beans.followup.ModDisease;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import clips.delegate.directory.complex.DirectoryMKB10Item;
import beans.followup.entity.FollowupDetails;
import beans.followup.entity.FollowupEventDetails;
import clips.delegate.client.ClientLocal;
import clips.delegate.doctor.Iconable;
import cli_fmw.delegate.cache.DelegateExtraDataRO;
import cli_fmw.delegate.cache.DelegateSecondaryCacheDgt;
import cli_fmw.delegate.cache.ExtraDataManager;
import cli_fmw.delegate.client.Address;
import cli_fmw.main.ClipsException;
import cli_fmw.delegate.lists.DataChunkList;
import cli_fmw.delegate.utils.TimeLocal;
import clips.doctor.followup.TableModelFollowUpEvents;
import cli_fmw.main.DelegatePrintable;
import cli_fmw.main.DirectoryItemReplacedException;
import cli_fmw.report.CombinedReportCreator;
import cli_fmw.report.FormReportCreator;
import cli_fmw.utils.SelectorEditable;
import clips.delegate.client.CollaboratorLocal;
import clips.delegate.directory.ro.DirectoryCollaborator;
import clips.delegate.directory.ro.DirectoryCollaboratorItem;
import clips.delegate.directory.simple.dvr.DirectoryDvrItem;
import clips.delegate.directory.simple.reasonDown.DirectoryReasonDown;
import clips.delegate.directory.simple.reasonDown.DirectoryReasonDownItem;
import clips.delegate.directory.simple.reasonUp.DirectoryReasonUp;
import clips.delegate.directory.simple.reasonUp.DirectoryReasonUpItem;
import clips.delegate.doctor.DiseaseLocal;
import clips.delegate.doctor.Signable;
import clips.delegate.doctor.diagnosis.DiagnosisLocal;
import clips.delegate.service.SerRenLocal;
import clips.doctor.newEMC.init.generic.DelegateNode;
import clips.login.UserInfo;
import beans.doctor.diagnosis.DiagnosisDetails;
import beans.doctor.disease.DiseaseDetails;
import beans.followup.FollowupBean;
import beans.user.client.entity.ClientDetails;
import cli_fmw.delegate.AuditListener;
import framework.beans.ModificationInfo;
import framework.generic.ClipsServerException;
import framework.utils.Converter;
import framework.utils.ROList;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.table.TableModel;
public class FollowupLocal extends DelegateNode<FollowupBeanRemote, FollowupDetails, DiseaseLocal>
implements Iconable, DelegatePrintable, Signable {
private DelegateExtraDataRO<ClientLocal> client = new DelegateExtraDataRO<ClientLocal>(getEDM()) {
@Override
protected ClientLocal init() throws Exception {
ClientDetails cd = getDetails().clientDetails;
if (cd == null){
return getSerrenUp().getDiseaseLocal().getEmcLocal().getClient();
}else{
return new ClientLocal(cd, getAuditListener());
}
}
@Override
protected ClientLocal initNew() {
return null;
}
};
private static ImageIcon icon = null;
FollowupItemList followupEventList = new FollowupItemList(getEDM());
private DelegateSecondaryCacheDgt<SerRenLocal> serrenUp
= new DelegateSecondaryCacheDgt<SerRenLocal>(getSCM(), false, "услуге, в рамках которой поставлен на учёт") {
@Override
protected SerRenLocal init() throws Exception {
return new SerRenLocal(getDetails().serrenUpID, getAuditListener());
}
@Override
protected void setPrimary(SerRenLocal type) throws ClipsException {
getDetails().serrenUpID = type.getID();
}
};
private DelegateSecondaryCacheDgt<SerRenLocal> serrenDown
= new DelegateSecondaryCacheDgt<SerRenLocal>(getSCM(), true, "услуге, в рамках которой снят с учёта") {
@Override
protected SerRenLocal init() throws Exception {
if (getDetails().serrenDownID != 0){
return new SerRenLocal(getDetails().serrenDownID, getAuditListener());
}else{
return null;
}
}
@Override
protected void setPrimary(SerRenLocal type) throws ClipsException {
if (type != null){
getDetails().serrenDownID = type.getID();
}else{
getDetails().serrenDownID = 0;
}
}
};
private DelegateSecondaryCacheDgt<DiagnosisLocal> diagnosisUp
= new DelegateSecondaryCacheDgt<DiagnosisLocal>(getSCM(), true, "диагноз постановки на учёт") {
@Override
protected DiagnosisLocal init() throws Exception {
if (getDetails().diagnosisUpID != 0){
return new DiagnosisLocal(getDetails().diagnosisUpID, getAuditListener());
}else{
return null;
}
}
@Override
protected void setPrimary(DiagnosisLocal type) throws ClipsException {
if (type != null){
getDetails().diagnosisUpID = type.getID();
}else{
getDetails().diagnosisUpID = 0;
}
}
};
/**
* создание новой диспансеризации
* @param e
* @throws ClipsException
*/
public FollowupLocal(SerRenLocal serrenUp) throws ClipsException {
super(serrenUp.getAuditListener());
getDetails().collaboratorUpID = UserInfo.get().getCollaborator().getID();
getDetails().dateUp = TimeLocal.getCurrentTime().getTime();
this.serrenUp.initBy(serrenUp);
this.client.initBy(serrenUp.getDiseaseLocal().getEmcLocal().getClient());
}
public FollowupLocal(int id, SerRenLocal serrenUp, AuditListener al) throws ClipsException {
super(id, al);
this.serrenUp.initBy(serrenUp);
this.client.initBy(serrenUp.getDiseaseLocal().getEmcLocal().getClient());
}
public FollowupLocal(FollowupDetails details, SerRenLocal serrenUp, AuditListener al) throws ClipsException {
super(details, al);
this.serrenUp.initBy(serrenUp);
this.client.initBy(serrenUp.getDiseaseLocal().getEmcLocal().getClient());
}
/**
* Конструктор доступный из пакета, создан для нужд поисковика
* @param details
* @throws cli_fmw.main.ClipsException
*/
FollowupLocal(FollowupDetails details, AuditListener al) throws ClipsException {
super(details, al);
}
public void createDiseaseForEvent(FollowupEventData event) throws ClipsException {
try {
ModDisease modDisease = getBean().createDisease(event.getDetails());
DiseaseDetails dd = modDisease.diseaseDetails;
DiseaseLocal disease = new DiseaseLocal(getClient().getEMC(), dd, getAuditListener());
getClient().getEMC().addDisease(disease);
event.setDisease(disease);
fireContentStateEvent();
super.addChild(disease);
auditDetailsList.addAll(modDisease.auditDetList);
fireAuditEvent();
} catch (Exception ex) {
clearBean();
throw new ClipsException("Не удалось создать заболевание", ex);
}
}
@Override
protected FollowupDetails getNewDetails() {
return new FollowupDetails();
}
public ClientLocal getClient() throws ClipsException {
return client.get();
}
public Date getDateDown() throws ClipsException {
return getDetails().dateDown;
}
public void setDateDown(Date dateDown) throws ClipsException {
getDetails().dateDown = dateDown;
fireContentStateEvent();
}
public Date getDateUp() throws ClipsException {
return getDetails().dateUp;
}
public void setDateUp(Date dateUp) throws ClipsException {
getDetails().dateUp = dateUp;
fireContentStateEvent();
}
public DiagnosisLocal getDiagnosis() throws ClipsException {
return diagnosisUp.get();
}
public void setDiagnosis(DiagnosisLocal diagnosis) throws ClipsException {
diagnosisUp.set(diagnosis);
fireContentStateEvent();
}
public DirectoryReasonUpItem getReasonUp() throws ClipsException {
DirectoryReasonUp reasonUpList = DirectoryLocator.getDirectory(DirectoryReasonUp.class);
try {
return reasonUpList.getItemFromID(getDetails().reasonUp);
} catch (DirectoryItemReplacedException ex) {
reload();
return reasonUpList.getItemFromID(getDetails().reasonUp);
}
}
public void setReasonUp(DirectoryReasonUpItem reasonUp) throws ClipsException {
getDetails().reasonUp = reasonUp.getID();
fireContentStateEvent();
}
public DirectoryReasonDownItem getReasonDown() throws ClipsException {
DirectoryReasonDown reasonDownList = DirectoryLocator.getDirectory(DirectoryReasonDown.class);
try {
return reasonDownList.getItemFromID(getDetails().reasonDown);
} catch (DirectoryItemReplacedException ex) {
reload();
return reasonDownList.getItemFromID(getDetails().reasonDown);
}
}
public void setReasonDown(DirectoryReasonDownItem reasonDown) throws ClipsException {
getDetails().reasonDown = reasonDown.getID();
fireContentStateEvent();
}
public String getValue() throws ClipsException {
return getDetails().value;
}
public void setValue(String value) throws ClipsException {
getDetails().value = value;
fireContentStateEvent();
}
public DirectoryCollaboratorItem getCollaboratorDown() throws ClipsException {
DirectoryCollaborator dir = DirectoryLocator.getDirectory(DirectoryCollaborator.class, false);
return dir.getItemFromID(getDetails().collaboratorDownID);
}
public void setCollaboratorDown(DirectoryCollaboratorItem collaboratorDown) throws ClipsException {
getDetails().collaboratorDownID = collaboratorDown.getID();
fireContentStateEvent();
}
public DirectoryCollaboratorItem getCollaboratorUp() throws ClipsException {
DirectoryCollaborator dir = DirectoryLocator.getDirectory(DirectoryCollaborator.class, false);
return dir.getItemFromID(getDetails().collaboratorUpID);
}
public void setCollaboratorUp(DirectoryCollaboratorItem collaboratorUp) throws ClipsException {
getDetails().collaboratorUpID = collaboratorUp.getID();
fireContentStateEvent();
}
public SerRenLocal getSerrenDown() throws ClipsException {
return serrenDown.get();
}
public void setSerrenDown(SerRenLocal serrenDown) throws ClipsException {
this.serrenDown.set(serrenDown);
fireContentStateEvent();
}
public SerRenLocal getSerrenUp() throws ClipsException {
return serrenUp.get();
}
public void setSerrenUp(SerRenLocal serrenUp) throws ClipsException {
this.serrenUp.set(serrenUp);
fireContentStateEvent();
}
public boolean isDown() throws ClipsException {
return (getDateDown() != null);
}
public FollowupEventData getLastEvent() throws ClipsException {
FollowupEventData eventData = null;
SelectorEditable<FollowupEventData> list = getFollowupEventList();
if (list.size() > 0) {
eventData = list.get(0);
for (int i = 1; i < list.size(); i++) {
if (list.get(i).getDate().after(eventData.getDate())) {
eventData = list.get(i);
}
}
}
return eventData;
}
@Override
public ImageIcon getIcon() {
return icon;
}
@Override
public String toString() {
String diagnosis = "";
String isdown = "";
try {
diagnosis = (getDiagnosis() != null ? getDiagnosis().getMkb().getDiseaseCode() : "") + " ";
if (isDown()) {
isdown = "(снят)";
}
} catch (ClipsException ex) {
ex.printStackTrace();
}
return "Д-учет " + diagnosis + isdown;
}
@Override
protected String getBeanName() {
return FollowupBean.class.getSimpleName();
}
public ArrayList<DiagnosisLocal> getDiagnosisIn() throws ClipsException {
ArrayList<DiagnosisLocal> res = new ArrayList<DiagnosisLocal>();
try {
for(DiagnosisDetails d: getBean().getDiagnosisIn()) {
res.add(new DiagnosisLocal(d.id, getAuditListener()));
}
return res;
} catch (ClipsServerException ex) {
throw new ClipsException("Не удалось получить диагнозы, поставленные в рамках ДУ", ex);
}
}
public SelectorEditable<FollowupEventData> getFollowupEventList() throws ClipsException {
return followupEventList.selector();
}
@Override
public ROList<DiseaseLocal> getChildren() throws ClipsException {
SelectorEditable<FollowupEventData> se = this.getFollowupEventList();
List<DiseaseLocal> children = new ArrayList<DiseaseLocal>();
for (int i = 0; i < se.size(); i++) {
FollowupEventData followupEventData = se.get(i);
if (followupEventData.getDisease() != null){
children.add(followupEventData.getDisease());
}
}
return new ROList<DiseaseLocal>(children);
}
/**
*
* @param child
* @throws ClipsException
*/
@Override
protected void removeChildImpl(DiseaseLocal child) throws ClipsException {
//do nothing
SelectorEditable<FollowupEventData> se = this.getFollowupEventList();
for (int i = 0; i < se.size(); i++) {
FollowupEventData followupEventData = se.get(i);
DiseaseLocal dis = followupEventData.getDisease();
if (dis != null && dis.getID() == child.getID()){
se.remove(followupEventData);
followupEventList.clearCache();
return;
}
}
throw new IllegalArgumentException("Not existent child");
}
@Override
public int getChildCount() {
try {
return getChildren().size();
} catch (ClipsException ex) {
ex.printStackTrace();//Use onli in emc tree
return 0;
}
}
/**
* Работает на костыле, см метод <b>createDiseaseForEvent</b>
*
* @param child
* @throws ClipsException
* @deprecated
*/
@Deprecated
@Override
protected void addChildImpl(DiseaseLocal child) throws ClipsException {
}
class FollowupItemList extends DataChunkList<FollowupEventData> {
public FollowupItemList(ExtraDataManager contaner) {
super(contaner);
}
@Override
protected void deleteDB(FollowupEventData data) throws Exception {
getBean().removeEvent(data.getDetails().getId());
}
@Override
protected int saveDB(FollowupEventData data) throws Exception {
ModificationInfo info = getBean().setEvent(data.getDetails());
auditDetailsList.addAll(info.getAudit());
fireAuditEvent();
return info.getId();
}
@Override
protected void loadDB() throws Exception {
if (!isNewlyCreated()) {
Map<FollowupEventDetails, Integer> eventList = getBean().getEventList();
for(FollowupEventDetails d: eventList.keySet()) {
initByDetails(new FollowupEventData(FollowupLocal.this, d, eventList.get(d), getAuditListener()));
}
}
}
}
@Override
public void print(CombinedReportCreator cCreator) throws ClipsException {
FormReportCreator creater = cCreator.createFormReporter(getClass(), 1);
ClientLocal cl = getClient();
HashMap<String, Object> data = cl.getPrintFields();
Address address = cl.getAddress();
if (address != null) {
data.put("pacient_address", address.toStringShort());
}
data.put("collab_fio", getCollaboratorUp().getTitle());
data.put("collab_appointment", getCollabUpDrv());
data.put("followup_date", Converter.dateToString(getDateUp()));
data.put("followdown_date", Converter.dateToString(getDateDown()));
if (isDown()) {
data.put("followdown_reason", getReasonDown().getTitle());
}
SelectorEditable<FollowupEventData> fevents = getFollowupEventList();
int size = fevents.size();
Date events[] = new Date[size];
Date eventsReal[] = new Date[size];
for (int i = 0; i < size; i++) {
events[i] = fevents.get(i).getDate();
if (fevents.get(i).getDisease() != null) {
eventsReal[i] = fevents.get(i).getDisease().getCreated();
}
else {
eventsReal[i] = null;
}
}
data.put("event_dates", events);
data.put("event_dates_real", eventsReal);
DiagnosisLocal diag = getDiagnosis();
DirectoryMKB10Item mkbItem = diag.getMkb();
data.put("disease_title", mkbItem.getTitle());
data.put("disease_code", mkbItem.getDiseaseCode());
String accomp = "";
for (int i = 0; i < diag.getAccompDiagnosis().size(); i++) {
accomp += diag.getAccompDiagnosis().get(i).getMkb().getDiseaseCode() + " ";
}
data.put("disease_associated", accomp);
String extraData = "";
for (DiagnosisLocal diagIn : getDiagnosisIn()) {
extraData += "(" + Converter.dateToString(diagIn.getDate()) + ") ";
extraData += diagIn.getMkb().getDiseaseCode();
if (diagIn.getComplication() != null && diagIn.getComplication().getID() != 0) {
extraData += "; осложнение: " + diagIn.getComplication().getDiseaseCode();
}
ArrayList<DiagnosisLocal> accDiags = (ArrayList<DiagnosisLocal>) diagIn.getAccompDiagnosis();
if (accDiags != null && !accDiags.isEmpty()) {
extraData += "; сопутствующие: ";
for (DiagnosisLocal accDiag : accDiags) {
extraData += accDiag.getMkb().getDiseaseCode() + " ";
}
}
extraData += "\n";
}
data.put("extra_data", extraData);
creater.createReport(data, null);
creater.finish();
}
public TableModel getItemsTableModel() throws ClipsException {
return new TableModelFollowUpEvents(this);
}
@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 (getCollaboratorUp() != null) {
coll = getCollaboratorUp().getTitle();
} else {
return "Подпись отсутствует";
}
String s = "Подписано " + Converter.dateToString(getDateUp())
+ " - " + coll;
if (isSignCorrect() ) {
return s;
} else {
return "Подпись не соответствует данным!";
}
//END STUB
}
//********** для печати
public String getCollabUpDrv() throws ClipsException {
DirectoryCollaboratorItem item = getCollaboratorUp();
if (item != null && item.getID() != 0) {
CollaboratorLocal collab = new CollaboratorLocal(item.getID(), getAuditListener());
if (collab.getFunctions().size() > 0) {
DirectoryDvrItem funcItem = collab.getFunctions().get(0).getDvr();
if (funcItem != null) {
return funcItem.getTitle();
}
}
}
return "";
}
public String getCollabDownDrv() throws ClipsException {
if (!isDown()) {
return "";
}
DirectoryCollaboratorItem item = getCollaboratorDown();
if (item != null && item.getID() != 0) {
CollaboratorLocal collab = new CollaboratorLocal(item.getID(), getAuditListener());
if (collab.getFunctions().size() > 0) {
DirectoryDvrItem funcItem = collab.getFunctions().get(0).getDvr();
if (funcItem != null) {
return funcItem.getTitle();
}
}
}
return "";
}
public DirectoryMKB10Item getMkb() throws ClipsException {
return getDiagnosis().getMkb();
}
}