예제 #1
0
  @Override
  public boolean processQuery(String query, ImportInspector dialog, OutputPrinter status) {
    stopFetching = false;
    try {
      List<String> citations = getCitations(query);
      if (citations == null) {
        return false;
      }
      if (citations.isEmpty()) {
        status.showMessage(
            Localization.lang("No entries found for the search string '%0'", query),
            Localization.lang("Search %0", SCIENCE_DIRECT),
            JOptionPane.INFORMATION_MESSAGE);
        return false;
      }

      int i = 0;
      for (String cit : citations) {
        if (stopFetching) {
          break;
        }
        BibsonomyScraper.getEntry(cit).ifPresent(dialog::addEntry);
        dialog.setProgress(++i, citations.size());
      }

      return true;

    } catch (IOException e) {
      LOGGER.warn("Communcation problems", e);
      status.showMessage(
          Localization.lang("Error while fetching from %0", SCIENCE_DIRECT)
              + ": "
              + e.getMessage());
    }
    return false;
  }
예제 #2
0
  @Override
  public boolean processQuery(String query, ImportInspector iIDialog, OutputPrinter frameOP) {

    shouldContinue = true;

    query = query.trim().replace(';', ',');

    if (query.matches("\\d+[,\\d+]*")) {
      frameOP.setStatus(Localization.lang("Fetching Medline by id..."));

      List<BibtexEntry> bibs = MedlineImporter.fetchMedline(query, frameOP);

      if (bibs.isEmpty()) {
        frameOP.showMessage(Localization.lang("No references found"));
      }

      for (BibtexEntry entry : bibs) {
        iIDialog.addEntry(entry);
      }
      return true;
    }

    if (!query.isEmpty()) {
      frameOP.setStatus(Localization.lang("Fetching Medline by term..."));

      String searchTerm = toSearchTerm(query);

      // get the ids from entrez
      SearchResult result = getIds(searchTerm, 0, 1);

      if (result.count == 0) {
        frameOP.showMessage(Localization.lang("No references found"));
        return false;
      }

      int numberToFetch = result.count;
      if (numberToFetch > MedlineFetcher.PACING) {

        while (true) {
          String strCount =
              JOptionPane.showInputDialog(
                  Localization.lang("References found")
                      + ": "
                      + numberToFetch
                      + "  "
                      + Localization.lang("Number of references to fetch?"),
                  Integer.toString(numberToFetch));

          if (strCount == null) {
            frameOP.setStatus(Localization.lang("Medline import canceled"));
            return false;
          }

          try {
            numberToFetch = Integer.parseInt(strCount.trim());
            break;
          } catch (RuntimeException ex) {
            frameOP.showMessage(Localization.lang("Please enter a valid number"));
          }
        }
      }

      for (int i = 0; i < numberToFetch; i += MedlineFetcher.PACING) {
        if (!shouldContinue) {
          break;
        }

        int noToFetch = Math.min(MedlineFetcher.PACING, numberToFetch - i);

        // get the ids from entrez
        result = getIds(searchTerm, i, noToFetch);

        List<BibtexEntry> bibs = MedlineImporter.fetchMedline(result.ids, frameOP);
        for (BibtexEntry entry : bibs) {
          iIDialog.addEntry(entry);
        }
        iIDialog.setProgress(i + noToFetch, numberToFetch);
      }
      return true;
    }
    frameOP.showMessage(
        Localization.lang(
            "Please enter a comma separated list of Medline IDs (numbers) or search terms."),
        Localization.lang("Input error"),
        JOptionPane.ERROR_MESSAGE);
    return false;
  }
예제 #3
0
  @Override
  public boolean processQuery(String query, ImportInspector inspector, OutputPrinter status) {
    shouldContinue = true;
    try {
      status.setStatus(Localization.lang("Searching..."));
      HttpResponse<JsonNode> jsonResponse;
      query = URLEncoder.encode(query, "UTF-8");
      jsonResponse =
          Unirest.get(API_URL + query + "&api_key=" + API_KEY + "&p=1")
              .header("accept", "application/json")
              .asJson();
      JSONObject jo = jsonResponse.getBody().getObject();
      int hits = jo.getJSONArray("result").getJSONObject(0).getInt("total");
      int numberToFetch = 0;
      if (hits > 0) {
        if (hits > maxPerPage) {
          while (true) {
            String strCount =
                JOptionPane.showInputDialog(
                    Localization.lang("References found")
                        + ": "
                        + hits
                        + "  "
                        + Localization.lang("Number of references to fetch?"),
                    Integer.toString(hits));

            if (strCount == null) {
              status.setStatus(Localization.lang("Search canceled"));
              return false;
            }

            try {
              numberToFetch = Integer.parseInt(strCount.trim());
              break;
            } catch (RuntimeException ex) {
              status.showMessage(Localization.lang("Please enter a valid number"));
            }
          }
        } else {
          numberToFetch = hits;
        }

        int fetched = 0; // Keep track of number of items fetched for the progress bar
        for (int startItem = 1; startItem <= numberToFetch; startItem += maxPerPage) {
          if (!shouldContinue) {
            break;
          }

          int noToFetch = Math.min(maxPerPage, numberToFetch - startItem);
          jsonResponse =
              Unirest.get(
                      API_URL
                          + query
                          + "&api_key="
                          + API_KEY
                          + "&p="
                          + noToFetch
                          + "&s="
                          + startItem)
                  .header("accept", "application/json")
                  .asJson();
          jo = jsonResponse.getBody().getObject();
          if (jo.has("records")) {
            JSONArray results = jo.getJSONArray("records");
            for (int i = 0; i < results.length(); i++) {
              JSONObject springerJsonEntry = results.getJSONObject(i);
              BibtexEntry entry = jep.SpringerJSONtoBibtex(springerJsonEntry);
              inspector.addEntry(entry);
              fetched++;
              inspector.setProgress(fetched, numberToFetch);
            }
          }
        }
        return true;
      } else {
        status.showMessage(
            Localization.lang("No entries found for the search string '%0'", query),
            Localization.lang("Search %0", "Springer"),
            JOptionPane.INFORMATION_MESSAGE);
        return false;
      }
    } catch (UnirestException e) {
      LOGGER.warn("Problem searching Springer", e);
    } catch (UnsupportedEncodingException e) {
      LOGGER.warn("Cannot encode query", e);
    }
    return false;
  }