Commit 9c3be09a authored by Matija Obreza's avatar Matija Obreza
Browse files

Updated Geo info in Accession API v1

- using jpaQueryFactory
- Returning Number[][]
parent 24edd59b
......@@ -29,6 +29,7 @@ import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.api.FilteredPage;
import org.genesys.catalog.model.dataset.Dataset;
......@@ -240,19 +241,29 @@ public class AccessionController {
return accessionDetails;
}
@PostMapping(value = "/geo", produces = MediaType.APPLICATION_JSON_VALUE)
@PostMapping(value = "/mapinfo", produces = MediaType.APPLICATION_JSON_VALUE)
@JsonView({ JsonViews.Public.class })
public GeoInfoJson getGeoInfo(@PathVariable(value = "f", required = false) String filterCode, @RequestBody(required = false) AccessionFilter filter) throws IOException {
public AccessionMapInfo mapInfo(@RequestParam(value = "f", required = false) String filterCode, @RequestBody(required = false) AccessionFilter filter) throws IOException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, AccessionFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
GeoInfoJson geoInfo = new GeoInfoJson();
geoInfo.bounds = accessionService.getBounds(filter);
geoInfo.accessionCount = accessionService.countAccessions(filter);
geoInfo.tileServers = cdnServers;
AccessionMapInfo mapInfo = new AccessionMapInfo();
mapInfo.filterCode = filterCode;
mapInfo.filter = filter;
if (StringUtils.isBlank(filterCode)) {
// Entire map
mapInfo.bounds = new Number[][] { { -170, 80 }, { 170, -80 } };
} else {
mapInfo.bounds = accessionService.getGeoBounds(filter);
}
mapInfo.accessionCount = accessionService.countAccessions(filter);
mapInfo.tileServers = cdnServers;
return geoInfo;
return mapInfo;
}
/**
......@@ -391,8 +402,11 @@ public class AccessionController {
public List<Subset> subsets;
}
public static class GeoInfoJson {
public List<Object[]> bounds;
public static class AccessionMapInfo {
public String filterCode;
public AccessionFilter filter;
public Number[][] bounds;
public long accessionCount;
public String[] tileServers;
}
......
......@@ -46,12 +46,12 @@ public interface AccessionService {
Page<Accession> list(AccessionFilter filter, Pageable page);
/**
* Gets the bounds.
* Gets the geo bounds of maximum and minimum accession localities
*
* @param filter the filter
* @return the bounds
* @return the bounds [ [ a,b ], [ c, d] ]
*/
List<Object[]> getBounds(AccessionFilter filter);
Number[][] getGeoBounds(AccessionFilter filter);
/**
* Gets accession by uuid
......
......@@ -39,7 +39,7 @@ public class AccessionGeoFilter extends BasicModelFilter<AccessionGeoFilter, Acc
/** The elevation. */
public NumberFilter<Double> elevation;
public Boolean referenced = true;
public Boolean referenced;
/**
* Builds the query.
......
......@@ -15,15 +15,12 @@
*/
package org.genesys2.server.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import javax.persistence.EntityManager;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.genesys.AccessionId;
......@@ -41,15 +38,13 @@ import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.support.Querydsl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.core.types.dsl.PathBuilderFactory;
import com.querydsl.jpa.JPQLQuery;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
/**
* Accession services.
......@@ -71,9 +66,7 @@ public class AccessionServiceImpl implements AccessionService {
private AccessionCounter accessionCounter;
@Autowired
private EntityManager entityManager;
private PathBuilder<Accession> accessionPathBuilder = new PathBuilderFactory().create(Accession.class);
private JPAQueryFactory jpaQueryFactory;
private <T extends AccessionData> T lazyLoad(T accession) {
if (accession != null) {
......@@ -169,23 +162,17 @@ public class AccessionServiceImpl implements AccessionService {
}
@Override
public List<Object[]> getBounds(AccessionFilter filter) {
QAccession accession = QAccession.accession;
Querydsl querydsl = new Querydsl(entityManager, accessionPathBuilder);
JPQLQuery<Tuple> query = querydsl.createQuery(accession).select(accession.accessionId.geo.latitude.min(), accession.accessionId.geo.longitude.max(),
accession.accessionId.geo.latitude.max(), accession.accessionId.geo.longitude.min()).from(accession).where(filter.buildQuery());
public Number[][] getGeoBounds(AccessionFilter filter) {
final QAccession accession = QAccession.accession;
JPAQuery<Tuple> query = jpaQueryFactory.selectFrom(accession).select(accession.accessionId.geo.latitude.min(), accession.accessionId.geo.longitude.max(),
accession.accessionId.geo.latitude.max(), accession.accessionId.geo.longitude.min());
if (filter != null)
query.where(filter.buildQuery());
Object[] results = query.fetchOne().toArray();
List<Object[]> latLngBounds = new ArrayList<>();
// corner1
latLngBounds.add(new Object[] { results[0], results[1] });
// corner2
latLngBounds.add(new Object[] { results[2], results[3] });
return latLngBounds;
return new Number[][] { new Number[] { (Number)results[0], (Number)results[1] }, new Number[] { (Number)results[2], (Number)results[3] } };
}
@Override
......
......@@ -20,6 +20,7 @@ import java.io.Serializable;
import java.util.Iterator;
import java.util.Properties;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
......@@ -42,6 +43,8 @@ import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.querydsl.jpa.impl.JPAQueryFactory;
import liquibase.integration.spring.SpringLiquibase;
@EnableJpaRepositories(basePackages = { "org.genesys.blocks.persistence", "org.genesys.blocks.security.persistence", "org.genesys.blocks.oauth.persistence",
......@@ -118,6 +121,11 @@ public class DatabaseConfig {
public JdbcTemplate jdbcTemplate(final DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean
public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
return new JPAQueryFactory(entityManager);
}
@Bean(name = "databaseMigration")
public SpringLiquibase databaseMigration() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment