Exemplo n.º 1
0
 /**
  * Used as an alternative to {@link #beginNextSubtask()}. Instead of calling {@link
  * #beginNextSubtask()} once after each subtask (except the last), you can instead call
  * beginSubTask at the beginning of every subtask including the first.
  */
 public void beginSubtask() {
   if (numOperations == 0) {
     throw new IllegalStateException("This operation has no subtasks");
   }
   currentSubTaskMessage = "";
   if (!beganFirstSubTask) {
     beganFirstSubTask = true;
   } else {
     beginNextSubtask();
   }
 }
  @Override
  public List<AnnotatedPluginDocument> performOperation(
      AnnotatedPluginDocument[] annotatedDocuments,
      ProgressListener progressListener,
      Options options)
      throws DocumentOperationException {
    int PAGE_SIZE = 2;
    try {
      String query = options.getValueAsString("query");
      int count = 0;
      InputStream countInput = queryServer("query count " + query);
      // Get the response
      BufferedReader rd = new BufferedReader(new InputStreamReader(countInput));
      String line;
      while ((line = rd.readLine()) != null) {
        count = Integer.parseInt(line.trim());
      }
      rd.close();

      int numberOfPages = (int) Math.ceil(count / PAGE_SIZE);

      CompositeProgressListener composite =
          new CompositeProgressListener(progressListener, numberOfPages);

      for (int page = 0; page < numberOfPages; page++) {
        InputStream inputStream =
            queryServer("query page_size " + PAGE_SIZE + " page_number " + page + " text " + query);
        File outputFile =
            new File(
                System.getProperty("user.home")
                    + File.separator
                    + "traceData"
                    + File.separator
                    + "part"
                    + page
                    + ".tgz");
        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile));
        List<String> ids = new ArrayList<String>();
        String line2 = null;
        while ((line2 = in.readLine()) != null) {
          if (composite.isCanceled()) {
            break;
          }
          System.out.println(line2);
          // out.write(n);
          ids.add(line2);
        }
        if (composite.isCanceled()) {
          break;
        }
        in.close();
        InputStream tgzIn = queryServer("retrieve_gz all " + StringUtilities.join(", ", ids));
        int n;
        int byteCount = 0;
        while ((n = tgzIn.read()) >= 0) {
          byteCount++;
          composite.setMessage("Downloaded " + byteCount + " bytes");
          if (composite.isCanceled()) {
            break;
          }
          // out.write(n);
        }
        tgzIn.close();
        out.close();
        if (composite.isCanceled()) {
          break;
        }
        composite.beginNextSubtask();
        break;
        // out.close();
      }

    } catch (IOException ex) {
      ex.toString();
      ex.printStackTrace();
      throw new DocumentOperationException(ex.getMessage(), ex);
    }
    return Collections.emptyList();
  }
Exemplo n.º 3
0
 /**
  * Convenience method to start the next operation AND set a new message.
  *
  * @param message message to set (will be passed to setMessage()
  */
 public void beginNextSubtask(String message) {
   beginNextSubtask();
   currentSubTaskMessage = message;
   setMessage("");
 }