Exemplo n.º 1
0
    @Override
    public void actionPerformed(ActionEvent e) {
      try {
        if (!((String) tableNames.getSelectedItem()).equals("INCIDENT")) {
          JOptionPane.showMessageDialog(
              null, "Не выбрана необходимая таблица.", "Message", JOptionPane.INFORMATION_MESSAGE);
          return;
        }
        if (table.getSelectedRow() == -1) {
          JOptionPane.showMessageDialog(
              null, "Запись не выбрана.", "Message", JOptionPane.INFORMATION_MESSAGE);
          return;
        }
        int selectedRow = table.getSelectedRow();
        int incidentNumber = Integer.parseInt(table.getValueAt(selectedRow, 0).toString());

        String query =
            "select personnumber, firstName, lastName, passportnumber from " + "alex.person";
        PreparedStatement pStatment = connection.prepareStatement(query);
        ResultSet result = pStatment.executeQuery();
        Map<String, Integer> persons = new HashMap<String, Integer>();
        while (result.next()) {
          persons.put(
              result.getString("firstname")
                  + " "
                  + result.getString("lastname")
                  + ": "
                  + result.getString("passportnumber"),
              Integer.valueOf(result.getInt("personnumber")));
        }

        query = "select * from alex.status";
        pStatment = connection.prepareStatement(query);
        result = pStatment.executeQuery();
        Map<String, Integer> status = new HashMap<String, Integer>();
        while (result.next()) {
          status.put(
              result.getString("statusname"), Integer.valueOf(result.getInt("statusnumber")));
        }

        ConnectWithPersonDialog dialog = new ConnectWithPersonDialog(thisFrame, persons, status);
        dialog.setVisible(rootPaneCheckingEnabled);
        if (!dialog.getOk()) {
          return;
        }

        BaseUpdater updater = new BaseUpdater();
        boolean resultBool =
            updater.addPersonToIncident(
                connection, dialog.getPersonKey(), incidentNumber, dialog.getStatusKey());

        if (resultBool) {
          JOptionPane.showMessageDialog(
              null,
              "Лицо было добавлено к происшествию.",
              "Message",
              JOptionPane.INFORMATION_MESSAGE);
        } else {
          JOptionPane.showMessageDialog(
              null, "Лицо не было добавлено к происшествию", "Ошибка", JOptionPane.ERROR_MESSAGE);
        }

      } catch (SQLException ex) {
        JOptionPane.showMessageDialog(
            null,
            "Ошибка. Лицо не было добавлено к происшествию",
            "Ошибка",
            JOptionPane.ERROR_MESSAGE);
      }
    }