/*
 * Copyright 2014, Robert Dyer,
 *                 and Bowling Green State University
 *
 * 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 edu.iastate.cs.boa;

/**
 * An example Boa client.  Takes the username and password as first 2 arguments
 * on commandline, then does some simple tasks to show the API works.
 *
 * @author rdyer
 */
public class SampleClient {
	public static void main(final String[] args) throws Exception {
		if (args.length != 2) {
			System.err.println("Error: wrong number of arguments");
			System.err.println("Use: SampleClient <username> <password>");
			System.exit(-1);
		}


		// create a client and log into the remote server
		final BoaClient client = new BoaClient();

		client.login(args[0], args[1]);
		System.out.println("logged in");


		// dump the list of available input datasets
		for (final InputHandle d : client.getDatasets())
			System.out.println(d);


		// how many jobs do they have
		System.out.println("number of jobs: " + client.getJobCount());
		System.out.println("number of public jobs: " + client.getJobCount(true));


		// show the oldest 10 jobs
		for (final JobHandle j : client.getJobList(Math.max(10, client.getJobCount()) - 10, 10))
			System.out.println(j);

		// show the most recently submitted job
		final JobHandle lastJob = client.getLastJob();
		System.out.println("Last job: " + lastJob);
		System.out.println("URL: " + lastJob.getUrl());
		System.out.println("Public URL: " + lastJob.getPublicUrl());
		System.out.println("Public? " + lastJob.getPublic());
		lastJob.setPublic(!lastJob.getPublic());
		System.out.println("Public? " + lastJob.getPublic());
		lastJob.setPublic(!lastJob.getPublic());
		System.out.println("Source:");
		System.out.println("---------------------");
		System.out.println(lastJob.getSource());
		System.out.println("---------------------");
		for (final String err : lastJob.getCompilerErrors())
			System.out.println("Compile error: " + err);
		System.out.println("Output:");
		System.out.println("---------------------");
		System.out.println(lastJob.getOutput());
		System.out.println("---------------------");


		// create a new job by submitting a query and then do things with it
		final JobHandle j = client.query("o: output sum of int;\no << 1;");
		System.out.println("Submitted: " + j);

		j.stop();
		System.out.println("Stopped job: " + j);

		j.delete();
		System.out.println("Deleted job: " + j);


		// when finished, close the connection and log out of the remote server
		client.close();
		System.out.println("logged out");
	}
}

@@ -1779,16 +1779,51 @@
 ted job%0A
+%09%09if (client.getJobCount() %3E 0) %7B%0A%09
 %09%09final 
@@ -1861,24 +1861,25 @@
 astJob();%0A%09%09
+%09
 System.out.p
@@ -1910,16 +1910,17 @@
 stJob);%0A
+%09
 %09%09System
@@ -1955,32 +1955,33 @@
 ob.getUrl());%0A%09%09
+%09
 System.out.print
@@ -2021,24 +2021,25 @@
 blicUrl());%0A
+%09
 %09%09System.out
@@ -2076,32 +2076,33 @@
 getPublic());%0A%09%09
+%09
 lastJob.setPubli
@@ -2118,32 +2118,33 @@
 b.getPublic());%0A
+%09
 %09%09System.out.pri
@@ -2183,16 +2183,17 @@
 lic());%0A
+%09
 %09%09lastJo
@@ -2225,24 +2225,25 @@
 ublic());%0A%09%09
+%09
 System.out.p
@@ -2257,24 +2257,25 @@
 %22Source:%22);%0A
+%09
 %09%09System.out
@@ -2303,32 +2303,33 @@
 ----------%22);%0A%09%09
+%09
 System.out.print
@@ -2349,24 +2349,25 @@
 tSource());%0A
+%09
 %09%09System.out
@@ -2403,16 +2403,17 @@
 --%22);%0A%09%09
+%09
 for (fin
@@ -2452,32 +2452,33 @@
 lerErrors())%0A%09%09%09
+%09
 System.out.print
@@ -2506,16 +2506,17 @@
 + err);%0A
+%09
 %09%09System
@@ -2538,24 +2538,25 @@
 utput:%22);%0A%09%09
+%09
 System.out.p
@@ -2584,24 +2584,25 @@
 --------%22);%0A
+%09
 %09%09System.out
@@ -2626,32 +2626,33 @@
 getOutput());%0A%09%09
+%09
 System.out.print
@@ -2676,24 +2676,28 @@
 --------%22);%0A
+%09%09%7D%0A
 %0A%0A%09%09// creat

fix NPE in sample client