diff --git a/build.sh b/build.sh index ef4f10565705b3eb3d0fae54b60de23671f356bf..6a18d98ecec05885651675049490ca44c990ec7f 100755 --- a/build.sh +++ b/build.sh @@ -26,7 +26,7 @@ else DOCKER="sudo docker" fi -IMAGE="hbpmip/portal-backend" +IMAGE="kfilippopolitis/portal-backend" VCS_REF=$(git describe --tags --dirty) VERSION=$(git describe --tags --dirty) diff --git a/pom.xml b/pom.xml index 20a6f066122779eab771ca0d012dbcc4a8e8a208..87ef337aab0423568234cb76691ab29632ac265d 100644 --- a/pom.xml +++ b/pom.xml @@ -211,13 +211,6 @@ <artifactId>jaxb-runtime</artifactId> <version>2.4.0-b180830.0438</version> </dependency> - - <dependency> - <groupId>com.auth0</groupId> - <artifactId>java-jwt</artifactId> - <version>3.8.3</version> - </dependency> - <!-- https://mvnrepository.com/artifact/com.github.jmchilton.blend4j/blend4j --> <dependency> <groupId>com.github.jmchilton.blend4j</groupId> diff --git a/src/main/java/eu/hbp/mip/controllers/AlgorithmsApi.java b/src/main/java/eu/hbp/mip/controllers/AlgorithmsApi.java index 8310973a308ec810853519552dc48855f494e4d7..fcde558b8c006b4d945a5eed51812d73a5e27891 100644 --- a/src/main/java/eu/hbp/mip/controllers/AlgorithmsApi.java +++ b/src/main/java/eu/hbp/mip/controllers/AlgorithmsApi.java @@ -8,7 +8,7 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import eu.hbp.mip.controllers.galaxy.retrofit.RetroFitGalaxyClients; import eu.hbp.mip.controllers.galaxy.retrofit.RetrofitClientInstance; -import eu.hbp.mip.model.AlgorithmDTO; +import eu.hbp.mip.model.DTOs.AlgorithmDTO; import eu.hbp.mip.model.UserInfo; import eu.hbp.mip.model.galaxy.WorkflowDTO; import eu.hbp.mip.utils.CustomResourceLoader; diff --git a/src/main/java/eu/hbp/mip/controllers/ArticlesApi.java b/src/main/java/eu/hbp/mip/controllers/ArticlesApi.java deleted file mode 100644 index d4eb46e9f3d072255e23be8a37a4e956add16873..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/controllers/ArticlesApi.java +++ /dev/null @@ -1,193 +0,0 @@ -/* - * Created by mirco on 04.12.15. - */ - -package eu.hbp.mip.controllers; - - -import com.github.slugify.Slugify; -import eu.hbp.mip.model.Article; -import eu.hbp.mip.model.User; -import eu.hbp.mip.model.UserInfo; -import eu.hbp.mip.repositories.ArticleRepository; -import eu.hbp.mip.utils.Logging; -import io.swagger.annotations.*; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; - -import javax.validation.Valid; -import java.io.IOException; -import java.util.Date; -import java.util.Iterator; - -import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; - -@RestController -@RequestMapping(value = "/articles", produces = {APPLICATION_JSON_VALUE}) -@Api(value = "/articles", description = "the articles API") -public class ArticlesApi { - - @Autowired - private UserInfo userInfo; - - @Autowired - private ArticleRepository articleRepository; - - @ApiOperation(value = "Get articles", response = Article.class, responseContainer = "List") - @RequestMapping(method = RequestMethod.GET) - public ResponseEntity<Iterable> getArticles( - @ApiParam(value = "Only ask own articles") @RequestParam(value = "own", required = false) Boolean own, - @ApiParam(value = "Only ask results matching status", allowableValues = "draft, published") @RequestParam(value = "status", required = false) String status - ) { - User user = userInfo.getUser(); - Iterable<Article> articles; - Logging.LogUserAction(user.getUsername(), "(GET) /articles", "Loading articles..."); - - if (own != null && own) { - articles = articleRepository.findByCreatedBy(user); - } else { - articles = articleRepository.findByStatusOrCreatedBy("published", user); - } - int articlesSize = 0; - if (status != null) { - for (Iterator<Article> i = articles.iterator(); i.hasNext(); ) { - Article a = i.next(); - if (!status.equals(a.getStatus())) { - i.remove(); - } - articlesSize++; - } - } - Logging.LogUserAction(userInfo.getUser().getUsername(), "(GET) /articles", "Successfully Loaded " + articlesSize + " articles"); - - return ResponseEntity.ok(articles); - } - - - @ApiOperation(value = "Create an article") - @ApiResponses(value = {@ApiResponse(code = 201, message = "Article created")}) - @RequestMapping(method = RequestMethod.POST) - public ResponseEntity<Void> addAnArticle( - @RequestBody @ApiParam(value = "Article to create", required = true) @Valid Article article - ) { - - User user = userInfo.getUser(); - Logging.LogUserAction(user.getUsername(), "(POST) /articles", "Creating article..."); - - article.setCreatedAt(new Date()); - if ("published".equals(article.getStatus())) { - article.setPublishedAt(new Date()); - } - article.setCreatedBy(user); - - long count = 1; - for (int i = 1; count > 0; i++) { - count = articleRepository.countByTitle(article.getTitle()); - - if (count > 0) { - String title = article.getTitle(); - if (i > 1) { - title = title.substring(0, title.length() - 4); - } - article.setTitle(title + " (" + i + ")"); - } - } - - String slug; - try { - slug = new Slugify().slugify(article.getTitle()); - } catch (IOException e) { - slug = ""; - //LOGGER.trace("Cannot slugify title", e); - } - - boolean alreadyExists = true; - for (int i = 1; alreadyExists; i++) { - alreadyExists = articleRepository.exists(slug); - if (alreadyExists) { - if (i > 1) { - slug = slug.substring(0, slug.length() - 2); - } - slug += "-" + i; - } - article.setSlug(slug); - } - articleRepository.save(article); - - Logging.LogUserAction(user.getUsername(), "(POST) /articles", "Successfully created article with id : " + article.getSlug()); - return new ResponseEntity<>(HttpStatus.CREATED); - } - - - @ApiOperation(value = "Get an article", response = Article.class) - @RequestMapping(value = "/{slug}", method = RequestMethod.GET) - public ResponseEntity<Article> getAnArticle( - @ApiParam(value = "slug", required = true) @PathVariable("slug") String slug - ) { - Logging.LogUserAction(userInfo.getUser().getUsername(), "(GET) /articles/{slug}", "Loading article with id : " + slug); - - User user = userInfo.getUser(); - Article article; - article = articleRepository.findOne(slug); - - if (article == null) { - //LOGGER.warn("Cannot find article : " + slug); - Logging.LogUserAction(userInfo.getUser().getUsername(), "(GET) /articles/{slug}", "Article not found"); - return ResponseEntity.badRequest().body(null); - } - - if (!"published".equals(article.getStatus()) && !article.getCreatedBy().getUsername().equals(user.getUsername())) { - return new ResponseEntity<>(HttpStatus.FORBIDDEN); - } - - Logging.LogUserAction(user.getUsername(), "(GET) /articles/{slug}", "Successfully Loaded article with id : " + slug); - - return ResponseEntity.ok(article); - } - - - @ApiOperation(value = "Update an article") - @ApiResponses(value = {@ApiResponse(code = 204, message = "Article updated")}) - @RequestMapping(value = "/{slug}", method = RequestMethod.PUT) - public ResponseEntity<Void> updateAnArticle( - @ApiParam(value = "slug", required = true) @PathVariable("slug") String slug, - @RequestBody @ApiParam(value = "Article to update", required = true) @Valid Article article - ) { - Logging.LogUserAction(userInfo.getUser().getUsername(), "(PUT) /articles/{slug}", "Updating article with id : " + slug); - - User user = userInfo.getUser(); - - String author = articleRepository.findOne(slug).getCreatedBy().getUsername(); - - if (!user.getUsername().equals(author)) { - return new ResponseEntity<>(HttpStatus.FORBIDDEN); - } - - String oldTitle = articleRepository.findOne(slug).getTitle(); - - String newTitle = article.getTitle(); - - if (!newTitle.equals(oldTitle)) { - long count = 1; - for (int i = 1; count > 0 && !newTitle.equals(oldTitle); i++) { - newTitle = article.getTitle(); - count = articleRepository.countByTitle(newTitle); - if (count > 0 && !newTitle.equals(oldTitle)) { - if (i > 1) { - newTitle = newTitle.substring(0, newTitle.length() - 4); - } - article.setTitle(newTitle + " (" + i + ")"); - } - } - } - - articleRepository.save(article); - - Logging.LogUserAction(userInfo.getUser().getUsername(), "(PUT) /articles/{slug}", "Successfully pdated article with id : " + slug); - - return new ResponseEntity<>(HttpStatus.NO_CONTENT); - } - -} diff --git a/src/main/java/eu/hbp/mip/controllers/ExperimentApi.java b/src/main/java/eu/hbp/mip/controllers/ExperimentApi.java index f0515147455a391e409f4dcc63c32da1f54e1502..d28d8aac35cf08c7be49f96e64b1bfb5410e2662 100644 --- a/src/main/java/eu/hbp/mip/controllers/ExperimentApi.java +++ b/src/main/java/eu/hbp/mip/controllers/ExperimentApi.java @@ -1,43 +1,20 @@ package eu.hbp.mip.controllers; -import com.github.jmchilton.blend4j.galaxy.GalaxyInstance; -import com.github.jmchilton.blend4j.galaxy.GalaxyInstanceFactory; -import com.github.jmchilton.blend4j.galaxy.WorkflowsClient; -import com.github.jmchilton.blend4j.galaxy.beans.Workflow; -import com.github.jmchilton.blend4j.galaxy.beans.WorkflowDetails; -import com.github.jmchilton.blend4j.galaxy.beans.WorkflowInputDefinition; -import com.google.common.collect.Lists; -import com.google.gson.*; -import eu.hbp.mip.controllers.galaxy.retrofit.RetroFitGalaxyClients; -import eu.hbp.mip.controllers.galaxy.retrofit.RetrofitClientInstance; -import eu.hbp.mip.model.*; -import eu.hbp.mip.model.ExperimentExecutionDTO.AlgorithmExecutionDTO.AlgorithmExecutionParamDTO; -import eu.hbp.mip.model.galaxy.ErrorResponse; -import eu.hbp.mip.model.galaxy.GalaxyWorkflowResult; -import eu.hbp.mip.model.galaxy.PostWorkflowToGalaxyDtoResponse; -import eu.hbp.mip.repositories.ExperimentRepository; -import eu.hbp.mip.repositories.ModelRepository; -import eu.hbp.mip.utils.ClaimUtils; -import eu.hbp.mip.utils.HTTPUtil; -import eu.hbp.mip.utils.Logging; +import com.google.gson.Gson; +import eu.hbp.mip.model.DTOs.ExperimentDTO; +import eu.hbp.mip.model.UserInfo; +import eu.hbp.mip.services.ExperimentService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; -import org.codehaus.jettison.json.JSONException; -import org.codehaus.jettison.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; -import retrofit2.Call; -import retrofit2.Response; -import java.io.IOException; -import java.util.*; +import java.util.List; -import static java.lang.Thread.sleep; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; /* @@ -50,784 +27,41 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; @Api(value = "/experiments") public class ExperimentApi { - private static final Gson gson = new Gson(); - @Autowired private UserInfo userInfo; - private static final Gson gsonOnlyExposed = new GsonBuilder().serializeNulls() - .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").excludeFieldsWithoutExposeAnnotation().create(); - - @Value("#{'${services.exareme.queryExaremeUrl}'}") - private String queryExaremeUrl; - - @Value("#{'${services.workflows.workflowUrl}'}") - private String workflowUrl; - - @Value("#{'${services.workflows.jwtSecret}'}") - private String jwtSecret; - - @Value("#{'${services.galaxy.galaxyUrl}'}") - private String galaxyUrl; - - @Value("#{'${services.galaxy.galaxyApiKey}'}") - private String galaxyApiKey; - - // Enable HBP collab authentication (1) or disable it (0). Default is 1 - @Value("#{'${hbp.authentication.enabled:1}'}") - private boolean authenticationIsEnabled; - @Autowired - private ModelRepository modelRepository; + private ExperimentService experimentService; - @Autowired - private ExperimentRepository experimentRepository; + @ApiOperation(value = "Get experiments", response = ExperimentDTO.class, responseContainer = "List") + @RequestMapping(method = RequestMethod.GET) + public ResponseEntity<String> getExperiments() { + return experimentService.getExperiments("(GET) /experiments"); + } - @ApiOperation(value = "get an experiment", response = Experiment.class) + @ApiOperation(value = "Get an experiment", response = ExperimentDTO.class) @RequestMapping(value = "/{uuid}", method = RequestMethod.GET) public ResponseEntity<String> getExperiment( @ApiParam(value = "uuid", required = true) @PathVariable("uuid") String uuid) { - - Experiment experiment; - UUID experimentUuid; - User user = userInfo.getUser(); - - Logging.LogUserAction(user.getUsername(), "(GET) /experiments/{uuid}", "Loading Experiment with uuid : " + uuid); - - try { - experimentUuid = UUID.fromString(uuid); - } catch (IllegalArgumentException iae) { - Logging.LogUserAction(user.getUsername(), "(GET) /experiments/{uuid}", "Invalid Experiment UUID."); - return ResponseEntity.badRequest().body("Invalid Experiment UUID"); - } - - experiment = experimentRepository.findOne(experimentUuid); - - if (experiment == null) { - Logging.LogUserAction(user.getUsername(), "(GET) /experiments/{uuid}", "Experiment Not found."); - return new ResponseEntity<>("Not found", HttpStatus.NOT_FOUND); - } - - if (!experiment.isShared() && !experiment.getCreatedBy().getUsername().equals(user.getUsername())) { - Logging.LogUserAction(user.getUsername(), "(GET) /experiments/{uuid}", "Accessing Experiment is unauthorized."); - return new ResponseEntity<>("You don't have access to the experiment.", HttpStatus.UNAUTHORIZED); - } - - Logging.LogUserAction(user.getUsername(), "(GET) /experiments/{uuid}", "Experiment was Loaded with uuid : " + uuid + "."); - - return new ResponseEntity<>(gsonOnlyExposed.toJson(experiment.jsonify()), HttpStatus.OK); - } - - - @ApiOperation(value = "Create an experiment", response = Experiment.class) - @RequestMapping(value = "/runAlgorithm", method = RequestMethod.POST) - public ResponseEntity<String> runExperiment(Authentication authentication, @RequestBody ExperimentExecutionDTO experimentExecutionDTO) { - User user = userInfo.getUser(); - // Get the type and name of algorithm - String algorithmType = experimentExecutionDTO.getAlgorithms().get(0).getType(); - String algorithmName = experimentExecutionDTO.getAlgorithms().get(0).getName(); - - StringBuilder parametersLogMessage = new StringBuilder(", Parameters:\n"); - for (ExperimentExecutionDTO.AlgorithmExecutionDTO.AlgorithmExecutionParamDTO params : experimentExecutionDTO.getAlgorithms().get(0).getParameters()) { - parametersLogMessage - .append(" ") - .append(params.getLabel()) - .append(" -> ") - .append(params.getValue()) - .append("\n"); - } - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "Executing " + algorithmName + parametersLogMessage); - - if (authenticationIsEnabled) { - // Getting the dataset from the experiment parameters - String experimentDatasets = null; - for (ExperimentExecutionDTO.AlgorithmExecutionDTO.AlgorithmExecutionParamDTO parameter : experimentExecutionDTO.getAlgorithms().get(0).getParameters()) { - if (parameter.getLabel().equals("dataset")) { - experimentDatasets = parameter.getValue(); - break; - } - } - - if (experimentDatasets == null || experimentDatasets.equals("")) { - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", - "A dataset should be specified to run an algorithm."); - return ResponseEntity.badRequest().body("Please provide at least one dataset to run the algorithm."); - } - - // --- Validating proper access rights on the datasets --- - if (!ClaimUtils.userHasDatasetsAuthorization(user.getUsername(), authentication.getAuthorities(), experimentDatasets)) { - return ResponseEntity.badRequest().body("You are not authorized to use these datasets."); - } - } - - // Run with the appropriate engine - if (algorithmType.equals("workflow")) { - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "Algorithm runs on Galaxy."); - return runGalaxyWorkflow(experimentExecutionDTO); - } else { - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "Algorithm runs on Exareme."); - return runExaremeAlgorithm(experimentExecutionDTO); - } + return experimentService.getExperiment(uuid, "(GET) /experiments/{uuid}"); } - @ApiOperation(value = "Mark an experiment as viewed", response = Experiment.class) - @RequestMapping(value = "/{uuid}/markAsViewed", method = RequestMethod.GET) - public ResponseEntity<String> markExperimentAsViewed( - @ApiParam(value = "uuid", required = true) @PathVariable("uuid") String uuid) { - - Experiment experiment; - UUID experimentUuid; - User user = userInfo.getUser(); - - Logging.LogUserAction(user.getUsername(), "(GET) /experiments/{uuid}/markAsViewed", "Marking as viewed the experiment with uuid : " + uuid + "."); - - try { - experimentUuid = UUID.fromString(uuid); - } catch (IllegalArgumentException iae) { - Logging.LogUserAction(user.getUsername(), "(GET) /experiments/{uuid}/markAsViewed", "Invalid Experiment UUID" + uuid + "."); - return ResponseEntity.badRequest().body("Invalid Experiment UUID"); - } - - experiment = experimentRepository.findOne(experimentUuid); - if (!experiment.getCreatedBy().getUsername().equals(user.getUsername())) { - Logging.LogUserAction(user.getUsername(), "(GET) /experiments/{uuid}/markAsViewed", "You're not the owner of this experiment"); - return new ResponseEntity<>("You're not the owner of this experiment", HttpStatus.UNAUTHORIZED); - } - experiment.setResultsViewed(true); - experimentRepository.save(experiment); - - Logging.LogUserAction(user.getUsername(), "(GET) /experiments/{uuid}/markAsViewed", "Experiment with uuid: " + uuid + " was marked as viewed."); - - return new ResponseEntity<>(gsonOnlyExposed.toJson(experiment.jsonify()), HttpStatus.OK); + @ApiOperation(value = "Create an experiment", response = ExperimentDTO.class) + @RequestMapping(method = RequestMethod.POST) + public ResponseEntity<String> createExperiment(Authentication authentication, @RequestBody ExperimentDTO experimentDTO) { + return experimentService.createExperiment(authentication, experimentDTO, "(POST) /experiments"); } - @ApiOperation(value = "Mark an experiment as shared", response = Experiment.class) - @RequestMapping(value = "/{uuid}/markAsShared", method = RequestMethod.GET) - public ResponseEntity<String> markExperimentAsShared( - @ApiParam(value = "uuid", required = true) @PathVariable("uuid") String uuid) { - - Logging.LogUserAction(userInfo.getUser().getUsername(), "(GET) /experiments/{uuid}/markAsShared", "Marking as shared the experiment with uuid : " + uuid + "."); - - return doMarkExperimentAsShared(uuid, true); + @ApiOperation(value = "Update an experiment", response = ExperimentDTO.class) + @RequestMapping(value = "/{uuid}", method = RequestMethod.PATCH) + public ResponseEntity<String> updateExperiment(@RequestBody ExperimentDTO experimentDTO,@ApiParam(value = "uuid", required = true) @PathVariable("uuid") String uuid) { + return experimentService.updateExperiment(uuid, experimentDTO, "(PATCH) /experiments/{uuid}"); } - @ApiOperation(value = "Mark an experiment as unshared", response = Experiment.class) - @RequestMapping(value = "/{uuid}/markAsUnshared", method = RequestMethod.GET) - public ResponseEntity<String> markExperimentAsUnshared( + @ApiOperation(value = "Delete an experiment", response = boolean.class) + @RequestMapping(value = "/{uuid}", method = RequestMethod.DELETE) + public ResponseEntity<String> deleteExperiment( @ApiParam(value = "uuid", required = true) @PathVariable("uuid") String uuid) { - Logging.LogUserAction(userInfo.getUser().getUsername(), "(GET) /experiments/{uuid}/markAs/Unshared", "Marking as unshared the experiment with uuid : " + uuid + "."); - - return doMarkExperimentAsShared(uuid, false); - } - - @ApiOperation(value = "list experiments", response = Experiment.class, responseContainer = "List") - @RequestMapping(method = RequestMethod.GET, params = {"maxResultCount"}) - public ResponseEntity<String> listExperiments( - @ApiParam(value = "maxResultCount") @RequestParam int maxResultCount) { - User user = userInfo.getUser(); - Logging.LogUserAction(user.getUsername(), "(GET) /experiments/{maxResultCount}", "Listing experiments with a maximum amount of : " + maxResultCount + "."); - - List<Experiment> expList = doListExperiments(false, null); - Logging.LogUserAction(user.getUsername(), "(GET) /experiments/{maxResultCount}", "Successfully listed experiments."); - - return new ResponseEntity<>(gsonOnlyExposed.toJson(expList), HttpStatus.OK); - } - - @ApiOperation(value = "list experiments", response = Experiment.class, responseContainer = "List") - @RequestMapping(method = RequestMethod.GET, params = {"slug", "maxResultCount"}) - public ResponseEntity<String> listExperiments(@ApiParam(value = "slug") @RequestParam("slug") String modelSlug, - @ApiParam(value = "maxResultCount") @RequestParam("maxResultCount") int maxResultCount) { - User user = userInfo.getUser(); - Logging.LogUserAction(user.getUsername(), "(GET) /experiments/{slug}/{maxResultCount}", "Listing experiments with a maximum amount of :" + maxResultCount + "with modelSlug : " + modelSlug + "."); - - if (maxResultCount <= 0 || modelSlug == null || "".equals(modelSlug)) { - return new ResponseEntity<>("You must provide at least a slug or a limit of result", - HttpStatus.BAD_REQUEST); - } - - List<Experiment> expList = doListExperiments(false, null); - Logging.LogUserAction(user.getUsername(), "(GET) /experiments/{slug}/{maxResultCount}", "Successfully listed my experiments."); - return new ResponseEntity<>(gsonOnlyExposed.toJson(expList), HttpStatus.OK); - } - - @ApiOperation(value = "list my experiments", response = Experiment.class, responseContainer = "List") - @RequestMapping(method = RequestMethod.GET, params = {"mine"}) - public ResponseEntity<String> listMyExperiments(Authentication authentication, @ApiParam(value = "mine") @RequestParam("mine") boolean mine) { - User user = userInfo.getUser(); - Logging.LogUserAction(user.getUsername(), "(GET) /experiments/{mine}", "Listing my experiments."); - List<Experiment> expList = doListExperiments(true, null); - Logging.LogUserAction(user.getUsername(), "(GET) /experiments/{mine}", "Successfully listed my experiments."); - return new ResponseEntity<>(gsonOnlyExposed.toJson(expList), HttpStatus.OK); - } - - private List<Experiment> doListExperiments(boolean mine, String modelSlug) { - User user = userInfo.getUser(); - - Iterable<Experiment> myExperiments = experimentRepository.findByCreatedBy(user); - List<Experiment> expList = Lists.newLinkedList(myExperiments); - if (!mine) { - Iterable<Experiment> sharedExperiments = experimentRepository.findByShared(true); - List<Experiment> sharedExpList = Lists.newLinkedList(sharedExperiments); - expList.addAll(sharedExpList); - } - - if (modelSlug != null && !"".equals(modelSlug)) { - for (Iterator<Experiment> it = expList.iterator(); it.hasNext(); ) { - Experiment e = it.next(); - e.setResult(null); - e.setAlgorithms(null); - e.setValidations(null); - if (!e.getModel().getSlug().equals(modelSlug)) { - it.remove(); - } - } - } - return expList; - } - - private ResponseEntity<String> doMarkExperimentAsShared(String uuid, boolean shared) { - Experiment experiment; - UUID experimentUuid; - User user = userInfo.getUser(); - - try { - experimentUuid = UUID.fromString(uuid); - } catch (IllegalArgumentException iae) { - //LOGGER.trace("Invalid UUID", iae); - //LOGGER.warn("An invalid Experiment UUID was received !"); - Logging.LogUserAction(user.getUsername(), "List my experiments", "Listing my experiments."); - return ResponseEntity.badRequest().body("Invalid Experiment UUID"); - } - - experiment = experimentRepository.findOne(experimentUuid); - - if (!experiment.getCreatedBy().getUsername().equals(user.getUsername())) - return new ResponseEntity<>("You're not the owner of this experiment", HttpStatus.UNAUTHORIZED); - - experiment.setShared(shared); - experimentRepository.save(experiment); - - Logging.LogUserAction(user.getUsername(), "Experiment updated (marked as shared)", ""); - - return new ResponseEntity<>(gsonOnlyExposed.toJson(experiment.jsonify()), HttpStatus.OK); - } - - /* ------------------------------- EXPERIMENT MODEL METHODS ----------------------------------------------------*/ - - public Experiment createExperiment(ExperimentExecutionDTO experimentExecutionDTO) { - User user = userInfo.getUser(); - - Experiment experiment = new Experiment(); - experiment.setUuid(UUID.randomUUID()); - experiment.setCreatedBy(user); - experiment.setAlgorithms(gson.toJson(experimentExecutionDTO.getAlgorithms())); - Model model = modelRepository.findOne(experimentExecutionDTO.getModel()); - experiment.setModel(model); - experiment.setName(experimentExecutionDTO.getName()); - experimentRepository.save(experiment); - - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", " id : " + experiment.getUuid()); - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", " algorithms : " + experiment.getAlgorithms()); - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", " model : " + experiment.getModel().getSlug()); - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", " name : " + experiment.getName()); - return experiment; - } - - private void saveExperiment(Experiment experiment) { - User user = userInfo.getUser(); - - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", " id : " + experiment.getUuid()); - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", " algorithms : " + experiment.getAlgorithms()); - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", " model : " + experiment.getModel().getSlug()); - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", " name : " + experiment.getName()); - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", " historyId : " + experiment.getWorkflowHistoryId()); - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", " status : " + experiment.getWorkflowStatus()); - - experimentRepository.save(experiment); - - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "Saved experiment"); - } - - private void finishExperiment(Experiment experiment) { - experiment.setFinished(new Date()); - experimentRepository.save(experiment); - } - - /* -------------------------------------- EXAREME CALLS ---------------------------------------------------------*/ - - /** - * The runExaremeExperiment will POST the algorithm to the exareme client - * - * @param experimentExecutionDTO is the request with the experiment information - * @return the response to be returned - */ - public ResponseEntity<String> runExaremeAlgorithm(ExperimentExecutionDTO experimentExecutionDTO) { - User user = userInfo.getUser(); - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "Running the algorithm..."); - - Experiment experiment = createExperiment(experimentExecutionDTO); - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "Created experiment with uuid :" + experiment.getUuid()); - - // Run the 1st algorithm from the list - String algorithmName = experimentExecutionDTO.getAlgorithms().get(0).getName(); - - // Get the parameters - List<ExperimentExecutionDTO.AlgorithmExecutionDTO.AlgorithmExecutionParamDTO> algorithmParameters - = experimentExecutionDTO.getAlgorithms().get(0).getParameters(); - - String body = gson.toJson(algorithmParameters); - String url = queryExaremeUrl + "/" + algorithmName; - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "url: " + url + ", body: " + body); - - ResponseEntity<String> response = new ResponseEntity<>(gsonOnlyExposed.toJson(experiment.jsonify()), HttpStatus.OK); - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", - "Completed, returning: " + experiment.toString()); - - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", - "Starting exareme execution thread"); - new Thread(() -> { - // ATTENTION: Inside the Thread only LogExperimentAction should be used, not LogUserAction! - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "Thread named :" + Thread.currentThread().getName() + " with id :" + Thread.currentThread().getId() + " started!"); - - try { - StringBuilder results = new StringBuilder(); - int code = HTTPUtil.sendPost(url, body, results); - - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "Algorithm finished with code: " + code); - - // Results are stored in the experiment object - experiment.setResult("[" + results.toString() + "]"); - experiment.setHasError(code >= 400); - experiment.setHasServerError(code >= 500); - } catch (Exception e) { - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "There was an exception: " + e.getMessage()); - - experiment.setHasError(true); - experiment.setHasServerError(true); - experiment.setResult(e.getMessage()); - } - - finishExperiment(experiment); - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "Finished the experiment: " + experiment.toString()); - }).start(); - - return response; - } - - /* --------------------------------------- GALAXY CALLS ---------------------------------------------------------*/ - - - /** - * The runWorkflow will POST the algorithm to the galaxy client - * - * @param experimentExecutionDTO is the request with the experiment information - * @return the response to be returned - */ - public ResponseEntity<String> runGalaxyWorkflow(ExperimentExecutionDTO experimentExecutionDTO) { - User user = userInfo.getUser(); - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "Running a workflow..."); - - Experiment experiment = createExperiment(experimentExecutionDTO); - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "Created experiment with uuid :" + experiment.getUuid()); - - - // Run the 1st algorithm from the list - String workflowId = experimentExecutionDTO.getAlgorithms().get(0).getName(); - - // Get the parameters - List<AlgorithmExecutionParamDTO> algorithmParameters - = experimentExecutionDTO.getAlgorithms().get(0).getParameters(); - - // Convert the parameters to workflow parameters - HashMap<String, String> algorithmParamsIncludingEmpty = new HashMap<>(); - if (algorithmParameters != null) { - for (AlgorithmExecutionParamDTO param : algorithmParameters) { - algorithmParamsIncludingEmpty.put(param.getName(), param.getValue()); - } - } - - // Get all the algorithm parameters because the frontend provides only the non-null - final GalaxyInstance instance = GalaxyInstanceFactory.get(galaxyUrl, galaxyApiKey); - final WorkflowsClient workflowsClient = instance.getWorkflowsClient(); - Workflow workflow = null; - for (Workflow curWorkflow : workflowsClient.getWorkflows()) { - if (curWorkflow.getId().equals(workflowId)) { - workflow = curWorkflow; - break; - } - } - if (workflow == null) { - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", - "Could not find algorithm code: " + workflowId); - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body(new ErrorResponse("Could not find galaxy algorithm.").toString()); - } - final WorkflowDetails workflowDetails = workflowsClient.showWorkflow(workflow.getId()); - for (Map.Entry<String, WorkflowInputDefinition> workflowParameter : workflowDetails.getInputs().entrySet()) { - if (!(algorithmParamsIncludingEmpty.containsKey(workflowParameter.getValue().getUuid()))) { - algorithmParamsIncludingEmpty.put(workflowParameter.getValue().getUuid(), ""); - } - } - - // Create the body of the request - HashMap<String, HashMap<String, String>> requestBody = new HashMap<>(); - requestBody.put("inputs", algorithmParamsIncludingEmpty); - JsonObject requestBodyJson = new JsonParser().parse(gson.toJson(requestBody)).getAsJsonObject(); - - // Create the request client - RetroFitGalaxyClients service = RetrofitClientInstance.getRetrofitInstance().create(RetroFitGalaxyClients.class); - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "Running Galaxy workflow with id: " + workflow.getId()); - - // Call Galaxy to run the workflow - Call<PostWorkflowToGalaxyDtoResponse> call = service.postWorkflowToGalaxy(workflow.getId(), galaxyApiKey, requestBodyJson); - try { - Response<PostWorkflowToGalaxyDtoResponse> response = call.execute(); - - if (response.code() == 200) { // Call succeeded - String responseBody = gson.toJson(response.body()); - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "Response: " + responseBody); - - String historyId = (String) new JSONObject(responseBody).get("history_id"); - experiment.setWorkflowHistoryId(historyId); - experiment.setWorkflowStatus("running"); - experiment.setHasError(false); - experiment.setHasServerError(response.code() >= 500); - - } else { // Something unexpected happened - String msgErr = gson.toJson(response.errorBody()); - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "Error Response: " + msgErr); - - // Values are read from streams. - JSONObject jObjectError = new JSONObject(msgErr); - String errMsg = jObjectError.get("err_msg").toString(); - - experiment.setResult("[" + new ErrorResponse(errMsg).toString() + "]"); - experiment.setHasError(response.code() >= 400); - experiment.setHasServerError(response.code() >= 500); - } - - } catch (Exception e) { - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "An exception occurred: " + e.getMessage()); - experiment.setHasError(true); - experiment.setHasServerError(true); - experiment.setResult(e.getMessage()); - } - saveExperiment(experiment); - - // Start the process of fetching the status - updateWorkflowExperiment(experiment); - - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "Run workflow completed!"); - - return new ResponseEntity(gsonOnlyExposed.toJson(experiment.jsonify()), HttpStatus.OK); - } - - - /** - * This method creates a thread that will fetch the workflow result when it is ready - * - * @param experiment will be used to fetch it's workflow status, it should have the workflowHistoryId initialized - * and the result should not already be fetched - * @return nothing, just updates the experiment - */ - public void updateWorkflowExperiment(Experiment experiment) { - User user = userInfo.getUser(); - - if (experiment == null) { - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "The experiment does not exist."); - return; - } - - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", - " Experiment id : " + experiment.getUuid()); - if (experiment.getWorkflowHistoryId() == null) { - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "History Id does not exist."); - return; - } - - Logging.LogUserAction(user.getUsername(), "(POST) /experiments/runAlgorithm", "Starting Thread..."); - new Thread(() -> { - while (true) { - // ATTENTION: Inside the Thread only LogExperimentAction should be used, not LogExperimentAction! - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "Thread is running..."); - - try { - sleep(2000); - } catch (InterruptedException e) { - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "Sleep was disrupted: " + e.getMessage()); - } - - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "Fetching status for experiment Id: " + experiment.getUuid()); - - String state = getWorkflowStatus(experiment); - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "State is: " + state); - - switch (state) { - case "running": - // Do nothing, when the experiment is created the status is set to running - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "Workflow is still running."); - break; - - case "completed": - // Get only the job result that is visible - List<GalaxyWorkflowResult> workflowJobsResults = getWorkflowResults(experiment); - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "Results are: " + workflowJobsResults.toString()); - - boolean resultFound = false; - for (GalaxyWorkflowResult jobResult : workflowJobsResults) { - if (jobResult.getVisible()) { - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "Visible result are: " + jobResult.getId()); - - String result = getWorkflowResultBody(experiment, jobResult.getId()); - - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "Result: " + result); - if (result == null) { - experiment.setHasError(true); - experiment.setHasServerError(true); - } - experiment.setResult("[" + result + "]"); - experiment.setWorkflowStatus("completed"); - resultFound = true; - } - } - - if (!resultFound) { // If there is no visible result - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "No visible result"); - experiment.setResult("[" + new ErrorResponse("The workflow has no visible result.").toString() + "]"); - experiment.setHasError(true); - experiment.setHasServerError(true); - } - - finishExperiment(experiment); - break; - - case "error": - // Get the job result that failed - workflowJobsResults = getWorkflowResults(experiment); - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "Error results are: " + workflowJobsResults.toString()); - - boolean failedJobFound = false; - for (GalaxyWorkflowResult jobResult : workflowJobsResults) { - if (jobResult.getState().equals("error")) { - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "Failed job is: " + jobResult.getId()); - - String result = getWorkflowJobError(jobResult.getId(), experiment); - - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "Job result: " + result); - if (result == null) { - experiment.setHasError(true); - experiment.setHasServerError(true); - } - experiment.setResult("[" + result + "]"); - experiment.setWorkflowStatus("error"); - failedJobFound = true; - } - } - - if (!failedJobFound) { // If there is no visible failed job - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "No failed result"); - experiment.setResult("[" + new ErrorResponse("The workflow has no failed result.").toString() + "]"); - experiment.setHasError(true); - experiment.setHasServerError(true); - } - finishExperiment(experiment); - break; - - default: // InternalError or unexpected result - experiment.setResult("[" + new ErrorResponse("An unexpected error occurred.").toString() + "]"); - experiment.setHasError(true); - experiment.setHasServerError(true); - finishExperiment(experiment); - break; - } - - // If result exists return - if (experiment.getResult() != null) { - Logging.LogExperimentAction(experiment.getName(), experiment.getUuid(), "Result exists: " + experiment.getResult()); - return; - } - } - }).start(); - } - - - /** - * @param experiment The experiment of the workflow - * @return "running" -> When the workflow is still running - * "internalError" -> When an exception or a bad request occurred - * "error" -> When the workflow produced an error - * "completed" -> When the workflow completed successfully - */ - public String getWorkflowStatus(Experiment experiment) { - String historyId = experiment.getWorkflowHistoryId(); - String experimentName = experiment.getName(); - UUID experimentId = experiment.getUuid(); - - // ATTENTION: This function is used from a Thread. Only LogExperimentAction should be used, not LogUserAction! - Logging.LogExperimentAction(experimentName, experimentId, " History Id : " + historyId); - - // Create the request client - RetroFitGalaxyClients service = RetrofitClientInstance.getRetrofitInstance().create(RetroFitGalaxyClients.class); - Call<Object> call = service.getWorkflowStatusFromGalaxy(historyId, galaxyApiKey); - - String result = null; - try { - Response<Object> response = call.execute(); - if (response.code() >= 400) { - Logging.LogExperimentAction(experimentName, experimentId, " Response code: " - + response.code() + "" + " with body: " + (response.errorBody() != null ? response.errorBody().string() : " ")); - return "internalError"; - } - result = new Gson().toJson(response.body()); - Logging.LogExperimentAction(experimentName, experimentId, " Result: " + result); - - } catch (IOException e) { - Logging.LogExperimentAction(experimentName, experimentId, " An exception happened: " + e.getMessage()); - return "internalError"; - } - - String state = null; - try { - JSONObject resultJson = new JSONObject(result); - state = resultJson.getString("state"); - } catch (JSONException e) { - Logging.LogExperimentAction(experimentName, experimentId, " An exception happened: " + e.getMessage()); - return "internalError"; - } - - Logging.LogExperimentAction(experimentName, experimentId, " Completed!"); - switch (state) { - case "ok": - return "completed"; - case "error": - return "error"; - case "running": - case "new": - case "waiting": - case "queued": - return "running"; - default: - return "internalError"; - } - } - - /** - * @param experiment The experiment of the workflow - * @return a List<GalaxyWorkflowResult> or null when an error occurred - */ - public List<GalaxyWorkflowResult> getWorkflowResults(Experiment experiment) { - - String historyId = experiment.getWorkflowHistoryId(); - String experimentName = experiment.getName(); - UUID experimentId = experiment.getUuid(); - Logging.LogExperimentAction(experimentName, experimentId, " historyId : " + historyId); - - RetroFitGalaxyClients service = RetrofitClientInstance.getRetrofitInstance().create(RetroFitGalaxyClients.class); - Call<List<GalaxyWorkflowResult>> call = service.getWorkflowResultsFromGalaxy(historyId, galaxyApiKey); - - List<GalaxyWorkflowResult> getGalaxyWorkflowResultList = null; - try { - Response<List<GalaxyWorkflowResult>> response = call.execute(); - if (response.code() >= 400) { - Logging.LogExperimentAction(experimentName, experimentId, " Response code: " - + response.code() + "" + " with body: " + (response.errorBody() != null ? response.errorBody().string() : " ")); - return null; - } - getGalaxyWorkflowResultList = response.body(); - Logging.LogExperimentAction(experimentName, experimentId, " Result: " + response.body()); - - } catch (IOException e) { - Logging.LogExperimentAction(experimentName, experimentId, " An exception happened: " + e.getMessage()); - return null; - } - - Logging.LogExperimentAction(experimentName, experimentId, " Completed!"); - return getGalaxyWorkflowResultList; - + return experimentService.deleteExperiment(uuid, "(DELETE) /experiments/{uuid}"); } - - /** - * @param experiment The experiment of the workflow - * @param contentId the id of the job result that we want - * @return the result of the specific workflow job, null if there was an error - */ - public String getWorkflowResultBody(Experiment experiment, String contentId) { - - String historyId = experiment.getWorkflowHistoryId(); - String experimentName = experiment.getName(); - UUID experimentId = experiment.getUuid(); - - Logging.LogExperimentAction(experimentName, experimentId, " historyId : " + historyId); - - RetroFitGalaxyClients service = RetrofitClientInstance.getRetrofitInstance().create(RetroFitGalaxyClients.class); - Call<Object> call = - service.getWorkflowResultsBodyFromGalaxy(historyId, contentId, galaxyApiKey); - - String resultJson = null; - try { - Response<Object> response = call.execute(); - if (response.code() >= 400) { - Logging.LogExperimentAction(experimentName, experimentId, " Response code: " - + response.code() + "" + " with body: " + (response.errorBody() != null ? response.errorBody().string() : " ")); - return null; - } - resultJson = new Gson().toJson(response.body()); - Logging.LogExperimentAction(experimentName, experimentId, " Result: " + resultJson); - - } catch (IOException e) { - Logging.LogExperimentAction(experimentName, experimentId, - " An exception happened: " + e.getMessage()); - return null; - } - - Logging.LogExperimentAction(experimentName, experimentId, " Completed!"); - return resultJson; - } - - - /** - * @param jobId the id of the workflow job that failed - * @return the error that was produced or null if an error occurred - */ - public String getWorkflowJobError(String jobId, Experiment experiment) { - String experimentName = experiment.getName(); - UUID experimentId = experiment.getUuid(); - - Logging.LogExperimentAction(experimentName, experimentId, " jobId : " + jobId); - RetroFitGalaxyClients service = RetrofitClientInstance.getRetrofitInstance().create(RetroFitGalaxyClients.class); - Call<Object> callError = service.getErrorMessageOfWorkflowFromGalaxy(jobId, galaxyApiKey); - - String fullError = null; - String returnError = null; - try { - Response<Object> response = callError.execute(); - if (response.code() >= 400) { - Logging.LogExperimentAction(experimentName, experimentId, "Response code: " - + response.code() + " with body: " + (response.errorBody() != null ? response.errorBody().string() : " ")); - return null; - } - - // Parsing the stderr of the job that failed - String jsonString = new Gson().toJson(response.body()); - JsonElement jsonElement = new JsonParser().parse(jsonString); - JsonObject rootObject = jsonElement.getAsJsonObject(); - fullError = rootObject.get("stderr").getAsString(); - Logging.LogExperimentAction(experimentName, experimentId, "Error: " + fullError); - - String[] arrOfStr = fullError.split("ValueError", 0); - String specError = arrOfStr[arrOfStr.length - 1]; - returnError = specError.substring(1); - Logging.LogExperimentAction(experimentName, experimentId, "Parsed Error: " + returnError); - - } catch (IOException e) { - Logging.LogExperimentAction(experimentName, experimentId, "Exception: " + e.getMessage()); - return null; - } - - Logging.LogExperimentAction(experimentName, experimentId, "Completed successfully!"); - - return returnError; - } - - -} +} \ No newline at end of file diff --git a/src/main/java/eu/hbp/mip/controllers/FilesAPI.java b/src/main/java/eu/hbp/mip/controllers/FilesAPI.java deleted file mode 100644 index 69e7f3ba94702ee9b5465920d3234ca4dcac0248..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/controllers/FilesAPI.java +++ /dev/null @@ -1,47 +0,0 @@ -package eu.hbp.mip.controllers; - -import eu.hbp.mip.model.User; -import eu.hbp.mip.model.UserInfo; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; -import eu.hbp.mip.utils.Logging; - -/** - * Created by mirco on 08.05.17. - */ - -@RestController -@RequestMapping(value = "/protected") -@Api(value = "/protected", description = "the protected files API") -public class FilesAPI { - - - @Autowired - private UserInfo userInfo; - - @ApiOperation(value = "Get protected files") - @RequestMapping(value = "/{filename:.+}", method = RequestMethod.GET) - public ResponseEntity<Void> getProtectedFile( - @ApiParam(value = "filename", required = true) @PathVariable("filename") String filename - ) { - User user = userInfo.getUser(); - Logging.LogUserAction(user.getUsername(), "(GET) /protected/{filename:.+}", "Loading protected file with filename : " + filename); - - String filepath = "/protected/" + filename; - Logging.LogUserAction(user.getUsername(), "(GET) /protected/{filename:.+}" + filepath, "Downloaded protected file"); - - HttpHeaders headers = new HttpHeaders(); - headers.add("X-Accel-Redirect", filepath); - - return new ResponseEntity<>(headers, HttpStatus.OK); - } -} diff --git a/src/main/java/eu/hbp/mip/controllers/MiningApi.java b/src/main/java/eu/hbp/mip/controllers/MiningApi.java deleted file mode 100644 index 6a198cc0d4571c58b1ff424e590bc418893a39f1..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/controllers/MiningApi.java +++ /dev/null @@ -1,110 +0,0 @@ -package eu.hbp.mip.controllers; - -import eu.hbp.mip.utils.HTTPUtil; - -import com.google.gson.Gson; - -import eu.hbp.mip.model.UserInfo; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -import eu.hbp.mip.utils.Logging; - - -import java.util.*; -import java.io.IOException; - -import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; - -/** - * Created by mirco on 06.01.17. - */ -@RestController -@RequestMapping(value = "/mining", produces = {APPLICATION_JSON_VALUE}) -@Api(value = "/mining", description = "the mining API") -public class MiningApi { - - private static final Gson gson = new Gson(); - - @Autowired - private UserInfo userInfo; - - @Value("#{'${services.exareme.queryExaremeUrl:http://localhost:9090/mining/query}'}") - public String queryExaremeUrl; - - @ApiOperation(value = "Create a histogram on Exareme", response = String.class) - @RequestMapping(value = "/histograms", method = RequestMethod.POST) - public ResponseEntity runExaremeHistograms(@RequestBody List<HashMap<String, String>> queryList) { - Logging.LogUserAction(userInfo.getUser().getUsername(), "(POST) /mining/histogram", "Executing histogram..."); - - String query = gson.toJson(queryList); - String url = queryExaremeUrl + "/" + "MULTIPLE_HISTOGRAMS"; - - try { - StringBuilder results = new StringBuilder(); - int code = HTTPUtil.sendPost(url, query, results); - - Logging.LogUserAction(userInfo.getUser().getUsername(), "(POST) /mining/histogram", "Executed histogram with result :" + results.toString()); - return ResponseEntity.ok(gson.toJson(results.toString())); - } catch (IOException e) { - Logging.LogUserAction(userInfo.getUser().getUsername(), "(POST) /mining/histogram", "Histogram algorithm was not found"); - return new ResponseEntity<>("Not found", HttpStatus.NOT_FOUND); - } - } - - @ApiOperation(value = "Create a descriptive statistic on Exareme", response = String.class) - @RequestMapping(value = "/descriptive_stats", method = RequestMethod.POST) - public ResponseEntity runExaremeDescriptiveStats(@RequestBody List<HashMap<String, String>> queryList) { - Logging.LogUserAction(userInfo.getUser().getUsername(), "(POST) /experiments/descriptive_stats", "Executing Exareme descriptive stats..."); - - String query = gson.toJson(queryList); - String url = queryExaremeUrl + "/" + "DESCRIPTIVE_STATS"; - - try { - StringBuilder results = new StringBuilder(); - int code = HTTPUtil.sendPost(url, query, results); - Logging.LogUserAction(userInfo.getUser().getUsername(), "(POST) /experiments/descriptive_stats", "Executed descriptive stats with result : " + results.toString()); - return ResponseEntity.ok(gson.toJson(results.toString())); - } catch (IOException e) { - Logging.LogUserAction(userInfo.getUser().getUsername(), "(POST) /experiments/descriptive_stats", "Descriptive stats algorithm was not found"); - return new ResponseEntity<>("Not found", HttpStatus.NOT_FOUND); - } - } - - @ApiOperation(value = "Create a descriptive statistic on Exareme", response = String.class) - @RequestMapping(value = "/descriptive_stats_v2", method = RequestMethod.POST) - public ResponseEntity runExaremeDescriptiveStatsV2(@RequestBody List<HashMap<String, String>> queryList) { - Logging.LogUserAction(userInfo.getUser().getUsername(), "(POST) /experiments/descriptive_stats_v2", "Executing an Exareme descriptive stats v2"); - - String query = gson.toJson(queryList); - String url = queryExaremeUrl + "/" + "DESCRIPTIVE_STATS_v2"; - - try { - StringBuilder results = new StringBuilder(); - int code = HTTPUtil.sendPost(url, query, results); - - Logging.LogUserAction(userInfo.getUser().getUsername(), "(POST) /experiments/descriptive_stats_v2", "Successfully executed descriptive stats v2 with results : " + results.toString()); - return ResponseEntity.ok(gson.toJson(results.toString())); - } catch (IOException e) { - Logging.LogUserAction(userInfo.getUser().getUsername(), "(POST) /experiments/descriptive_stats_v2", "Descriptive stats v2 algorithm was not found"); - return new ResponseEntity<>("Not found", HttpStatus.NOT_FOUND); - } - } - - @ApiOperation(value = "Check if a formula is valid", response = String.class) - @RequestMapping(value = "/checkFormula", method = RequestMethod.POST) - public ResponseEntity checkFormulaValidity(String formula) { - Logging.LogUserAction(userInfo.getUser().getUsername(), "(POST) /experiments/checkFormula", "Executing checkFormula ..."); - - return ResponseEntity.ok(""); - } -} diff --git a/src/main/java/eu/hbp/mip/controllers/ModelsApi.java b/src/main/java/eu/hbp/mip/controllers/ModelsApi.java deleted file mode 100644 index 6f013ee336815bbb25514b37cdb764fe7710e8fd..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/controllers/ModelsApi.java +++ /dev/null @@ -1,259 +0,0 @@ -/* - * Created by mirco on 04.12.15. - */ - -package eu.hbp.mip.controllers; - -import com.github.slugify.Slugify; -import eu.hbp.mip.model.Model; -import eu.hbp.mip.model.User; -import eu.hbp.mip.model.UserInfo; -import eu.hbp.mip.model.Variable; -import eu.hbp.mip.repositories.*; -import io.swagger.annotations.*; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; -import eu.hbp.mip.utils.Logging; - -import java.io.IOException; -import java.util.*; - -import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; - -@RestController -@RequestMapping(value = "/models", produces = {APPLICATION_JSON_VALUE}) -@Api(value = "/models", description = "the models API") -public class ModelsApi { - - - @Autowired - private UserInfo userInfo; - - @Autowired - private DatasetRepository datasetRepository; - - @Autowired - private ModelRepository modelRepository; - - @Autowired - private QueryRepository queryRepository; - - @Autowired - private ConfigRepository configRepository; - - @Autowired - private VariableRepository variableRepository; - - @ApiOperation(value = "get models", response = Model.class, responseContainer = "List") - @RequestMapping(method = RequestMethod.GET) - public ResponseEntity<List> getModels( - @ApiParam(value = "Only ask own models") @RequestParam(value = "own", required = false) Boolean own, - @ApiParam(value = "Only ask published models") @RequestParam(value = "valid", required = false) Boolean valid - ) { - Logging.LogUserAction(userInfo.getUser().getUsername(), "(GET) /models", "Loading models ..."); - - User user = userInfo.getUser(); - - Iterable<Model> models; - if (own != null && own) { - models = modelRepository.findByCreatedByOrderByCreatedAt(user); - } else { - models = modelRepository.findByValidOrCreatedByOrderByCreatedAt(true, user); - } - - if (valid != null && models != null) { - for (Iterator<Model> i = models.iterator(); i.hasNext(); ) { - Model m = i.next(); - if (valid != m.getValid()) { - i.remove(); - } - } - } - - List<Object> modelsList = new LinkedList<>(); - models = models != null ? models : new LinkedList<>(); - for (Model m : models) { - m.setDataset(datasetRepository.findOne(m.getDataset().getCode())); - modelsList.add(m); - } - - Logging.LogUserAction(userInfo.getUser().getUsername(), "(GET) /models", "Successfully loaded " + modelsList.size() + " models."); - - return ResponseEntity.ok(modelsList); - } - - - @ApiOperation(value = "Create a model", response = Model.class) - @ApiResponses(value = {@ApiResponse(code = 201, message = "Model created")}) - @RequestMapping(method = RequestMethod.POST) - public ResponseEntity<Model> addAModel( - @RequestBody @ApiParam(value = "Model to create", required = true) Model model - ) { - User user = userInfo.getUser(); - Logging.LogUserAction(user.getUsername(), "(POST) /models", "Creating a model"); - - model.setTitle(model.getConfig().getTitle().get("text")); - model.setCreatedBy(user); - model.setCreatedAt(new Date()); - if (model.getValid() == null) { - model.setValid(false); - } - - ensureTitleUniqueness(model); - ensureSlugUniqueness(model); - - Map<String, String> map = new HashMap<>(model.getConfig().getTitle()); - map.put("text", model.getTitle()); - model.getConfig().setTitle(map); - - saveVariables(model.getQuery().getVariables()); - saveVariables(model.getQuery().getCovariables()); - saveVariables(model.getQuery().getGrouping()); - saveVariables(model.getQuery().getTrainingDatasets()); - - configRepository.save(model.getConfig()); - queryRepository.save(model.getQuery()); - if (model.getDataset() != null) { - datasetRepository.save(model.getDataset()); - } - modelRepository.save(model); - - Logging.LogUserAction(user.getUsername(), "(POST) /models", "Created model with id : " + model.getSlug() + ", model.config and model.query"); - - return ResponseEntity.status(HttpStatus.CREATED).body(model); - } - - private void saveVariables(@RequestBody @ApiParam(value = "Model to create", required = true) List<Variable> variables) { - for (Variable var : variables) { - variableRepository.save(var); - } - } - - private void ensureSlugUniqueness(@RequestBody @ApiParam(value = "Model to create", required = true) Model model) { - String slug = createSlug(model.getTitle()); - boolean slugExists = true; - for (int i = 1; slugExists; i++) { - slugExists = modelRepository.exists(slug); - if (slugExists) { - if (i > 1) { - slug = slug.substring(0, slug.length() - 2); - } - slug += "-" + i; - } - model.setSlug(slug); - } - } - - private String createSlug(@RequestBody @ApiParam(value = "Model to create", required = true) String title) { - String slug; - try { - slug = new Slugify().slugify(title); - } catch (IOException e) { - slug = ""; // Should never happen - //LOGGER.trace("Cannot slugify title", e); - } - return slug; - } - - private void ensureTitleUniqueness(@RequestBody @ApiParam(value = "Model to create", required = true) Model model) { - boolean titleExists = true; - for (int i = 1; titleExists; i++) { - String title = model.getTitle(); - titleExists = modelRepository.countByTitle(title) > 0; - if (titleExists) { - if (i > 1) { - title = title.substring(0, title.length() - 4); - } - model.setTitle(title + " (" + i + ")"); - } - } - } - - @ApiOperation(value = "Get a model", response = Model.class) - @RequestMapping(value = "/{slug}", method = RequestMethod.GET) - public ResponseEntity<Model> getAModel( - @ApiParam(value = "slug", required = true) @PathVariable("slug") String slug - ) { - - User user = userInfo.getUser(); - - Logging.LogUserAction(user.getUsername(), "(GET) /models/{slug}", "Loading model with id : " + slug); - - Model model = modelRepository.findOne(slug); - if (model == null) { - //LOGGER.warn("Cannot find model : " + slug); - Logging.LogUserAction(user.getUsername(), "(GET) /models/{slug}", "Model was not found"); - return ResponseEntity.badRequest().body(null); - } - - if (!model.getValid() && !model.getCreatedBy().getUsername().equals(user.getUsername())) { - Logging.LogUserAction(user.getUsername(), "(GET) /models/{slug}", "You are not authorized to retrieve models. "); - return new ResponseEntity<>(HttpStatus.FORBIDDEN); - } - - List<String> yAxisVars = configRepository.findOne(model.getConfig().getId()).getyAxisVariables(); - Collection<String> yAxisVarsColl = new LinkedHashSet<>(yAxisVars); - model.getConfig().setyAxisVariables(new LinkedList<>(yAxisVarsColl)); - - Logging.LogUserAction(user.getUsername(), "(GET) /models/{slug}", "Loaded model with id : " + slug); - return ResponseEntity.ok(model); - } - - - @ApiOperation(value = "Update a model", response = Void.class) - @ApiResponses(value = {@ApiResponse(code = 204, message = "Model updated")}) - @RequestMapping(value = "/{slug}", method = RequestMethod.PUT) - public ResponseEntity<Void> updateAModel( - @ApiParam(value = "slug", required = true) @PathVariable("slug") String slug, - @RequestBody @ApiParam(value = "Model to update", required = true) Model model - ) { - User user = userInfo.getUser(); - Logging.LogUserAction(user.getUsername(), "(PUT) /models/{slug}", "Updating model with id : " + slug); - Model oldModel = modelRepository.findOne(slug); - - if (!user.getUsername().equals(oldModel.getCreatedBy().getUsername())) { - return new ResponseEntity<>(HttpStatus.FORBIDDEN); - } - - model.setTitle(model.getConfig().getTitle().get("text")); - - String oldTitle = oldModel.getTitle(); - String newTitle = model.getTitle(); - - // If title has been updated, ensure it is unique - if (!newTitle.equals(oldTitle)) { - boolean newTitleExists = true; - for (int i = 1; newTitleExists && !newTitle.equals(oldTitle); i++) { - newTitle = model.getTitle(); - newTitleExists = modelRepository.countByTitle(newTitle) > 0; - if (newTitleExists && !newTitle.equals(oldTitle)) { - if (i > 1) { - newTitle = newTitle.substring(0, newTitle.length() - 4); - } - model.setTitle(newTitle + " (" + i + ")"); - } - } - } - - Map<String, String> map = new HashMap<>(model.getConfig().getTitle()); - map.put("text", model.getTitle()); - model.getConfig().setTitle(map); - - saveVariables(model.getQuery().getVariables()); - saveVariables(model.getQuery().getCovariables()); - saveVariables(model.getQuery().getGrouping()); - saveVariables(model.getQuery().getTrainingDatasets()); - - configRepository.save(model.getConfig()); - queryRepository.save(model.getQuery()); - datasetRepository.save(model.getDataset()); - modelRepository.save(model); - - Logging.LogUserAction(user.getUsername(), "(PUT) /models/{slug}", "Updated model and saved/updated model.config and model.query"); - - return new ResponseEntity<>(HttpStatus.NO_CONTENT); - } - -} diff --git a/src/main/java/eu/hbp/mip/controllers/PathologiesApi.java b/src/main/java/eu/hbp/mip/controllers/PathologiesApi.java index 77916981465827ae4e0806366663a16c7ad1e70b..66a97896bc805667e263b8a1e65565969ea1f63e 100644 --- a/src/main/java/eu/hbp/mip/controllers/PathologiesApi.java +++ b/src/main/java/eu/hbp/mip/controllers/PathologiesApi.java @@ -6,7 +6,7 @@ package eu.hbp.mip.controllers; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; -import eu.hbp.mip.model.PathologyDTO; +import eu.hbp.mip.model.DTOs.PathologyDTO; import eu.hbp.mip.model.UserInfo; import eu.hbp.mip.utils.ClaimUtils; import eu.hbp.mip.utils.CustomResourceLoader; diff --git a/src/main/java/eu/hbp/mip/controllers/StatsApi.java b/src/main/java/eu/hbp/mip/controllers/StatsApi.java deleted file mode 100644 index 8bd109e535b7c29b5dac96b68b2ba59329ca6a59..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/controllers/StatsApi.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Created by mirco on 18.01.16. - */ - -package eu.hbp.mip.controllers; - -import eu.hbp.mip.model.GeneralStats; -import eu.hbp.mip.model.UserInfo; -import eu.hbp.mip.repositories.ArticleRepository; -import eu.hbp.mip.repositories.UserRepository; -import eu.hbp.mip.utils.Logging; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; - -@RestController -@RequestMapping(value = "/stats", produces = {APPLICATION_JSON_VALUE}) -@Api(value = "/stats", description = "the stats API") -public class StatsApi { - @Autowired - private UserRepository userRepository; - - @Autowired - private UserInfo userInfo; - - @Autowired - private ArticleRepository articleRepository; - - - @ApiOperation(value = "Get general statistics", response = GeneralStats.class) - @RequestMapping(method = RequestMethod.GET) - public ResponseEntity<GeneralStats> getGeneralStatistics() { - Logging.LogUserAction(userInfo.getUser().getUsername(), "(GET) /stats", "Loading general statistics"); - - GeneralStats stats = new GeneralStats(); - - stats.setUsers(userRepository.count()); - stats.setArticles(articleRepository.count()); - - Logging.LogUserAction(userInfo.getUser().getUsername(), "(GET) /stats", "Loaded " + userRepository.count() + " user statistics and " + articleRepository.count() + " artcle statistics."); - return ResponseEntity.ok(stats); - } - -} diff --git a/src/main/java/eu/hbp/mip/model/Article.java b/src/main/java/eu/hbp/mip/model/Article.java index 95c7cbbae0701558b249dec7a5cf0b9a07bd3e92..b79f2a33a8275484a0e9a680a93619dbea2ea5c2 100644 --- a/src/main/java/eu/hbp/mip/model/Article.java +++ b/src/main/java/eu/hbp/mip/model/Article.java @@ -52,10 +52,6 @@ public class Article { @JoinColumn(name = "updatedby_username") private User updatedBy = null; - @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST) - private List<Tag> tags = new LinkedList<>(); - - public Article() { /* * Empty constructor is needed by Hibernate @@ -154,13 +150,4 @@ public class Article { this.updatedBy = updatedBy; } - - public List<Tag> getTags() { - return tags; - } - - public void setTags(List<Tag> tags) { - this.tags = tags; - } - } diff --git a/src/main/java/eu/hbp/mip/model/Config.java b/src/main/java/eu/hbp/mip/model/Config.java deleted file mode 100644 index 2362194b2b8338d7ab7a0b781c7891e88acfd293..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/model/Config.java +++ /dev/null @@ -1,112 +0,0 @@ -/** - * Created by mirco on 25.02.16. - */ - -package eu.hbp.mip.model; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import io.swagger.annotations.ApiModel; - -import javax.persistence.*; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -@Entity -@Table(name = "`config`") -@ApiModel -@JsonIgnoreProperties(value = {"id"}) -public class Config { - - @Id - @GeneratedValue(strategy = GenerationType.SEQUENCE) - private Long id = null; - - private String type = null; - - private Integer height = null; - - @ElementCollection(fetch = FetchType.EAGER) - @CollectionTable(name = "config_yAxisVariables", joinColumns = @JoinColumn(name = "config_id")) - private List<String> yAxisVariables = new LinkedList<>(); - - private String xAxisVariable = null; - - private Boolean hasXAxis = null; - - @ElementCollection(fetch = FetchType.EAGER) - @CollectionTable(name = "config_title", joinColumns = @JoinColumn(name = "config_id")) - private Map<String, String> title = new HashMap<>(); - - - public Config() { - /* - * Empty constructor is needed by Hibernate - */ - } - - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - - public Integer getHeight() { - return height; - } - - public void setHeight(Integer height) { - this.height = height; - } - - - public List<String> getyAxisVariables() { - return yAxisVariables; - } - - public void setyAxisVariables(List<String> yAxisVariables) { - this.yAxisVariables = yAxisVariables; - } - - - public String getxAxisVariable() { - return xAxisVariable; - } - - public void setxAxisVariable(String xAxisVariable) { - this.xAxisVariable = xAxisVariable; - } - - - public Boolean getHasXAxis() { - return hasXAxis; - } - - public void setHasXAxis(Boolean hasXAxis) { - this.hasXAxis = hasXAxis; - } - - - public Map<String, String> getTitle() { - return title; - } - - public void setTitle(Map<String, String> title) { - this.title = title; - } - -} diff --git a/src/main/java/eu/hbp/mip/model/DAOs/ExperimentDAO.java b/src/main/java/eu/hbp/mip/model/DAOs/ExperimentDAO.java new file mode 100644 index 0000000000000000000000000000000000000000..682f121cea95c13bf88c83c71ddcd5815ffe7884 --- /dev/null +++ b/src/main/java/eu/hbp/mip/model/DAOs/ExperimentDAO.java @@ -0,0 +1,203 @@ +package eu.hbp.mip.model.DAOs; + +import com.google.gson.Gson; +import com.google.gson.annotations.Expose; +import eu.hbp.mip.model.DTOs.AlgorithmDTO; +import eu.hbp.mip.model.DTOs.ExperimentDTO; +import eu.hbp.mip.model.User; +import eu.hbp.mip.utils.JsonConverters; + +import javax.persistence.*; +import java.util.Date; +import java.util.UUID; + +/** + * Created by habfast on 21/04/16. + */ +@Entity +@Table(name = "`experiment`") +public class ExperimentDAO { + + private static final Gson gson = new Gson(); + + @Id + @Column(columnDefinition = "uuid", updatable = false) + @org.hibernate.annotations.Type(type = "pg-uuid") + @Expose + private UUID uuid; + + @Column(columnDefinition = "TEXT") + @Expose + private String name; + + @Expose + @ManyToOne + @JoinColumn(name = "created_by_username") + private User createdBy; + + @Column(name="workflow_history_id", columnDefinition = "TEXT") + @Expose + private String workflowHistoryId; + + @Column(columnDefinition = "TEXT") + @Expose + private Status status; + + @Column(columnDefinition = "TEXT") + @Expose + private String result; + + @Expose + private Date finished; + + @Expose + private String algorithm; + + @Expose + private Date created = new Date(); + + @Expose + private boolean shared = false; + + // whether or not the experiment's result have been viewed by its owner + @Expose + private boolean viewed = false; + + public enum Status { + error, + pending, + success + } + + public enum MimeTypes { + ERROR("text/plain+error"), + WARNING("text/plain+warning"), + USER_WARNING("text/plain+user_error"), + HIGHCHARTS("application/vnd.highcharts+json"), + JSON("application/json"), + JSONBTREE("application/binary-tree+json"), + PFA("application/pfa+json"), + JSONDATA("application/vnd.dataresource+json"), + HTML("text/html"), + TEXT("text/plain"); + + private String types; + + //Constructor to initialize the instance variable + MimeTypes(String types) { + this.types = types; + } + + public String getTypes() { + return this.types; + } + } + + public ExperimentDAO() { + /* + * Empty constructor is needed by Hibernate + */ + } + + public ExperimentDTO convertToDTO() + { + ExperimentDTO experimentDTO = new ExperimentDTO(); + experimentDTO.setAlgorithm(JsonConverters.convertJsonStringToObject(this.algorithm, AlgorithmDTO.class)); + experimentDTO.setCreated(this.created); + experimentDTO.setCreatedBy(this.createdBy.getUsername()); + experimentDTO.setName(this.name); + experimentDTO.setResult(JsonConverters.convertJsonStringToObject(this.result, ExperimentDTO.ResultDTO.class)); + experimentDTO.setShared(this.shared); + experimentDTO.setUuid(this.uuid.toString()); + experimentDTO.setViewed(this.viewed); + return experimentDTO; + } + + public String getAlgorithm() { + return algorithm; + } + + public void setAlgorithm(String algorithm) { + this.algorithm = algorithm; + } + + public String getWorkflowHistoryId() { + return workflowHistoryId; + } + + public void setWorkflowHistoryId(String workflowHistoryId) { + this.workflowHistoryId = workflowHistoryId; + } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } + + public String getResult() { + return result; + } + + public void setResult(String result) { + this.result = result; + } + + public Date getFinished() { + return finished; + } + + public void setFinished(Date finished) { + this.finished = finished; + } + + public Date getCreated() { + return created; + } + + public void setCreated(Date created) { + this.created = created; + } + + public UUID getUuid() { + return uuid; + } + + public void setUuid(UUID uuid) { + this.uuid = uuid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public User getCreatedBy() { + return createdBy; + } + + public void setCreatedBy(User createdBy) { + this.createdBy = createdBy; + } + + public boolean isViewed() { + return viewed; + } + + public void setViewed(boolean viewed) { + this.viewed = viewed; + } + + public boolean isShared() { + return shared; + } + + public void setShared(boolean shared) { + this.shared = shared; + } +} diff --git a/src/main/java/eu/hbp/mip/model/AlgorithmDTO.java b/src/main/java/eu/hbp/mip/model/DTOs/AlgorithmDTO.java similarity index 95% rename from src/main/java/eu/hbp/mip/model/AlgorithmDTO.java rename to src/main/java/eu/hbp/mip/model/DTOs/AlgorithmDTO.java index ed0ef2a15606ef09f139bad65c90c3cf436c2474..0a49c5ad672aa0b14e6830bf6d683f95a08af6b4 100644 --- a/src/main/java/eu/hbp/mip/model/AlgorithmDTO.java +++ b/src/main/java/eu/hbp/mip/model/DTOs/AlgorithmDTO.java @@ -1,4 +1,4 @@ -package eu.hbp.mip.model; +package eu.hbp.mip.model.DTOs; import com.google.gson.annotations.SerializedName; diff --git a/src/main/java/eu/hbp/mip/model/DTOs/ExperimentDTO.java b/src/main/java/eu/hbp/mip/model/DTOs/ExperimentDTO.java new file mode 100644 index 0000000000000000000000000000000000000000..b4214dd0a4de6aa50fa13f7aeb91652ce2c32137 --- /dev/null +++ b/src/main/java/eu/hbp/mip/model/DTOs/ExperimentDTO.java @@ -0,0 +1,126 @@ +package eu.hbp.mip.model.DTOs; + +import eu.hbp.mip.model.DAOs.ExperimentDAO; + +import java.util.Date; +import java.util.List; + +public class ExperimentDTO { + + private String uuid; + private String name; + private String createdBy; + private Date created; + private Boolean shared; + private Boolean viewed; + private ExperimentDTO.ResultDTO result; + private ExperimentDAO.Status status; + + private AlgorithmDTO algorithm; + + public AlgorithmDTO getAlgorithm() { + return algorithm; + } + + public void setAlgorithm(AlgorithmDTO algorithm) { + this.algorithm = algorithm; + } + + public String getUuid() { + return uuid; + } + + public void setUuid(String uuid) { + this.uuid = uuid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCreatedBy() { + return createdBy; + } + + public void setCreatedBy(String createdBy) { + this.createdBy = createdBy; + } + + public Date getCreated() { + return created; + } + + public void setCreated(Date created) { + this.created = created; + } + + public Boolean getShared() { + return shared; + } + + public void setShared(Boolean shared) { + this.shared = shared; + } + + public Boolean getViewed() { + return viewed; + } + + public void setViewed(Boolean viewed) { + this.viewed = viewed; + } + + public ExperimentDTO.ResultDTO getResult() { + return result; + } + + public void setResult(ExperimentDTO.ResultDTO result) { + this.result = result; + } + + public ExperimentDAO.Status getStatus() { + return status; + } + + public void setStatus(ExperimentDAO.Status status) { + this.status = status; + } + + public static class OutputDTO { + + private String data; + private ExperimentDAO.MimeTypes mimeTypes; + + public String getData() { + return this.data; + } + + public void setData(String data) { + this.data = data; + } + + public ExperimentDAO.MimeTypes getMimeTypes() { + return mimeTypes; + } + + public void setMimeTypes(ExperimentDAO.MimeTypes mimeTypes) { + this.mimeTypes = mimeTypes; + } + } + + public static class ResultDTO { + private List<OutputDTO> result; + + public List<OutputDTO> getResult() { + return this.result; + } + + public void setResult(List<OutputDTO> result) { + this.result = result; + } + } +} diff --git a/src/main/java/eu/hbp/mip/model/PathologyDTO.java b/src/main/java/eu/hbp/mip/model/DTOs/PathologyDTO.java similarity index 93% rename from src/main/java/eu/hbp/mip/model/PathologyDTO.java rename to src/main/java/eu/hbp/mip/model/DTOs/PathologyDTO.java index c58238b623e18aa5f7942d9e4816f905ce48f3f8..6a4580e81bbf9e7e371cf4c8297723d3b946f9c0 100644 --- a/src/main/java/eu/hbp/mip/model/PathologyDTO.java +++ b/src/main/java/eu/hbp/mip/model/DTOs/PathologyDTO.java @@ -1,4 +1,4 @@ -package eu.hbp.mip.model; +package eu.hbp.mip.model.DTOs; import com.google.gson.annotations.SerializedName; diff --git a/src/main/java/eu/hbp/mip/model/Dataset.java b/src/main/java/eu/hbp/mip/model/Dataset.java deleted file mode 100644 index d920571d9526dca8d29316d8d56578dc3d0b3b9b..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/model/Dataset.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Created by mirco on 04.12.15. - */ - -package eu.hbp.mip.model; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; -import io.swagger.annotations.ApiModel; - -import javax.persistence.*; -import java.util.Date; -import java.util.LinkedList; -import java.util.List; - -// TODO: deprecate - -@Entity -@Table(name = "`dataset`") -@ApiModel -@JsonInclude(JsonInclude.Include.NON_NULL) -public class Dataset { - - @Id - private String code = null; - - private Date date = null; - - // Aka covariables - @ElementCollection - @CollectionTable(name = "dataset_header", joinColumns = @JoinColumn(name = "dataset_code")) - private List<String> header = new LinkedList<>(); - - @ElementCollection - @CollectionTable(name = "dataset_grouping", joinColumns = @JoinColumn(name = "dataset_code")) - private List<String> grouping = new LinkedList<>(); - - @ElementCollection - @CollectionTable(name = "dataset_variable", joinColumns = @JoinColumn(name = "dataset_code")) - private List<String> variable = new LinkedList<>(); - - // TODO: - @Transient - @JsonProperty(access = JsonProperty.Access.READ_ONLY) - private Object data = null; - - - public Dataset() { - /* - * Empty constructor is needed by Hibernate - */ - } - - - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = code; - } - - - public Date getDate() { - return date; - } - - public void setDate(Date date) { - this.date = date; - } - - - public List<String> getHeader() { - return header; - } - - public void setHeader(List<String> header) { - this.header = header; - } - - - public List<String> getGrouping() { - return grouping; - } - - public void setGrouping(List<String> grouping) { - this.grouping = grouping; - } - - - public List<String> getVariable() { - return variable; - } - - public void setVariable(List<String> variable) { - this.variable = variable; - } - - public Object getData() { - return data; - } - - public void setData(Object data) { - this.data = data; - } -} diff --git a/src/main/java/eu/hbp/mip/model/DatasetDescription.java b/src/main/java/eu/hbp/mip/model/DatasetDescription.java deleted file mode 100644 index c77c50aeab3a4256ecadf78aa2f25f89a05a0b92..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/model/DatasetDescription.java +++ /dev/null @@ -1,46 +0,0 @@ -package eu.hbp.mip.model; - -import com.fasterxml.jackson.annotation.JsonInclude; -import io.swagger.annotations.ApiModel; - -@ApiModel -@JsonInclude(JsonInclude.Include.NON_NULL) -public class DatasetDescription { - - private String code; - private String label; - private String description; - private String anonymisationLevel; - - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = code; - } - - public String getLabel() { - return label; - } - - public void setLabel(String label) { - this.label = label; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getAnonymisationLevel() { - return anonymisationLevel; - } - - public void setAnonymisationLevel(String anonymisationLevel) { - this.anonymisationLevel = anonymisationLevel; - } -} diff --git a/src/main/java/eu/hbp/mip/model/Experiment.java b/src/main/java/eu/hbp/mip/model/Experiment.java deleted file mode 100644 index c40d91d3441ce131e45e5dbda0934c4a3ab0c30b..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/model/Experiment.java +++ /dev/null @@ -1,232 +0,0 @@ -package eu.hbp.mip.model; - -import com.google.gson.*; -import com.google.gson.annotations.Expose; -import org.hibernate.annotations.Cascade; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.persistence.*; -import java.util.*; - -/** - * Created by habfast on 21/04/16. - */ -@Entity -@Table(name = "`experiment`") -public class Experiment { - - private static final Gson gson = new Gson(); - - @Id - @Column(columnDefinition = "uuid") - @org.hibernate.annotations.Type(type = "pg-uuid") - @Expose - private UUID uuid; - - @Column(columnDefinition = "TEXT") - @Expose - private String name; - - @Expose - @ManyToOne - @JoinColumn(name = "createdby_username") - private User createdBy; - - @ManyToOne - @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE) - @Expose - private Model model; - - @Column(columnDefinition = "TEXT") - @Expose - private String algorithms; - - @Column(columnDefinition = "TEXT") - private String validations; - - @Column(columnDefinition = "TEXT") - @Expose - private String workflowHistoryId; - - @Column(columnDefinition = "TEXT") - @Expose - private String workflowStatus; - - @Column(columnDefinition = "TEXT") - @Expose - private String result; - - @Expose - private Date created = new Date(); - - @Expose - private Date finished; - - @Expose - private boolean hasError = false; - - @Expose - private boolean hasServerError = false; - - @Expose - private boolean shared = false; - - // whether or not the experiment's result have been resultsViewed by its owner - @Expose - private boolean resultsViewed = false; - - public Experiment() { - /* - * Empty constructor is needed by Hibernate - */ - } - - - public JsonObject jsonify() { - JsonObject exp = gson.toJsonTree(this).getAsJsonObject(); - JsonParser parser = new JsonParser(); - - if (this.algorithms != null) { - exp.remove("algorithms"); - JsonArray jsonAlgorithms = parser.parse(this.algorithms).getAsJsonArray(); - exp.add("algorithms", jsonAlgorithms); - } - - if (this.validations != null) { - exp.remove("validations"); - JsonArray jsonValidations = parser.parse(this.validations).getAsJsonArray(); - exp.add("validations", jsonValidations); - } - - if (this.result != null && !this.hasServerError) { - exp.remove("result"); - - JsonElement jsonResult = parser.parse(this.result); - exp.add("result", jsonResult); - - } - - return exp; - } - - public String getValidations() { - return validations; - } - - public void setValidations(String validations) { - this.validations = validations; - } - - public String getAlgorithms() { - return algorithms; - } - - public void setAlgorithms(String algorithms) { - this.algorithms = algorithms; - } - - public Model getModel() { - return model; - } - - public void setModel(Model model) { - this.model = model; - } - - public boolean isHasError() { - return hasError; - } - - public void setHasError(boolean hasError) { - this.hasError = hasError; - } - - public String getWorkflowHistoryId() { - return workflowHistoryId; - } - - public void setWorkflowHistoryId(String workflowHistoryId) { - this.workflowHistoryId = workflowHistoryId; - } - - public String getWorkflowStatus() { - return workflowStatus; - } - - public void setWorkflowStatus(String workflowStatus) { - this.workflowStatus = workflowStatus; - } - - public String getResult() { - return result; - } - - public void setResult(String result) { - this.result = result; - } - - public Date getFinished() { - return finished; - } - - public void setFinished(Date finished) { - this.finished = finished; - } - - public Date getCreated() { - return created; - } - - public void setCreated(Date created) { - this.created = created; - } - - public UUID getUuid() { - return uuid; - } - - public void setUuid(UUID uuid) { - this.uuid = uuid; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public User getCreatedBy() { - return createdBy; - } - - public void setCreatedBy(User createdBy) { - this.createdBy = createdBy; - } - - public boolean isResultsViewed() { - return resultsViewed; - } - - public void setResultsViewed(boolean resultsViewed) { - this.resultsViewed = resultsViewed; - } - - public boolean isHasServerError() { - return hasServerError; - } - - public void setHasServerError(boolean hasServerError) { - this.hasServerError = hasServerError; - } - - public boolean isShared() { - return shared; - } - - public void setShared(boolean shared) { - this.shared = shared; - } -} diff --git a/src/main/java/eu/hbp/mip/model/ExperimentExecutionDTO.java b/src/main/java/eu/hbp/mip/model/ExperimentExecutionDTO.java deleted file mode 100644 index a68cd9121d852503bc01eb6b2f0d23dd8af525dd..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/model/ExperimentExecutionDTO.java +++ /dev/null @@ -1,106 +0,0 @@ -package eu.hbp.mip.model; - -import java.util.List; - -public class ExperimentExecutionDTO { - - private String name; - private String model; - private List<AlgorithmExecutionDTO> algorithms; - - public String getModel() { - return model; - } - - public void setModel(String model) { - this.model = model; - } - - public List<AlgorithmExecutionDTO> getAlgorithms() { - return algorithms; - } - - public void setAlgorithms(List<AlgorithmExecutionDTO> algorithms) { - this.algorithms = algorithms; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public static class AlgorithmExecutionDTO { - - private String name; - private String label; - private String type; - - private List<AlgorithmExecutionParamDTO> parameters; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public List<AlgorithmExecutionDTO.AlgorithmExecutionParamDTO> getParameters() { - return parameters; - } - - public void setParameters(List<AlgorithmExecutionDTO.AlgorithmExecutionParamDTO> parameters) { - this.parameters = parameters; - } - - public String getLabel() { - return label; - } - - public void setLabel(String label) { - this.label = label; - } - - public static class AlgorithmExecutionParamDTO { - - private String name; - private String label; - private String value; - - public String getName() { - return this.name; - } - - public void setName(String name) { - this.name = name; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - - public String getLabel() { - return label; - } - - public void setLabel(String label) { - this.label = label; - } - } - } -} diff --git a/src/main/java/eu/hbp/mip/model/Group.java b/src/main/java/eu/hbp/mip/model/Group.java deleted file mode 100644 index abb195a932669d28785b407ad3738ab8087cffe7..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/model/Group.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Created by mirco on 04.12.15. - */ - -package eu.hbp.mip.model; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import io.swagger.annotations.ApiModel; - -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; - -@Entity -@Table(name = "`group`") -@ApiModel -@JsonIgnoreProperties(value = {"parent"}) -@JsonInclude(JsonInclude.Include.NON_NULL) -public class Group { - - @Id - private String code = null; - - - public Group() { - /* - * Empty constructor is needed by Hibernate - */ - } - - - public String getCode() { - return code; - } - -} diff --git a/src/main/java/eu/hbp/mip/model/Mining.java b/src/main/java/eu/hbp/mip/model/Mining.java deleted file mode 100644 index 46cfd6c3ec64b0a7e552cd4f6e210a24a746fc1e..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/model/Mining.java +++ /dev/null @@ -1,64 +0,0 @@ -package eu.hbp.mip.model; - -import com.google.gson.*; - -import java.util.Date; - -public class Mining { - - private static final Gson gson = new Gson(); - - private final String jobId; - private final String node; - private final String function; - private final String shape; - private final Date timestamp; - private final String data; - - public Mining(String jobId, String node, String function, String shape, Date timestamp, String data) { - this.jobId = jobId; - this.node = node; - this.function = function; - this.shape = shape; - this.timestamp = timestamp; - this.data = data; - } - - public String getJobId() { - return jobId; - } - - public String getNode() { - return node; - } - - public String getFunction() { - return function; - } - - public String getShape() { - return shape; - } - - public Date getTimestamp() { - return timestamp; - } - - public String getData() { - return data; - } - - public JsonObject jsonify() { - JsonObject exp = gson.toJsonTree(this).getAsJsonObject(); - JsonParser parser = new JsonParser(); - - if (this.data != null) { - exp.remove("data"); - JsonElement jsonResult = parser.parse(this.data); - exp.add("data", jsonResult); - } - - return exp; - } - -} diff --git a/src/main/java/eu/hbp/mip/model/MiningQuery.java b/src/main/java/eu/hbp/mip/model/MiningQuery.java deleted file mode 100644 index 171c55032eee4b3c5f63d636c137d6ae41bfe051..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/model/MiningQuery.java +++ /dev/null @@ -1,96 +0,0 @@ -package eu.hbp.mip.model; - -import com.google.gson.Gson; - -import java.util.LinkedList; -import java.util.List; - -/** - * Created by mirco on 06.01.17. - */ -public class MiningQuery { - - private List<Variable> variables; - private List<Variable> covariables; - private List<Variable> grouping; - private List<Variable> datasets; - private String filters; - private ExperimentExecutionDTO.AlgorithmExecutionDTO algorithm; - - public MiningQuery() { - this.variables = new LinkedList<>(); - this.covariables = new LinkedList<>(); - this.grouping = new LinkedList<>(); - this.datasets = new LinkedList<>(); - this.filters = ""; - } - - public List<Variable> getVariables() { - return variables; - } - - public void setVariables(List<Variable> variables) { - this.variables = variables; - } - - public void addVariable(Variable variable) { - this.variables.add(variable); - } - - public List<Variable> getCovariables() { - return covariables; - } - - public void setCovariables(List<Variable> covariables) { - this.covariables = covariables; - } - - public void addCovariable(Variable variable) { - this.covariables.add(variable); - } - - public List<Variable> getGrouping() { - return grouping; - } - - public void setGrouping(List<Variable> grouping) { - this.grouping = grouping; - } - - public List<Variable> getDatasets() { - return datasets; - } - - public void setDataset(List<Variable> datasets) { - this.datasets = datasets; - } - - public void addDataset(Variable variable) { - this.datasets.add(variable); - } - - public void addGrouping(Variable variable) { - this.grouping.add(variable); - } - - public String getFilters() { - return filters; - } - - public void setFilters(String filters) { - this.filters = filters; - } - - public ExperimentExecutionDTO.AlgorithmExecutionDTO getAlgorithm() { - return algorithm; - } - - public void setAlgorithm(ExperimentExecutionDTO.AlgorithmExecutionDTO algorithm) { - this.algorithm = algorithm; - } - - @Override - public String toString() { - return new Gson().toJson(this); - } -} diff --git a/src/main/java/eu/hbp/mip/model/Model.java b/src/main/java/eu/hbp/mip/model/Model.java deleted file mode 100644 index b1590803720c5a0c38538667ef48c940d644399c..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/model/Model.java +++ /dev/null @@ -1,163 +0,0 @@ -/** - * Created by mirco on 04.12.15. - */ - -package eu.hbp.mip.model; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.google.gson.annotations.Expose; -import io.swagger.annotations.ApiModel; -import org.hibernate.annotations.Cascade; -import org.hibernate.annotations.CascadeType; - -import javax.persistence.*; -import java.util.Date; - -@Entity -@Table(name = "`model`") -@ApiModel -@JsonInclude(JsonInclude.Include.NON_NULL) -public class Model { - - @Id - @Expose - private String slug = null; - - @Expose - private String title = null; - - private String description = null; - - private Boolean valid = null; - - private Date createdAt = null; - - private Date updatedAt = null; - - @ManyToOne - @Cascade(CascadeType.SAVE_UPDATE) - private eu.hbp.mip.model.Query query = null; - - @ManyToOne - @Cascade(CascadeType.SAVE_UPDATE) - private Dataset dataset = null; - - @ManyToOne - @Cascade(CascadeType.SAVE_UPDATE) - private Config config = null; - - @ManyToOne - @JoinColumn(name = "createdby_username") - private User createdBy = null; - - @ManyToOne - @JoinColumn(name = "updatedby_username") - private User updatedBy = null; - - - public Model() { - /* - * Empty constructor is needed by Hibernate - */ - } - - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - - public String getSlug() { - return slug; - } - - public void setSlug(String slug) { - this.slug = slug; - } - - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - - public eu.hbp.mip.model.Query getQuery() { - return query; - } - - public void setQuery(eu.hbp.mip.model.Query query) { - this.query = query; - } - - - public Dataset getDataset() { - return dataset; - } - - public void setDataset(Dataset dataset) { - this.dataset = dataset; - } - - - public Boolean getValid() { - return valid; - } - - public void setValid(Boolean valid) { - this.valid = valid; - } - - - public Config getConfig() { - return config; - } - - public void setConfig(Config config) { - this.config = config; - } - - - public Date getCreatedAt() { - return createdAt; - } - - public void setCreatedAt(Date createdAt) { - this.createdAt = createdAt; - } - - - public Date getUpdatedAt() { - return updatedAt; - } - - public void setUpdatedAt(Date updatedAt) { - this.updatedAt = updatedAt; - } - - - public User getCreatedBy() { - return createdBy; - } - - public void setCreatedBy(User createdBy) { - this.createdBy = createdBy; - } - - - public User getUpdatedBy() { - return updatedBy; - } - - public void setUpdatedBy(User updatedBy) { - this.updatedBy = updatedBy; - } - -} diff --git a/src/main/java/eu/hbp/mip/model/Query.java b/src/main/java/eu/hbp/mip/model/Query.java deleted file mode 100644 index 8e4d78dcbb1534052e9108aa9cf6d35be6ffa26e..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/model/Query.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Created by mirco on 04.12.15. - */ - -package eu.hbp.mip.model; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; -import io.swagger.annotations.ApiModel; - -import javax.persistence.*; -import java.util.LinkedList; -import java.util.List; - -@Entity -@ApiModel -@Table(name = "`query`") -@JsonIgnoreProperties(value = {"id"}) -@JsonInclude(JsonInclude.Include.NON_NULL) -public class Query { - - @Id - @GeneratedValue(strategy = GenerationType.SEQUENCE) - private Long id = null; - - @ManyToMany - @JoinTable(name = "query_variable", joinColumns = { - @JoinColumn(name = "id", nullable = false, updatable = false)}, - inverseJoinColumns = {@JoinColumn(name = "code", - nullable = false, updatable = false)}) - private List<Variable> variables = new LinkedList<>(); - - @ManyToMany - @JoinTable(name = "query_covariable", joinColumns = { - @JoinColumn(name = "id", nullable = false, updatable = false)}, - inverseJoinColumns = {@JoinColumn(name = "code", - nullable = false, updatable = false)}) - private List<Variable> covariables = new LinkedList<>(); - - @ManyToMany - @JoinTable(name = "query_grouping", joinColumns = { - @JoinColumn(name = "id", nullable = false, updatable = false)}, - inverseJoinColumns = {@JoinColumn(name = "code", - nullable = false, updatable = false)}) - private List<Variable> grouping = new LinkedList<>(); - - @ManyToMany - @JoinTable(name = "query_training_datasets", joinColumns = { - @JoinColumn(name = "id", nullable = false, updatable = false)}, - inverseJoinColumns = {@JoinColumn(name = "code", - nullable = false, updatable = false)}) - private List<Variable> trainingDatasets = new LinkedList<>(); - - @ManyToMany - @JoinTable(name = "query_testing_datasets", joinColumns = { - @JoinColumn(name = "id", nullable = false, updatable = false)}, - inverseJoinColumns = {@JoinColumn(name = "code", - nullable = false, updatable = false)}) - private List<Variable> testingDatasets = new LinkedList<>(); - - @ManyToMany - @JoinTable(name = "query_validation_datasets", joinColumns = { - @JoinColumn(name = "id", nullable = false, updatable = false)}, - inverseJoinColumns = {@JoinColumn(name = "code", - nullable = false, updatable = false)}) - private List<Variable> validationDatasets = new LinkedList<>(); - - @Column(columnDefinition = "text") - private String filters = ""; - - @Column(columnDefinition = "text") - private String pathology = ""; - - - public Query() { - /* - * Empty constructor is needed by Hibernate - */ - } - - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - - @JsonProperty("variables") - public List<Variable> getVariables() { - return variables; - } - - public void setVariables(List<Variable> variables) { - this.variables = variables; - } - - - @JsonProperty("coVariables") - public List<Variable> getCovariables() { - return covariables; - } - - public void setCovariables(List<Variable> covariables) { - this.covariables = covariables; - } - - - @JsonProperty("groupings") - public List<Variable> getGrouping() { - return grouping; - } - - public void setGrouping(List<Variable> grouping) { - this.grouping = grouping; - } - - @JsonProperty("trainingDatasets") - public List<Variable> getTrainingDatasets() { - return trainingDatasets; - } - - public void setTrainingDatasets(List<Variable> trainingDatasets) { - this.trainingDatasets = trainingDatasets; - } - - @JsonProperty("testingDatasets") - public List<Variable> getTestingDatasets() { - return testingDatasets; - } - - public void setTestingDatasets(List<Variable> testingDatasets) { - this.testingDatasets = testingDatasets; - } - - @JsonProperty("validationDatasets") - public List<Variable> getValidationDatasets() { - return validationDatasets; - } - - public void setValidationDatasets(List<Variable> validationDatasets) { - this.validationDatasets = validationDatasets; - } - - public String getFilters() { - return filters; - } - - public void setFilters(String filters) { - this.filters = filters; - } - - public String getPathology() { - return pathology; - } - - public void setPathology(String pathology) { - this.pathology = pathology; - } - -} diff --git a/src/main/java/eu/hbp/mip/model/Tag.java b/src/main/java/eu/hbp/mip/model/Tag.java deleted file mode 100644 index 693c9865602ace72de50d067e3dd5e3eaac97526..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/model/Tag.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Created by mirco on 04.12.15. - */ - -package eu.hbp.mip.model; - -import com.fasterxml.jackson.annotation.JsonInclude; -import io.swagger.annotations.ApiModel; - -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; - -@Entity -@Table(name = "`tag`") -@ApiModel -@JsonInclude(JsonInclude.Include.NON_NULL) -public class Tag { - - @Id - private String name = null; - - - public Tag() { - /* - * Empty constructor is needed by Hibernate - */ - } - - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - -} diff --git a/src/main/java/eu/hbp/mip/model/User.java b/src/main/java/eu/hbp/mip/model/User.java index 64fee4b670e6285a0e06ada431975fd5c17cf50e..f1ee34059054493bfc3c37ad7d762ffacb486c6f 100644 --- a/src/main/java/eu/hbp/mip/model/User.java +++ b/src/main/java/eu/hbp/mip/model/User.java @@ -9,11 +9,11 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.google.gson.annotations.Expose; import io.swagger.annotations.ApiModel; -import javax.persistence.*; -import java.util.*; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.stream.Collectors; @Entity @Table(name = "`user`") @@ -29,58 +29,9 @@ public class User { @Expose private String fullname = null; - @Expose - private String firstname = null; - - @Expose - private String lastname = null; - - @Expose - private String picture = null; - - @Expose - private String web = null; - - @Expose - private String phone = null; - - @Expose - private String birthday = null; - - @Expose - private String gender = null; - - @Expose - private String city = null; - - @Expose - private String country = null; - - @Expose - private String password = null; - @Expose private String email = null; - @Expose - private String apikey = null; - - @Expose - private String team = null; - - @Expose - private Boolean isActive = null; - - @ElementCollection(fetch = FetchType.EAGER) - @CollectionTable(name = "user_languages", joinColumns = @JoinColumn(name = "user_username")) - @Expose - private List<String> languages = new LinkedList<>(); - - @ElementCollection(fetch = FetchType.EAGER) - @CollectionTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_username")) - @Expose - private List<String> roles = new LinkedList<>(); - private Boolean agreeNDA = null; @@ -113,49 +64,12 @@ public class User { this.fullname = m.group(1); } - p = Pattern.compile("given_name=([\\w ]+)"); - m = p.matcher(userInfo); - if (m.find()) { - this.firstname = m.group(1); - } - - p = Pattern.compile("family_name=([\\w ]+)"); - m = p.matcher(userInfo); - if (m.find()) { - this.lastname = m.group(1); - } - p = Pattern.compile("email=([\\w.]+@[\\w.]+)"); m = p.matcher(userInfo); if (m.find()) { this.email = m.group(1); } - p = Pattern.compile("title=([\\w ]+)"); - m = p.matcher(userInfo); - if (m.find()) { - if ("Mr".equals(m.group(1))) { - this.gender = "Male"; - } else { - this.gender = "Female"; - } - } - - p = Pattern.compile("contractor=([\\w ]+)"); - m = p.matcher(userInfo); - if (m.find()) { - this.team = m.group(1); - } - - p = Pattern.compile("picture=([-a-zA-Z0-9+&@#/%=~_|.: ]+)"); - m = p.matcher(userInfo); - if (m.find()) { - this.picture = m.group(1); - } - - if (this.picture == null || this.picture.isEmpty()) { - this.picture = "images/users/default_user.png"; - } } @@ -178,96 +92,6 @@ public class User { } - public String getFirstname() { - return firstname; - } - - public void setFirstname(String firstname) { - this.firstname = firstname; - } - - - public String getLastname() { - return lastname; - } - - public void setLastname(String lastname) { - this.lastname = lastname; - } - - - public String getPicture() { - return picture; - } - - public void setPicture(String picture) { - this.picture = picture; - } - - - public String getWeb() { - return web; - } - - public void setWeb(String web) { - this.web = web; - } - - - public String getPhone() { - return phone; - } - - public void setPhone(String phone) { - this.phone = phone; - } - - - public String getBirthday() { - return birthday; - } - - public void setBirthday(String birthday) { - this.birthday = birthday; - } - - - public String getGender() { - return gender; - } - - public void setGender(String gender) { - this.gender = gender; - } - - - public String getCity() { - return city; - } - - public void setCity(String city) { - this.city = city; - } - - - public String getCountry() { - return country; - } - - public void setCountry(String country) { - this.country = country; - } - - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getEmail() { return email; } @@ -276,52 +100,6 @@ public class User { this.email = email; } - - public String getApikey() { - return apikey; - } - - public void setApikey(String apikey) { - this.apikey = apikey; - } - - - public String getTeam() { - return team; - } - - public void setTeam(String team) { - this.team = team; - } - - - public Boolean getIsActive() { - return isActive; - } - - public void setIsActive(Boolean isActive) { - this.isActive = isActive; - } - - - public List<String> getLanguages() { - return languages; - } - - public void setLanguages(List<String> languages) { - this.languages = languages; - } - - - public List<String> getRoles() { - return roles; - } - - public void setRoles(List<String> roles) { - this.roles = roles; - } - - public Boolean getAgreeNDA() { return agreeNDA; } diff --git a/src/main/java/eu/hbp/mip/model/UserInfo.java b/src/main/java/eu/hbp/mip/model/UserInfo.java index 71eee3573b485d690c810b42c1344cc2170fb9f5..7bd9a97f3a8f16847410f73afc924fd05ff905cd 100644 --- a/src/main/java/eu/hbp/mip/model/UserInfo.java +++ b/src/main/java/eu/hbp/mip/model/UserInfo.java @@ -53,7 +53,6 @@ public class UserInfo { user.setUsername("anonymous"); user.setFullname("anonymous"); user.setEmail("anonymous@anonymous.com"); - user.setPicture("images/users/default_user.png"); } else { user = new User(getUserInfos()); } diff --git a/src/main/java/eu/hbp/mip/model/Value.java b/src/main/java/eu/hbp/mip/model/Value.java deleted file mode 100644 index 784452e591ffc8eb1f62ff5cc0c7f82694c78a3c..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/model/Value.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Created by mirco on 04.12.15. - */ - -package eu.hbp.mip.model; - -import com.fasterxml.jackson.annotation.JsonInclude; -import io.swagger.annotations.ApiModel; - -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; - -@Entity -@Table(name = "`value`") -@ApiModel -@JsonInclude(JsonInclude.Include.NON_NULL) -public class Value { - - @Id - private String code = null; - - private String label = null; - - - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = code; - } - - - public String getLabel() { - return label; - } - - public void setLabel(String label) { - this.label = label; - } - -} diff --git a/src/main/java/eu/hbp/mip/model/Variable.java b/src/main/java/eu/hbp/mip/model/Variable.java deleted file mode 100644 index 596be8d775673af068235dabed39323a4440de49..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/model/Variable.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Created by mirco on 04.12.15. - */ - -package eu.hbp.mip.model; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.google.gson.annotations.Expose; -import io.swagger.annotations.ApiModel; - -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; -import java.util.List; - -@Entity -@Table(name = "`variable`") -@ApiModel -@JsonIgnoreProperties(value = {"queries"}) -@JsonInclude(JsonInclude.Include.NON_NULL) -public class Variable { - - @Id - @Expose - private String code = null; - - /** - * Empty constructor is needed by Hibernate - */ - public Variable() { - } - - public Variable(String code) { - this.code = code; - } - - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = code; - } - - public static String stringFromVariables(List<Variable> variables, String operator) { - StringBuilder sb = new StringBuilder(); - int i = 0; - for (Variable s : variables) { - i++; - sb.append(s.getCode()); - if (i < variables.size()) { - sb.append(operator); - } - } - return sb.toString(); - } - -} diff --git a/src/main/java/eu/hbp/mip/model/galaxy/WorkflowDTO.java b/src/main/java/eu/hbp/mip/model/galaxy/WorkflowDTO.java index fa0cb4b336ef2f8276db39c1a03097f51a5c8ea7..4b01b38198d52e25d0f9455f770a9a5918d77874 100644 --- a/src/main/java/eu/hbp/mip/model/galaxy/WorkflowDTO.java +++ b/src/main/java/eu/hbp/mip/model/galaxy/WorkflowDTO.java @@ -2,7 +2,7 @@ package eu.hbp.mip.model.galaxy; import com.google.gson.Gson; import com.google.gson.annotations.SerializedName; -import eu.hbp.mip.model.AlgorithmDTO; +import eu.hbp.mip.model.DTOs.AlgorithmDTO; import java.util.*; diff --git a/src/main/java/eu/hbp/mip/repositories/ConfigRepository.java b/src/main/java/eu/hbp/mip/repositories/ConfigRepository.java deleted file mode 100644 index deb286150c0ba9c7833b4dfa66a61d3890c011c4..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/repositories/ConfigRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package eu.hbp.mip.repositories; - -import eu.hbp.mip.model.Config; -import org.springframework.data.repository.CrudRepository; - -/** - * Created by mirco on 11.07.16. - */ - -public interface ConfigRepository extends CrudRepository<Config, Long> { -} diff --git a/src/main/java/eu/hbp/mip/repositories/DatasetRepository.java b/src/main/java/eu/hbp/mip/repositories/DatasetRepository.java deleted file mode 100644 index 7c6da1e9745b8a144296d51a5d85434111213b96..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/repositories/DatasetRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package eu.hbp.mip.repositories; - -import eu.hbp.mip.model.Dataset; -import org.springframework.data.repository.CrudRepository; - -/** - * Created by mirco on 11.07.16. - */ - -public interface DatasetRepository extends CrudRepository<Dataset, String> { -} diff --git a/src/main/java/eu/hbp/mip/repositories/ExperimentRepository.java b/src/main/java/eu/hbp/mip/repositories/ExperimentRepository.java index 2f6ae857d49018c5d9a3cb429f17cf3d2b80d313..eb1d254156e38529de29335a7f3cdfdd49cd7686 100644 --- a/src/main/java/eu/hbp/mip/repositories/ExperimentRepository.java +++ b/src/main/java/eu/hbp/mip/repositories/ExperimentRepository.java @@ -1,6 +1,6 @@ package eu.hbp.mip.repositories; -import eu.hbp.mip.model.Experiment; +import eu.hbp.mip.model.DAOs.ExperimentDAO; import eu.hbp.mip.model.User; import org.springframework.data.repository.CrudRepository; @@ -10,8 +10,8 @@ import java.util.UUID; * Created by mirco on 11.07.16. */ -public interface ExperimentRepository extends CrudRepository<Experiment, UUID> { - Iterable<Experiment> findByCreatedBy(User user); +public interface ExperimentRepository extends CrudRepository<ExperimentDAO, UUID> { + Iterable<ExperimentDAO> findByCreatedBy(User user); - Iterable<Experiment> findByShared(Boolean shared); + Iterable<ExperimentDAO> findByShared(Boolean shared); } diff --git a/src/main/java/eu/hbp/mip/repositories/ModelRepository.java b/src/main/java/eu/hbp/mip/repositories/ModelRepository.java deleted file mode 100644 index c25b1e5bb6c4a5e888851b08ae3f0a801d5a4553..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/repositories/ModelRepository.java +++ /dev/null @@ -1,17 +0,0 @@ -package eu.hbp.mip.repositories; - -import eu.hbp.mip.model.Model; -import eu.hbp.mip.model.User; -import org.springframework.data.repository.CrudRepository; - -/** - * Created by mirco on 11.07.16. - */ - -public interface ModelRepository extends CrudRepository<Model, String> { - Long countByTitle(String Title); - - Iterable<Model> findByCreatedByOrderByCreatedAt(User user); - - Iterable<Model> findByValidOrCreatedByOrderByCreatedAt(Boolean valid, User user); -} diff --git a/src/main/java/eu/hbp/mip/repositories/QueryRepository.java b/src/main/java/eu/hbp/mip/repositories/QueryRepository.java deleted file mode 100644 index f0b95d39c4c3a480fe2bec20ce75e46361889e71..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/repositories/QueryRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package eu.hbp.mip.repositories; - -import eu.hbp.mip.model.Query; -import org.springframework.data.repository.CrudRepository; - -/** - * Created by mirco on 11.07.16. - */ - -public interface QueryRepository extends CrudRepository<Query, Long> { -} diff --git a/src/main/java/eu/hbp/mip/repositories/TagRepository.java b/src/main/java/eu/hbp/mip/repositories/TagRepository.java deleted file mode 100644 index 4edb4090e0336ac034daf245d5ca3bb084a6f5ae..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/repositories/TagRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package eu.hbp.mip.repositories; - -import eu.hbp.mip.model.Tag; -import org.springframework.data.repository.CrudRepository; - -/** - * Created by mirco on 11.07.16. - */ - -public interface TagRepository extends CrudRepository<Tag, String> { -} diff --git a/src/main/java/eu/hbp/mip/repositories/ValueRepository.java b/src/main/java/eu/hbp/mip/repositories/ValueRepository.java deleted file mode 100644 index b41574d6082542ec1539f8e14203e61eb27e0614..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/repositories/ValueRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package eu.hbp.mip.repositories; - -import eu.hbp.mip.model.Value; -import org.springframework.data.repository.CrudRepository; - -/** - * Created by mirco on 11.07.16. - */ - -public interface ValueRepository extends CrudRepository<Value, String> { -} diff --git a/src/main/java/eu/hbp/mip/repositories/VariableRepository.java b/src/main/java/eu/hbp/mip/repositories/VariableRepository.java deleted file mode 100644 index 3ded949436f345143bb9e60a51bc0ee2ab20019b..0000000000000000000000000000000000000000 --- a/src/main/java/eu/hbp/mip/repositories/VariableRepository.java +++ /dev/null @@ -1,10 +0,0 @@ -package eu.hbp.mip.repositories; - -import eu.hbp.mip.model.Variable; -import org.springframework.data.repository.CrudRepository; - -/** - * Created by mirco on 13.09.16. - */ -public interface VariableRepository extends CrudRepository<Variable, String> { -} diff --git a/src/main/java/eu/hbp/mip/services/ExperimentService.java b/src/main/java/eu/hbp/mip/services/ExperimentService.java new file mode 100644 index 0000000000000000000000000000000000000000..bd0bed3596c6de60154cacd291e6d34e9a4a74c1 --- /dev/null +++ b/src/main/java/eu/hbp/mip/services/ExperimentService.java @@ -0,0 +1,776 @@ +package eu.hbp.mip.services; + +import com.github.jmchilton.blend4j.galaxy.GalaxyInstance; +import com.github.jmchilton.blend4j.galaxy.GalaxyInstanceFactory; +import com.github.jmchilton.blend4j.galaxy.WorkflowsClient; +import com.github.jmchilton.blend4j.galaxy.beans.Workflow; +import com.github.jmchilton.blend4j.galaxy.beans.WorkflowDetails; +import com.github.jmchilton.blend4j.galaxy.beans.WorkflowInputDefinition; +import com.google.common.collect.Lists; +import com.google.gson.*; +import eu.hbp.mip.controllers.galaxy.retrofit.RetroFitGalaxyClients; +import eu.hbp.mip.controllers.galaxy.retrofit.RetrofitClientInstance; +import eu.hbp.mip.model.DAOs.ExperimentDAO; +import eu.hbp.mip.model.DTOs.AlgorithmDTO; +import eu.hbp.mip.model.DTOs.ExperimentDTO; +import eu.hbp.mip.model.User; +import eu.hbp.mip.model.UserInfo; +import eu.hbp.mip.model.galaxy.ErrorResponse; +import eu.hbp.mip.model.galaxy.GalaxyWorkflowResult; +import eu.hbp.mip.model.galaxy.PostWorkflowToGalaxyDtoResponse; +import eu.hbp.mip.repositories.ExperimentRepository; +import eu.hbp.mip.utils.ClaimUtils; +import eu.hbp.mip.utils.HTTPUtil; +import eu.hbp.mip.utils.JsonConverters; +import eu.hbp.mip.utils.Logging; +import org.codehaus.jettison.json.JSONException; +import org.codehaus.jettison.json.JSONObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Service; +import retrofit2.Call; +import retrofit2.Response; + +import java.io.IOException; +import java.util.*; + +import static java.lang.Thread.sleep; + +@Service +public class ExperimentService { + + @Autowired + private UserInfo userInfo; + + private static final Gson gsonOnlyExposed = new GsonBuilder().serializeNulls() + .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").excludeFieldsWithoutExposeAnnotation().create(); + + @Value("#{'${services.exareme.queryExaremeUrl}'}") + private String queryExaremeUrl; + + @Value("#{'${services.workflows.workflowUrl}'}") + private String workflowUrl; + + @Value("#{'${services.workflows.jwtSecret}'}") + private String jwtSecret; + + @Value("#{'${services.galaxy.galaxyUrl}'}") + private String galaxyUrl; + + @Value("#{'${services.galaxy.galaxyApiKey}'}") + private String galaxyApiKey; + + // Enable HBP collab authentication (1) or disable it (0). Default is 1 + @Value("#{'${hbp.authentication.enabled:1}'}") + private boolean authenticationIsEnabled; + + @Autowired + private ExperimentRepository experimentRepository; + + private static final Gson gson = new Gson(); + + public ResponseEntity<String> getExperiment(String uuid, String endpoint) { + + ExperimentDAO experimentDAO; + User user = userInfo.getUser(); + + Logging.LogUserAction(user.getUsername(), endpoint, "Loading Experiment with uuid : " + uuid); + + experimentDAO = loadExperiment(uuid); + + if (experimentDAO == null) { + Logging.LogUserAction(user.getUsername(), endpoint, "Experiment Not found."); + return new ResponseEntity<>("Not found", HttpStatus.NOT_FOUND); + } + + if (!experimentDAO.isShared() && !experimentDAO.getCreatedBy().getUsername().equals(user.getUsername())) { + Logging.LogUserAction(user.getUsername(), endpoint, "Accessing Experiment is unauthorized."); + return new ResponseEntity<>("You don't have access to the experiment.", HttpStatus.UNAUTHORIZED); + } + ExperimentDTO experimentDTO = experimentDAO.convertToDTO(); + Logging.LogUserAction(user.getUsername(), endpoint, "Experiment was Loaded with uuid : " + uuid + "."); + + return new ResponseEntity<>(JsonConverters.convertObjectToJsonString(experimentDTO), HttpStatus.OK); + } + + public ResponseEntity<String> createExperiment(Authentication authentication, ExperimentDTO experimentDTO, String endpoint) { + User user = userInfo.getUser(); + + //Checking if check (POST) /experiments has proper input. + if (checkPostExperimentProperInput(experimentDTO)){ + Logging.LogUserAction(user.getUsername(), endpoint, + "Invalid input."); + return ResponseEntity.badRequest().body("Please provide proper input."); + } + // Get the type and name of algorithm + String algorithmType = experimentDTO.getAlgorithm().getType(); + String algorithmName = experimentDTO.getAlgorithm().getName(); + + StringBuilder parametersLogMessage = new StringBuilder(", Parameters:\n"); + for (AlgorithmDTO.AlgorithmParamDTO params : experimentDTO.getAlgorithm().getParameters()) { + parametersLogMessage + .append(" ") + .append(params.getLabel()) + .append(" -> ") + .append(params.getValue()) + .append("\n"); + } + Logging.LogUserAction(user.getUsername(), endpoint, "Executing " + algorithmName + parametersLogMessage); + + if (authenticationIsEnabled) { + // Getting the dataset from the experiment parameters + String experimentDatasets = null; + for (AlgorithmDTO.AlgorithmParamDTO parameter : experimentDTO.getAlgorithm().getParameters()) { + if (parameter.getLabel().equals("dataset")) { + experimentDatasets = parameter.getValue(); + break; + } + } + + if (experimentDatasets == null || experimentDatasets.equals("")) { + Logging.LogUserAction(user.getUsername(), endpoint, + "A dataset should be specified to run an algorithm."); + return ResponseEntity.badRequest().body("Please provide at least one dataset to run the algorithm."); + } + + // --- Validating proper access rights on the datasets --- + if (!ClaimUtils.userHasDatasetsAuthorization(user.getUsername(), authentication.getAuthorities(), experimentDatasets)) { + return ResponseEntity.badRequest().body("You are not authorized to use these datasets."); + } + } + + // Run with the appropriate engine + if (algorithmType.equals("workflow")) { + Logging.LogUserAction(user.getUsername(), endpoint, "Algorithm runs on Galaxy."); + return runGalaxyWorkflow(experimentDTO, endpoint); + } else { + Logging.LogUserAction(user.getUsername(), endpoint, "Algorithm runs on Exareme."); + return runExaremeAlgorithm(experimentDTO, endpoint); + } + } + + public ResponseEntity<String> getExperiments( String endpoint) { + User user = userInfo.getUser(); + Logging.LogUserAction(user.getUsername(), endpoint, "Listing my experiments."); + + Iterable<ExperimentDAO> myExperiments = experimentRepository.findByCreatedBy(user); + List<ExperimentDAO> expList = Lists.newLinkedList(myExperiments); + Iterable<ExperimentDAO> sharedExperiments = experimentRepository.findByShared(true); + List<ExperimentDAO> sharedExpList = Lists.newLinkedList(sharedExperiments); + expList.addAll(sharedExpList); + Logging.LogUserAction(user.getUsername(), endpoint, "Successfully listed my experiments."); + List<ExperimentDTO> experimentDTOs = new LinkedList<>(); + + for (ExperimentDAO experimentDAO: expList){ + ExperimentDTO experimentDTO = experimentDAO.convertToDTO(); + experimentDTOs.add(experimentDTO); + } + return new ResponseEntity<>(new Gson().toJson(experimentDTOs ), HttpStatus.OK); + } + + public ResponseEntity<String> updateExperiment(String uuid, ExperimentDTO experimentDTO, String endpoint) + { + ExperimentDAO experimentDAO; + User user = userInfo.getUser(); + Logging.LogUserAction(user.getUsername(), endpoint, "Updating experiment with uuid : " + experimentDTO.getUuid() + "."); + //Checking if check (PUT) /experiments has proper input. + if (checkPutExperimentProperInput(experimentDTO)){ + Logging.LogUserAction(user.getUsername(), endpoint, + "Invalid input."); + return ResponseEntity.badRequest().body("Please provide proper input."); + } + + if((experimentDTO.getName() == null || experimentDTO.getName().length() == 0) + && experimentDTO.getShared() == null + && experimentDTO.getViewed() == null + && experimentDTO.getAlgorithm() == null) + { + return ResponseEntity.badRequest().body("Input is required."); + } + + experimentDAO = loadExperiment(uuid); + + if (!experimentDAO.getCreatedBy().getUsername().equals(user.getUsername())) + return new ResponseEntity<>("You're not the owner of this experiment", HttpStatus.UNAUTHORIZED); + + if(experimentDTO.getName() != null && experimentDTO.getName().length() != 0) + { + experimentDAO.setName(experimentDTO.getName()); + } + + if(experimentDTO.getShared() != null) + { + experimentDAO.setShared(experimentDTO.getShared()); + } + + if(experimentDTO.getViewed() != null) + { + experimentDAO.setViewed(experimentDTO.getViewed()); + } + + experimentRepository.save(experimentDAO); + + Logging.LogUserAction(user.getUsername(), endpoint, "Updated experiment with uuid : " + experimentDTO.getUuid() + "."); + + experimentDTO = experimentDAO.convertToDTO(); + return new ResponseEntity<>(JsonConverters.convertObjectToJsonString(experimentDTO), HttpStatus.OK); + } + + public ResponseEntity<String> deleteExperiment(String uuid, String endpoint) + { + ExperimentDAO experimentDAO; + User user = userInfo.getUser(); + Logging.LogUserAction(user.getUsername(), endpoint, "Deleting experiment with uuid : " + uuid + "."); + + experimentDAO = loadExperiment(uuid); + + if (!experimentDAO.getCreatedBy().getUsername().equals(user.getUsername())) + return new ResponseEntity<>(endpoint, HttpStatus.UNAUTHORIZED); + + experimentRepository.delete(experimentDAO); + + Logging.LogUserAction(user.getUsername(), endpoint, "Deleted experiment with uuid : " + uuid + "."); + return new ResponseEntity<>(HttpStatus.OK); + } + + // /* ------------------------------- PRIVATE METHODS ----------------------------------------------------*/ + private boolean checkPostExperimentProperInput(ExperimentDTO experimentDTO) + { + return experimentDTO.getShared() != null + || experimentDTO.getViewed() != null + || experimentDTO.getCreated() != null + || experimentDTO.getCreatedBy() != null + || experimentDTO.getResult() != null + || experimentDTO.getStatus() != null + || experimentDTO.getUuid() != null; + } + + private boolean checkPutExperimentProperInput(ExperimentDTO experimentDTO) + { + return experimentDTO.getUuid() != null + || experimentDTO.getCreated() != null + || experimentDTO.getResult() != null + || experimentDTO.getStatus() != null; + } + + private ExperimentDAO loadExperiment(String uuid){ + + UUID experimentUuid ; + + try { + experimentUuid = UUID.fromString(uuid); + } catch (IllegalArgumentException iae) { + return null; + } + + return experimentRepository.findOne(experimentUuid); + } + + private ExperimentDAO createExperiment(ExperimentDTO experimentDTO, String endpoint) { + User user = userInfo.getUser(); + + ExperimentDAO experimentDAO = new ExperimentDAO(); + experimentDAO.setUuid(UUID.randomUUID()); + experimentDAO.setCreatedBy(user); + experimentDAO.setAlgorithm(JsonConverters.convertObjectToJsonString(experimentDTO.getAlgorithm())); + experimentDAO.setName(experimentDTO.getName()); + experimentRepository.save(experimentDAO); + + Logging.LogUserAction(user.getUsername(), endpoint, " id : " + experimentDAO.getUuid()); + Logging.LogUserAction(user.getUsername(), endpoint, " algorithms : " + experimentDAO.getAlgorithm()); + Logging.LogUserAction(user.getUsername(), endpoint, " name : " + experimentDAO.getName()); + return experimentDAO; + } + + private void saveExperiment(ExperimentDAO experimentDAO, String endpoint) { + User user = userInfo.getUser(); + + Logging.LogUserAction(user.getUsername(), endpoint, " id : " + experimentDAO.getUuid()); + Logging.LogUserAction(user.getUsername(), endpoint, " algorithms : " + experimentDAO.getAlgorithm()); + Logging.LogUserAction(user.getUsername(), endpoint, " name : " + experimentDAO.getName()); + Logging.LogUserAction(user.getUsername(), endpoint, " historyId : " + experimentDAO.getWorkflowHistoryId()); + Logging.LogUserAction(user.getUsername(), endpoint, " status : " + experimentDAO.getStatus()); + + experimentRepository.save(experimentDAO); + + Logging.LogUserAction(user.getUsername(), endpoint, "Saved experiment"); + } + + private void finishExperiment(ExperimentDAO experimentDAO) { + experimentDAO.setFinished(new Date()); + experimentRepository.save(experimentDAO); + } + + /* -------------------------------------- EXAREME CALLS ---------------------------------------------------------*/ + + /** + * The runExaremeExperiment will POST the algorithm to the exareme client + * + * @param experimentDTO is the request with the experiment information + * @return the response to be returned + */ + public ResponseEntity<String> runExaremeAlgorithm(ExperimentDTO experimentDTO, String endpoint) { + User user = userInfo.getUser(); + Logging.LogUserAction(user.getUsername(), endpoint, "Running the algorithm..."); + + ExperimentDAO experimentDAO = createExperiment(experimentDTO, endpoint); + Logging.LogUserAction(user.getUsername(), endpoint, "Created experiment with uuid :" + experimentDAO.getUuid()); + + // Run the 1st algorithm from the list + String algorithmName = experimentDTO.getAlgorithm().getName(); + + // Get the parameters + List<AlgorithmDTO.AlgorithmParamDTO> algorithmParameters + = experimentDTO.getAlgorithm().getParameters(); + + String body = gson.toJson(algorithmParameters); + String url = queryExaremeUrl + "/" + algorithmName; + Logging.LogUserAction(user.getUsername(), endpoint, "url: " + url + ", body: " + body); + + Logging.LogUserAction(user.getUsername(), endpoint, + "Completed, returning: " + experimentDTO.toString()); + + Logging.LogUserAction(user.getUsername(), endpoint, + "Starting exareme execution thread"); + new Thread(() -> { + // ATTENTION: Inside the Thread only LogExperimentAction should be used, not LogUserAction! + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "Thread named :" + Thread.currentThread().getName() + " with id :" + Thread.currentThread().getId() + " started!"); + + try { + StringBuilder results = new StringBuilder(); + int code = HTTPUtil.sendPost(url, body, results); + + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "Algorithm finished with code: " + code); + + // Results are stored in the experiment object + ExperimentDTO.ResultDTO resultDTO = JsonConverters.convertJsonStringToObject(String.valueOf(results), ExperimentDTO.ResultDTO.class); + + experimentDAO.setResult(JsonConverters.convertObjectToJsonString(resultDTO)); +// experimentDAO.setHasError(code >= 400); +// experimentDAO.setHasServerError(code >= 500); + } catch (Exception e) { + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "There was an exception: " + e.getMessage()); + + experimentDAO.setStatus(ExperimentDAO.Status.error); + experimentDAO.setResult(e.getMessage()); + } + + finishExperiment(experimentDAO); + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "Finished the experiment: " + experimentDAO.toString()); + }).start(); + experimentDTO = experimentDAO.convertToDTO(); + return new ResponseEntity<>(JsonConverters.convertObjectToJsonString(experimentDTO), HttpStatus.OK); + } + + /* --------------------------------------- GALAXY CALLS ---------------------------------------------------------*/ + + + /** + * The runWorkflow will POST the algorithm to the galaxy client + * + * @param experimentDTO is the request with the experiment information + * @return the response to be returned + */ + public ResponseEntity<String> runGalaxyWorkflow(ExperimentDTO experimentDTO, String endpoint) { + User user = userInfo.getUser(); + Logging.LogUserAction(user.getUsername(), endpoint, "Running a workflow..."); + + ExperimentDAO experimentDAO = createExperiment(experimentDTO, endpoint); + Logging.LogUserAction(user.getUsername(), endpoint, "Created experiment with uuid :" + experimentDAO.getUuid()); + + + // Run the 1st algorithm from the list + String workflowId = experimentDTO.getAlgorithm().getName(); + + // Get the parameters + List<AlgorithmDTO.AlgorithmParamDTO> algorithmParameters + = experimentDTO.getAlgorithm().getParameters(); + + // Convert the parameters to workflow parameters + HashMap<String, String> algorithmParamsIncludingEmpty = new HashMap<>(); + if (algorithmParameters != null) { + for (AlgorithmDTO.AlgorithmParamDTO param : algorithmParameters) { + algorithmParamsIncludingEmpty.put(param.getName(), param.getValue()); + } + } + + // Get all the algorithm parameters because the frontend provides only the non-null + final GalaxyInstance instance = GalaxyInstanceFactory.get(galaxyUrl, galaxyApiKey); + final WorkflowsClient workflowsClient = instance.getWorkflowsClient(); + Workflow workflow = null; + for (Workflow curWorkflow : workflowsClient.getWorkflows()) { + if (curWorkflow.getId().equals(workflowId)) { + workflow = curWorkflow; + break; + } + } + if (workflow == null) { + Logging.LogUserAction(user.getUsername(), endpoint, + "Could not find algorithm code: " + workflowId); + return ResponseEntity.badRequest() + .body(new ErrorResponse("Could not find galaxy algorithm.").toString()); + } + final WorkflowDetails workflowDetails = workflowsClient.showWorkflow(workflow.getId()); + for (Map.Entry<String, WorkflowInputDefinition> workflowParameter : workflowDetails.getInputs().entrySet()) { + if (!(algorithmParamsIncludingEmpty.containsKey(workflowParameter.getValue().getUuid()))) { + algorithmParamsIncludingEmpty.put(workflowParameter.getValue().getUuid(), ""); + } + } + + // Create the body of the request + HashMap<String, HashMap<String, String>> requestBody = new HashMap<>(); + requestBody.put("inputs", algorithmParamsIncludingEmpty); + JsonObject requestBodyJson = new JsonParser().parse(gson.toJson(requestBody)).getAsJsonObject(); + + // Create the request client + RetroFitGalaxyClients service = RetrofitClientInstance.getRetrofitInstance().create(RetroFitGalaxyClients.class); + Logging.LogUserAction(user.getUsername(), endpoint, "Running Galaxy workflow with id: " + workflow.getId()); + + // Call Galaxy to run the workflow + Call<PostWorkflowToGalaxyDtoResponse> call = service.postWorkflowToGalaxy(workflow.getId(), galaxyApiKey, requestBodyJson); + try { + Response<PostWorkflowToGalaxyDtoResponse> response = call.execute(); + + if (response.code() == 200) { // Call succeeded + String responseBody = gson.toJson(response.body()); + Logging.LogUserAction(user.getUsername(), endpoint, "Response: " + responseBody); + + String historyId = (String) new JSONObject(responseBody).get("history_id"); + experimentDAO.setWorkflowHistoryId(historyId); + experimentDAO.setStatus(ExperimentDAO.Status.pending); +// experimentDAO.setHasServerError(response.code() >= 500); + + } else { // Something unexpected happened + String msgErr = gson.toJson(response.errorBody()); + Logging.LogUserAction(user.getUsername(), endpoint, "Error Response: " + msgErr); + + // Values are read from streams. + JSONObject jObjectError = new JSONObject(msgErr); + String errMsg = jObjectError.get("err_msg").toString(); + + experimentDAO.setResult(errMsg); +// experimentDAO.setHasError(response.code() >= 400); +// experimentDAO.setHasServerError(response.code() >= 500); + } + + } catch (Exception e) { + Logging.LogUserAction(user.getUsername(), endpoint, "An exception occurred: " + e.getMessage()); + experimentDAO.setStatus(ExperimentDAO.Status.error); + experimentDAO.setResult(e.getMessage()); + } + saveExperiment(experimentDAO, endpoint); + + // Start the process of fetching the status + updateWorkflowExperiment(experimentDAO, endpoint); + + Logging.LogUserAction(user.getUsername(), endpoint, "Run workflow completed!"); + + experimentDTO = experimentDAO.convertToDTO(); + return new ResponseEntity<>(JsonConverters.convertObjectToJsonString(experimentDTO), HttpStatus.OK); + } + + + /** + * This method creates a thread that will fetch the workflow result when it is ready + * + * @param experimentDAO will be used to fetch it's workflow status, it should have the workflowHistoryId initialized + * and the result should not already be fetched + * @return nothing, just updates the experiment + */ + public void updateWorkflowExperiment(ExperimentDAO experimentDAO, String endpoint) { + User user = userInfo.getUser(); + + if (experimentDAO == null) { + Logging.LogUserAction(user.getUsername(), endpoint, "The experiment does not exist."); + return; + } + + Logging.LogUserAction(user.getUsername(), endpoint, + " Experiment id : " + experimentDAO.getUuid()); + if (experimentDAO.getWorkflowHistoryId() == null) { + Logging.LogUserAction(user.getUsername(), endpoint, "History Id does not exist."); + return; + } + + Logging.LogUserAction(user.getUsername(), endpoint, "Starting Thread..."); + new Thread(() -> { + while (true) { + // ATTENTION: Inside the Thread only LogExperimentAction should be used, not LogExperimentAction! + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "Thread is running..."); + + try { + sleep(2000); + } catch (InterruptedException e) { + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "Sleep was disrupted: " + e.getMessage()); + } + + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "Fetching status for experiment Id: " + experimentDAO.getUuid()); + + String state = getWorkflowStatus(experimentDAO); + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "State is: " + state); + + switch (state) { + case "running": + // Do nothing, when the experiment is created the status is set to running + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "Workflow is still running."); + break; + + case "completed": + // Get only the job result that is visible + List<GalaxyWorkflowResult> workflowJobsResults = getWorkflowResults(experimentDAO); + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "Results are: " + workflowJobsResults.toString()); + + boolean resultFound = false; + for (GalaxyWorkflowResult jobResult : workflowJobsResults) { + if (jobResult.getVisible()) { + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "Visible result are: " + jobResult.getId()); + + String result = getWorkflowResultBody(experimentDAO, jobResult.getId()); + + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "ResultDTO: " + result); + if (result == null) { + experimentDAO.setStatus(ExperimentDAO.Status.error); + } + experimentDAO.setResult("[" + result + "]"); + experimentDAO.setStatus(ExperimentDAO.Status.success); + resultFound = true; + } + } + + if (!resultFound) { // If there is no visible result + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "No visible result"); + experimentDAO.setResult("[" + new ErrorResponse("The workflow has no visible result.").toString() + "]"); + experimentDAO.setStatus(ExperimentDAO.Status.error); + } + + finishExperiment(experimentDAO); + break; + + case "error": + // Get the job result that failed + workflowJobsResults = getWorkflowResults(experimentDAO); + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "Error results are: " + workflowJobsResults.toString()); + + boolean failedJobFound = false; + for (GalaxyWorkflowResult jobResult : workflowJobsResults) { + if (jobResult.getState().equals("error")) { + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "Failed job is: " + jobResult.getId()); + + String result = getWorkflowJobError(jobResult.getId(), experimentDAO); + + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "Job result: " + result); + if (result == null) { + experimentDAO.setStatus(ExperimentDAO.Status.error); + } + experimentDAO.setResult("[" + result + "]"); + experimentDAO.setStatus(ExperimentDAO.Status.error); + failedJobFound = true; + } + } + + if (!failedJobFound) { // If there is no visible failed job + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "No failed result"); + experimentDAO.setResult("[" + new ErrorResponse("The workflow has no failed result.").toString() + "]"); + experimentDAO.setStatus(ExperimentDAO.Status.error); + } + finishExperiment(experimentDAO); + break; + + default: // InternalError or unexpected result + experimentDAO.setResult("[" + new ErrorResponse("An unexpected error occurred.").toString() + "]"); + experimentDAO.setStatus(ExperimentDAO.Status.error); + finishExperiment(experimentDAO); + break; + } + + // If result exists return + if (experimentDAO.getResult() != null) { + Logging.LogExperimentAction(experimentDAO.getName(), experimentDAO.getUuid(), "ResultDTO exists: " + experimentDAO.getResult()); + return; + } + } + }).start(); + } + + + /** + * @param experimentDAO The experiment of the workflow + * @return "running" -> When the workflow is still running + * "internalError" -> When an exception or a bad request occurred + * "error" -> When the workflow produced an error + * "completed" -> When the workflow completed successfully + */ + public String getWorkflowStatus(ExperimentDAO experimentDAO) { + String historyId = experimentDAO.getWorkflowHistoryId(); + String experimentName = experimentDAO.getName(); + UUID experimentId = experimentDAO.getUuid(); + + // ATTENTION: This function is used from a Thread. Only LogExperimentAction should be used, not LogUserAction! + Logging.LogExperimentAction(experimentName, experimentId, " History Id : " + historyId); + + // Create the request client + RetroFitGalaxyClients service = RetrofitClientInstance.getRetrofitInstance().create(RetroFitGalaxyClients.class); + Call<Object> call = service.getWorkflowStatusFromGalaxy(historyId, galaxyApiKey); + + String result = null; + try { + Response<Object> response = call.execute(); + if (response.code() >= 400) { + Logging.LogExperimentAction(experimentName, experimentId, " Response code: " + + response.code() + "" + " with body: " + (response.errorBody() != null ? response.errorBody().string() : " ")); + return "internalError"; + } + result = new Gson().toJson(response.body()); + Logging.LogExperimentAction(experimentName, experimentId, " ResultDTO: " + result); + + } catch (IOException e) { + Logging.LogExperimentAction(experimentName, experimentId, " An exception happened: " + e.getMessage()); + return "internalError"; + } + + String state = null; + try { + JSONObject resultJson = new JSONObject(result); + state = resultJson.getString("state"); + } catch (JSONException e) { + Logging.LogExperimentAction(experimentName, experimentId, " An exception happened: " + e.getMessage()); + return "internalError"; + } + + Logging.LogExperimentAction(experimentName, experimentId, " Completed!"); + switch (state) { + case "ok": + return "completed"; + case "error": + return "error"; + case "running": + case "new": + case "waiting": + case "queued": + return "running"; + default: + return "internalError"; + } + } + + /** + * @param experimentDAO The experiment of the workflow + * @return a List<GalaxyWorkflowResult> or null when an error occurred + */ + public List<GalaxyWorkflowResult> getWorkflowResults(ExperimentDAO experimentDAO) { + + String historyId = experimentDAO.getWorkflowHistoryId(); + String experimentName = experimentDAO.getName(); + UUID experimentId = experimentDAO.getUuid(); + Logging.LogExperimentAction(experimentName, experimentId, " historyId : " + historyId); + + RetroFitGalaxyClients service = RetrofitClientInstance.getRetrofitInstance().create(RetroFitGalaxyClients.class); + Call<List<GalaxyWorkflowResult>> call = service.getWorkflowResultsFromGalaxy(historyId, galaxyApiKey); + + List<GalaxyWorkflowResult> getGalaxyWorkflowResultList = null; + try { + Response<List<GalaxyWorkflowResult>> response = call.execute(); + if (response.code() >= 400) { + Logging.LogExperimentAction(experimentName, experimentId, " Response code: " + + response.code() + "" + " with body: " + (response.errorBody() != null ? response.errorBody().string() : " ")); + return null; + } + getGalaxyWorkflowResultList = response.body(); + Logging.LogExperimentAction(experimentName, experimentId, " ResultDTO: " + response.body()); + + } catch (IOException e) { + Logging.LogExperimentAction(experimentName, experimentId, " An exception happened: " + e.getMessage()); + return null; + } + + Logging.LogExperimentAction(experimentName, experimentId, " Completed!"); + return getGalaxyWorkflowResultList; + + } + + /** + * @param experimentDAO The experiment of the workflow + * @param contentId the id of the job result that we want + * @return the result of the specific workflow job, null if there was an error + */ + public String getWorkflowResultBody(ExperimentDAO experimentDAO, String contentId) { + + String historyId = experimentDAO.getWorkflowHistoryId(); + String experimentName = experimentDAO.getName(); + UUID experimentId = experimentDAO.getUuid(); + + Logging.LogExperimentAction(experimentName, experimentId, " historyId : " + historyId); + + RetroFitGalaxyClients service = RetrofitClientInstance.getRetrofitInstance().create(RetroFitGalaxyClients.class); + Call<Object> call = + service.getWorkflowResultsBodyFromGalaxy(historyId, contentId, galaxyApiKey); + + String resultJson = null; + try { + Response<Object> response = call.execute(); + if (response.code() >= 400) { + Logging.LogExperimentAction(experimentName, experimentId, " Response code: " + + response.code() + "" + " with body: " + (response.errorBody() != null ? response.errorBody().string() : " ")); + return null; + } + resultJson = new Gson().toJson(response.body()); + Logging.LogExperimentAction(experimentName, experimentId, " ResultDTO: " + resultJson); + + } catch (IOException e) { + Logging.LogExperimentAction(experimentName, experimentId, + " An exception happened: " + e.getMessage()); + return null; + } + + Logging.LogExperimentAction(experimentName, experimentId, " Completed!"); + return resultJson; + } + + + /** + * @param jobId the id of the workflow job that failed + * @return the error that was produced or null if an error occurred + */ + public String getWorkflowJobError(String jobId, ExperimentDAO experimentDAO) { + String experimentName = experimentDAO.getName(); + UUID experimentId = experimentDAO.getUuid(); + + Logging.LogExperimentAction(experimentName, experimentId, " jobId : " + jobId); + RetroFitGalaxyClients service = RetrofitClientInstance.getRetrofitInstance().create(RetroFitGalaxyClients.class); + Call<Object> callError = service.getErrorMessageOfWorkflowFromGalaxy(jobId, galaxyApiKey); + + String fullError = null; + String returnError = null; + try { + Response<Object> response = callError.execute(); + if (response.code() >= 400) { + Logging.LogExperimentAction(experimentName, experimentId, "Response code: " + + response.code() + " with body: " + (response.errorBody() != null ? response.errorBody().string() : " ")); + return null; + } + + // Parsing the stderr of the job that failed + String jsonString = new Gson().toJson(response.body()); + JsonElement jsonElement = new JsonParser().parse(jsonString); + JsonObject rootObject = jsonElement.getAsJsonObject(); + fullError = rootObject.get("stderr").getAsString(); + Logging.LogExperimentAction(experimentName, experimentId, "Error: " + fullError); + + String[] arrOfStr = fullError.split("ValueError", 0); + String specError = arrOfStr[arrOfStr.length - 1]; + returnError = specError.substring(1); + Logging.LogExperimentAction(experimentName, experimentId, "Parsed Error: " + returnError); + + } catch (IOException e) { + Logging.LogExperimentAction(experimentName, experimentId, "Exception: " + e.getMessage()); + return null; + } + + Logging.LogExperimentAction(experimentName, experimentId, "Completed successfully!"); + + return returnError; + } +} diff --git a/src/main/java/eu/hbp/mip/utils/ClaimUtils.java b/src/main/java/eu/hbp/mip/utils/ClaimUtils.java index e71d3f19083d03ae0067176b2fccfe4f06175915..5872819bc883efd98a453e363a46ad0a1cb537cd 100644 --- a/src/main/java/eu/hbp/mip/utils/ClaimUtils.java +++ b/src/main/java/eu/hbp/mip/utils/ClaimUtils.java @@ -1,7 +1,7 @@ package eu.hbp.mip.utils; import com.google.gson.Gson; -import eu.hbp.mip.model.PathologyDTO; +import eu.hbp.mip.model.DTOs.PathologyDTO; import org.springframework.security.core.GrantedAuthority; import java.util.ArrayList; diff --git a/src/main/java/eu/hbp/mip/utils/JsonConverters.java b/src/main/java/eu/hbp/mip/utils/JsonConverters.java new file mode 100644 index 0000000000000000000000000000000000000000..52305124aff3c2078d6c87ba95c1b4235782da5a --- /dev/null +++ b/src/main/java/eu/hbp/mip/utils/JsonConverters.java @@ -0,0 +1,27 @@ +package eu.hbp.mip.utils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; + +import java.lang.reflect.Type; + +public class JsonConverters { + Gson gson = new Gson(); + + public static String convertObjectToJsonString(Object object) { + ObjectMapper mapper = new ObjectMapper(); + //Converting the Object to JSONString + try { + return mapper.writeValueAsString(object); + } catch (JsonProcessingException e) { + return e.getMessage(); + } + } + + public static <T> T convertJsonStringToObject(String jsonString, Type typeOfT) { + if(jsonString == null || jsonString.isEmpty()) + return null; + return new Gson().fromJson(jsonString, typeOfT); + } +} diff --git a/src/main/resources/db/migration/V7_0__NewExperimentStructure.sql b/src/main/resources/db/migration/V7_0__NewExperimentStructure.sql new file mode 100644 index 0000000000000000000000000000000000000000..b69ae2b702742e36025e99ba04ebde23610c10f4 --- /dev/null +++ b/src/main/resources/db/migration/V7_0__NewExperimentStructure.sql @@ -0,0 +1,45 @@ +ALTER TABLE experiment +DROP COLUMN haserror, +DROP COLUMN hasservererror, +DROP COLUMN validations, +DROP COLUMN model_slug; + +ALTER TABLE experiment +RENAME algorithms TO algorithm; +ALTER TABLE experiment +RENAME createdby_username TO created_by_username; +ALTER TABLE experiment +RENAME workflowhistoryid TO workflow_history_id; +ALTER TABLE experiment +RENAME resultsviewed TO viewed; +ALTER TABLE experiment +RENAME workflowstatus TO status; + +ALTER TABLE "user" +DROP COLUMN birthday, +DROP COLUMN city, +DROP COLUMN country, +DROP COLUMN firstname, +DROP COLUMN gender, +DROP COLUMN isactive, +DROP COLUMN lastname, +DROP COLUMN password, +DROP COLUMN phone, +DROP COLUMN picture, +DROP COLUMN team, +DROP COLUMN web; + +DROP TABLE "config_title", "config_yaxisvariables"; +DROP TABLE "dataset_variable", "dataset_grouping", "dataset_data", "dataset_header"; +DROP TABLE "query_variable", "query_grouping", "query_filter", "query_covariable"; +DROP TABLE "article_tag", "tag"; +DROP TABLE "variable_value"; +DROP TABLE "query_training_datasets", "query_validation_datasets", "query_testing_datasets"; +DROP TABLE "variable", "value"; +DROP TABLE "group_group", "group"; +DROP TABLE "model"; +DROP TABLE "query"; +DROP TABLE "dataset"; +DROP TABLE "config"; +DROP TABLE "vote", "app"; +DROP TABLE "user_roles", "user_languages"; diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 0726d82f41564d850f92eaa301010d39ea89b068..eb9ace1a6b2ae5675fdd27e00077b4844873eaa4 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -10,8 +10,7 @@ <logger name="eu.hbp.mip"> <appender-ref ref="FILE1" /> </logger> - <include resource="org/springframework/boot/logging/logback/base.xml"/> - <logger name="org.springframework.web" level="ERROR"> + <logger name="org.springframework.web" level="INFO"> <appender-ref ref="FILE1" /> </logger> <logger name="eu.hbp.mip.utils" level="INFO" additivity="false">