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; ...@@ -29,6 +29,7 @@ import java.util.UUID;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.JsonViews; import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.api.FilteredPage; import org.genesys.catalog.api.FilteredPage;
import org.genesys.catalog.model.dataset.Dataset; import org.genesys.catalog.model.dataset.Dataset;
...@@ -240,19 +241,29 @@ public class AccessionController { ...@@ -240,19 +241,29 @@ public class AccessionController {
return accessionDetails; return accessionDetails;
} }
@PostMapping(value = "/geo", produces = MediaType.APPLICATION_JSON_VALUE) @PostMapping(value = "/mapinfo", produces = MediaType.APPLICATION_JSON_VALUE)
@JsonView({ JsonViews.Public.class }) @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) { if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, AccessionFilter.class); filter = shortFilterService.filterByCode(filterCode, AccessionFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
} }
GeoInfoJson geoInfo = new GeoInfoJson(); AccessionMapInfo mapInfo = new AccessionMapInfo();
geoInfo.bounds = accessionService.getBounds(filter); mapInfo.filterCode = filterCode;
geoInfo.accessionCount = accessionService.countAccessions(filter); mapInfo.filter = filter;
geoInfo.tileServers = cdnServers; 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 { ...@@ -391,8 +402,11 @@ public class AccessionController {
public List<Subset> subsets; public List<Subset> subsets;
} }
public static class GeoInfoJson { public static class AccessionMapInfo {
public List<Object[]> bounds; public String filterCode;
public AccessionFilter filter;
public Number[][] bounds;
public long accessionCount; public long accessionCount;
public String[] tileServers; public String[] tileServers;
} }
......
...@@ -46,12 +46,12 @@ public interface AccessionService { ...@@ -46,12 +46,12 @@ public interface AccessionService {
Page<Accession> list(AccessionFilter filter, Pageable page); Page<Accession> list(AccessionFilter filter, Pageable page);
/** /**
* Gets the bounds. * Gets the geo bounds of maximum and minimum accession localities
* *
* @param filter the filter * @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 * Gets accession by uuid
......
...@@ -39,7 +39,7 @@ public class AccessionGeoFilter extends BasicModelFilter<AccessionGeoFilter, Acc ...@@ -39,7 +39,7 @@ public class AccessionGeoFilter extends BasicModelFilter<AccessionGeoFilter, Acc
/** The elevation. */ /** The elevation. */
public NumberFilter<Double> elevation; public NumberFilter<Double> elevation;
public Boolean referenced = true; public Boolean referenced;
/** /**
* Builds the query. * Builds the query.
......
...@@ -15,15 +15,12 @@ ...@@ -15,15 +15,12 @@
*/ */
package org.genesys2.server.service.impl; package org.genesys2.server.service.impl;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import javax.persistence.EntityManager;
import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionData; import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.genesys.AccessionId; import org.genesys2.server.model.genesys.AccessionId;
...@@ -41,15 +38,13 @@ import org.springframework.cache.annotation.Cacheable; ...@@ -41,15 +38,13 @@ import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.support.Querydsl;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.querydsl.core.Tuple; import com.querydsl.core.Tuple;
import com.querydsl.core.types.dsl.PathBuilder; import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.core.types.dsl.PathBuilderFactory; import com.querydsl.jpa.impl.JPAQueryFactory;
import com.querydsl.jpa.JPQLQuery;
/** /**
* Accession services. * Accession services.
...@@ -71,9 +66,7 @@ public class AccessionServiceImpl implements AccessionService { ...@@ -71,9 +66,7 @@ public class AccessionServiceImpl implements AccessionService {
private AccessionCounter accessionCounter; private AccessionCounter accessionCounter;
@Autowired @Autowired
private EntityManager entityManager; private JPAQueryFactory jpaQueryFactory;
private PathBuilder<Accession> accessionPathBuilder = new PathBuilderFactory().create(Accession.class);
private <T extends AccessionData> T lazyLoad(T accession) { private <T extends AccessionData> T lazyLoad(T accession) {
if (accession != null) { if (accession != null) {
...@@ -169,23 +162,17 @@ public class AccessionServiceImpl implements AccessionService { ...@@ -169,23 +162,17 @@ public class AccessionServiceImpl implements AccessionService {
} }
@Override @Override
public List<Object[]> getBounds(AccessionFilter filter) { public Number[][] getGeoBounds(AccessionFilter filter) {
QAccession accession = QAccession.accession; final QAccession accession = QAccession.accession;
Querydsl querydsl = new Querydsl(entityManager, accessionPathBuilder); 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());
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()); if (filter != null)
query.where(filter.buildQuery());
Object[] results = query.fetchOne().toArray(); Object[] results = query.fetchOne().toArray();
return new Number[][] { new Number[] { (Number)results[0], (Number)results[1] }, new Number[] { (Number)results[2], (Number)results[3] } };
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;
} }
@Override @Override
......
...@@ -20,6 +20,7 @@ import java.io.Serializable; ...@@ -20,6 +20,7 @@ import java.io.Serializable;
import java.util.Iterator; import java.util.Iterator;
import java.util.Properties; import java.util.Properties;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import org.apache.tomcat.jdbc.pool.DataSource; import org.apache.tomcat.jdbc.pool.DataSource;
...@@ -42,6 +43,8 @@ import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; ...@@ -42,6 +43,8 @@ import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.querydsl.jpa.impl.JPAQueryFactory;
import liquibase.integration.spring.SpringLiquibase; import liquibase.integration.spring.SpringLiquibase;
@EnableJpaRepositories(basePackages = { "org.genesys.blocks.persistence", "org.genesys.blocks.security.persistence", "org.genesys.blocks.oauth.persistence", @EnableJpaRepositories(basePackages = { "org.genesys.blocks.persistence", "org.genesys.blocks.security.persistence", "org.genesys.blocks.oauth.persistence",
...@@ -118,6 +121,11 @@ public class DatabaseConfig { ...@@ -118,6 +121,11 @@ public class DatabaseConfig {
public JdbcTemplate jdbcTemplate(final DataSource dataSource) { public JdbcTemplate jdbcTemplate(final DataSource dataSource) {
return new JdbcTemplate(dataSource); return new JdbcTemplate(dataSource);
} }
@Bean
public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
return new JPAQueryFactory(entityManager);
}
@Bean(name = "databaseMigration") @Bean(name = "databaseMigration")
public SpringLiquibase databaseMigration() { public SpringLiquibase databaseMigration() {
......
Supports Markdown
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