package at.arz.latte.rodeo.client;

import java.util.List;

import at.arz.latte.rodeo.scm.ScmLocation;
import at.arz.latte.rodeo.scm.ScmName;
import at.arz.latte.rodeo.scm.restapi.ScmResult;
import at.arz.latte.rodeo.workspace.restapi.DirItem;
import at.arz.latte.rodeo.workspace.restapi.DirListResult;

public class WorkspaceClient {

	public static final String PROJECTS_PATH = "api/projects";
	public static final String WORKSPACE_PATH = "api/workspaces";

	private RodeoClient client;
	private ScmClient scmClient;
	private StepClient stepClient;
	private JobClient jobClient;

	public WorkspaceClient(RodeoClient client) {
		this.client = client;
		this.scmClient = new ScmClient(client);
		this.stepClient = new StepClient(client);
		this.jobClient = new JobClient(client);
	}

	public List<DirItem> listWorkspace() {
		return client.begin(WORKSPACE_PATH).get(DirListResult.class).getItems();
	}

	public List<DirItem> listWorkspace(String path) {
		return client.begin(WORKSPACE_PATH + "/" + path).get(DirListResult.class).getItems();
	}

	public void printDirList(List<DirItem> dirList) {
		for (DirItem dirItem : dirList) {
			System.out.println(format(dirItem));
		}
	}

	String format(DirItem dirItem) {
		return String.format("%-20s %2$tY-%2$tm-%2$te %2$tT", dirItem.getName(), dirItem.getLastModified());
	}

	public void remove(String path) {
		client.begin(WORKSPACE_PATH + "/" + path).delete();
	}
	
	public ScmLocation getScmLocation(ScmName scmName) {
		for (ScmResult result : scmClient.listAll()) {
			if (result.getName().equals(scmName)) {
				return result.getLocation();
			}
		}
		throw new RuntimeException("unknown scm repository:" + scmName);
	}

}

@@ -27,24 +27,53 @@
 .client;%0D%0A%0D%0A
+import java.io.InputStream;%0D%0A
 import java.
@@ -825,32 +825,361 @@
 client);%0D%0A%09%7D%0D%0A%0D%0A
+%09public InputStream downloadFile(String path) %7B%0D%0A%09%09return client.begin(WORKSPACE_PATH + %22/%22 + path).get(InputStream.class);%0D%0A%09%7D%0D%0A%0D%0A%09public void updateFile(String path, InputStream file) %7B%0D%0A%09%09try %7B%0D%0A%09%09%09client.begin(WORKSPACE_PATH + %22/%22 + path).post(file);%0D%0A%09%09%7D catch (Exception e) %7B%0D%0A%09%09%09throw new RuntimeException(e);%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%0D%0A
 %09public List%3CDir

add update and download for files