private void btnOKActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_btnOKActionPerformed
    EntityManager em = OPDE.createEM();
    try {
      em.getTransaction().begin();
      TradeForm myTradeForm = em.merge(tradeForm);
      em.lock(myTradeForm, LockModeType.OPTIMISTIC);
      myTradeForm.setSubtext(txtZusatz.getText());
      DosageForm dosageForm = em.merge((DosageForm) cmbForm.getSelectedItem());
      em.lock(dosageForm, LockModeType.OPTIMISTIC);
      myTradeForm.setDosageForm(dosageForm);

      em.getTransaction().commit();

      tradeForm = myTradeForm;
    } catch (Exception e) {
      if (em.getTransaction().isActive()) {
        em.getTransaction().rollback();
      }
      OPDE.fatal(e);
    } finally {
      em.close();
    }
    dispose();
  } // GEN-LAST:event_btnOKActionPerformed
  private void initDialog() {
    initPhase = true;
    cmbDaysWeeks.setModel(
        new DefaultComboBoxModel(
            new String[] {SYSTools.xx("misc.msg.Days"), SYSTools.xx("misc.msg.weeks")}));
    cbWeightControlled.setText(
        SYSTools.xx("opde.medication.medproduct.wizard.subtext.weightControlled"));
    cbWeightControlled.setSelected(tradeForm.isWeightControlled());
    cbExpiresAfterOpened.setText(SYSTools.xx("tradeform.subtext.expiresAfterOpenedIn"));
    cbExpiresAfterOpened.setSelected(tradeForm.getDaysToExpireAfterOpened() != null);
    txtExpiresIn.setEnabled(cbExpiresAfterOpened.isSelected());
    cmbDaysWeeks.setEnabled(cbExpiresAfterOpened.isSelected());
    Pair<Integer, Integer> pair = TradeFormTools.getExpiresIn(tradeForm);
    if (pair != null) {
      txtExpiresIn.setText(
          pair.getFirst() > 0 ? pair.getFirst().toString() : pair.getSecond().toString());
      cmbDaysWeeks.setSelectedIndex(pair.getFirst() > 0 ? 0 : 1);
    }

    EntityManager em = OPDE.createEM();
    Query query = em.createQuery("SELECT m FROM DosageForm m ORDER BY m.preparation, m.usageText");
    cmbForm.setModel(new DefaultComboBoxModel(query.getResultList().toArray(new DosageForm[] {})));
    cmbForm.setRenderer(DosageFormTools.getRenderer(0));
    em.close();

    cmbForm.setSelectedItem(tradeForm.getDosageForm());
    txtZusatz.setText(SYSTools.catchNull(tradeForm.getSubtext()));

    btnAdd.setEnabled(OPDE.getAppInfo().isAllowedTo(InternalClassACL.MANAGER, "opde.medication"));
    btnEdit.setEnabled(OPDE.getAppInfo().isAllowedTo(InternalClassACL.MANAGER, "opde.medication"));
    initPhase = false;
  }
 private void cbExpiresAfterOpenedItemStateChanged(ItemEvent e) {
   if (initPhase) return;
   txtExpiresIn.setEnabled(e.getStateChange() == ItemEvent.SELECTED);
   cmbDaysWeeks.setEnabled(e.getStateChange() == ItemEvent.SELECTED);
   if (e.getStateChange() == ItemEvent.SELECTED) {
     txtExpiresIn.setText("7");
     cmbDaysWeeks.setSelectedIndex(0);
     tradeForm.setDaysToExpireAfterOpened(7);
   } else {
     tradeForm.setDaysToExpireAfterOpened(null);
   }
 }
 private void txtExpiresInFocusLost(FocusEvent e) {
   if (initPhase) return;
   Integer i = SYSTools.checkInteger(txtExpiresIn.getText());
   if (i == null || i.compareTo(0) <= 0) {
     i = 7;
     txtExpiresIn.setText("7");
   }
   if (cmbDaysWeeks.getSelectedIndex() == 1) {
     tradeForm.setDaysToExpireAfterOpened(i * 7);
   } else {
     tradeForm.setDaysToExpireAfterOpened(i);
   }
 }
 private void cmbDaysWeeksItemStateChanged(ItemEvent e) {
   if (initPhase) return;
   if (e.getStateChange() == ItemEvent.SELECTED) {
     Integer i = SYSTools.checkInteger(txtExpiresIn.getText());
     if (i == null || i.compareTo(0) <= 0) {
       i = 7;
       txtExpiresIn.setText("7");
     }
     if (cmbDaysWeeks.getSelectedIndex() == 1) {
       tradeForm.setDaysToExpireAfterOpened(i * 7);
     } else {
       tradeForm.setDaysToExpireAfterOpened(i);
     }
   }
 }
  private void btnOKActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_btnOKActionPerformed

    if (!saveOK()) return;

    EntityManager em = OPDE.createEM();
    try {
      em.getTransaction().begin();
      MedProducts myProduct = em.merge(product);

      myProduct.setText(txtName.getText().trim());
      myProduct.setSideEffects(txtSideEffects.getText().trim());
      myProduct.setACME(em.merge((ACME) cmbAcme.getSelectedItem()));

      for (TradeForm tf : myProduct.getTradeforms()) {
        em.lock(em.merge(tf), LockModeType.OPTIMISTIC_FORCE_INCREMENT);
        for (MedPackage mp : tf.getPackages()) {
          em.lock(em.merge(mp), LockModeType.OPTIMISTIC_FORCE_INCREMENT);
        }
      }

      em.lock(myProduct, LockModeType.OPTIMISTIC);

      em.getTransaction().commit();

      product = myProduct;
    } catch (Exception e) {
      if (em.getTransaction().isActive()) {
        em.getTransaction().rollback();
      }
      OPDE.fatal(e);
    } finally {
      em.close();
    }
    dispose();

    // hier gehts weiter. prüf auch die anderen locks bei den anderen editoren

  } // GEN-LAST:event_btnOKActionPerformed
 private void cbWeightControlledItemStateChanged(ItemEvent e) {
   tradeForm.setWeightControlled(e.getStateChange() == ItemEvent.SELECTED);
 }