/**
*
*/
package org.mbhcare.client.presenter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.mbhcare.client.event.DiagnosisEventBus;
import org.mbhcare.client.event.FetalsEventBus;
import org.mbhcare.client.event.PatientsEventBus;
import org.mbhcare.client.i18n.DiagnosisConstants;
import org.mbhcare.client.report.DiagnosisEditReport;
import org.mbhcare.client.rpc.AppAsyncCallback;
import org.mbhcare.client.rpc.DiagnosisServiceAsync;
import org.mbhcare.client.rpc.FetalsServiceAsync;
import org.mbhcare.client.rpc.PatientsServiceAsync;
import org.mbhcare.client.view.diagnosis.DiagnosisEditView;
import org.mbhcare.shared.Utils;
import org.mbhcare.shared.entity.Diagnosis;
import org.mbhcare.shared.entity.EntityDisplay;
import org.mbhcare.shared.entity.Patient;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasWidgets;
/**
* @author MCOSTA
*
*/
public class DiagnosisEditPresenter implements Presenter, DiagnosisEditView.Presenter {
private final DiagnosisEditView view;
private DiagnosisServiceAsync service = DiagnosisEventBus.getService();
private PatientsServiceAsync patientservice = PatientsEventBus.getService();
private FetalsServiceAsync fetalservice = FetalsEventBus.getService();
private DiagnosisConstants constants = DiagnosisEventBus.getConstants();
private Patient patient;
private Diagnosis diagnosis;
DiagnosisEditReport diagnosisEditReport;
private List<EntityDisplay> listFetal;
private List<EntityDisplay> listImage;
public DiagnosisEditPresenter(DiagnosisEditView view, String id) {
diagnosisEditReport = new DiagnosisEditReport();
this.view = view;
view.setPresenter(this);
diagnosis = new Diagnosis();
if(id != null) doLoad(id);
else updateDisplayFromData();
}
public DiagnosisEditPresenter(final DiagnosisEditView view, String id, String patientId) {
diagnosisEditReport = new DiagnosisEditReport();
this.view = view;
view.setPresenter(this);
view.reset();
diagnosis = new Diagnosis();
doLoadPatient(patientId);
doLoadFetals(patientId,"");
}
/**
* @return
*/
public boolean hasValidInput() {
boolean valid = true;
StringBuilder sb = new StringBuilder();
// if (!FieldVerifier.isValidString(view.getFirstName().getValue()) &&
// !FieldVerifier.isValidString(view.getLastName().getValue())) {
// sb.append(messages.errorField(constants.formFirstName()) + "\n");
// valid = false;
// }
//
sb.append(" ");
if(valid == false)
Window.alert(sb.toString());
return valid;
}
private void doLoadPatient(final String patientId) {
if(!patientId.equals("")) {
patientservice.selectById(patientId, new AppAsyncCallback<Patient>() {
@Override
public void onSuccess(Patient result) {
patient = result;
diagnosis.setPatient(patient.getId());
view.setPatientId(patient.getId());
view.getFirstName().setValue(patient.getFirstName());
view.getLastName().setValue(patient.getLastName());
view.getBirthYear().setValue(patient.getBirthYear());
}
});
}
}
private void doLoadFetals(final String patientId, final String fetalId) {
if(!patientId.equals("")) {
fetalservice.selectByPatient(patientId, new AppAsyncCallback<List<EntityDisplay>>() {
@Override
public void onSuccess(List<EntityDisplay> result) {
listFetal = result;
view.setListFetal(result);
view.setFetalId(fetalId);
}
});
}
}
private void doLoadImage(final String diagnosisId) {
if(diagnosisId != null && !diagnosisId.equals("")) {
service.selectDisplayImageByDiagnosis(diagnosisId, new AppAsyncCallback<List<EntityDisplay>>() {
@Override
public void onSuccess(List<EntityDisplay> result) {
listImage = result;
view.setListImage(result);
}
});
}
}
/**
* @return
*/
private void doSave() {
updateDataFromDisplay();
service.save(diagnosis, new AppAsyncCallback<Diagnosis>() {
@Override
public void onSuccess(Diagnosis result) {
diagnosis = result;
History.newItem("main/patients/edit/" + diagnosis.getPatient());
}
});
}
/**
* @return
*/
private void doNew() {
updateDataFromDisplay();
service.save(diagnosis, new AppAsyncCallback<Diagnosis>() {
@Override
public void onSuccess(Diagnosis result) {
diagnosis = result;
view.setDiagnosisId(diagnosis.getId());
view.setPatientId(diagnosis.getPatient());
view.setNewFlag(true);
//view.getFormUpload().submit();
doSaveImage(diagnosis);
}
});
}
/**
* @return
*/
private void doSaveImage(Diagnosis newDiagnosis) {
service.saveImage(newDiagnosis.getId(), view.getListImage(), new AppAsyncCallback<Void>() {
@Override
public void onSuccess(Void result) {
//doLoadImage(diagnosis.getId());
History.newItem("main/patients/edit/" + diagnosis.getPatient());
}
});
}
/**
*
*/
private void doLoad(final String id) {
view.getFormPanel().setVisible(false);
view.getLoadingPanel().setVisible(true);
service.selectById(id, new AppAsyncCallback<Diagnosis>() {
@Override
public void onSuccess(Diagnosis result) {
diagnosis = result;
doLoadPatient(diagnosis.getPatient());
doLoadFetals(diagnosis.getPatient(),diagnosis.getFetal());
doLoadImage(diagnosis.getId());
view.getFormPanel().setVisible(true);
view.getLoadingPanel().setVisible(false);
updateDisplayFromData();
}
});
}
/* (non-Javadoc)
* @see org.mbhcare.client.presenter.Presenter#go(com.google.gwt.user.client.ui.HasWidgets)
*/
@Override
public void go(final HasWidgets container) {
container.clear();
container.add(view.asWidget());
view.focus();
}
public void updateDataFromDisplay() {
if (view.getNgayKham().getValue() == null){
diagnosis.setNgaykham(new Date()); //set today
}
else{
diagnosis.setNgaykham(view.getNgayKham().getValue());
}
//Window.alert(view.getFetalId());
diagnosis.setFetal(view.getFetalId());
diagnosis.setFetalDisplayType(view.getFetalDisplayType());
diagnosis.setDiagnostictype(view.getDiagnosisType());
diagnosis.setNgaykinhcuoi(view.getNgayKinhCuoi().getValue());
diagnosis.setDuongkinhtruocsau(Utils.parseInt(view.getDuongKinhTruocSau().getValue()));//num
diagnosis.setTuthe(view.getTuThe());//string
diagnosis.setNoimactucung(Utils.parseInt(view.getNoiMacTuCung().getValue()));//num
diagnosis.setMatdo(view.getMatDo().getValue());
diagnosis.setLongtucung(view.getLongTuCung().getValue());
diagnosis.setBuongtrungtrai(view.getBuongTrungTrai().getValue());
diagnosis.setBuongtrungphai(view.getBuongTrungPhai().getValue());
diagnosis.setSoluongthai(Utils.parseInt(view.getSoLuongThai().getValue()));//num
diagnosis.setNgoithai(view.getNgoiThai().getValue());
diagnosis.setTimthai(view.getTimThai());//num
diagnosis.setDuongkinhluongdinh(Utils.parseInt(view.getDuongKinhLuongDinh().getValue()));//num
diagnosis.setDuongkinhnganbung(Utils.parseInt(view.getDuongKinhNganBung().getValue()));//num
diagnosis.setChieudaixuongdui(Utils.parseInt(view.getChieuDaiXuongDui().getValue()));//num
diagnosis.setChuvibung(Utils.parseInt(view.getChuViBung().getValue()));//num
diagnosis.setChuvivongdau(Utils.parseInt(view.getChuViVongDau().getValue()));//num
diagnosis.setDayronquanco(view.getDayRonQuanCo().getValue());
diagnosis.setCannangthai(Utils.parseInt(view.getCanNangThai().getValue()));//num
diagnosis.setLuongnuocoi(view.getLuongNuocOoi().getValue());//num
diagnosis.setVitrinhaubam(view.getViTriNhauBam().getValue());
diagnosis.setDotruongthanh(view.getDoTruongThanh().getValue());
diagnosis.setBatthuongcuame(view.getBatThuongCuaMe().getValue());
diagnosis.setTuoithai(Utils.parseInt(view.getTuoiThai().getValue()));//num
diagnosis.setDongmachronRI(Utils.parseDouble(view.getDongMachRonRI().getValue()));//num
diagnosis.setDongmachnaogiuaRI(Utils.parseDouble(view.getDongMachNaoGiuaRI().getValue()));//num
diagnosis.setDongmachronSD(Utils.parseDouble(view.getDongmachRonSD().getValue()));//num
diagnosis.setDongmachnaogiuaSD(Utils.parseDouble(view.getDongMachNaoGiuaSD().getValue()));//num
diagnosis.setChieudaidaumong(Utils.parseInt(view.getChieuDaiDauMong().getValue()));//num
diagnosis.setDomodagay(Utils.parseInt(view.getDoMoDaGay().getValue()));//num
diagnosis.setHinhthaihocthainhi(view.getHinhThaiHocThaiNhi().getValue());
diagnosis.setChieudaixuongmui(Utils.parseInt(view.getChieuDaiXuongMui().getValue()));//num
diagnosis.setDuongkinhngangtieunao(Utils.parseInt(view.getDuongKinhNgangTieuNao().getValue()));//num
diagnosis.setNgatunaothat(Utils.parseInt(view.getNgaTuNaoThat().getValue()));//num
diagnosis.setDaumatco(view.getDauMatCo().getValue());
diagnosis.setLongnguc(view.getLongNguc().getValue());
diagnosis.setBung(view.getBung().getValue());
diagnosis.setTuchi(view.getTuChi().getValue());
diagnosis.setKetluanchung(view.getKetLuanChung().getValue());
}
public void updateDisplayFromData() {
if (diagnosis == null) return ;
view.reset();
//Window.alert(diagnosis.getFetal());
//view.setFetalId(diagnosis.getFetal());
view.setDiagnosisType(diagnosis.getDiagnostictype());
view.getNgayKham().setValue(diagnosis.getNgaykham());
view.getNgayKinhCuoi().setValue(diagnosis.getNgaykinhcuoi());
view.getDuongKinhTruocSau().setValue(diagnosis.getDuongkinhtruocsau().toString());//num
view.setTuThe(diagnosis.getTuthe());//string
view.getNoiMacTuCung().setValue(diagnosis.getNoimactucung().toString());//num
view.getMatDo().setValue(diagnosis.getMatdo());
view.getLongTuCung().setValue(diagnosis.getLongtucung());
view.getBuongTrungTrai().setValue(diagnosis.getBuongtrungtrai());
view.getBuongTrungPhai().setValue(diagnosis.getBuongtrungphai());
view.getSoLuongThai().setValue(diagnosis.getSoluongthai().toString());
view.getNgoiThai().setValue(diagnosis.getNgoithai());
view.setTimThai(diagnosis.getTimthai().intValue());
view.getDuongKinhLuongDinh().setValue(diagnosis.getDuongkinhluongdinh().toString());
view.getDuongKinhNganBung().setValue(diagnosis.getDuongkinhnganbung().toString());
view.getChieuDaiXuongDui().setValue(diagnosis.getChieudaixuongdui().toString());
view.getChuViBung().setValue(diagnosis.getChuvibung().toString());
view.getChuViVongDau().setValue(diagnosis.getChuvivongdau().toString());
view.getDayRonQuanCo().setValue(diagnosis.getDayronquanco());
view.getCanNangThai().setValue(diagnosis.getCannangthai().toString());
view.getLuongNuocOoi().setValue(diagnosis.getLuongnuocoi());
view.getViTriNhauBam().setValue(diagnosis.getVitrinhaubam());
view.getDoTruongThanh().setValue(diagnosis.getDotruongthanh());
view.getBatThuongCuaMe().setValue(diagnosis.getBatthuongcuame());
view.getTuoiThai().setValue(diagnosis.getTuoithai().toString());
view.setTuoiThaiType(diagnosis.getTuoithai().intValue());
view.getDongMachRonRI().setValue(diagnosis.getDongmachronRI().toString());
view.getDongMachNaoGiuaRI().setValue(diagnosis.getDongmachnaogiuaRI().toString());
view.getDongmachRonSD().setValue(diagnosis.getDongmachronSD().toString());
view.getDongMachNaoGiuaSD().setValue(diagnosis.getDongmachnaogiuaSD().toString());
view.getChieuDaiDauMong().setValue(diagnosis.getChieudaidaumong().toString());
view.getDoMoDaGay().setValue(diagnosis.getDomodagay().toString());
view.getHinhThaiHocThaiNhi().setValue(diagnosis.getHinhthaihocthainhi());
view.getChieuDaiXuongMui().setValue(diagnosis.getChieudaixuongmui().toString());
view.getDuongKinhNgangTieuNao().setValue(diagnosis.getDuongkinhngangtieunao().toString());
view.getNgaTuNaoThat().setValue(diagnosis.getNgatunaothat().toString());
view.getDauMatCo().setValue(diagnosis.getDaumatco());
view.getLongNguc().setValue(diagnosis.getLongnguc());
view.getBung().setValue(diagnosis.getBung());
view.getTuChi().setValue(diagnosis.getTuchi());
view.getKetLuanChung().setValue(diagnosis.getKetluanchung());
view.setDiagnosisId(diagnosis.getId());
view.setDiagnosisImage(diagnosis.getId());
view.focus();
}
@Override
public void onCancelButtonClicked() {
//History.newItem("main/diagnosis");
History.newItem("main/patients/edit/" + diagnosis.getPatient());
}
@Override
public void onListButtonClicked() {
History.newItem("main/diagnosis");
}
@Override
public void onSubmitButtonClicked() {
if(hasValidInput()){
if (!view.getDiagnosisId().equals("")){
//Window.alert("doSave : id" + view.getDiagnosisId());
doSave();
}
else {
//Window.alert("doNew : id" + view.getDiagnosisId());
doNew();
}
}
}
@Override
public void onPrintButtonClicked() {
Date date = new Date();
// Calendar ca1 = Calendar.getInstance();
// ca1.add(Calendar.MONTH, -9); // subtract 9 Months in Date in Calendar
service.selectNineMonths(patient.getId().toString(), view.getFetalId(), new AppAsyncCallback<List<EntityDisplay>>() {
@Override
public void onSuccess(List<EntityDisplay> result) {
updateDataFromDisplay();
diagnosisEditReport = new DiagnosisEditReport();
diagnosisEditReport.show(patient,diagnosis, view, result);
}
});
}
@Override
public void onUploadButtonClicked() {
view.getFormUpload().submit();
}
@Override
public void onRefreshImageList() {
doLoadImage(diagnosis.getId());
}
@Override
public void onDeleteButtonClicked(String id) {
deleteSelectedImage(id);
}
@Override
public void onSaveUploadImage(String id) {
List<String> ids = new ArrayList<String>();
ids.add(id);
service.saveImage(diagnosis.getId(), ids, new AppAsyncCallback<Void>() {
@Override
public void onSuccess(Void result) {
doLoadImage(diagnosis.getId());
}
});
}
private void deleteSelectedImage(String id) {
if(id.equals("")) {
Window.alert(constants.noItemsSelected());
} else {
if(Window.confirm(constants.areYouSure())) {
List<String> ids = new ArrayList<String>();
ids.add(id);
service.deleteImage(ids, new AppAsyncCallback<Void>() {
@Override
public void onSuccess(Void result) {
doLoadImage(diagnosis.getId());
}
});
}
}
}
}