Exemple #1
0
  @Override
  public void init() {

    dbs = new DBStrings();
    dbs.initialize();
    DBConnectDialog dbd = new DBConnectDialog(frame, dbs);
    dbs = dbd.getDBStrings();
    // panel.metaData().getDBStrings();

    // get DBStrings from user if necessary
    if (dbs.isConfigValid()) {
      connectedToDB = true;
    } else {
      // init DB strings if necessary
      if (!dbs.isInitialized()) {
        dbs.initialize();
      }

      // show connection dialog
      dbd = new DBConnectDialog(frame, dbs);
      dbd.setLocationRelativeTo(frame);
      dbd.setVisible(true);

      connectedToDB = dbd.isConnectedToDB();

      // store database strings
      if (connectedToDB) {
        dbs = dbd.getDBStrings();
        dbd.dispose();
      }
    }
  }
Exemple #2
0
  private void performImport() {
    if (!connectedToDB) {
      return;
    }

    frame.output(Localization.lang("Attempting SQL import..."));
    DBExporterAndImporterFactory factory = new DBExporterAndImporterFactory();
    DatabaseImporter importer = factory.getImporter(dbs.getDbPreferences().getServerType());
    try {
      try (Connection conn = importer.connectToDB(dbs);
          Statement statement = conn.createStatement();
          ResultSet rs = statement.executeQuery(SQLUtil.queryAllFromTable("jabref_database"))) {

        Vector<Vector<String>> matrix = new Vector<>();

        while (rs.next()) {
          Vector<String> v = new Vector<>();
          v.add(rs.getString("database_name"));
          matrix.add(v);
        }

        if (matrix.isEmpty()) {
          JOptionPane.showMessageDialog(
              frame,
              Localization.lang("There are no available databases to be imported"),
              Localization.lang("Import from SQL database"),
              JOptionPane.INFORMATION_MESSAGE);
        } else {
          DBImportExportDialog dialogo =
              new DBImportExportDialog(frame, matrix, DBImportExportDialog.DialogType.IMPORTER);
          if (dialogo.removeAction) {
            String dbName = dialogo.selectedDB;
            DatabaseUtil.removeDB(dialogo, dbName, conn, databaseContext);
            performImport();
          } else if (dialogo.moreThanOne) {
            databases =
                importer.performImport(
                    dbs,
                    dialogo.listOfDBs,
                    frame.getCurrentBasePanel().getBibDatabaseContext().getMode());
            for (DBImporterResult res : databases) {
              databaseContext = res.getDatabaseContext();
              dbs.isConfigValid(true);
            }
            frame.output(
                Localization.lang(
                    "%0 databases will be imported", Integer.toString(databases.size())));
          } else {
            frame.output(Localization.lang("Importing canceled"));
          }
        }
      }
    } catch (Exception ex) {
      String preamble =
          Localization.lang("Could not import from SQL database for the following reason:");
      String errorMessage = SQLUtil.getExceptionMessage(ex);
      dbs.isConfigValid(false);
      JOptionPane.showMessageDialog(
          frame,
          preamble + '\n' + errorMessage,
          Localization.lang("Import from SQL database"),
          JOptionPane.ERROR_MESSAGE);
      frame.output(Localization.lang("Error importing from database"));
      LOGGER.error("Error importing from database", ex);
    }
  }