/*
* EmcLocal.java
*
* Created on 12 Декабрь 2007 г., 11:15
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package clips.delegate.doctor;
import beans.doctor.emc.EmcBeanRemote;
import cli_fmw.delegate.cache.ExtraDataManager;
import beans.doctor.diagnosis.DiagnosisDetails;
import beans.doctor.emc.EmcDetails;
import clips.delegate.client.ClientLocal;
import clips.delegate.doctor.followup.FollowupLocal;
import cli_fmw.delegate.cache.DelegateExtraDataRO;
import cli_fmw.main.ClipsException;
import cli_fmw.main.DelegatePrintable;
import clips.delegate.DEC.DECClient;
import cli_fmw.report.CombinedReportCreator;
import cli_fmw.report.FormReportCreator;
import beans.doctor.disease.DiseaseDetails;
import beans.doctor.emc.EmcBean;
import beans.followup.entity.FollowupDetails;
import cli_fmw.delegate.AuditListener;
import clips.doctor.newEMC.init.generic.ChildrenListStateless;
import clips.doctor.newEMC.init.generic.DelegateNode;
import clips.delegate.service.SerRenLocal;
import framework.utils.Converter;
import framework.utils.Pair;
import framework.utils.ROList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.swing.Icon;
import javax.swing.ImageIcon;
/**
*
* @author Администратор
*/
public class EmcLocal extends DelegateNode<EmcBeanRemote, EmcDetails, DelegateNode<?,?,?>>
implements Iconable, DelegatePrintable, Signable {
private DiseaseListCache diseaseList = new DiseaseListCache(getEDM());
private FollowupCache followups = new FollowupCache(getEDM());
private DECClient client = new DECClient(this, "информацию о пациенте, которому принадлежит ЭМК", getAuditListener());
private static ImageIcon icon = new ImageIcon(EmcLocal.class.getResource("/resources/icons/emc.gif"));
@Override
public int getChildCount() throws ClipsException {
return followups.getSize() + diseaseList.get().size();
}
/**
*
*/
private class DiseaseListCache extends DelegateExtraDataRO<List<DiseaseLocal>> {
public DiseaseListCache(ExtraDataManager contaner) {
super(contaner);
}
@Override
protected List<DiseaseLocal> init() throws Exception {
List<DiseaseLocal> res = new ArrayList<DiseaseLocal>();
List<Pair<DiseaseDetails, DiagnosisDetails>> list = getBean().getDiseaseList();
for (Pair<DiseaseDetails, DiagnosisDetails> pair : list) {
DiseaseLocal disease = new DiseaseLocal(EmcLocal.this, pair.first, getAuditListener());
if (pair.second != null){
disease.setSpecDiagnosisMkbID(pair.second.mkbID);
}
res.add(disease);
}
return res;
}
@Override
protected List<DiseaseLocal> initNew() {
return new ArrayList<DiseaseLocal>();
}
}
/**
*
*/
private class FollowupCache extends ChildrenListStateless<FollowupLocal, FollowupDetails> {
public FollowupCache(ExtraDataManager contaner) {
super(contaner);
}
@Override
protected List<FollowupLocal> initNew() {
return new ArrayList<FollowupLocal>();
}
@Override
protected FollowupLocal createDelegate(FollowupDetails d) throws Exception {
return new FollowupLocal(d, findSerrenUp(d), getAuditListener());
}
@Override
protected List<FollowupDetails> initDetails() throws Exception {
List<FollowupDetails> followupList = getBean().getFollowupList();
for (Iterator<FollowupDetails> it = followupList.iterator(); it.hasNext();) {
if (findSerrenUp(it.next()) == null){
it.remove();
}
}
return followupList;
}
}
/**
*
* @param c
* @param details
* @throws cli_fmw.main.ClipsException
*/
public EmcLocal(ClientLocal c, EmcDetails details, AuditListener al) throws ClipsException {
super(details, al);
client.initBy(c);
}
public EmcLocal(int id, AuditListener al) throws ClipsException{
super(id, al);
}
@Override
protected EmcDetails getNewDetails() {
return new EmcDetails();
}
public String getLifeAnamnez() throws ClipsException {
return getDetails().anamnez;
}
public void setLifeAnamnez(String lifeAnamnez) throws ClipsException {
if (lifeAnamnez == null || lifeAnamnez.equals("")) {
getDetails().anamnez = null;
} else {
getDetails().anamnez = lifeAnamnez;
}
fireContentStateEvent();
}
/**
* Брать номер из пациента
* @return
* @throws cli_fmw.main.ClipsException
* @deprecated
*/
@Deprecated
public String getNumber() throws ClipsException {
return getDetails().number;
}
/**
* Засовывать номер в пациента
* @param number
* @throws cli_fmw.main.ClipsException
* @deprecated
*/
@Deprecated
public void setNumber(String number) throws ClipsException {
if (number == null || number.equals("")) {
getDetails().number = null;
} else {
getDetails().number = number;
}
fireContentStateEvent();
}
public ClientLocal getClient() throws ClipsException {
return client.get();
}
@Override
public String toString() {
return "Медицинская карта";
}
/**
* Возвращает список заболеваний и список диспансеризаций, ссылающихся на данную ЕМЦ
* @return
* @throws ClipsException
*/
@Override
public ROList<DelegateNode<?,?,?>> getChildren() throws ClipsException {
List<DelegateNode<?,?,?>> list = new ArrayList<DelegateNode<?,?,?>>();
Iterator<DiseaseLocal> iterator = getDiseaseList().iterator();
while (iterator.hasNext()) {
DiseaseLocal diseaseLocal = iterator.next();
if(diseaseLocal.isFollowupEvent()){
// iterator.remove(); В ЭМК в заболеваниях будут приходы, а в чилдренах не будет
}else{
list.add(diseaseLocal);
}
}
list.addAll(followups.get());
return new ROList<DelegateNode<?,?,?>>(list);
}
@Override
protected void removeChildImpl(DelegateNode child) throws ClipsException {
if(child instanceof DiseaseLocal) {
diseaseList.get().remove((DiseaseLocal)child);
} else if(child instanceof FollowupLocal) {
followups.get().remove((FollowupLocal)child);
} else {
throw new IllegalArgumentException("Not existent child");
}
}
@Override
protected void addChildImpl(DelegateNode<?, ?, ?> child) throws ClipsException {
if (child instanceof FollowupLocal){
addFollowup((FollowupLocal) child);
}else if (child instanceof DiseaseLocal){
addDisease((DiseaseLocal) child);
}
}
/**
*
* @return может вполне вернуть итем с
* @throws cli_fmw.main.ClipsException
*/
public List<DiseaseLocal> getDiseaseList() throws ClipsException {
return diseaseList.get();
}
public List<FollowupLocal> getFollowUps() throws ClipsException {
return followups.get();
}
public void addDisease(DiseaseLocal diseaseLocal) throws ClipsException {
diseaseList.get().add(diseaseLocal);
}
public void addFollowup(FollowupLocal fup) throws ClipsException {
followups.get().add(fup);
}
@Override
public Icon getIcon() {
return icon;
}
@Override
protected String getBeanName() {
return EmcBean.class.getSimpleName();
}
@Override
public void print(CombinedReportCreator cCreator) throws ClipsException {
FormReportCreator creater = cCreator.createFormReporter(getClass(), 1);
HashMap<String, Object> extraFields = getClient().getPrintFields();
creater.createReport(null, null, extraFields.entrySet(), null);
creater.finish();
}
private SerRenLocal findSerrenUp(FollowupDetails details) throws ClipsException{
for (DiseaseLocal diseaseLocal : getDiseaseList()) {
if (diseaseLocal.getID() == details.diseaseUpID){
for (SerRenLocal serRenLocal : diseaseLocal.getSerrenList()) {
if (serRenLocal.getID() == details.serrenUpID){
return serRenLocal;
}
}
}
}
return null;
}
@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;
List<DiseaseLocal> diseaseList1 = getDiseaseList();
DiseaseLocal disease = diseaseList1.size() == 0 ? null : diseaseList1.get(diseaseList1.size() - 1);
if (disease != null && disease.getCollaborator() != null) {
coll = disease.getCollaborator().getTitle();
} else {
return "Подпись отсутствует";
}
String s = "Подписано " + Converter.dateToString(disease.getCreated())
+ " - " + coll;
if (isSignCorrect() ) {
return s;
} else {
return "Подпись не соответствует данным!";
}
//END STUB
}
}