Commit 88c3b808 authored by Matija Obreza's avatar Matija Obreza

Merge branch 'removed-accessionRefs-indexing' into 'master'

Fix: Removing AccessionRef must schedule indexing of accessionRef#accession to update ES index

See merge request genesys-pgr/genesys-server!456
parents bd49d16b 85edec33
......@@ -104,7 +104,7 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Acl
private Partner owner;
/** The accession identifiers. */
@OneToMany(mappedBy = "dataset", cascade = { CascadeType.REFRESH, CascadeType.DETACH, CascadeType.REMOVE }, orphanRemoval = true, fetch = FetchType.LAZY)
@OneToMany(mappedBy = "dataset", cascade = {}, fetch = FetchType.LAZY)
@Field(type = FieldType.Object)
@JsonIgnore
private List<DatasetAccessionRef> accessionRefs;
......
......@@ -30,14 +30,6 @@ import org.springframework.stereotype.Repository;
@Repository
public interface DatasetAccessionRefRepository extends AccessionRefRepository<DatasetAccessionRef> {
/**
* Delete for dataset.
*
* @param dataset the dataset
* @return the long
*/
long deleteForDataset(Dataset dataset);
/**
* @param dataset
* @return
......
......@@ -149,6 +149,14 @@ public class DatasetAccessionRefRepositoryCustomImpl implements DatasetAccession
delete(entity);
}
}
@Override
@Transactional
public void delete(Iterable<DatasetAccessionRef> entities) {
for (DatasetAccessionRef entity : entities) {
delete(entity);
}
}
/**
* Delete.
......@@ -161,15 +169,6 @@ public class DatasetAccessionRefRepositoryCustomImpl implements DatasetAccession
em.remove(em.contains(entity) ? entity : em.merge(entity));
}
/* (non-Javadoc)
* @see org.genesys.catalog.persistence.dataset.DatasetAccessionRefRepository#deleteForDataset(org.genesys.catalog.model.dataset.Dataset)
*/
@Override
@Transactional
public long deleteForDataset(Dataset dataset) {
return jpaQueryFactory.delete(QDatasetAccessionRef.datasetAccessionRef).where(QDatasetAccessionRef.datasetAccessionRef.dataset.eq(dataset)).execute();
}
/* (non-Javadoc)
* @see org.genesys.catalog.persistence.dataset.DatasetAccessionRefRepository#findAll(org.genesys.catalog.model.dataset.Dataset, org.springframework.data.domain.Pageable)
*/
......
......@@ -105,6 +105,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
......@@ -252,11 +253,17 @@ public class DatasetServiceImpl implements DatasetService {
dataset = getUnpublishedDataset(dataset);
LOG.info("Update AccessionIdentifiers. Input dataset: {}, input accessionRefs {}", dataset, accessionRefs);
long deleted = accessionRefRepository.deleteForDataset(dataset);
LOG.trace("Deleted {} refs for dataset", deleted);
deleteAccessionRefs(dataset);
return addAccessionRefs(dataset, accessionRefs);
}
private void deleteAccessionRefs(Dataset dataset) {
Iterables.partition(dataset.getAccessionRefs(), 1000).forEach(batch -> {
accessionRefRepository.delete(batch);
});
dataset.getAccessionRefs().clear();
}
/**
* {@inheritDoc}
*/
......@@ -760,7 +767,8 @@ public class DatasetServiceImpl implements DatasetService {
LOG.warn("Could not delete file {} from dataset {}", repositoryFile.getUuid(), dataset.getUuid());
}
}
deleteAccessionRefs(loadedDataset);
datasetRepository.delete(loadedDataset);
try {
......
......@@ -17,6 +17,7 @@ package org.genesys2.brapi.model;
import java.util.Arrays;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMethod;
/**
......@@ -46,6 +47,9 @@ public class Call {
}
this.methods = requestMethods;
this.dataTypes = dataTypes;
if (this.dataTypes == null || this.dataTypes.length == 0) {
this.dataTypes = new String[] { MediaType.APPLICATION_JSON_VALUE };
}
}
/**
......
......@@ -113,7 +113,7 @@ public class MeController extends ApiBaseController {
if (authentication instanceof OAuth2Authentication) {
OAuth2Authentication oauthAuth = (OAuth2Authentication) authentication;
// System.err.println(oauthAuth);
LOG.trace("authentication: {}", oauthAuth);
if (oauthAuth.isClientOnly()) {
LOG.debug("OAuth client-only authentication: {}", oauthAuth);
......@@ -124,7 +124,6 @@ public class MeController extends ApiBaseController {
Authentication userAuth = oauthAuth.getUserAuthentication();
if (userAuth != null) {
// System.err.println(userAuth);
LOG.debug("OAuth user authentication: {} {}", userAuth.getClass(), userAuth);
Object principal = userAuth.getPrincipal();
LOG.debug("OAuth user principal: {} {}", principal.getClass(), principal);
......
......@@ -214,20 +214,6 @@ public class SubsetController extends ApiBaseController {
return subsetService.listAccessions(subsetService.getSubset(uuid), page.toPageRequest(100));
}
/**
* Remove accessions form subset.
*
* @param uuid the uuid
* @param version the version
* @param accessionsUuids the accessions UUIDs to be removed
* @return the subset
*/
@PostMapping(value = "/remove-accessions/{uuid},{version}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Subset removeAccessions(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version, @RequestBody final Set<UUID> accessionsUuids) {
final Subset subset = subsetService.getSubset(uuid, version);
return subsetService.removeAccessions(subset, accessionsUuids);
}
/**
* Add accessions to subset.
*
......
......@@ -80,6 +80,23 @@ public class ExtraReindexingAspect {
}
}
/**
* After AccessionRefRepository#delete
*
* @param joinPoint the join point
*/
@AfterReturning(value = "execution(* org.genesys2.server.persistence.AccessionRefRepository.delete(..))")
public void afterDeleteAccessionRef(final JoinPoint joinPoint) {
LOG.trace("accessionRefRepository#delete {} {}", joinPoint.toLongString(), joinPoint.getTarget());
try {
final Object[] args = joinPoint.getArgs();
scheduleReindexing(args[0]);
} catch (Throwable e) {
LOG.error(e.getMessage(), e);
}
}
/**
* Flatten collections (if Iterable) and check each object class if indexed
......
......@@ -127,7 +127,7 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
private Partner owner;
/** The accessionRefs. */
@OneToMany(mappedBy="subset", orphanRemoval=true, fetch=FetchType.LAZY, cascade= { CascadeType.REFRESH, CascadeType.DETACH, CascadeType.REMOVE })
@OneToMany(mappedBy = "subset", fetch = FetchType.LAZY, cascade = {})
@Field(type = FieldType.Object)
@JsonIgnore
private List<SubsetAccessionRef> accessionRefs;
......
......@@ -33,6 +33,8 @@ public interface AccessionRefRepository<T extends AccessionRef> {
*/
void deleteAll();
void delete(Iterable<T> entities);
/**
* Save
*
......
......@@ -31,14 +31,6 @@ import org.springframework.stereotype.Repository;
@Repository
public interface SubsetAccessionRefRepository extends AccessionRefRepository<SubsetAccessionRef> {
/**
* Delete for subset.
*
* @param subset the subset
* @return the long
*/
long deleteForSubset(Subset subset);
/**
* @param subset the subset
* @return
......
......@@ -152,6 +152,14 @@ public class SubsetAccessionRefRepositoryCustomImpl implements SubsetAccessionRe
}
}
@Override
@Transactional
public void delete(Iterable<SubsetAccessionRef> entities) {
for (SubsetAccessionRef entity: entities) {
delete(entity);
}
}
/**
* Delete.
*
......@@ -163,15 +171,6 @@ public class SubsetAccessionRefRepositoryCustomImpl implements SubsetAccessionRe
em.remove(em.contains(entity) ? entity : em.merge(entity));
}
/* (non-Javadoc)
* @see org.genesys2.server.persistence.SubsetAccessionRefRepository#deleteForSubset(org.genesys2.server.model.impl.Subset)
*/
@Override
@Transactional
public long deleteForSubset(Subset subset) {
return jpaQueryFactory.delete(QSubsetAccessionRef.subsetAccessionRef).where(QSubsetAccessionRef.subsetAccessionRef.subset.eq(subset)).execute();
}
/* (non-Javadoc)
* @see org.genesys2.server.persistence.SubsetAccessionRefRepository#delete(org.genesys2.server.model.impl.Subset, java.util.Collection)
*/
......
......@@ -145,15 +145,6 @@ public interface SubsetService {
*/
Subset delete(Subset subset);
/**
* Method removing preserved accessionIds from Subset.
*
* @param input current Subset
* @param accesionsUuids the accessions UUIDs
* @return updated Subset in db.
*/
Subset removeAccessions(Subset input, Set<UUID> accesionsUuids);
/**
* Method adding new accessionId to Subset.
*
......
......@@ -16,7 +16,7 @@
package org.genesys2.server.service.impl;
import static org.genesys2.server.model.impl.QSubsetCreator.subsetCreator;
import static org.genesys2.server.model.impl.QSubsetCreator.*;
import java.io.IOException;
import java.io.OutputStream;
......@@ -35,12 +35,11 @@ import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.validation.Valid;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.catalog.model.Partner;
import org.genesys2.server.component.aspect.NotifyOnPublished;
import org.genesys2.server.component.aspect.NotifyForReview;
import org.genesys2.server.component.aspect.NotifyOnPublished;
import org.genesys2.server.component.security.SecurityUtils;
import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.exception.NotFoundElement;
......@@ -56,7 +55,6 @@ import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.model.impl.SubsetAccessionRef;
import org.genesys2.server.model.impl.SubsetCreator;
import org.genesys2.server.model.impl.SubsetVersions;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.persistence.SubsetAccessionRefRepository;
import org.genesys2.server.persistence.SubsetCreatorRepository;
......@@ -89,7 +87,9 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.PathBuilder;
......@@ -121,9 +121,6 @@ public class SubsetServiceImpl implements SubsetService {
@Autowired
private SubsetVersionsRepository subsetVersionsRepository;
@Autowired
private AccessionRepository accessionRepository;
@Autowired
private FaoInstituteRepository instituteRepository;
......@@ -391,25 +388,11 @@ public class SubsetServiceImpl implements SubsetService {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#input, 'DELETE')")
public Subset delete(final Subset input) {
final Subset loadedSubset = getUnpublishedSubset(input);
deleteAccessionRefs(loadedSubset);
subsetRepository.delete(loadedSubset);
return loadedSubset;
}
/**
* {@inheritDoc}
*/
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#input, 'WRITE')")
public Subset removeAccessions(final Subset input, final Set<UUID> accessionsUuids) {
final Subset loadedSubset = getUnpublishedSubset(input);
LOG.info("Remove accessions of subset {}.", input);
accessionRefRepository.delete(loadedSubset, accessionRepository.find(accessionsUuids));
loadedSubset.setAccessionCount((int) accessionRefRepository.countBySubset(loadedSubset));
return lazyLoad(subsetRepository.save(loadedSubset));
}
/**
* {@inheritDoc}
*/
......@@ -420,9 +403,16 @@ public class SubsetServiceImpl implements SubsetService {
final Subset loadedSubset = getUnpublishedSubset(input);
LOG.info("Set accessions to Subset. Input accessions {}", input);
accessionRefRepository.deleteForSubset(loadedSubset);
deleteAccessionRefs(loadedSubset);
return addAccessionRefs(loadedSubset, accessionRefs);
}
private void deleteAccessionRefs(final Subset loadedSubset) {
Iterables.partition(loadedSubset.getAccessionRefs(), 1000).forEach(batch -> {
accessionRefRepository.delete(batch);
});
loadedSubset.getAccessionRefs().clear();
}
/**
* {@inheritDoc}
......
......@@ -19,6 +19,7 @@ import org.genesys.catalog.model.Partner;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.DatasetCreator;
import org.genesys.catalog.model.dataset.DatasetLocation;
import org.genesys.catalog.persistence.dataset.DatasetAccessionRefRepository;
import org.genesys.catalog.persistence.dataset.DatasetRepository;
import org.genesys.catalog.persistence.dataset.DatasetVersionsRepository;
import org.genesys.catalog.service.DatasetService;
......@@ -54,6 +55,8 @@ public abstract class AbstractDatasetServiceTest extends CatalogServiceTest {
@Autowired
protected DatasetRepository datasetRepository;
@Autowired
protected DatasetAccessionRefRepository accessionRefRepository;
@Autowired
protected RepositoryFilePersistence repositoryFilePersistence;
@Autowired
protected AccessionRepository accessionRepository;
......@@ -73,6 +76,7 @@ public abstract class AbstractDatasetServiceTest extends CatalogServiceTest {
@After
@Override
public void cleanup() throws Exception {
accessionRefRepository.deleteAll();
datasetVersionsRepository.deleteAll();
datasetRepository.deleteAll();
descriptorRepository.deleteAll();
......
......@@ -15,6 +15,7 @@ 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;
import org.genesys2.server.persistence.SubsetAccessionRefRepository;
import org.genesys2.server.persistence.SubsetCreatorRepository;
import org.genesys2.server.persistence.SubsetRepository;
import org.genesys2.server.service.InstituteService;
......@@ -41,6 +42,9 @@ public class SubsetServiceTest extends CatalogServiceTest {
@Autowired
private InstituteService instituteService;
@Autowired
private SubsetAccessionRefRepository accessionRefRepository;
private Subset subset;
private SubsetCreator subsetCreator;
......@@ -73,6 +77,7 @@ public class SubsetServiceTest extends CatalogServiceTest {
@Override
public void cleanup() throws Exception {
subsetCreatorRepository.deleteAll();
accessionRefRepository.deleteAll();
subsetRepository.deleteAll();
super.cleanup();
......
......@@ -124,8 +124,8 @@ public class SubsetRestControllerTest extends AbstractApiTest {
@Override
@Transactional
public void cleanup() throws Exception {
subsetVersionsRepository.deleteAll();
accessionRefRepository.deleteAll();
subsetVersionsRepository.deleteAll();
subsetRepository.deleteAll();
accessionRepository.deleteAll();
accessionIdRepository.deleteAll();
......@@ -280,30 +280,6 @@ public class SubsetRestControllerTest extends AbstractApiTest {
/*@formatter:on*/
}
@Test
public void removeAccessionsFromSubsetTest() throws Exception {
Subset subset = subsetService.create(institute, setUpSubset());
assertThat(subset, notNullValue());
Set<AccessionRef> newAccessions = setUpAccessions();
Set<UUID> accessionUUIDs = newAccessions.stream().map(ref -> ref.getAccession().getUuid()).collect(Collectors.toSet());
subset = subsetService.addAccessionRefs(subset, newAccessions);
assertThat(subset.getAccessionCount(), is(newAccessions.size()));
/*@formatter:off*/
mockMvc.perform(post(SubsetController.API_BASE.concat("/remove-accessions/{uuid},{version}"), subset.getUuid(), subset.getVersion())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(accessionUUIDs)))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
/*@formatter:on*/
final Subset updated = subsetService.getSubset(subset.getUuid());
assertThat(updated.getAccessionCount(), is(0));
}
@Test
public void addAccessionsToSubsetTest() throws Exception {
final Subset subset = subsetService.create(institute, setUpSubset());
......
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