spd-client-library/src/main/java/org/gcube/data/spd/client/proxies/DefaultExecutor.java

188 lines
5.2 KiB
Java

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<WebTarget> delegate;
public DefaultExecutor(ProxyDelegate<WebTarget> delegate) {
super();
this.delegate = delegate;
}
private Call<WebTarget, String> getCallForJobs(final String input, final JobType job){
Call<WebTarget, String> call = new Call<WebTarget, String>() {
@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<WebTarget, String> call = new Call<WebTarget, String>() {
@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<WebTarget, String> call = new Call<WebTarget, String>() {
@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<WebTarget, CompleteJobStatus> call = new Call<WebTarget, CompleteJobStatus>() {
@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<WebTarget, Empty> call = new Call<WebTarget, Empty>() {
@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<String> input)
throws InvalidIdentifierException {
Call<WebTarget, Boolean> call = new Call<WebTarget, Boolean>() {
@Override
public Boolean call(WebTarget executor) throws Exception {
return executor.path("input").path(jobId).request().put(Entity.xml(new SerializableList<String>(input)), Boolean.class);
}
};
try {
return delegate.make(call);
}catch(Exception e) {
throw new InvalidIdentifierException();
}
}
private void sendInput(String jobId, Stream<String> stream) throws Exception{
int bunch = 30;
List<String> collected = new ArrayList<String>(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<String>(0));
}
@Override
public String createDwCAByChildren(String taxonKey) throws Exception {
return delegate.make(getCallForJobs(taxonKey, JobType.DWCAByChildren));
}
@Override
public String createDwCAByIds(Stream<String> 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<String> 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<String> 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<String> ids)
throws Exception {
String jobId = delegate.make(getCallForJobs(null, JobType.DarwinCore));
try{
sendInput(jobId, ids);
}catch(Exception e){
e.printStackTrace();
}
return jobId;
}
}