package org.gcube.data.spd.client.proxies; import java.util.ArrayList; import java.util.List; import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; import org.gcube.common.clients.Call; import org.gcube.common.clients.delegates.ProxyDelegate; import org.gcube.common.clients.stubs.jaxws.JAXWSUtils.Empty; import org.gcube.data.spd.model.service.exceptions.InvalidIdentifierException; import org.gcube.data.spd.model.service.types.CompleteJobStatus; import org.gcube.data.spd.model.service.types.JobType; import org.gcube.data.spd.model.service.types.SubmitJob; import org.gcube.data.spd.model.util.SerializableList; import org.gcube.data.streams.Stream; public class DefaultExecutor implements ExecutorClient{ private final ProxyDelegate delegate; public DefaultExecutor(ProxyDelegate delegate) { super(); this.delegate = delegate; } private Call getCallForJobs(final String input, final JobType job){ Call call = new Call() { @Override public String call(WebTarget executor) throws Exception { SubmitJob jobRequest = new SubmitJob(input, job); return executor.path("execute").request().post(Entity.xml(jobRequest), String.class); } }; return call; } @Override public String getErrorLink(final String jobId) throws InvalidIdentifierException { Call call = new Call() { @Override public String call(WebTarget executor) throws Exception { return executor.path("error").path(jobId).request().get(String.class); } }; try { return delegate.make(call); }catch(Exception e) { throw new InvalidIdentifierException(); } } @Override public String getResultLink(final String jobId) throws InvalidIdentifierException { Call call = new Call() { @Override public String call(WebTarget executor) throws Exception { return executor.path("result").path(jobId).request().get(String.class); } }; try { return delegate.make(call); }catch(Exception e) { throw new InvalidIdentifierException(); } } @Override public CompleteJobStatus getStatus(final String jobId) throws InvalidIdentifierException { Call call = new Call() { @Override public CompleteJobStatus call(WebTarget executor) throws Exception { return executor.path("status").path(jobId).request().get(CompleteJobStatus.class); } }; try { return delegate.make(call); }catch(Exception e) { throw new InvalidIdentifierException(); } } @Override public void removeJob(final String jobId) throws InvalidIdentifierException { Call call = new Call() { @Override public Empty call(WebTarget executor) throws Exception { executor.path(jobId).request().delete(); return new Empty(); } }; try { delegate.make(call); }catch(Exception e) { throw new InvalidIdentifierException(); } } private Boolean sendInputCall(final String jobId, final List input) throws InvalidIdentifierException { Call call = new Call() { @Override public Boolean call(WebTarget executor) throws Exception { return executor.path("input").path(jobId).request().put(Entity.xml(new SerializableList(input)), Boolean.class); } }; try { return delegate.make(call); }catch(Exception e) { throw new InvalidIdentifierException(); } } private void sendInput(String jobId, Stream stream) throws Exception{ int bunch = 30; List collected = new ArrayList(30); while (stream.hasNext()){ collected.add(stream.next()); if (collected.size()>=bunch){ if (!sendInputCall(jobId, collected)) throw new Exception(); collected.clear(); } } if (collected.size()>0) if (!sendInputCall(jobId, collected)) throw new Exception(); sendInputCall(jobId, new ArrayList(0)); } @Override public String createDwCAByChildren(String taxonKey) throws Exception { return delegate.make(getCallForJobs(taxonKey, JobType.DWCAByChildren)); } @Override public String createDwCAByIds(Stream ids) throws Exception { String jobId = delegate.make(getCallForJobs(null, JobType.DWCAById)); try{ sendInput(jobId, ids); }catch(Exception e){ e.printStackTrace(); } return jobId; } @Override public String createCSV(Stream ids) throws Exception { String jobId = delegate.make(getCallForJobs(null, JobType.CSV)); try{ sendInput(jobId, ids); }catch(Exception e){ e.printStackTrace(); } return jobId; } @Override public String createCSVforOM(Stream ids) throws Exception { String jobId = delegate.make(getCallForJobs(null, JobType.CSVForOM)); try{ sendInput(jobId, ids); }catch(Exception e){ e.printStackTrace(); } return jobId; } @Override public String createDarwincoreFromOccurrenceKeys(Stream ids) throws Exception { String jobId = delegate.make(getCallForJobs(null, JobType.DarwinCore)); try{ sendInput(jobId, ids); }catch(Exception e){ e.printStackTrace(); } return jobId; } }