Commit bdf9cd88 authored by Matija Obreza's avatar Matija Obreza

Cleanup

parent 2bd49d09
......@@ -23,7 +23,6 @@ import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
......@@ -86,12 +85,11 @@ public class DatasetAccessionRefRepositoryCustomImpl implements DatasetAccession
LOG.info("Batch {} with size {}", batchCounter.incrementAndGet(), batch.size());
CriteriaQuery<DatasetAccessionRef> cq = criteriaBuilder.createQuery(DatasetAccessionRef.class);
Root<DatasetAccessionRef> root = cq.from(DatasetAccessionRef.class);
Join<Object, Object> dataset = root.join("dataset");
cq.distinct(true);
// cq.select(root);
// Path<Object> theDoi = root.get("doi");
Path<Object> theDatasetId = dataset.get("id");
Path<Object> theDataset = root.get("dataset");
Path<Object> theInstCode = root.get("instCode");
Path<Object> theAcceNumb = root.get("acceNumb");
Path<Object> theGenus = root.get("genus");
......@@ -100,7 +98,7 @@ public class DatasetAccessionRefRepositoryCustomImpl implements DatasetAccession
// A lot of .. (instCode=? and acceNumb=? and genus=?)
for (DatasetAccessionRef ref : batch) {
restrictions.add(criteriaBuilder.and(criteriaBuilder.equal(theDatasetId, ref.getDataset().getId()), criteriaBuilder.equal(theInstCode, ref
restrictions.add(criteriaBuilder.and(criteriaBuilder.equal(theDataset, ref.getDataset()), criteriaBuilder.equal(theInstCode, ref
.getInstCode()), criteriaBuilder.equal(theAcceNumb, ref
.getAcceNumb()), criteriaBuilder.equal(theGenus, ref.getGenus())));
}
......
......@@ -601,7 +601,7 @@ public class DatasetServiceImpl implements DatasetService {
throw new InvalidApiUsageException("Cannot modify a published Dataset.");
}
LOG.warn("Matching " + accessionRefs.size() + " with Accessions");
LOG.warn("Adding {} accession references to Dataset.", accessionRefs.size());
List<DatasetAccessionRef> dArs = Lists.newArrayList();
accessionRefs.forEach(ref -> {
DatasetAccessionRef dAr = new DatasetAccessionRef(ref);
......@@ -632,7 +632,6 @@ public class DatasetServiceImpl implements DatasetService {
throw ex;
}
transactionManager.commit(status);
LOG.warn("Done relinking records");;
} catch (InterruptedException e) {
LOG.warn("Interrupted rematcher");
......@@ -1005,7 +1004,7 @@ public class DatasetServiceImpl implements DatasetService {
});
});
LOG.warn("Matched accessions after {}ms", stopWatch.getTime());
LOG.warn("Matched {} accession refs after {}ms", accessionRefs.size(), stopWatch.getTime());
return accessionRefs;
}
......@@ -1023,6 +1022,9 @@ public class DatasetServiceImpl implements DatasetService {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public Dataset rematchDatasetAccessions(Dataset dataset) {
dataset = datasetRepository.findByUuid(dataset.getUuid());
if (dataset == null) {
return dataset;
}
LOG.warn("Linking {} accessions with dataset {}", dataset.getAccessionCount(), dataset.getId());
final AtomicInteger batchCounter = new AtomicInteger(0);
......@@ -1035,6 +1037,7 @@ public class DatasetServiceImpl implements DatasetService {
});
// Save in this transaction
accessionRefRepository.update(accessionRefs);
LOG.warn("Done relinking {} accession refs.", accessionRefs.size());
return lazyLoad(dataset);
}
......
......@@ -24,7 +24,6 @@ import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
......@@ -88,12 +87,11 @@ public class SubsetAccessionRefRepositoryCustomImpl implements SubsetAccessionRe
LOG.info("Batch {} with size {}", batchCounter.incrementAndGet(), batch.size());
CriteriaQuery<SubsetAccessionRef> cq = criteriaBuilder.createQuery(SubsetAccessionRef.class);
Root<SubsetAccessionRef> root = cq.from(SubsetAccessionRef.class);
Join<Object, Object> subset = root.join("subset");
cq.distinct(true);
// cq.select(root);
// Path<Object> theDoi = root.get("doi");
Path<Object> theSubsetId = subset.get("id");
Path<Object> theSubset = root.get("subset");
Path<Object> theInstCode = root.get("instCode");
Path<Object> theAcceNumb = root.get("acceNumb");
Path<Object> theGenus = root.get("genus");
......@@ -102,7 +100,7 @@ public class SubsetAccessionRefRepositoryCustomImpl implements SubsetAccessionRe
// A lot of .. (instCode=? and acceNumb=? and genus=?)
for (SubsetAccessionRef ref : batch) {
restrictions.add(criteriaBuilder.and(criteriaBuilder.equal(theSubsetId, ref.getSubset().getId()), criteriaBuilder.equal(theInstCode, ref
restrictions.add(criteriaBuilder.and(criteriaBuilder.equal(theSubset, ref.getSubset()), criteriaBuilder.equal(theInstCode, ref
.getInstCode()), criteriaBuilder.equal(theAcceNumb, ref
.getAcceNumb()), criteriaBuilder.equal(theGenus, ref.getGenus())));
}
......
......@@ -254,7 +254,11 @@ public class SubsetServiceImpl implements SubsetService {
*/
@Override
public Subset getSubset(final UUID uuid) {
return subsetRepository.getByUuid(uuid);
Subset subset = subsetRepository.getByUuid(uuid);
if (subset == null) {
throw new NotFoundElement("Record not found by UUID=" + uuid);
}
return subset;
}
/**
......@@ -264,8 +268,9 @@ public class SubsetServiceImpl implements SubsetService {
@PostAuthorize("hasRole('ADMINISTRATOR') || returnObject==null || returnObject.isPublished() || hasPermission(returnObject, 'read')")
public Subset getSubset(final UUID uuid, final int version) {
final Subset subset = subsetRepository.getByUuidAndVersion(uuid, version);
if (subset == null)
if (subset == null) {
throw new ConcurrencyFailureException("Record with that version doesn't exist");
}
return subsetRepository.getByUuidAndVersion(uuid, version);
}
......@@ -333,21 +338,24 @@ public class SubsetServiceImpl implements SubsetService {
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#input, 'WRITE')")
public Subset addAccessionRefs(final Subset input, @Valid final Set<AccessionRef> accessionRefs) {
LOG.info("Add accessions to Subset. Input accessions {}", input);
final Subset subset = getSubset(input);
if (subset.isPublished()) {
throw new InvalidApiUsageException("Subset is published, no edits allowed.");
final Subset loadedSubset = getSubset(input);
if (loadedSubset == null) {
throw new NotFoundElement("Subset doesn't exist");
}
if (loadedSubset.isPublished()) {
throw new InvalidApiUsageException("Cannot modify a published Subset.");
}
LOG.warn("Adding {} accession references to Subset.", accessionRefs.size());
List<SubsetAccessionRef> sArs = Lists.newArrayList();
accessionRefs.forEach(ref -> {
SubsetAccessionRef sAr = new SubsetAccessionRef(ref);
sAr.setSubset(subset);
sAr.setSubset(loadedSubset);
sArs.add(sAr);
});
accessionRefRepository.save(sArs);
subset.setAccessionCount((int) accessionRefRepository.countBySubset(subset));
LOG.warn("Done saving {} accession refs, have {} in subset", accessionRefs.size(), subset.getAccessionCount());
loadedSubset.setAccessionCount((int) accessionRefRepository.countBySubset(loadedSubset));
LOG.warn("Done saving {} accession refs, have {} in subset", accessionRefs.size(), loadedSubset.getAccessionCount());
threadPoolTaskExecutor.execute(() -> {
try {
......@@ -362,20 +370,19 @@ public class SubsetServiceImpl implements SubsetService {
TransactionStatus status = transactionManager.getTransaction(def);
try {
// execute your business logic here
rematchSubsetAccessions(subset);
rematchSubsetAccessions(loadedSubset);
} catch (Throwable ex) {
LOG.error("Rolling back rematch. Exception: {}", ex.getMessage(), ex);
transactionManager.rollback(status);
throw ex;
}
transactionManager.commit(status);
LOG.warn("Done relinking records");;
} catch (InterruptedException e) {
LOG.warn("Interrupted rematcher");
}
});
return lazyLoad(subsetRepository.save(subset));
return lazyLoad(subsetRepository.save(loadedSubset));
}
/**
......@@ -573,6 +580,9 @@ public class SubsetServiceImpl implements SubsetService {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#subset, 'write')")
public Subset rematchSubsetAccessions(Subset subset) {
subset = subsetRepository.getByUuid(subset.getUuid());
if (subset == null) {
return subset;
}
LOG.warn("Linking {} accessions with subset {}", subset.getAccessionCount(), subset.getId());
final AtomicInteger batchCounter = new AtomicInteger(0);
......@@ -585,6 +595,7 @@ public class SubsetServiceImpl implements SubsetService {
});
// Save in this transaction
accessionRefRepository.update(accessionRefs);
LOG.warn("Done relinking {} accession refs.", accessionRefs.size());
return lazyLoad(subset);
}
......@@ -606,7 +617,7 @@ public class SubsetServiceImpl implements SubsetService {
LOG.info("Batch {} with size {}", batchCounter.incrementAndGet(), batch.size());
List<Accession> foundAccessions = accessionRepository.find(batch);
LOG.warn("Found {} matches for {} identifiers after {}ms", foundAccessions.size(), batch.size(), stopWatch.getTime());
LOG.info("Found {} matches for {} identifiers after {}ms", foundAccessions.size(), batch.size(), stopWatch.getTime());
// instCode / genus / acceNumb map
Map<String, Map<String, Map<String, Accession>>> instMap = new HashMap<>();
......@@ -651,7 +662,7 @@ public class SubsetServiceImpl implements SubsetService {
});
});
LOG.warn("Matched accessions after {}ms", stopWatch.getTime());
LOG.warn("Matched {} accession refs after {}ms", accessionRefs.size(), stopWatch.getTime());
return accessionRefs;
}
......
......@@ -168,7 +168,6 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
assertThat(input, not(nullValue()));
assertThat(input.getAccessionCount(), is(1));
datasetService.rematchDatasetAccessions(input);
assertThat(input.getAccessionRefs().iterator().next(), is(datasetAccessionRefs.iterator().next()));
final AccessionRef datasetAccessionRef = datasetService.listAccessions(input, new PageRequest(0, 10)).getContent().get(0);
assertThat(datasetAccessionRef, notNullValue());
......@@ -206,6 +205,7 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
// update dataset with a new list of accessions
input = datasetService.setAccessionRefs(input, accessionIdentifiers2);
datasetService.rematchDatasetAccessions(input);
assertThat(input, not(nullValue()));
// test if dataset has a new set of accessions
......
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