/*
 * Copyright 2012-2015 Aerospike, Inc.
 *
 * Portions may be licensed to Aerospike, Inc. under one or more contributor
 * license agreements WHICH ARE COMPATIBLE WITH THE APACHE LICENSE, VERSION 2.0.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.aerospike.client.task;

import com.aerospike.client.AerospikeException;
import com.aerospike.client.Info;
import com.aerospike.client.cluster.Cluster;
import com.aerospike.client.cluster.Node;
import com.aerospike.client.policy.Policy;
import com.aerospike.client.query.Statement;

/**
 * Task used to poll for long running server execute job completion.
 */
public final class ExecuteTask extends Task {
	private final long taskId;
	private final boolean scan;

	/**
	 * Initialize task with fields needed to query server nodes.
	 */
	public ExecuteTask(Cluster cluster, Policy policy, Statement statement) {
		super(cluster, policy);
		this.taskId = statement.getTaskId();
		this.scan = statement.isScan();
	}

	/**
	 * Query all nodes for task completion status.
	 */
	@Override
	protected boolean queryIfDone() throws AerospikeException {
		String command = (scan) ? "scan-list" : "query-list";
		Node[] nodes = cluster.getNodes();
		boolean done = false;

		for (Node node : nodes) {
			String response = Info.request(policy, node, command);
			String find = "job_id=" + taskId + ':';
			int index = response.indexOf(find);

			if (index < 0) {
				done = true;
				continue;
			}

			int begin = index + find.length();
			find = "job_status=";
			index = response.indexOf(find, begin);

			if (index < 0) {
				continue;
			}

			begin = index + find.length();
			int end = response.indexOf(':', begin);
			String status = response.substring(begin, end);
			
			if (status.equals("ABORTED")) {
				throw new AerospikeException.QueryTerminated();
			}
			else if (status.equals("IN PROGRESS")) {
				return false;
			}
			else if (status.equals("DONE")) {
				done = true;
			}
		}
		return done;
	}
}

@@ -1612,23 +1612,22 @@
 %09String 
-command
+module
  = (scan
@@ -1635,21 +1635,16 @@
  ? %22scan
--list
 %22 : %22que
@@ -1649,14 +1649,85 @@
 uery
--list%22
+%22;%0A%09%09String command = %22jobs:module=%22 + module + %22;cmd=get-job;trid=%22 + taskId
 ;%0A%09%09
@@ -1879,104 +1879,45 @@
 %0A%09%09%09
-String find = %22job_id=%22 + taskId + ':';%0A%09%09%09int index = response.indexOf(find);%0A%0A%09%09%09if (index %3C 0
+%0A%09%09%09if (response.startsWith(%22ERROR:%22)
 ) %7B%0A
@@ -1956,58 +1956,30 @@
 %09%09%7D%0A
+%09%09%09
 %0A%09%09%09
-int begin = index + find.length();%0A%09%09%09
+String 
 find = %22
 job_
@@ -1978,12 +1978,8 @@
  = %22
-job_
 stat
@@ -1987,16 +1987,20 @@
 s=%22;%0A%09%09%09
+int 
 index = 
@@ -2020,23 +2020,16 @@
 xOf(find
-, begin
 );%0A%0A%09%09%09i
@@ -2067,16 +2067,20 @@
 %09%09%7D%0A%0A%09%09%09
+int 
 begin = 
@@ -2226,116 +2226,14 @@
 ls(%22
-ABORTED%22)) %7B%0A%09%09%09%09throw new AerospikeException.QueryTerminated();%0A%09%09%09%7D%0A%09%09%09else if (status.equals(%22IN PROGRESS
+active
 %22)) 
@@ -2280,20 +2280,24 @@
 tus.
-equals(%22DONE
+startsWith(%22done
 %22)) 

Use jobs info command for ExecuteTask wait.