Commit 35949951 authored by Matija Obreza's avatar Matija Obreza

Merge branch '113-genesys-ui-updated-apis' into 'master'

Resolve "Subset and Dataset accession lists: server-side"

See merge request genesys-pgr/genesys-server!284
parents b440b739 9396a52c
......@@ -15,6 +15,10 @@
*/
package org.genesys.catalog.model.dataset;
import javax.persistence.*;
import java.util.List;
import java.util.Set;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import org.genesys.blocks.auditlog.annotations.Audited;
......@@ -36,10 +40,6 @@ import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import javax.persistence.*;
import java.util.List;
import java.util.Set;
/**
* {@link Dataset} captures the metadata information as a snapshot. Multiple
* versions of the versions may exist.
......@@ -78,7 +78,7 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
// index
indexes = { @Index(columnList = "datasetId, instCode, acceNumb"), @Index(columnList = "datasetId, genus") })
@Field(type = FieldType.Object)
@JsonView({ JsonViews.Minimal.class })
@JsonIgnore
private Set<AccessionRef> accessionRefs;
/** The descriptors. */
......
......@@ -117,6 +117,15 @@ public interface DatasetService {
*/
Dataset loadDataset(UUID uuid, int version);
/**
* Load AccessionRef list by Dataset.
*
* @param dataset the dataset
* @param page Pageable
* @return PageImpl of AccessionRef
*/
Page<AccessionRef> listAccessions(Dataset dataset, Pageable page);
/**
* List published datasets by accession.
*
......
......@@ -25,6 +25,7 @@ import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
......@@ -33,6 +34,7 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.genesys.blocks.security.service.CustomAclService;
......@@ -166,10 +168,6 @@ public class DatasetServiceImpl implements DatasetService {
dataset.setLocations(new ArrayList<>());
dataset.setCrops(source.getCrops());
if (source.getAccessionRefs() != null && !source.getAccessionRefs().isEmpty()) {
dataset.setAccessionRefs(lookupMatchingAccessions(source.getAccessionRefs()));
}
copyDescriptors(dataset, source.getDescriptors());
dataset = datasetRepository.save(dataset);
......@@ -229,7 +227,9 @@ public class DatasetServiceImpl implements DatasetService {
public Dataset updateAccessionRefs(Dataset dataset, final Set<AccessionRef> accessionRefs) {
LOG.info("Update AccessionIdentifiers. Input dataset: {}, input accessionRefs {}", dataset, accessionRefs);
dataset = loadDataset(dataset);
dataset.getAccessionRefs().clear();
if (dataset.getAccessionRefs() != null && !dataset.getAccessionRefs().isEmpty()) {
dataset.getAccessionRefs().clear();
}
dataset.setAccessionRefs(lookupMatchingAccessions(accessionRefs));
return lazyLoad(datasetRepository.save(dataset));
}
......@@ -379,6 +379,26 @@ public class DatasetServiceImpl implements DatasetService {
return lazyLoad(dataset);
}
/**
* {@inheritDoc}
*/
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') || dataset.published || hasPermission(dataset, 'read')")
public Page<AccessionRef> listAccessions(Dataset dataset, final Pageable page) {
dataset = loadDataset(dataset);
final List<AccessionRef> accessionRefs = new ArrayList<AccessionRef>(dataset.getAccessionRefs());
if (CollectionUtils.isEmpty(accessionRefs)) {
return new PageImpl<>(Collections.emptyList(), page, 0);
}
accessionRefs.sort((a, b) -> a.getAcceNumb().compareTo(b.getAcceNumb()));
int fromIndex = Math.multiplyExact(page.getPageNumber(), page.getPageSize());
int toIndex = Math.min(fromIndex + page.getPageSize(), accessionRefs.size());
return new PageImpl<>(accessionRefs.subList(fromIndex, toIndex), page, accessionRefs.size());
}
/**
* {@inheritDoc}
*/
......@@ -463,9 +483,6 @@ public class DatasetServiceImpl implements DatasetService {
throw new NotFoundElement("No such dataset");
}
if (dataset.getAccessionRefs() != null) {
dataset.getAccessionRefs().size();
}
if (dataset.getDescriptors() != null) {
dataset.getDescriptors().size();
}
......
......@@ -387,7 +387,7 @@ public class AccessionController {
public String[] tileServers;
}
private static class AccessionOverview {
public static class AccessionOverview {
public String filterCode;
public AccessionFilter filter;
public Map<String, TermResult> overview;
......
......@@ -218,6 +218,20 @@ public class DatasetController extends ApiBaseController {
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, datasetService.listDatasets(filterInfo.filter, page.toPageRequest(100, Sort.Direction.ASC, "id")));
}
/**
* Load AccessionRef list by Dataset
*
* @param uuid uuid of Dataset
* @param page Pageable
* @return the page
* @throws NotFoundElement
*/
@JsonView({ JsonViews.Public.class })
@GetMapping(value = "/accessions/{uuid}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Page<AccessionRef> listAccessions(@PathVariable("uuid") final UUID uuid, final Pagination page) throws NotFoundElement {
return datasetService.listAccessions(datasetService.loadDataset(uuid), page.toPageRequest(100));
}
/**
* Removes the descriptors.
*
......
......@@ -20,18 +20,22 @@ import java.io.IOException;
import java.util.Set;
import java.util.UUID;
import com.fasterxml.jackson.annotation.JsonView;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys.catalog.service.ShortFilterService.FilterInfo;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.api.FilteredPage;
import org.genesys2.server.api.Pagination;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.SubsetService;
import org.genesys2.server.service.filter.SubsetFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
......@@ -161,6 +165,20 @@ public class SubsetController {
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, subsetService.listSubsetsForCurrentUser(filterInfo.filter, page.toPageRequest(100, Sort.Direction.DESC, "lastModifiedDate")));
}
/**
* Load AccessionRef list by Subset
*
* @param uuid uuid of Subset
* @param page Pageable
* @return the page
* @throws NotFoundElement
*/
@JsonView({ JsonViews.Public.class })
@GetMapping(value = "/accessions/{uuid}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Page<AccessionRef> listAccessions(@PathVariable("uuid") final UUID uuid, final Pagination page) throws NotFoundElement {
return subsetService.listAccessions(subsetService.get(uuid), page.toPageRequest(100));
}
/**
* Remove accessions form subset.
*
......
......@@ -47,6 +47,7 @@ import org.hibernate.annotations.Type;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
/**
......@@ -94,7 +95,7 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
// index
indexes = { @Index(columnList = "subsetId, instCode, acceNumb"), @Index(columnList = "subsetId, genus") })
@Field(type = FieldType.Object)
@JsonView({ JsonViews.Public.class })
@JsonIgnore
private Set<AccessionRef> accessionRefs;
/** The accession count. */
......
......@@ -88,30 +88,36 @@ public class AccessionRepositoryCustomImpl implements AccessionRepositoryCustom,
cq.distinct(true);
cq.select(root.get("id"));
List<Predicate> restrictions = new ArrayList<Predicate>();
Set<String> uniqueDois = forUpdate.stream().map(aid -> aid.getDoi()).filter(doi -> doi != null).distinct().collect(Collectors.toSet());
List<Accession> res = new ArrayList<>(forUpdate.size());
Path<Object> theDoi = root.get("doi");
Path<Object> theInstCode = root.get("instituteCode");
Path<Object> theAcceNumb = root.get("accessionNumber");
Path<Object> theGenus = root.get("genus");
final int chunkSize = 200;
for (int fromIndex = 0; fromIndex < forUpdate.size(); fromIndex += chunkSize) {
List<? extends AccessionIdentifier3> sublist = forUpdate.subList(fromIndex, Math.min(forUpdate.size(), fromIndex + chunkSize));
if (uniqueDois.size() > 0) {
restrictions.add(theDoi.in(uniqueDois));
LOG.trace("*** {} dois={}", uniqueDois.size(), uniqueDois);
}
// A lot of .. (instCode=? and acceNumb=? and genus=?)
for (AccessionIdentifier3 ah : forUpdate) {
restrictions.add(criteriaBuilder.and(criteriaBuilder.equal(theInstCode, ah.getHoldingInstitute()), criteriaBuilder.equal(theAcceNumb, ah
.getAccessionNumber()), criteriaBuilder.equal(theGenus, ah.getGenus())));
List<Predicate> restrictions = new ArrayList<Predicate>();
Set<String> uniqueDois = sublist.stream().map(aid -> aid.getDoi()).filter(doi -> doi != null).distinct().collect(Collectors.toSet());
Path<Object> theDoi = root.get("doi");
Path<Object> theInstCode = root.get("instituteCode");
Path<Object> theAcceNumb = root.get("accessionNumber");
Path<Object> theGenus = root.get("genus");
if (uniqueDois.size() > 0) {
restrictions.add(theDoi.in(uniqueDois));
LOG.trace("*** {} dois={}", uniqueDois.size(), uniqueDois);
}
// A lot of .. (instCode=? and acceNumb=? and genus=?)
for (AccessionIdentifier3 ah : sublist) {
restrictions.add(criteriaBuilder.and(criteriaBuilder.equal(theInstCode, ah.getHoldingInstitute()), criteriaBuilder.equal(theAcceNumb, ah
.getAccessionNumber()), criteriaBuilder.equal(theGenus, ah.getGenus())));
}
cq.where(criteriaBuilder.or(restrictions.toArray(EMPTY_PREDICATE_ARRAY)));
res.addAll(jpaQueryFactory.selectFrom(QAccession.accession).where(QAccession.accession.id.in(em.createQuery(cq).getResultList())).fetch());
}
cq.where(criteriaBuilder.or(restrictions.toArray(EMPTY_PREDICATE_ARRAY)));
List<Accession> res = jpaQueryFactory.selectFrom(QAccession.accession).where(QAccession.accession.id.in(em.createQuery(cq).getResultList())).fetch();
if (LOG.isDebugEnabled())
LOG.trace("*** Loaded accessions {} of {}", res.size(), forUpdate.size());
......
......@@ -23,6 +23,7 @@ import java.util.UUID;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.model.impl.SubsetCreator;
......@@ -44,6 +45,15 @@ public interface SubsetService {
*/
Page<Subset> list(SubsetFilter filter, Pageable page);
/**
* Load AccessionRef list by Subset.
*
* @param subset the subset
* @param page Pageable
* @return PageImpl of AccessionRef
*/
Page<AccessionRef> listAccessions(Subset subset, Pageable page);
/**
* Count published subsets.
*
......
......@@ -27,6 +27,7 @@ import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.service.DatasetService;
......@@ -186,14 +187,23 @@ public class AccessionServiceImpl implements AccessionService {
@Override
public Map<UUID, AccessionIdentifier3> toUUID(List<? extends AccessionIdentifier3> identifiers) {
if (CollectionUtils.isEmpty(identifiers)) {
return Collections.emptyMap();
}
Map<UUID, AccessionIdentifier3> res = new HashMap<>();
List<Accession> foundAccessions = accessionRepository.find(identifiers);
final int chunkSize = 200;
for (Accession accession : foundAccessions) {
Optional<? extends AccessionIdentifier3> toPut = identifiers.stream().filter(id -> id.getAccessionNumber().equals(accession.getAccessionNumber()) && id.getGenus()
.equals(accession.getGenus()) && id.getHoldingInstitute().equals(accession.getInstCode())).findFirst();
for (int fromIndex = 0; fromIndex < identifiers.size(); fromIndex += chunkSize) {
List<Accession> foundAccessions = accessionRepository.find(identifiers.subList(fromIndex, Math.min(identifiers.size(), fromIndex + chunkSize)));
for (Accession accession : foundAccessions) {
Optional<? extends AccessionIdentifier3> toPut = identifiers.stream().filter(id -> id.getAccessionNumber().equals(accession.getAccessionNumber()) && id.getGenus()
.equals(accession.getGenus()) && id.getHoldingInstitute().equals(accession.getInstCode())).findFirst();
toPut.ifPresent(accessionIdentifier3 -> res.put(accession.getUuid(), accessionIdentifier3));
toPut.ifPresent(accessionIdentifier3 -> res.put(accession.getUuid(), accessionIdentifier3));
}
}
return res;
......@@ -201,13 +211,17 @@ public class AccessionServiceImpl implements AccessionService {
@Override
public List<Accession> forUuids(Set<UUID> uuids) {
final int chunkSize = 100;
List<UUID> uuidList = new ArrayList<>(uuids);
List<Accession> accessions = new ArrayList<>(uuidList.size());
if (CollectionUtils.isEmpty(uuids)) {
return Collections.emptyList();
}
List<UUID> uuidList = new ArrayList<>(uuids);
List<Accession> accessions = new ArrayList<>(uuids.size());
for (int i = 0; i < uuids.size(); i+= chunkSize){
List<UUID> chunk = uuidList.subList(i, Math.min(uuids.size(), i+chunkSize));
final int chunkSize = 200;
for (int fromIndex = 0; fromIndex < uuids.size(); fromIndex+= chunkSize){
List<UUID> chunk = uuidList.subList(fromIndex, Math.min(uuids.size(), fromIndex+chunkSize));
accessions.addAll((List<Accession>) accessionRepository.findAll(QAccession.accession.accessionId.uuid.in(chunk)));
}
......
......@@ -20,6 +20,7 @@ import static org.genesys2.server.model.impl.QSubsetCreator.subsetCreator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
......@@ -27,6 +28,7 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.exception.NotFoundElement;
......@@ -105,6 +107,23 @@ public class SubsetServiceImpl implements SubsetService {
return subsetRepository.findAll(published, page);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') || subset.published || hasPermission(subset, 'read')")
public Page<AccessionRef> listAccessions(Subset subset, final Pageable page) {
subset = loadSubset(subset);
final List<AccessionRef> accessionRefs = new ArrayList<AccessionRef>(subset.getAccessionRefs());
if (CollectionUtils.isEmpty(accessionRefs)) {
return new PageImpl<>(Collections.emptyList(), page, 0);
}
accessionRefs.sort((a, b) -> a.getAcceNumb().compareTo(b.getAcceNumb()));
int fromIndex = Math.multiplyExact(page.getPageNumber(), page.getPageSize());
int toIndex = Math.min(fromIndex + page.getPageSize(), accessionRefs.size());
return new PageImpl<>(accessionRefs.subList(fromIndex, toIndex), page, accessionRefs.size());
}
@Override
public long countSubsets(SubsetFilter filter) {
return subsetRepository.count(filter.buildQuery());
......@@ -138,33 +157,10 @@ public class SubsetServiceImpl implements SubsetService {
}
final Subset subset = new Subset();
copyValues(subset, source);
copyAccessions(subset, source.getAccessionRefs());
subset.setState(PublishState.DRAFT);
return deepLoad(subsetRepository.save(subset));
}
/**
* Copy subset accessions.
*
* @param target the target
* @param list the subset accessions
*/
private void copyAccessions(final Subset target, final Set<AccessionRef> list) {
if ((list == null) || (list.size() == 0)) {
if (target.getAccessionRefs() != null)
target.getAccessionRefs().clear();
return;
}
if (target.getAccessionRefs() != null && !target.getAccessionRefs().isEmpty()) {
target.getAccessionRefs().clear();
} else {
target.setAccessionRefs(new HashSet<>());
}
target.getAccessionRefs().addAll(list);
}
/**
* Copy values.
*
......@@ -203,9 +199,7 @@ public class SubsetServiceImpl implements SubsetService {
subset.getInstitute().getId();
if (subset.getCrops() != null)
subset.getCrops().size();
if (subset.getAccessionRefs() != null)
subset.getAccessionRefs().size();
subset.getCrops().size();
if (subset.getCreators() != null)
subset.getCreators().size();
......@@ -260,9 +254,6 @@ public class SubsetServiceImpl implements SubsetService {
public Subset update(final Subset source) {
LOG.info("Update Subset.");
final Subset subset = loadSubset(source);
subset.getAccessionRefs().clear();
copyAccessions(subset, source.getAccessionRefs());
copyValues(subset, source);
return deepLoad(subsetRepository.save(subset));
......
......@@ -15,9 +15,6 @@
*/
package org.genesys.test.catalog.services;
import java.util.List;
import java.util.Set;
import org.genesys.catalog.model.Partner;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.DatasetCreator;
......@@ -26,25 +23,14 @@ import org.genesys.catalog.persistence.dataset.DatasetRepository;
import org.genesys.catalog.persistence.dataset.DatasetVersionsRepository;
import org.genesys.catalog.service.DatasetService;
import org.genesys.filerepository.persistence.RepositoryFilePersistence;
import org.genesys2.server.api.model.AccessionHeaderJson;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.worker.AccessionUploader;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
public abstract class AbstractDatasetServiceTest extends CatalogServiceTest {
protected static final String TEST_INSTCODE = "XYZ001";
......@@ -73,10 +59,6 @@ public abstract class AbstractDatasetServiceTest extends CatalogServiceTest {
protected AccessionRepository accessionRepository;
@Autowired
protected FaoInstituteRepository instituteRepository;
@Autowired
private InstituteService instituteService;
@Autowired
private AccessionUploader accessionUploader;
public AbstractDatasetServiceTest() {
super();
......@@ -95,22 +77,16 @@ public abstract class AbstractDatasetServiceTest extends CatalogServiceTest {
datasetVersionsRepository.deleteAll();
descriptorRepository.deleteAll();
repositoryFilePersistence.deleteAll();
accessionRepository.deleteAll();
instituteRepository.deleteAll();
super.cleanup();
}
protected Dataset buildAndSaveDataset(final String datasetTitle, final String datasetDescription, final Partner partner, final PublishState state) {
final Set<AccessionRef> accessionRefs = Sets.newHashSet(new AccessionRef("InstCode", "TestAccNum", "TestGen", null));
return datasetService.createDataset(buildDataset(datasetTitle, datasetDescription, partner, accessionRefs, state));
return datasetService.createDataset(buildDataset(datasetTitle, datasetDescription, partner, state));
}
protected Dataset buildDataset(final String datasetTitle, final String datasetDescription, final Partner partner, final Set<AccessionRef> accessionRefs, final PublishState state) {
protected Dataset buildDataset(final String datasetTitle, final String datasetDescription, final Partner partner, final PublishState state) {
final Dataset input = new Dataset();
input.setTitle(datasetTitle);
input.setAccessionRefs(accessionRefs);
input.setDescription(datasetDescription);
input.setOwner(partner);
input.setState(state);
......@@ -140,47 +116,4 @@ public abstract class AbstractDatasetServiceTest extends CatalogServiceTest {
return new AccessionRef(instCode, acceNumb, genus, doi);
}
protected Accession upsertAccession(final String instCode, final String acceNumb, final String genus) {
FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
institute = new FaoInstitute();
institute.setCode(instCode);
institute.setFullName("Test institute " + instCode);
instituteService.update(Lists.newArrayList(institute));
institute = instituteService.getInstitute(instCode);
}
ObjectMapper objectMapper = new ObjectMapper();
ArrayNode updates = objectMapper.createArrayNode();
ObjectNode accession = updates.addObject();
accession.put("instituteCode", instCode);
accession.put("accessionNumber", acceNumb);
ObjectNode taxa = accession.putObject("taxonomy");
taxa.put("genus", genus);
accessionUploader.upsertAccessions(institute, updates);
return accessionRepository.findOne(institute, null, acceNumb, genus);
}
protected void deleteAccession(final String instCode, final String acceNumb, final String genus) {
FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
institute = new FaoInstitute();
institute.setCode(instCode);
institute.setFullName("Test institute " + instCode);
instituteService.update(Lists.newArrayList(institute));
institute = instituteService.getInstitute(instCode);
}
AccessionHeaderJson ah=new AccessionHeaderJson();
ah.instCode=instCode;
ah.acceNumb=acceNumb;
ah.genus=genus;
List<AccessionHeaderJson> identifiers=Lists.newArrayList(ah);
accessionUploader.deleteAccessions(institute, identifiers);
assert(accessionRepository.findOne(institute, null, acceNumb, genus) == null);
}
}
\ No newline at end of file
......@@ -17,6 +17,7 @@ package org.genesys.test.catalog.services;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Set;
import org.genesys.catalog.model.Partner;
......@@ -35,9 +36,20 @@ import org.genesys.catalog.service.PartnerService;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys.catalog.service.VocabularyService;
import org.genesys.test.base.AbstractServiceTest;
import org.genesys2.server.api.model.AccessionHeaderJson;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.worker.AccessionUploader;
import org.junit.After;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
/**
......@@ -97,6 +109,18 @@ public abstract class CatalogServiceTest extends AbstractServiceTest {
@Autowired
protected ShortFilterRepository shortFilterRepository;
@Autowired
private InstituteService instituteService;
@Autowired
private AccessionUploader accessionUploader;
@Autowired
protected AccessionRepository accessionRepository;
@Autowired
protected FaoInstituteRepository instituteRepository;
protected Partner partner;
protected Partner partner2;
......@@ -106,6 +130,9 @@ public abstract class CatalogServiceTest extends AbstractServiceTest {
partnerRepository.deleteAll();
partner = setupPartner(PARTNER_1, PARTNER_ACRONYM_1, Sets.newHashSet(URL_1));
partner2 = setupPartner(PARTNER_2, PARTNER_ACRONYM_2, Sets.newHashSet(URL_1));
accessionRepository.deleteAll();
instituteRepository.deleteAll();
super.cleanup();
}
......@@ -153,4 +180,46 @@ public abstract class CatalogServiceTest extends AbstractServiceTest {
return input;
}
protected Accession upsertAccession(final String instCode, final String acceNumb, final String genus) {
FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
institute = new FaoInstitute();
institute.setCode(instCode);
institute.setFullName("Test institute " + instCode);
instituteService.update(Lists.newArrayList(institute));
institute = instituteService.getInstitute(instCode);
}
ObjectMapper objectMapper = new ObjectMapper();
ArrayNode updates = objectMapper.createArrayNode();
ObjectNode accession = updates.addObject();
accession.put("instituteCode", instCode);
accession.put("accessionNumber", acceNumb);
ObjectNode taxa = accession.putObject("taxonomy");
taxa.put("genus", genus);
accessionUploader.upsertAccessions(institute, updates);
return accessionRepository.findOne(institute, null, acceNumb, genus);
}
protected void deleteAccession(final String instCode, final String acceNumb, final String genus) {
FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
institute = new FaoInstitute();
institute.setCode(instCode);
institute.setFullName("Test institute " + instCode);
instituteService.update(Lists.newArrayList(institute));
institute = instituteService.getInstitute(instCode);
}
AccessionHeaderJson ah = new AccessionHeaderJson();
ah.instCode = instCode;
ah.acceNumb = acceNumb;
ah.genus = genus;
List<AccessionHeaderJson> identifiers = Lists.newArrayList(ah);
accessionUploader.deleteAccessions(institute, identifiers);
assert(accessionRepository.findOne(institute, null, acceNumb, genus) == null);
}
}
......@@ -3,12 +3,18 @@ package org.genesys.test.catalog.services;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;