Switch to unified view

a/src/net/timbusproject/dpes/alternative/ReasonerClient/SOAP/DependencyReasonerClient.java b/src/net/timbusproject/dpes/alternative/ReasonerClient/SOAP/DependencyReasonerClient.java
...
...
16
 * See the License for the specific language governing permissions and limitation under the License.
16
 * See the License for the specific language governing permissions and limitation under the License.
17
 */
17
 */
18
18
19
package net.timbusproject.dpes.alternative.ReasonerClient.SOAP;
19
package net.timbusproject.dpes.alternative.ReasonerClient.SOAP;
20
20
21
import java.io.IOException;
21
import ch.lambdaj.function.convert.Converter;
22
23
import net.timbusproject.dpes.alternative.ReasonerClient.REST.RequestData;
22
import net.timbusproject.dpes.alternative.ReasonerClient.REST.RequestData;
24
import net.timbusproject.dpes.alternative.ReasonerClient.common.ReasonerClientException;
23
import net.timbusproject.dpes.alternative.ReasonerClient.common.ReasonerClientException;
25
import net.timbusproject.dpes.alternative.ReasonerClient.common.ResponseData;
24
import net.timbusproject.dpes.alternative.ReasonerClient.common.ResponseData;
26
import net.timbusproject.reasoner.Accept;
25
import net.timbusproject.reasoner.Result;
27
import net.timbusproject.reasoner.ServerService;
26
import net.timbusproject.reasoner.ServerService;
27
import net.timbusproject.reasoner.VersionedPackage;
28
import org.slf4j.LoggerFactory;
28
29
29
import org.json.JSONObject;
30
import java.io.IOException;
30
import org.slf4j.LoggerFactory;
31
import java.util.ArrayList;
32
import java.util.List;
33
34
import static ch.lambdaj.Lambda.convert;
31
35
32
public class DependencyReasonerClient {
36
public class DependencyReasonerClient {
33
37
34
    private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(DependencyReasonerClient.class);
38
    private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(DependencyReasonerClient.class);
35
39
36
    public DependencyReasonerClient() {
40
    public DependencyReasonerClient() {
37
    }
41
    }
38
42
39
    /***
43
    /**
40
     * This might take some minutes because of the synchronous invocation of the reasoner.
44
     * This might take some minutes because of the synchronous invocation of the reasoner.
41
     */
45
     */
42
    public ResponseData request(RequestData requestData) throws IOException, ReasonerClientException {
46
    public ResponseData request(RequestData requestData) throws IOException, ReasonerClientException {
43
        LOG.info("Issuing reasoning request...");
47
        LOG.info("Issuing reasoning request...");
44
        String uuid = initiateReasoning(requestData);
48
        String uuid = initiateReasoning(requestData);
45
        LOG.info("Got UUID: " + uuid);
49
        LOG.info("Got UUID: " + uuid);
46
        LOG.info("Waiting for completion...");
50
        LOG.info("Waiting for completion...");
47
        JSONObject response = retrieveResult(uuid);
51
        return retrieveResult(uuid);
48
        return new ResponseData(response);
49
    }
52
    }
50
53
51
    private JSONObject retrieveResult(String uuid) throws IOException, ReasonerClientException {
54
    private ResponseData retrieveResult(String uuid) throws IOException, ReasonerClientException {
52
        ServerService serverService = new ServerService();
55
        ServerService serverService = new ServerService();
53
        JSONObject response = new JSONObject(serverService.getServerPort().requestStatus(uuid, Accept.JSON).getBody());
56
        Result response = serverService.getServerPort().get(uuid);
54
        while (response.getString("status").equalsIgnoreCase("started")) {
57
        while (response == null) {
55
            try {
58
            try {
56
                Thread.sleep(1 * 1000);
59
                Thread.sleep(1 * 1000);
57
            } catch (InterruptedException e) {
60
            } catch (InterruptedException e) {
58
                throw new ReasonerClientException(e);
61
                throw new ReasonerClientException(e);
59
            }
62
            }
60
            response = new JSONObject(serverService.getServerPort().requestStatus(uuid, Accept.JSON).getBody());
63
            response = serverService.getServerPort().get(uuid);
61
        }
64
        }
62
        return response;
65
        if (response.getReason() != null && !response.getReason().isEmpty()) {
66
            throw new ReasonerClientException(response.getReason());
67
        }
68
        return new ResponseData(response);
63
    }
69
    }
64
70
65
    private String initiateReasoning(RequestData requestData) throws IOException, ReasonerClientException {
71
    private String initiateReasoning(RequestData requestData) throws IOException, ReasonerClientException {
66
        ServerService serverService = new ServerService();
72
        ServerService serverService = new ServerService();
67
        JSONObject response = new JSONObject(serverService.getServerPort().reason(requestData.toString(), Accept.JSON).getBody());
73
        return serverService.getServerPort().reason(
68
        String status = response.getString("status");
74
                toPackages(requestData.getToInstall()),
69
        if (!status.equalsIgnoreCase("started")) {
75
                toPackages(requestData.getToRemove()),
70
            throw new ReasonerClientException("Request failed, returned status: " + status);
76
                new ArrayList<VersionedPackage>(),
77
                requestData.getTarget(),
78
                requestData.getDataset()
79
        );
80
    }
81
82
    private List<VersionedPackage> toPackages(List<String> packageList) {
83
        return convert(packageList, new Converter<String, VersionedPackage>() {
84
            @Override
85
            public VersionedPackage convert(String s) {
86
                VersionedPackage pkg = new VersionedPackage();
87
                pkg.setName(s);
88
                return pkg;
89
            }
71
        }
90
        });
72
        return response.getString("uuid");
73
    }
91
    }
74
}
92
}