GenesysFilterServiceImpl.java
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys.server.service.impl;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.time.StopWatch;
import org.genesys.server.exception.SearchException;
import org.genesys.server.model.genesys.Accession;
import org.genesys.server.model.genesys.QAccession;
import org.genesys.server.model.genesys.QAccessionId;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.GenesysFilterService;
import org.genesys.server.service.filter.AccessionFilter;
import org.genesys.util.CoordUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
@Service
@Transactional(readOnly = true)
public class GenesysFilterServiceImpl implements GenesysFilterService {
private static final Logger LOG = LoggerFactory.getLogger(GenesysFilterServiceImpl.class);
@Autowired(required = false)
private ElasticsearchService elasticsearchService;
@Autowired
private JPAQueryFactory jpaQueryFactory;
@Override
public List<Double[]> listGeoTile(AccessionFilter filter, Integer limit, int zoom, int xtile, int ytile) throws SearchException {
QAccession qAccession = QAccession.accession;
QAccessionId qAccessionId = qAccession.accessionId();
BooleanBuilder filt = new BooleanBuilder();
tileQuery(filt, qAccessionId, zoom, xtile, ytile);
if (filter.isFulltextQuery()) {
filt.and(filter.buildPredicate());
return elasticsearchService.distinctCoordinates(filt, filter._text);
} else {
// filt.and(accessionGeo.isNotNull()).and(accessionGeo.latitude.isNotNull()).and(accessionGeo.longitude.isNotNull());
JPAQuery<Accession> query = jpaQueryFactory.selectFrom(qAccession);
filter.buildJpaQuery(query, qAccession);
query.where(filt);
StopWatch stopWatch = StopWatch.createStarted();
List<Tuple> results = query.select(qAccessionId.longitude, qAccessionId.latitude).distinct().fetch();
stopWatch.split();
LOG.debug("Got {} coordinates in {}", results.size(), stopWatch.toSplitString());
return results.parallelStream()
.map(item -> new Double[]{
item.get(0, Double.class),
item.get(1, Double.class),
}).collect(Collectors.toList());
}
}
// The tile query
private void tileQuery(BooleanBuilder filt, QAccessionId qAccessionId, int zoom, int xtile, int ytile) {
final double latN = CoordUtil.tileToLat(zoom, ytile);
final double latS = CoordUtil.tileToLat(zoom, ytile + 1);
final double diffLat = latN - latS;
final double lonW = CoordUtil.tileToLon(zoom, xtile);
final double lonE = CoordUtil.tileToLon(zoom, xtile + 1);
final double diffLon = lonE - lonW;
if (LOG.isDebugEnabled()) {
LOG.debug("{} <= lat <= {} corr={}", latS, latN, diffLat * .1);
LOG.debug("{} <= lon <= {} corr={}", lonW, lonE, diffLon * .1);
}
filt.and(qAccessionId.latitude.between(latS - diffLat * .1, latN + diffLat * .1)); // 10%
filt.and(qAccessionId.longitude.between(lonW - diffLon * .1, lonE + diffLon * .1)); // 10%
}
}