/**
*
*/
package systole.view.crud.patient.patientComponents.medicines;
import java.awt.Frame;
import java.util.Iterator;
import java.util.List;
import javax.swing.JDialog;
import systole.domain.clinicalInformation.Medicine;
import systole.domain.clinicalInformation.MedicinePatient;
import systole.domain.clinicalInformation.MedicinePatientId;
import systole.domain.persons.Patient;
import systole.exceptions.ExceptionDAO;
import systole.view.crud.patient.patientComponents.controller.ControllerEditionPatienComponent;
/**
* @author jmj
*
*/
public class ControllerEditionMedicineByPatient extends ControllerEditionPatienComponent {
/**
* @param patient
*/
public ControllerEditionMedicineByPatient(Patient patient) {
super(patient);
this.entityName = "Medicamento";
}
/* (non-Javadoc)
* @see systole.view.crud.controller.ControllerEntityEdition#loadEntityFromForm()
*/
@Override
protected void loadEntityFromForm() {
JDialogMedicineByPatient form = (JDialogMedicineByPatient) this.editForm;
MedicinePatient medicinePatient = (MedicinePatient) this.curretnEntity;
medicinePatient.setMedicine((Medicine) form.getCmbMedicines().getSelectedItem());
medicinePatient.setDosage(form.getTxtDosage().getText());
Integer age = (Integer) form.getjSpnYears().getValue();
medicinePatient.setAgeAtStartAsInteger(age);
}
/* (non-Javadoc)
* @see systole.view.crud.controller.ControllerEntityEdition#loadEntityOnForm()
*/
@Override
protected void loadEntityOnForm() {
JDialogMedicineByPatient form = (JDialogMedicineByPatient) this.editForm;
MedicinePatient medicinePatient = (MedicinePatient) this.curretnEntity;
form.getCmbMedicines().setSelectedItem(medicinePatient.getMedicine());
form.getTxtDosage().setText((medicinePatient.getDosage() == null) ? "" : medicinePatient.getDosage());
Integer age = medicinePatient.getAgeAtStartAsInteger();
form.getjSpnYears().setValue(age);
}
/* (non-Javadoc)
* @see systole.view.crud.controller.ControllerEntityEdition#save()
*/
@Override
protected void save() {
if (!this.isEditing()) {
MedicinePatient medicinePatient = (MedicinePatient) this.curretnEntity;
medicinePatient.setId(new MedicinePatientId(medicinePatient.getMedicine().getId(), medicinePatient.getPatient().getId()));
this.patient.getMedicinesPatient().add(medicinePatient);
}
}
/* (non-Javadoc)
* @see systole.view.crud.controller.ControllerEntityEdition#valid()
*/
@Override
protected String valid() {
if (!this.isEditing()) {
JDialogMedicineByPatient form = (JDialogMedicineByPatient) this.editForm;
if ((form.getCmbMedicines().getSelectedIndex() == -1) || (form.getCmbMedicines().getSelectedItem() == null)) {
return "Debe seleccionar un medicamento";
}
try {
Medicine medicine = (Medicine) form.getCmbMedicines().getSelectedItem();
if (this.facadeDB.getMedicineByPatientBroker().existMedicineByPatient(medicine, this.patient)) {
return "Ya existe el medicamento para el paciente";
}
} catch (ExceptionDAO ex) {
this.facadeDB.refreshSession();
return ex.getMessage();
}
}
return null;
}
@Override
protected void loadIconOnForm() {
}
@Override
protected Object createEntity() throws ExceptionDAO {
return new MedicinePatient(this.patient);
}
@Override
protected JDialog createEditionForm(Frame parent) {
return new JDialogMedicineByPatient(parent, this);
}
@Override
protected void initFormsControls() throws ExceptionDAO {
JDialogMedicineByPatient form = (JDialogMedicineByPatient) this.editForm;
form.getCmbMedicines().removeAllItems();
List<Medicine> list = this.facadeDB.getMedicineBroker().getAllMedicines();
Iterator<Medicine> it = list.iterator();
while (it.hasNext()) {
form.getCmbMedicines().addItem(it.next());
}
form.getCmbMedicines().setEnabled(!this.isEditing());
this.editForm = form;
}
}