Example of using Pipeline API

package com.example.pipelineapi;

import java.io.IOException;
import java.util.List;

import pipeline.api.PipelineAPI;
import pipeline.api.callback.ConnectionCallback;
import pipeline.api.callback.ExecutionCallback;
import pipeline.api.callback.JobStatusCallback;
import pipeline.api.callback.SessionListCallback;
import pipeline.api.callback.SessionReconnectCallback;
import pipeline.api.callback.SessionStatusListener;
import pipeline.api.callback.ValidationCallback;
import pipeline.api.workflow.Connection;
import pipeline.api.workflow.Job;
import pipeline.api.workflow.Module;
import pipeline.api.workflow.Session;
import pipeline.api.workflow.SessionStatus;
import pipeline.api.workflow.Workflow;
import validation.exceptions.ValidationException;

public class ApiExample implements ConnectionCallback, ValidationCallback, ExecutionCallback, 
	JobStatusCallback, SessionReconnectCallback, SessionStatusListener, SessionListCallback {

	private PipelineAPI pipelineApi;
	private Workflow workflow;
	
	public static void main(String[] args) throws InterruptedException, IOException {
		new ApiExample().start();
	}
	
	private void start() throws InterruptedException, IOException {
		String workflowFilePath = "C:\\path\to\workflow.pipe";

		pipelineApi = new PipelineAPI();

		workflow = pipelineApi.loadWorkflowFromFile(workflowFilePath);

		Connection connection = new Connection("username", "password", "10.0.0.1", 8001);

		pipelineApi.connect(connection, this);
	}
	
	@Override
	public void onJobStatus(Job job) {
		System.out.println("onJobStatus [id: "+job.getId() 
		+ ", sessionId: "+job.getSessionId()
		+ ", name: "+job.getModuleName()
		+", status: "+job.getStatus().name()
		+"]");
		
		if (pipelineApi.getConnections().iterator().hasNext()) {
			Connection connection = pipelineApi.getConnections().iterator().next();
			//pipelineApi.diconnect(connection);
		}
	}

	@Override
	public void onSessionCreated(Session session) {
		System.out.println("onSessionCreated = "+session.getId());
		// Example for subscription on session status updates
		Connection connection = pipelineApi.getConnections().iterator().next();
		pipelineApi.requestSessionStatus(session.getId(), connection, this);
	}

	@Override
	public void onValidationCompleted() {
		System.out.println("Validation completed.");
		pipelineApi.executeWorkflow(workflow, this);
	}

	@Override
	public void onValidationFailed(List<ValidationException> validationExceptions) {
		System.out.println("Validation failed. Exceptions count: "+validationExceptions.size());
	}

	@Override
	public void onConnectionEstablished(Connection connection) {
		System.out.println("Connection to " + connection.getAddress() + " established.");
		pipelineApi.validateWorkflow(workflow, this);
		pipelineApi.requestSessionList(connection, this);
	}

	@Override
	public void onConnectionFailed(Connection connection, String message) {
		System.out.println("Connection to " + connection.getAddress() + " failed. Message: "+message);
	}

	@Override
	public void onWorkflowLoaded(String sessionId, Workflow workflow) {
		System.out.println("Reconnect to session = "+sessionId+". Worfklow modules: "+workflow.getModules().size());
		reqeustModuleStatuses(sessionId, workflow);
	}

	@Override
	public void onSessionStatusUpdate(Session session) {
		System.out.println(session);
		
		if (session.getStatus().equals(SessionStatus.COMPLETED)) {
			Connection connection = pipelineApi.getConnections().iterator().next();
			// Example of reconnect to session
			pipelineApi.reconnectSession(session.getId(), connection, this);
		}
	}
	
	private void reqeustModuleStatuses(String sessionId, Workflow workflow) {
		for (Module module : workflow.getModules()) {
			pipelineApi.requestModuleStatus(sessionId, module, this);
		}
	}

	@Override
	public void onSessionList(List<Session> sessions) {
		System.out.println("Session list size = "+sessions.size());
	}
}