Package org.mbhcare.client.presenter

Source Code of org.mbhcare.client.presenter.PatientsEditPresenter

/**
*
*/
package org.mbhcare.client.presenter;

import java.util.List;

import org.mbhcare.client.event.DiagnosisEventBus;
import org.mbhcare.client.event.EventBus;
import org.mbhcare.client.event.PatientsEventBus;
import org.mbhcare.client.i18n.AppMessages;
import org.mbhcare.client.i18n.PatientsConstants;
import org.mbhcare.client.rpc.AppAsyncCallback;
import org.mbhcare.client.rpc.DiagnosisServiceAsync;
import org.mbhcare.client.rpc.PatientsServiceAsync;
import org.mbhcare.client.view.patients.PatientsEditView;
import org.mbhcare.client.view.patients.ResponseDialog;
import org.mbhcare.shared.FieldVerifier;
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 PatientsEditPresenter implements Presenter, PatientsEditView.Presenter {
 
  private final PatientsEditView view;
 
  private PatientsServiceAsync service = PatientsEventBus.getService();
  private DiagnosisServiceAsync diagnosisservice = DiagnosisEventBus.getService();
  private PatientsConstants constants = PatientsEventBus.getConstants();
  private AppMessages messages = EventBus.getMessages();
 
  private Patient patient;
  ResponseDialog responseDialog;
 
  private List<EntityDisplay> list;
 
  public PatientsEditPresenter(PatientsEditView view, String id) {
      responseDialog = new ResponseDialog();
     
      this.view = view;
      view.setPresenter(this);
     
      patient = new Patient();
      if(id != null){
        doLoad(id);
        view.getDiagnosisButton().setEnabled(true);
      }
      else {       
        updateDisplayFromData();
        view.getDiagnosisButton().setEnabled(false);
    }
  }
 
  /**
   * @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;
  }

  /**
   * @return
   */
  private void doSave() {
    updateDataFromDisplay();
   
    service.save(patient, new AppAsyncCallback<Patient>() {

      @Override
      public void onSuccess(Patient result) {
        History.newItem("main/patients");
      }
     
    });
  }
 
  /**
   *
   */
  private void doLoad(final String id) {
    view.getFormPanel().setVisible(false);
    view.getLoadingPanel().setVisible(true);
    service.selectById(id, new AppAsyncCallback<Patient>() {
 
      @Override
      public void onSuccess(Patient result) {
        patient = result;
        view.getFormPanel().setVisible(true);
        view.getLoadingPanel().setVisible(false);
        updateDisplayFromData();       
        updateDiagnosisList(id);     
      }     
    });
 
 
  /* (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() {
    patient.setFirstName(view.getFirstName().getValue());
    patient.setLastName(view.getLastName().getValue());
    //patient.setBirthDay(view.getBirthDay().getValue());
    patient.setBirthYear(view.getBirthYear().getValue());
    patient.setAddress(view.getAddress().getValue());
    patient.setPhone(view.getPhone().getValue());
    patient.setEmail(view.getEmail().getValue());
    patient.setNotes(view.getNotes().getValue());
  }
 
  public void updateDisplayFromData() {
    view.reset();
    view.getFirstName().setValue(patient.getFirstName());
    view.getLastName().setValue(patient.getLastName());
    //view.getBirthDay().setValue(patient.getBirthDay());
    view.getBirthYear().setValue(patient.getBirthYear());
    view.getAddress().setValue(patient.getAddress());
    view.getPhone().setValue(patient.getPhone());
    view.getEmail().setValue(patient.getEmail());
    view.getNotes().setValue(patient.getNotes());
   
    view.focus();
  }

  private void updateDiagnosisList(String id) {
    view.getLoadingSubPanel().setVisible(true);
    view.getListPanel().setVisible(false);
    diagnosisservice.selectByPatient(id,new AppAsyncCallback<List<EntityDisplay>>() {
     
      @Override
      public void onSuccess(List<EntityDisplay> result) {
        list = result;
        view.setData(result);
        view.getListPanel().setVisible(true);
        view.getLoadingSubPanel().setVisible(false);       
      }
     
    });
  }
 
  @Override
  public void onCancelButtonClicked() {
    History.newItem("main/patients");
  }

  @Override
  public void onListButtonClicked() {
    History.newItem("main/patients");
  }

  @Override
  public void onSubmitButtonClicked() {
    if(hasValidInput())
    doSave();   
  }
 
  @Override
  public void onSubmitAndAddButtonClicked() {
    if(hasValidInput())
      doSaveAndAdd();   
  }
 
  @Override
  public void onPrintButtonClicked() {
    updateDataFromDisplay();
    responseDialog.show(patient);
  }

  @Override
  public void onReloadButtonClicked(){
    updateDiagnosisList(patient.getId());
  }
 
  @Override
  public void onDiagnosisButtonClicked() {
    History.newItem("main/diagnosis/new/" + patient.getId());
  }
 
  @Override
  public void onSubmitAndDiagnosisButtonClicked() {
    doSaveAndDiagnosis()
  }
 
  /**
   * @return
   */
  private void doSaveAndDiagnosis() {
    updateDataFromDisplay();

    service.save(patient, new AppAsyncCallback<Patient>() {

      @Override
      public void onSuccess(Patient result) {
        if (result != null){

          History.newItem("main/diagnosis/new/" + result.getId());
        } else {
          History.newItem("main/patients");
        }
      }
     
    });
  }
 
  /**
   * @return
   */
  private void doSaveAndAdd() {
    updateDataFromDisplay();
   
    service.save(patient, new AppAsyncCallback<Patient>() {

      @Override
      public void onSuccess(Patient result) {
        if (result != null){
          History.newItem("main/patients/edit");
          view.reset();
          view.focus();
        } else {
   
        }
      }
     
    });
  }
 
  @Override
  public void onItemClicked(int item) {
    if(item > 0) {
      String id = list.get(item - 1).getData()[0];
   
      History.newItem("main/diagnosis/edit/" + id);
    }
  }

}
TOP

Related Classes of org.mbhcare.client.presenter.PatientsEditPresenter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.