Skip to content

Commit

Permalink
Optimize asynchorous queries
Browse files Browse the repository at this point in the history
  • Loading branch information
mnloures committed Jan 23, 2020
1 parent c458fd5 commit 3dd3386
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 37 deletions.
27 changes: 26 additions & 1 deletion src/main/java/org/strep/repositories/DatasetRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.strep.domain.License;

/**
* Interface that extends CrudRepository implementation of basic CRUD operations
Expand Down Expand Up @@ -89,7 +90,7 @@ public interface DatasetRepository extends CrudRepository<Dataset, String> {
* Return all system datasets owned by author and not private owned by anyone
*
* @param username the username of the author of the dataset
* @param type the type of the dataset: userdataset or systemdataset
* @param type the type of the dataset: Dataset.TYPE_USER or Dataset.TYPE_SYSTEM
* @return A list datasets owned by author and not private owned by anyone
*/
@Query(value = "SELECT * FROM dataset WHERE type=?2 AND available=true AND (author=?1 OR access<>'private')", nativeQuery = true)
Expand Down Expand Up @@ -172,4 +173,28 @@ public interface DatasetRepository extends CrudRepository<Dataset, String> {
@Query(value = "SELECT * FROM dataset WHERE name=?1", nativeQuery = true)
public Dataset findDatasetByName(String name);

/**
* Check if combining a list of datasets requires attribution
* @param datasetNames The datasets to be combined
* @return true if citation is required for combine the selected datasets
*/
@Query(value="SELECT SUM(IF(attribute_required,1,0)) FROM license,dataset WHERE license.name=dataset.id AND dataset.name IN (?1)", nativeQuery=true)
public int checkIfCombinationRequiresAttribution(Collection<String> datasetNames);

/**
* Combine citations of some datasets into one single string
* @param datasetNames The datasets to combine citations
* @return The String concatenation of datasets
*/
@Query(value="SELECT GROUP_CONCAT(IF(citation_request!=null,CONCAT(citation_request,'\\r\\n'),'') SEPARATOR '') FROM dataset WHERE dataset.name IN (?1)", nativeQuery=true)
public String combineAllCitations4Datasets(Collection<String> datasetNames);

/**
* Check if redistribution of combination of some datasets (as parameter) is not allowed
* @param datasetNames The datasets that is going to be combined
* @return true if redistribution is not allowed
*/
@Query(value="SELECT SUM(IF(redistribute,0,1)) FROM license,dataset WHERE license.name=dataset.id AND dataset.name IN (?1)", nativeQuery=true)
public int checkIfRedistributionIsNotAllowed(Collection<String> datasetNames);

}
11 changes: 11 additions & 0 deletions src/main/java/org/strep/repositories/LicenseRepository.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.strep.repositories;

import java.util.ArrayList;
import java.util.Collection;
import org.strep.domain.License;

import org.springframework.data.jpa.repository.Query;
Expand All @@ -18,4 +20,13 @@ public interface LicenseRepository extends CrudRepository<License, String>
*/
@Query(value = "SELECT * FROM license WHERE name LIKE %?1%", nativeQuery=true)
public Iterable<License> findByName(String searchInput);

/**
* Select all licenses of a list of dataset
* @param datasetNames The dataset names
* @return The list of licenses of the datasets
*/
@Query(value="SELECT * FROM license, dataset WHERE license.name=dataset.id AND dataset.name in (?1)", nativeQuery=true)
public ArrayList<License> getDatasetLicenses(Collection<String> datasetNames);

}
51 changes: 31 additions & 20 deletions src/main/java/org/strep/web/DatasetController.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import java.lang.Math;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import javax.servlet.http.HttpSession;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
Expand Down Expand Up @@ -566,18 +567,16 @@ public String filterDatasetsByLicense(Authentication authentication, Model model
StringBuilder message = new StringBuilder();
ArrayList<Dataset> allDatasets = new ArrayList<>();
List<String> filteredDatasets = new ArrayList<>();
UserDetails userDetails = (UserDetails) authentication.getPrincipal();

ArrayList<License> checkedDatasetsLicenses = new ArrayList<>();

for (String datasetName : datasets) {
allDatasets.add(datasetRepository.findDatasetByName(datasetName));
}
String username = userDetails.getUsername();

datasetRepository.getSystemDatasets(username,Dataset.TYPE_SYSTEM).forEach(allDatasets::add);

for (String datasetName : checkedDatasets) {
Dataset dataset = datasetRepository.findDatasetByName(datasetName);
filteredDatasets.add(dataset.getName());
checkedDatasetsLicenses.add(dataset.getLicense());
filteredDatasets.add(datasetName);
}
ArrayList<License> checkedDatasetsLicenses=licenseRepository.getDatasetLicenses(filteredDatasets);

int position = 0;
boolean exit = false;
Expand Down Expand Up @@ -634,15 +633,13 @@ public String composeCitationRequest(Authentication authentication, Model model,
@RequestParam(name = "checkedDatasets", required = true) String[] checkedDatasets) {

StringBuilder message = new StringBuilder();
ArrayList<Dataset> allDatasets = new ArrayList<>();
List<String> filteredDatasets = new ArrayList<>();
//ArrayList<Dataset> allDatasets = new ArrayList<>();
/* List<String> filteredDatasets = new ArrayList<>();
StringBuilder allCitationRequestFields = new StringBuilder();
String citationRequest = "";

ArrayList<License> checkedDatasetsLicenses = new ArrayList<>();
for (String datasetName : datasets) {
allDatasets.add(datasetRepository.findDatasetByName(datasetName));
}
for (String datasetName : checkedDatasets) {
Dataset dataset = datasetRepository.findDatasetByName(datasetName);
allCitationRequestFields.append(dataset.getCitationRequest());
Expand All @@ -664,8 +661,12 @@ public String composeCitationRequest(Authentication authentication, Model model,
}
}
}

model.addAttribute("citationRequest", citationRequest);
*/
Collection<String> datasetCollection=Arrays.asList(checkedDatasets);
if (datasetRepository.checkIfCombinationRequiresAttribution(datasetCollection)>0)
model.addAttribute("citationRequest", datasetRepository.combineAllCitations4Datasets(datasetCollection));
else model.addAttribute("citationRequest", "");

return "create_dataset::citation-request";
}

Expand Down Expand Up @@ -720,12 +721,16 @@ public String checkLicenses(Authentication authentication, Model model,
@RequestParam(name = "datasets", required = true) String[] datasets,
@RequestParam(name = "checkedDatasets", required = true) String[] checkedDatasets) {
Iterable<License> allLicenses = licenseRepository.findAll();
ArrayList<License> checkedDatasetsLicenses = new ArrayList<>();


ArrayList<License> checkedDatasetsLicenses =
licenseRepository.getDatasetLicenses(Arrays.asList(checkedDatasets));

/*
for (String datasetName : checkedDatasets) {
Dataset dataset = datasetRepository.findDatasetByName(datasetName);
checkedDatasetsLicenses.add(dataset.getLicense());
}
*/

ArrayList<License> licenses = new ArrayList<>();
for (License license : allLicenses) {
Expand Down Expand Up @@ -774,6 +779,7 @@ public String checkAccess(Authentication authentication, Model model,
@RequestParam(name = "datasets", required = true) String[] datasets,
@RequestParam(name = "checkedDatasets", required = true) String[] checkedDatasets) {

/**
String access = "";
ArrayList<License> checkedDatasetsLicenses = new ArrayList<>();
Expand All @@ -788,8 +794,12 @@ public String checkAccess(Authentication authentication, Model model,
break;
}
}

model.addAttribute("access", access);
*/

model.addAttribute("access",
(datasetRepository.checkIfRedistributionIsNotAllowed(Arrays.asList(checkedDatasets))>0)?Dataset.ACCESS_PRIVATE:""
);

return "create_dataset::check-access";
}

Expand Down Expand Up @@ -1180,6 +1190,7 @@ public String setCreateDataset(Authentication authentication, Model model, @Vali
model.addAttribute("host", HOST_NAME);
model.addAttribute("licenses", licenseRepository.findAll());
model.addAttribute("datasets", datasetRepository.getSystemDatasets());
System.out.println("ERROR " + bindingResult.getAllErrors().get(0).getDefaultMessage());
return "create_dataset";
} else {
boolean modeSpam = false;
Expand Down
32 changes: 17 additions & 15 deletions src/main/resources/static/js/jqueryFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function updateDatasetsList() {

$("#datasets-list").load(url, function (response, status, xhr) {
if (status == "error") {
alert("#update-datasets-list error")
alert("#update-datasets-list error")
location.reload();
}
});
Expand All @@ -77,14 +77,15 @@ function validateCitationRequest() {

$("#citation-request").load(url, function (response, status, xhr) {
if (status == "error") {
alert("#validate-citation-request error")
alert("#validate-citation-request error")
location.reload();
}
});
}
}

function checkLicenses() {
$("#createDatasetButton").attr("disabled", true);
var datasetsList = $("input[name=datasets]");
var selectedDatasets = $("input[name=datasets]:checked");
var vCheckedDatasets = [];
Expand Down Expand Up @@ -115,24 +116,23 @@ function checkLicenses() {
var urlCheckLicenses = "/dataset/filterDatasetsByLicense?" + params;
$("#datasets-list").load(urlCheckLicenses, function (response, status, xhr) {
if (status == "error") {
alert("#datasets-list error")
alert("#datasets-list error");
location.reload();
} else {
/* var urlComposeCitationRequest = "/dataset/composeCitationRequest?" + params;
var urlComposeCitationRequest = "/dataset/composeCitationRequest?" + params;
$("#citation-request").load(urlComposeCitationRequest, function (response, status, xhr) {
if (status == "error") {
alert("#citation-request error")
alert("#citation-request error");
location.reload();
}
});
*/

var urlCheckLicenses = "/dataset/checkLicenses?" + params;
$("#check-licenses").load(urlCheckLicenses, function (response, status, xhr) {
if (status == "error") {
alert("#check-licenses error")
alert("#check-licenses error");
location.reload();
}
}
});

var urlCheckAccess = "/dataset/checkAccess?" + params;
Expand All @@ -141,11 +141,13 @@ function checkLicenses() {
if (status == "error") {
alert("#check-access error")
location.reload();
} else {
$("#createDatasetButton").attr("disabled", false);
}
});
}
});
}
}
}

function updateTable(id) {
Expand Down Expand Up @@ -192,8 +194,8 @@ function updateTable(id) {
if (status == "error") {
location.reload();
} else {
$("input[name^='inputHam']").attr("disabled", false);
$("input[name^='inputSpam']").attr("disabled", false);
$("input[name^='inputHam']").attr("disabled", false);
$("input[name^='inputSpam']").attr("disabled", false);
}
});
} else {
Expand Down Expand Up @@ -351,15 +353,15 @@ $(document).ready(function () {
//$('#errorLabelName').remove();
var taskId = $("#selectTask option:selected").val()
if (taskId !== "") {

$("#dataset-file").attr("disabled", true);
$("#createPreprocessingTaskF").attr("action", "/task/preprocess/reuse");

// Fill name and description
var url = encodeURI("/task/fillFields?id=" + taskId);

$("#task-data").load(url, function (response, status, xhr) {
// $('#errorLabelName').remove();
// $('#errorLabelName').remove();
if (status === "error") {
alert("error");
location.reload();
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/create_dataset.html
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ <h4 class="create" th:text="#{create.dataset.parameters}"></h4>
<div class="row">
<div class="col-12 right">
<input type="button" class="btn btn-primary" onclick="window.location.href = '/dataset/list'" th:attr="value = #{back}">
<input type="submit" class="btn btn-primary" th:attr="value = #{create.dataset.button.submit}">
<input id="createDatasetButton" type="submit" class="btn btn-primary" th:attr="value = #{create.dataset.button.submit}" disabled="true">
</div>
</div>
</div>
Expand Down

0 comments on commit 3dd3386

Please sign in to comment.