package de.uniluebeck.mi.projmi6.controller; /** * Created by 631806 on 12.11.15. */ import ca.uhn.hl7v2.HL7Exception; import de.uniluebeck.mi.projmi6.db.DBHandler; import de.uniluebeck.mi.projmi6.hapi.HL7Sender; import de.uniluebeck.mi.projmi6.model.*; import de.uniluebeck.mi.projmi6.view.DateTimePicker; import javafx.beans.property.ObjectProperty; import javafx.beans.property.ReadOnlyObjectProperty; import javafx.beans.property.SimpleObjectProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.control.*; import javafx.scene.layout.GridPane; import java.io.IOException; import java.sql.SQLException; public class FallController { /** * A property to the case which data this controller manages. */ private final SimpleObjectProperty fallProperty = new SimpleObjectProperty<>(); /** * Controllers current state. */ private final SimpleObjectProperty state = new SimpleObjectProperty<>(State.VIEW); private MainController mainController; @FXML private DateTimePicker dtTmAufnahme, dtTmEntlassung; @FXML private Label fallId; @FXML private ComboBox fallFallart; @FXML private ComboBox fallKasse; @FXML private TextField fallVersichertennummer, fallEinweisenderArzt; @FXML private CheckBox fallSelbsteinweisung; @FXML private ComboBox fallHauptdiagnose; @FXML private Label fallCreator, fallEditor, fallCreateTime, fallEditTime; @FXML private Button btnFallSave, btnFallAbort, btnFallCancel, btnFallEnableEdit, btnFallSendHl7; @FXML private GridPane fallFields; /** * Contstructor. * @param mainController The main controller that creats this instance */ public FallController(MainController mainController) { this.mainController = mainController; } /** * Getter for the {@link #fallProperty()} */ public Fall getFall() { return fallProperty.get(); } /** * Setter for the {@link #fallProperty()} * @param fall */ public void setFall(Fall fall) { this.fallProperty.set(fall); } /** * The case that is shown in this controller. */ public SimpleObjectProperty fallProperty() { return fallProperty; } /** * Getter for the {@link #stateProperty()}. */ public State getState() { return state.get(); } /** * The controllers current state. */ public ReadOnlyObjectProperty stateProperty() { return state; } /** * For showing the main diagnosis, the Controller also needs a pointer to the list of case diagnosis. * @return */ public ObjectProperty> diagnosenProperty() { return fallHauptdiagnose.itemsProperty(); } /** * Getter for the {@link #diagnosenProperty()} */ public ObservableList getDiagnosen() { return fallHauptdiagnose.getItems(); } /** * Setter for the diagnosis in the main diagnosis combo box. * @see #diagnosenProperty() */ public void setDiagnosen(ObservableList list) { fallHauptdiagnose.setItems(list); } /** * FXMLLoaders initialize()-method. */ @FXML private void initialize() { fallEinweisenderArzt.disableProperty().bind(fallSelbsteinweisung.selectedProperty()); fallFallart.setItems(FXCollections.observableArrayList(FallArt.values())); fallKasse.setItems(mainController.getStammdaten().getKassen()); initButtons(); fallFields.disableProperty().bind(state.isEqualTo(State.VIEW)); fallProperty.addListener(((observable, oldValue, newValue) -> { if (state.get() == State.VIEW) { copyFallDataIntoField(fallProperty.get()); } })); fallHauptdiagnose.itemsProperty().addListener((observable1, oldValue1, newValue1) -> { copyHauptdiagnoseToComboBox(fallProperty.get()); }); state.addListener((observable, oldValue, newValue) -> { if (newValue == State.EDIT || newValue == State.CREATE) { mainController.lockForEdit(MainController.TabName.OVERVIEW); } else { mainController.unlockFromEdit(); } }); } /** * Hide the buttons depending on controller state. */ private void initButtons() { btnFallEnableEdit.managedProperty().bind( state.isEqualTo(State.VIEW).and(fallProperty.isNotNull()) ); btnFallEnableEdit.visibleProperty().bind(btnFallEnableEdit.managedProperty()); btnFallAbort.managedProperty().bind( state.isNotEqualTo(State.VIEW) ); btnFallAbort.visibleProperty().bind(btnFallAbort.managedProperty()); btnFallSave.managedProperty().bind( state.isNotEqualTo(State.VIEW) ); btnFallSave.visibleProperty().bind(btnFallSave.managedProperty()); btnFallCancel.managedProperty().bind( state.isEqualTo(State.VIEW).and(fallProperty.isNotNull()) ); btnFallCancel.visibleProperty().bind(btnFallCancel.managedProperty()); btnFallSendHl7.managedProperty().bind( state.isEqualTo(State.VIEW).and(fallProperty.isNotNull()) ); btnFallSendHl7.visibleProperty().bind(btnFallSendHl7.managedProperty()); } /** * EventHandler for the {@link #btnFallSendHl7} button. */ @FXML private void clickedSendHl7() { /* Natascha */ //TODO send funny message Patient patient = mainController.getPatientTablesController().getSelectedPatient(); Fall fall = fallProperty.get(); fall.setPatient(patient); try { HL7Sender.createMessageADT_A01(fall); } catch (HL7Exception | IOException | SQLException e) { e.printStackTrace(); } } /** * Toggle controller state to edit */ public void editFall() { this.state.set(State.EDIT); } /** * EventHandler for {@link #btnFallEnableEdit} */ @FXML private void clickedFallEnableEdit() { editFall(); } /** * EventHandler for the {@link #btnFallCancel}. */ @FXML private void clickedFallCancel() { if (fallProperty.get() != null) { fallProperty.get().setStorniert(true); try { DBHandler.setFall(fallProperty.get(), mainController.getCurrentMitarbeiter().getMitarbID(), true); } catch (Exception e) { e.printStackTrace(); } mainController.refreshCasesFromDb(mainController.getPatientTablesController().getSelectedPatient()); } } /** * EventHandler for the {@link #btnFallAbort} */ @FXML private void clickedFallAbort() { this.state.set(State.VIEW); copyFallDataIntoField(fallProperty.get()); } /** * EventHandler for the {@link #btnFallSave} button. */ @FXML private void clickedFallSave() { if (this.state.get() == State.CREATE) { Fall fall = new Fall(); copyFieldDataIntoFall(fall); try { int newfallid = DBHandler.setFall(fall, mainController.getCurrentMitarbeiter().getMitarbID()); fall.setFallID(newfallid); } catch (SQLException e) { e.printStackTrace(); } try { HL7Sender.createMessageADT_A01(fall); } catch (IOException | HL7Exception | SQLException e) { e.printStackTrace(); } } else { copyFieldDataIntoFall(fallProperty.get()); try { DBHandler.setFall(fallProperty.get(), mainController.getCurrentMitarbeiter().getMitarbID(), true); } catch (SQLException e) { e.printStackTrace(); } } this.state.set(State.VIEW); mainController.refreshCasesFromDb(mainController.getPatientTablesController().getSelectedPatient()); } /** * Change the controllers state, init the fields for assistance. */ public void createNewFall() { clearFields(); this.state.set(State.CREATE); Patient patient = mainController.getPatientTablesController().getSelectedPatient(); // Kasse by Default auf die im Patienten hinterlegten Kasse setzen. for (Kasse kasse : fallKasse.getItems()) { if (kasse.getKassenID() == patient.getKassenID()) { fallKasse.getSelectionModel().select(kasse); break; } } fallVersichertennummer.setText(patient.getVersichertennummer()); } /** * Clears the TextFields. */ private void clearFields() { if (state.get() == State.CREATE) { dtTmAufnahme.setToCurrentDateTime(); dtTmEntlassung.setToCurrentDateTime(); } else { dtTmAufnahme.setDateTime(null); dtTmEntlassung.setDateTime(null); } fallId.setText(""); fallCreateTime.setText(""); fallCreator.setText(""); fallEditTime.setText(""); fallEditor.setText(""); fallEinweisenderArzt.setText(""); fallSelbsteinweisung.setSelected(false); fallVersichertennummer.setText(""); fallKasse.setValue(null); fallHauptdiagnose.setValue(null); fallHauptdiagnose.setItems(null); fallFallart.setValue(null); } /** * Copy the text entered in the views into a Fall object. * @param fall The object for copying the data into. */ private void copyFieldDataIntoFall(Fall fall) { fall.setPatient(mainController.getPatientTablesController().getSelectedPatient()); fall.setAufnahmeDatum(dtTmAufnahme.getDateTime()); fall.setEntlassungsDatum(dtTmEntlassung.getDateTime()); if (fallSelbsteinweisung.isSelected()) { fall.setSelbsteinweisung(true); fall.setEinweisenderArzt(null); } else { fall.setEinweisenderArzt(fallEinweisenderArzt.getText()); fall.setSelbsteinweisung(false); } fall.setVersichertenNummer(fallVersichertennummer.getText()); fall.setKasse(fallKasse.getValue()); fall.setFallArt(fallFallart.getValue()); if (fallHauptdiagnose.getSelectionModel().getSelectedItem() != null) { fall.setHauptdiagnoseId(fallHauptdiagnose.getSelectionModel().getSelectedItem().getDiagID()); } //fall.setVorstellDatum(); } /** * Set the field values in the view to the value of the {@link Fall} object. * @param fall The object whose data will be copied. */ private void copyFallDataIntoField(Fall fall) { if (fall == null) { clearFields(); return; } dtTmAufnahme.setDateTime(fall.getAufnahmeDatum()); dtTmEntlassung.setDateTime(fall.getEntlassungsDatum()); fallId.setText(fall.getFallID() + ""); //(fall.getPatient().getVorname()+" "+fall.getPatient().getNachname()); //TODO fallCreateTime.setText(fall.getErstellDatumZeit() != null ? fall.getErstellDatumZeit().toString() : ""); fallCreator.setText(Integer.toString(fall.getErsteller())); fallEditTime.setText(fall.getBearbeitetDatumZeit() != null ? fall.getBearbeitetDatumZeit().toString() : ""); fallEditor.setText(Integer.toString(fall.getBearbeiter())); fallEinweisenderArzt.setText(fall.getEinweisenderArzt()); fallSelbsteinweisung.setSelected(fall.getSelbsteinweisung()); fallVersichertennummer.setText(fall.getVersichertenNummer()); fallKasse.setValue(fall.getKasse()); copyHauptdiagnoseToComboBox(fall); fallFallart.setValue(fall.getFallArt()); } /** * This method sets the main diagnosis in the ComboBox * @param fall The case whose main diagnosis will be picked */ private void copyHauptdiagnoseToComboBox(Fall fall) { if (fallHauptdiagnose.getItems() == null || fall == null) { fallHauptdiagnose.setValue(null); return; } for (Diagnose diagnose : fallHauptdiagnose.getItems()) { System.out.println(diagnose.getDiagID() + "=" + fall.getHauptdiagnoseId()); if (diagnose.getDiagID().equalsIgnoreCase(fall.getHauptdiagnoseId())) { fallHauptdiagnose.getSelectionModel().select(diagnose); return; } } } /** * State definitions. */ public enum State { CREATE, EDIT, VIEW } }