fix hibernate, dependencies, empty query

This commit is contained in:
Michele Artini 2020-07-29 11:31:18 +02:00
parent 1e3654808a
commit c8b5ca96d2
8 changed files with 155 additions and 100 deletions

View File

@ -33,8 +33,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.thymeleaf.extras</groupId> <groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId> <artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.postgresql</groupId> <groupId>org.postgresql</groupId>
@ -43,7 +42,7 @@
<dependency> <dependency>
<groupId>com.vladmihalcea</groupId> <groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId> <artifactId>hibernate-types-52</artifactId>
<version>2.3.5</version> <version>2.9.13</version>
</dependency> </dependency>
<!-- JAXB API, java.xml.bind module --> <!-- JAXB API, java.xml.bind module -->

View File

@ -33,7 +33,8 @@ public class MainApplication {
.select() .select()
.apis(RequestHandlerSelectors.any()) .apis(RequestHandlerSelectors.any())
.paths(p -> p.startsWith("/api/")) .paths(p -> p.startsWith("/api/"))
.build().apiInfo(new ApiInfoBuilder() .build()
.apiInfo(new ApiInfoBuilder()
.title("D-Net Organizations Service APIs") .title("D-Net Organizations Service APIs")
.description("APIs documentation") .description("APIs documentation")
.version("1.1") .version("1.1")
@ -43,4 +44,5 @@ public class MainApplication {
.build()); .build());
} }
} }

View File

@ -73,6 +73,7 @@ public class OrganizationController {
@RequestMapping(value = "/save", method = RequestMethod.POST) @RequestMapping(value = "/save", method = RequestMethod.POST)
public List<String> save(@RequestBody final OrganizationView org, final Authentication authentication) { public List<String> save(@RequestBody final OrganizationView org, final Authentication authentication) {
if (StringUtils.isBlank(org.getName())) { if (StringUtils.isBlank(org.getName())) {
throw new RuntimeException("Missing field: name"); throw new RuntimeException("Missing field: name");
} else if (StringUtils.isBlank(org.getCountry())) { } else if (StringUtils.isBlank(org.getCountry())) {
@ -240,15 +241,15 @@ public class OrganizationController {
@RequestMapping(value = "/browse/countries", method = RequestMethod.GET) @RequestMapping(value = "/browse/countries", method = RequestMethod.GET)
public List<BrowseEntry> browseCountries(final Authentication authentication) { public List<BrowseEntry> browseCountries(final Authentication authentication) {
return UserInfo.isSuperAdmin(authentication) return UserInfo.isSuperAdmin(authentication)
? organizationSimpleViewRepository.browseCountries() ? databaseUtils.browseCountries()
: organizationSimpleViewRepository.browseCountriesForUser(authentication.getName()); : databaseUtils.browseCountriesForUser(authentication.getName());
} }
@RequestMapping(value = "/browse/types", method = RequestMethod.GET) @RequestMapping(value = "/browse/types", method = RequestMethod.GET)
public List<BrowseEntry> browseOrganizationTypes(final Authentication authentication) { public List<BrowseEntry> browseOrganizationTypes(final Authentication authentication) {
return UserInfo.isSuperAdmin(authentication) return UserInfo.isSuperAdmin(authentication)
? organizationSimpleViewRepository.browseTypes() ? databaseUtils.browseTypes()
: organizationSimpleViewRepository.browseTypesForUser(authentication.getName()); : databaseUtils.browseTypesForUser(authentication.getName());
} }
@RequestMapping(value = "/conflicts/fix/{masterId}", method = RequestMethod.POST) @RequestMapping(value = "/conflicts/fix/{masterId}", method = RequestMethod.POST)

View File

@ -1,9 +1,31 @@
package eu.dnetlib.organizations.model.utils; package eu.dnetlib.organizations.model.utils;
public interface BrowseEntry { import java.io.Serializable;
String getValue(); public class BrowseEntry implements Serializable {
int getCount(); /**
*
*/
private static final long serialVersionUID = 8854955977257064470L;
private String value;
private int count;
public String getValue() {
return value;
}
public void setValue(final String value) {
this.value = value;
}
public int getCount() {
return count;
}
public void setCount(final int count) {
this.count = count;
}
} }

View File

@ -1,13 +1,10 @@
package eu.dnetlib.organizations.repository.readonly; package eu.dnetlib.organizations.repository.readonly;
import java.util.List;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import eu.dnetlib.organizations.model.utils.BrowseEntry;
import eu.dnetlib.organizations.model.view.OrganizationSimpleView; import eu.dnetlib.organizations.model.view.OrganizationSimpleView;
@Repository @Repository
@ -20,29 +17,8 @@ public interface OrganizationSimpleViewRepository extends ReadOnlyRepository<Org
@Query(value = "select o.* from organizations_simple_view o left outer join user_countries uc on (uc.country = o.country) where o.name ilike %:text% and uc.email = :email", nativeQuery = true) @Query(value = "select o.* from organizations_simple_view o left outer join user_countries uc on (uc.country = o.country) where o.name ilike %:text% and uc.email = :email", nativeQuery = true)
Page<OrganizationSimpleView> findByNameForUser(String text, String email, Pageable pageable); Page<OrganizationSimpleView> findByNameForUser(String text, String email, Pageable pageable);
// BROWSE BY COUNTRY
@Query(value = "select country as value, count(*) as count from organizations group by country order by count desc", nativeQuery = true)
List<BrowseEntry> browseCountries();
// BROWSE BY COUNTRY FOR USER
@Query(value = "select o.country as value, count(o.country) as count from user_countries uc left outer join organizations o on (uc.country = o.country) where uc.email=?1 group by o.country order by count desc", nativeQuery = true)
List<BrowseEntry> browseCountriesForUser(String email);
Page<OrganizationSimpleView> findByCountry(String country, Pageable pageable); Page<OrganizationSimpleView> findByCountry(String country, Pageable pageable);
// BROWSE BY ORG TYPE
@Query(value = "select type as value, count(*) as count from organizations group by type order by count desc", nativeQuery = true)
List<BrowseEntry> browseTypes();
// BROWSE BY ORG TYPE FOR USER
@Query(value = "select o.type as value, count(o.type) as count "
+ "from organizations o "
+ "left outer join user_countries uc on (uc.country = o.country) "
+ "where uc.email=?1 "
+ "group by o.type "
+ "order by count desc;", nativeQuery = true)
List<BrowseEntry> browseTypesForUser(String email);
Page<OrganizationSimpleView> findByType(String type, Pageable pageable); Page<OrganizationSimpleView> findByType(String type, Pageable pageable);
@Query(value = "select o.* from organizations_simple_view o left outer join user_countries uc on (uc.country = o.country) where uc.email = ?2 and o.type = ?1", nativeQuery = true) @Query(value = "select o.* from organizations_simple_view o left outer join user_countries uc on (uc.country = o.country) where uc.email = ?2 and o.type = ?1", nativeQuery = true)

View File

@ -16,6 +16,7 @@ import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
@ -31,6 +32,7 @@ import eu.dnetlib.organizations.model.Relationship;
import eu.dnetlib.organizations.model.Url; import eu.dnetlib.organizations.model.Url;
import eu.dnetlib.organizations.model.User; import eu.dnetlib.organizations.model.User;
import eu.dnetlib.organizations.model.UserCountry; import eu.dnetlib.organizations.model.UserCountry;
import eu.dnetlib.organizations.model.utils.BrowseEntry;
import eu.dnetlib.organizations.model.view.OrganizationView; import eu.dnetlib.organizations.model.view.OrganizationView;
import eu.dnetlib.organizations.model.view.UserView; import eu.dnetlib.organizations.model.view.UserView;
import eu.dnetlib.organizations.repository.AcronymRepository; import eu.dnetlib.organizations.repository.AcronymRepository;
@ -68,7 +70,12 @@ public class DatabaseUtils {
private JdbcTemplate jdbcTemplate; private JdbcTemplate jdbcTemplate;
public enum VocabularyTable { public enum VocabularyTable {
languages, countries, org_types, id_types, rel_types, simrel_types languages,
countries,
org_types,
id_types,
rel_types,
simrel_types
} }
@Transactional @Transactional
@ -236,4 +243,36 @@ public class DatabaseUtils {
return Arrays.asList(r1, r2); return Arrays.asList(r1, r2);
} }
// BROWSE BY COUNTRY
public List<BrowseEntry> browseCountries() {
final String sql = "select country as value, count(*) as count from organizations group by country order by count desc";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(BrowseEntry.class));
}
// BROWSE BY COUNTRY FOR USER
public List<BrowseEntry> browseCountriesForUser(final String email) {
final String sql =
"select o.country as value, count(o.country) as count from user_countries uc left outer join organizations o on (uc.country = o.country) where uc.email=? group by o.country order by count desc";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(BrowseEntry.class), email);
}
// BROWSE BY ORG TYPE
public List<BrowseEntry> browseTypes() {
final String sql = "select type as value, count(*) as count from organizations group by type order by count desc";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(BrowseEntry.class));
}
// BROWSE BY ORG TYPE FOR USER
public List<BrowseEntry> browseTypesForUser(final String email) {
final String sql = "select o.type as value, count(o.type) as count "
+ "from organizations o "
+ "left outer join user_countries uc on (uc.country = o.country) "
+ "where uc.email=? "
+ "group by o.type "
+ "order by count desc;";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(BrowseEntry.class), email);
}
} }

View File

@ -31,6 +31,7 @@ orgsModule.directive('selectOrgModal', function($http) {
scope.search = function(text, page, size) { scope.search = function(text, page, size) {
scope.searchOrgs = {}; scope.searchOrgs = {};
$http.get('api/organizations/search/' + page + '/' + size + '?q=' + text).then(function successCallback(res) { $http.get('api/organizations/search/' + page + '/' + size + '?q=' + text).then(function successCallback(res) {
if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); } if((typeof res.data) == 'string') { alert("Session expired !"); location.reload(true); }
scope.searchValue = text; scope.searchValue = text;
@ -438,12 +439,19 @@ orgsModule.controller('newOrgCtrl', function ($scope, $http, $routeParams, $loca
orgsModule.controller('searchCtrl', function ($scope, $location) { orgsModule.controller('searchCtrl', function ($scope, $location) {
$scope.searchText = ''; $scope.searchText = '';
$scope.search = function() { $scope.search = function() {
if ($scope.searchText) {
$location.url('/searchResults/0/50/' + encodeURIComponent(encodeURIComponent($scope.searchText))); $location.url('/searchResults/0/50/' + encodeURIComponent(encodeURIComponent($scope.searchText)));
} else {
$location.url('/searchResults/0/50/_');
}
} }
}); });
orgsModule.controller('searchResultsCtrl', function ($scope, $http, $routeParams, $location) { orgsModule.controller('searchResultsCtrl', function ($scope, $http, $routeParams, $location) {
$scope.searchText = '';
if ($routeParams.text != '_') {
$scope.searchText = decodeURIComponent($routeParams.text); $scope.searchText = decodeURIComponent($routeParams.text);
}
$scope.orgs = {}; $scope.orgs = {};
$http.get('api/organizations/search/' + $routeParams.page + '/' + $routeParams.size + '?q=' + $scope.searchText).then(function successCallback(res) { $http.get('api/organizations/search/' + $routeParams.page + '/' + $routeParams.size + '?q=' + $scope.searchText).then(function successCallback(res) {
@ -454,11 +462,19 @@ orgsModule.controller('searchResultsCtrl', function ($scope, $http, $routeParams
}); });
$scope.prev = function() { $scope.prev = function() {
if ($scope.searchText) {
$location.url('/searchResults/' + ($scope.orgs.number - 1) + '/' + $scope.orgs.size + '/' + encodeURIComponent($scope.searchText)); $location.url('/searchResults/' + ($scope.orgs.number - 1) + '/' + $scope.orgs.size + '/' + encodeURIComponent($scope.searchText));
} else {
$location.url('/searchResults/' + ($scope.orgs.number - 1) + '/' + $scope.orgs.size + '/_');
}
} }
$scope.next = function() { $scope.next = function() {
if ($scope.searchText) {
$location.url('/searchResults/' + ($scope.orgs.number + 1) + '/' + $scope.orgs.size + '/' + encodeURIComponent($scope.searchText)); $location.url('/searchResults/' + ($scope.orgs.number + 1) + '/' + $scope.orgs.size + '/' + encodeURIComponent($scope.searchText));
} else {
$location.url('/searchResults/' + ($scope.orgs.number + 1) + '/' + $scope.orgs.size + '/_');
}
} }
}); });

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version> <version>2.3.2.RELEASE</version>
<relativePath /> <relativePath />
</parent> </parent>