Commit 90ca0877 authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch '624-subsetting-provide-tileids-by-cropname' into 'main'

Subsetting: Provide tileIndexes grouped by crop.shortName

Closes #624

See merge request genesys-pgr/genesys-server!684
parents c08031c1 6adb47e0
......@@ -801,8 +801,23 @@ public class AccessionController {
public Set<Integer> allTileIndex3min(@RequestParam(name = "f", required = false) final String filterCode, @RequestBody(required = false) final AccessionFilter filter) throws Exception {
FilterInfo<AccessionFilter> filterInfo = shortFilterProcessor.processFilter(filterCode, filter, AccessionFilter.class);
return accessionService.listTileIndex3min(filterInfo.filter);
}
/**
* Get term overview for filters
*
* @param filterCode short filter code
* @param filter the filter
* @return the page
* @throws Exception
*/
@PostMapping(value = "/tileIndex3min", produces = { MediaType.APPLICATION_JSON_VALUE }, params = { "crop" })
@JsonView({ JsonViews.Public.class })
public Map<String, Set<Integer>> allTileIndex3minByCrop(@RequestParam(name = "f", required = false) final String filterCode, @RequestBody(required = false) final AccessionFilter filter) throws Exception {
FilterInfo<AccessionFilter> filterInfo = shortFilterProcessor.processFilter(filterCode, filter, AccessionFilter.class);
return accessionService.listTileIndex3minByCrop(filterInfo.filter);
}
}
......@@ -357,4 +357,15 @@ public interface AccessionService {
* @throws SearchException
*/
Set<Integer> listTileIndex3min(AccessionFilter filter) throws SearchException, Exception;
/**
* Returns a map of {@link #listTileIndex3min(AccessionFilter)} grouped by crop.shortName.
* When the filter for crops is empty (filter.crop) then the map key is "NOCROP".
*
* @param filter
* @return distinct list of tileIndex3deg by crop.shortName
* @throws SearchException
* @throws Exception
*/
Map<String, Set<Integer>> listTileIndex3minByCrop(AccessionFilter filter) throws SearchException, Exception;
}
......@@ -30,6 +30,7 @@ import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.auditlog.service.AuditTrailService;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.QDataset;
......@@ -51,6 +52,7 @@ import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.QCrop;
import org.genesys2.server.model.impl.QSubset;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.persistence.AccessionIdRepository;
......@@ -82,6 +84,7 @@ import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.hazelcast.internal.util.CollectionUtil;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQuery;
......@@ -147,6 +150,8 @@ public class AccessionServiceImpl implements AccessionService {
private static final int LOAD_CHUNK_SIZE = 200;
private static final String NOCROP = "NOCROP";
private <T extends AccessionData> T lazyLoad(T accession) {
if (accession != null) {
AccessionId accessionId = accession.getAccessionId();
......@@ -751,4 +756,23 @@ public class AccessionServiceImpl implements AccessionService {
return new HashSet<>(tileIndexes3d);
}
}
@Override
public Map<String, Set<Integer>> listTileIndex3minByCrop(AccessionFilter filter) throws SearchException, Exception {
// Check if filter includes crop selection
if (CollectionUtil.isEmpty(filter.crop)) {
// No crop selected, return under "NOCROP"
return Map.of(NOCROP, listTileIndex3min(filter));
} else {
// Crops are selected, handle each crop separately
var tileIndexes = new HashMap<String, Set<Integer>>();
for (var cropCode : filter.crop) {
var filterForCrop = filter.copy(AccessionFilter.class);
filterForCrop.crop = Set.of(cropCode);
tileIndexes.put(cropCode, listTileIndex3min(filterForCrop));
}
return tileIndexes;
}
}
}
......@@ -142,6 +142,11 @@ public class AccessionSearchTest extends AbstractElasticServicesTest {
var tile3deg = accessionService.listTileIndex3min(filter);
assertThat(tile3deg, hasSize(1));
assertThat(tile3deg, contains(accession.getAccessionId().getTileIndex3min()));
var cropTile3deg = accessionService.listTileIndex3minByCrop(filter);
assertThat(cropTile3deg.keySet(), hasSize(1));
assertThat(cropTile3deg.keySet(), contains("NOCROP"));
assertThat(cropTile3deg.get("NOCROP"), contains(accession.getAccessionId().getTileIndex3min()));
}
@Test
......
......@@ -46,6 +46,7 @@ import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.impl.SearchException;
import org.genesys2.server.service.worker.AccessionOpResponse;
import org.genesys2.server.service.worker.AccessionUploader;
import org.hamcrest.collection.IsMapContaining;
import org.genesys2.server.service.worker.AccessionOpResponse.UpsertResult;
import org.junit.Before;
import org.junit.Test;
......@@ -969,6 +970,11 @@ public class AccessionUploaderTest extends AbstractServicesTest {
var tiles = accessionService.listTileIndex3min(filter);
assertThat(tiles, not(nullValue()));
assertThat(tiles, contains(0));
var cropTiles = accessionService.listTileIndex3minByCrop(filter);
assertThat(cropTiles, not(nullValue()));
assertThat(cropTiles.keySet(), contains("NOCROP"));
assertThat(cropTiles.get("NOCROP"), contains(0));
filter.aegis = true;
tiles = accessionService.listTileIndex3min(filter);
......
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