Commit 82804573 authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch 'versioned-modifieddate-and-other-fixes' into 'main'

Versioned modifieddate and other fixes

See merge request grin-global/grin-global-server!282
parent 7ee2a92c
...@@ -359,15 +359,15 @@ public class AccessionServiceImpl extends FilteredCRUDServiceImpl<Accession, Acc ...@@ -359,15 +359,15 @@ public class AccessionServiceImpl extends FilteredCRUDServiceImpl<Accession, Acc
throw new InvalidApiUsageException("No accessions in acquisition batch"); throw new InvalidApiUsageException("No accessions in acquisition batch");
} }
Method method = acquisitionBatch.methodId == null ? null : methodRepository.getOne(acquisitionBatch.methodId); Method method = acquisitionBatch.methodId == null ? null : methodRepository.getById(acquisitionBatch.methodId);
if (method != null) { if (method != null) {
LOG.info("Registering new material with method {}", method.getName()); LOG.info("Registering new material with method {}", method.getName());
} }
Site site = siteRepository.getOne(acquisitionBatch.siteId); Site site = siteRepository.getById(acquisitionBatch.siteId);
LOG.info("Registering new material with site {}", site.getSiteShortName()); LOG.info("Registering new material with site {}", site.getSiteShortName());
InventoryMaintenancePolicy inventoryMaintPolicy = inventoryMaintenancePolicyRepository.getOne(acquisitionBatch.inventoryMaintenancePolicyId); InventoryMaintenancePolicy inventoryMaintPolicy = inventoryMaintenancePolicyRepository.getById(acquisitionBatch.inventoryMaintenancePolicyId);
LOG.info("Using policy {}", inventoryMaintPolicy.getMaintenanceName()); LOG.info("Using policy {}", inventoryMaintPolicy.getMaintenanceName());
// Make accessions // Make accessions
......
...@@ -80,7 +80,7 @@ public abstract class BaseAttachmentSupport<T extends CooperatorOwnedModel, A ex ...@@ -80,7 +80,7 @@ public abstract class BaseAttachmentSupport<T extends CooperatorOwnedModel, A ex
@Override @Override
@Transactional @Transactional
public A uploadFile(T entity, MultipartFile file, R metadata) throws IOException, InvalidRepositoryPathException, InvalidRepositoryFileDataException { public A uploadFile(T entity, MultipartFile file, R metadata) throws IOException, InvalidRepositoryPathException, InvalidRepositoryFileDataException {
T savedEntity = owningEntityRepository.getOne(entity.getId()); T savedEntity = owningEntityRepository.getById(entity.getId());
Path repositoryPath = createRepositoryPath(savedEntity); Path repositoryPath = createRepositoryPath(savedEntity);
LOG.info("Upload file {} to path {}", file.getOriginalFilename(), repositoryPath); LOG.info("Upload file {} to path {}", file.getOriginalFilename(), repositoryPath);
...@@ -114,7 +114,7 @@ public abstract class BaseAttachmentSupport<T extends CooperatorOwnedModel, A ex ...@@ -114,7 +114,7 @@ public abstract class BaseAttachmentSupport<T extends CooperatorOwnedModel, A ex
@Override @Override
@Transactional @Transactional
public A removeFile(T entity, Long attachmentId) { public A removeFile(T entity, Long attachmentId) {
entity = owningEntityRepository.getOne(entity.getId()); entity = owningEntityRepository.getById(entity.getId());
A toDelete = attachFinder.findOne(qActionOwningEntityId.eq(entity.getId()).and(qAttachId.eq(attachmentId))) A toDelete = attachFinder.findOne(qActionOwningEntityId.eq(entity.getId()).and(qAttachId.eq(attachmentId)))
.orElseThrow(() -> new NotFoundElement("No such attach for provided entity")); .orElseThrow(() -> new NotFoundElement("No such attach for provided entity"));
......
...@@ -92,7 +92,7 @@ public abstract class BaseTranslationSupport<T extends TranslatedCooperatorOwned ...@@ -92,7 +92,7 @@ public abstract class BaseTranslationSupport<T extends TranslatedCooperatorOwned
@Override @Override
public Translation<T> getTranslated(T input) { public Translation<T> getTranslated(T input) {
T savedEntity = owningEntityRepository.getOne(input.getId()); T savedEntity = owningEntityRepository.getById(input.getId());
SysLang targetLanguage = languageService.getLanguage(LocaleContextHolder.getLocale()); SysLang targetLanguage = languageService.getLanguage(LocaleContextHolder.getLocale());
......
...@@ -110,8 +110,9 @@ public abstract class CRUDServiceImpl<T extends EmptyModel, R extends JpaReposit ...@@ -110,8 +110,9 @@ public abstract class CRUDServiceImpl<T extends EmptyModel, R extends JpaReposit
T current = get(source.getId()); T current = get(source.getId());
if (current instanceof AuditedModel) { if (current instanceof AuditedModel) {
AuditedModel audited = (AuditedModel) current; AuditedModel audited = (AuditedModel) current;
AuditedModel sourceAudited = (AuditedModel) source;
// check that target has the same modifiedDate as source // check that target has the same modifiedDate as source
if (((AuditedModel) source).getModifiedDate() == null || audited.getModifiedDate().getTime() != (((AuditedModel) source).getModifiedDate().getTime())) { if (audited.getModifiedDate() != null && (sourceAudited.getModifiedDate() == null || audited.getModifiedDate().getTime() != sourceAudited.getModifiedDate().getTime())) {
throw new ObjectOptimisticLockingFailureException(source.getClass(), source.getId()); throw new ObjectOptimisticLockingFailureException(source.getClass(), source.getId());
} }
} }
......
...@@ -94,7 +94,7 @@ public class CropServiceImpl extends FilteredCRUDServiceImpl<Crop, CropFilter, C ...@@ -94,7 +94,7 @@ public class CropServiceImpl extends FilteredCRUDServiceImpl<Crop, CropFilter, C
@Override @Override
protected Path createRepositoryPath(Crop crop) { protected Path createRepositoryPath(Crop crop) {
crop = owningEntityRepository.getOne(crop.getId()); crop = owningEntityRepository.getById(crop.getId());
return Paths.get("/crop/" + crop.getName()); return Paths.get("/crop/" + crop.getName());
} }
......
...@@ -110,7 +110,7 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe ...@@ -110,7 +110,7 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe
@Override @Override
protected Path createRepositoryPath(CropTrait cropTrait) { protected Path createRepositoryPath(CropTrait cropTrait) {
cropTrait = owningEntityRepository.getOne(cropTrait.getId()); cropTrait = owningEntityRepository.getById(cropTrait.getId());
Hibernate.initialize(cropTrait.getCrop()); Hibernate.initialize(cropTrait.getCrop());
Crop crop = cropTrait.getCrop(); Crop crop = cropTrait.getCrop();
...@@ -155,7 +155,7 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe ...@@ -155,7 +155,7 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe
@Override @Override
protected Path createRepositoryPath(CropTraitCode cropTraitCode) { protected Path createRepositoryPath(CropTraitCode cropTraitCode) {
cropTraitCode = owningEntityRepository.getOne(cropTraitCode.getId()); cropTraitCode = owningEntityRepository.getById(cropTraitCode.getId());
Hibernate.initialize(cropTraitCode.getCropTrait()); Hibernate.initialize(cropTraitCode.getCropTrait());
CropTrait cropTrait = cropTraitCode.getCropTrait(); CropTrait cropTrait = cropTraitCode.getCropTrait();
......
...@@ -130,7 +130,7 @@ public class InventoryServiceImpl extends FilteredCRUDServiceImpl<Inventory, Inv ...@@ -130,7 +130,7 @@ public class InventoryServiceImpl extends FilteredCRUDServiceImpl<Inventory, Inv
*/ */
@Override @Override
protected Path createRepositoryPath(Inventory inventory) { protected Path createRepositoryPath(Inventory inventory) {
inventory = owningEntityRepository.getOne(inventory.getId()); inventory = owningEntityRepository.getById(inventory.getId());
Hibernate.initialize(inventory.getAccession()); Hibernate.initialize(inventory.getAccession());
Accession accession = inventory.getAccession(); Accession accession = inventory.getAccession();
......
...@@ -73,7 +73,7 @@ public class InventoryViabilityRuleServiceImpl extends FilteredCRUDServiceImpl<I ...@@ -73,7 +73,7 @@ public class InventoryViabilityRuleServiceImpl extends FilteredCRUDServiceImpl<I
@Override @Override
@Transactional @Transactional
public InventoryViabilityRuleMap removeInventoryViabilityRuleMap(long id) { public InventoryViabilityRuleMap removeInventoryViabilityRuleMap(long id) {
var viabilityRuleMap = viabilityRuleMapRepository.getOne(id); var viabilityRuleMap = viabilityRuleMapRepository.getById(id);
viabilityRuleMapRepository.delete(viabilityRuleMap); viabilityRuleMapRepository.delete(viabilityRuleMap);
return viabilityRuleMap; return viabilityRuleMap;
} }
......
...@@ -226,7 +226,7 @@ public class InventoryViabilityServiceImpl extends FilteredCRUDServiceImpl<Inven ...@@ -226,7 +226,7 @@ public class InventoryViabilityServiceImpl extends FilteredCRUDServiceImpl<Inven
@PostAuthorize("@ggceSec.actionAllowed('ViabilityTest', 'CREATE', returnObject.inventory.site)") @PostAuthorize("@ggceSec.actionAllowed('ViabilityTest', 'CREATE', returnObject.inventory.site)")
public InventoryViability create(InventoryViability source) { public InventoryViability create(InventoryViability source) {
assert(source.getId() == null); assert(source.getId() == null);
source.setInventory(inventoryRepository.getOne(source.getInventory().getId())); source.setInventory(inventoryRepository.getById(source.getInventory().getId()));
var rule = source.getInventoryViabilityRule(); var rule = source.getInventoryViabilityRule();
if (rule != null) { if (rule != null) {
...@@ -356,7 +356,7 @@ public class InventoryViabilityServiceImpl extends FilteredCRUDServiceImpl<Inven ...@@ -356,7 +356,7 @@ public class InventoryViabilityServiceImpl extends FilteredCRUDServiceImpl<Inven
public InventoryViabilityData create(InventoryViabilityData source) { public InventoryViabilityData create(InventoryViabilityData source) {
assert(source.getId() == null); assert(source.getId() == null);
// reload from database: for post-authorize // reload from database: for post-authorize
source.setInventoryViability(inventoryViabilityRepository.getOne(source.getInventoryViability().getId())); source.setInventoryViability(inventoryViabilityRepository.getById(source.getInventoryViability().getId()));
var saved = repository.save(source); var saved = repository.save(source);
// Validate sum counts // Validate sum counts
......
...@@ -119,7 +119,7 @@ public class OrderRequestItemServiceImpl extends FilteredCRUDServiceImpl<OrderRe ...@@ -119,7 +119,7 @@ public class OrderRequestItemServiceImpl extends FilteredCRUDServiceImpl<OrderRe
OrderRequestItemActionRequest request = new OrderRequestItemActionRequest(); OrderRequestItemActionRequest request = new OrderRequestItemActionRequest();
if (CommunityCodeValues.ORDER_REQUEST_ACTION_RETRIEVE.is(actionCode)) { if (CommunityCodeValues.ORDER_REQUEST_ACTION_RETRIEVE.is(actionCode)) {
orderRequest = orderRequestRepository.getOne(orderRequest.getId()); orderRequest = orderRequestRepository.getById(orderRequest.getId());
// Update item status to PENDING // Update item status to PENDING
List<OrderRequestItem> items = orderRequest.getOrderRequestItems().stream() List<OrderRequestItem> items = orderRequest.getOrderRequestItems().stream()
// filter for allowed codes // filter for allowed codes
...@@ -129,7 +129,7 @@ public class OrderRequestItemServiceImpl extends FilteredCRUDServiceImpl<OrderRe ...@@ -129,7 +129,7 @@ public class OrderRequestItemServiceImpl extends FilteredCRUDServiceImpl<OrderRe
orderRequestService.updateItemStatus(orderRequest, CommunityCodeValues.ORDER_REQUEST_ITEM_STATUS_PENDING.value, items); orderRequestService.updateItemStatus(orderRequest, CommunityCodeValues.ORDER_REQUEST_ITEM_STATUS_PENDING.value, items);
} }
orderRequest = orderRequestRepository.getOne(orderRequest.getId()); orderRequest = orderRequestRepository.getById(orderRequest.getId());
request.id = orderRequest.getOrderRequestItems().stream() request.id = orderRequest.getOrderRequestItems().stream()
// filter for allowed codes // filter for allowed codes
.filter((ori) -> CommunityCodeValues.ORDER_REQUEST_ITEM_STATUS_PENDING.is(ori.getStatusCode())) .filter((ori) -> CommunityCodeValues.ORDER_REQUEST_ITEM_STATUS_PENDING.is(ori.getStatusCode()))
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
package org.gringlobal.service.impl; package org.gringlobal.service.impl;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
......
...@@ -19,7 +19,7 @@ import java.util.List; ...@@ -19,7 +19,7 @@ import java.util.List;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.javacrumbs.shedlock.core.SchedulerLock; import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.apache.commons.lang3.time.StopWatch; import org.apache.commons.lang3.time.StopWatch;
import org.gringlobal.model.kpi.Execution; import org.gringlobal.model.kpi.Execution;
import org.gringlobal.persistence.kpi.ExecutionRepository; import org.gringlobal.persistence.kpi.ExecutionRepository;
......
...@@ -49,7 +49,6 @@ import org.springframework.security.access.prepost.PreAuthorize; ...@@ -49,7 +49,6 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.opencsv.CSVReader; import com.opencsv.CSVReader;
...@@ -87,7 +86,6 @@ public class UsdaGeographyUpdater { ...@@ -87,7 +86,6 @@ public class UsdaGeographyUpdater {
* @throws Exception * @throws Exception
*/ */
@PreAuthorize("hasAuthority('GROUP_ADMINS')") @PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Transactional
public void update() throws Exception { public void update() throws Exception {
LOG.info("Updating Region and Geography from USDA GRIN folder {}", downloadFolder.getAbsolutePath()); LOG.info("Updating Region and Geography from USDA GRIN folder {}", downloadFolder.getAbsolutePath());
UsdaTaxonomyUpdater.downloadDataIfNeeded(downloadFolder); UsdaTaxonomyUpdater.downloadDataIfNeeded(downloadFolder);
...@@ -109,6 +107,7 @@ public class UsdaGeographyUpdater { ...@@ -109,6 +107,7 @@ public class UsdaGeographyUpdater {
LOG.info("Loading region.txt"); LOG.info("Loading region.txt");
Map<Long, Region> regTheirsToOurs = Collections.synchronizedMap(new HashMap<>()); Map<Long, Region> regTheirsToOurs = Collections.synchronizedMap(new HashMap<>());
Map<Long, Geography> geoTheirsToOurs = Collections.synchronizedMap(new HashMap<>()); Map<Long, Geography> geoTheirsToOurs = Collections.synchronizedMap(new HashMap<>());
Map<Geography, Geography> geoOldToUpdated = Collections.synchronizedMap(new HashMap<>());
List<Region> allRegions = regionRepository.findAll(); List<Region> allRegions = regionRepository.findAll();
...@@ -156,7 +155,7 @@ public class UsdaGeographyUpdater { ...@@ -156,7 +155,7 @@ public class UsdaGeographyUpdater {
region.setNote(regionRow.note); region.setNote(regionRow.note);
region.setCreatedDate(regionRow.createdDate); region.setCreatedDate(regionRow.createdDate);
region.setModifiedDate(regionRow.modifiedDate); // region.setModifiedDate(regionRow.modifiedDate); // Do not update @Versioned modifiedDate
region = regionRepository.save(region); region = regionRepository.save(region);
regTheirsToOurs.put(regionRow.regionId, region); regTheirsToOurs.put(regionRow.regionId, region);
...@@ -207,7 +206,14 @@ public class UsdaGeographyUpdater { ...@@ -207,7 +206,14 @@ public class UsdaGeographyUpdater {
} }
} }
} }
LOG.warn("Updated {} geographies", geographies.size()); LOG.warn("Updated {}/{} geographies", geographies.size(), result.size());
// List of geographies is in the exact same order as the original list
// we need to replace references in geoTheirsToOurs with new references
for (int i = result.size() - 1; i>=0; i--) {
geoOldToUpdated.put(result.get(i), geographies.get(i));
}
return geographies; return geographies;
}); });
}); });
...@@ -224,7 +230,8 @@ public class UsdaGeographyUpdater { ...@@ -224,7 +230,8 @@ public class UsdaGeographyUpdater {
Thread.sleep(100); Thread.sleep(100);
} }
} }
LOG.warn("Updating current geography references for {} records", ggGeo.size()); LOG.warn("Updating current geography references for {} records", ggGeo.size());
// Update currentGeography // Update currentGeography
...@@ -234,7 +241,9 @@ public class UsdaGeographyUpdater { ...@@ -234,7 +241,9 @@ public class UsdaGeographyUpdater {
List<Geography> toSave = new ArrayList<>(); List<Geography> toSave = new ArrayList<>();
for (GeographyRow geographyRow : batch) { for (GeographyRow geographyRow : batch) {
Geography geography = geoTheirsToOurs.get(geographyRow.geographyId); Geography geography = geoTheirsToOurs.get(geographyRow.geographyId);
geography = geoOldToUpdated.get(geography);
Geography currentGeography = geoTheirsToOurs.get(geographyRow.currentGeographyId); Geography currentGeography = geoTheirsToOurs.get(geographyRow.currentGeographyId);
currentGeography = geoOldToUpdated.get(currentGeography);
if (currentGeography != null) { if (currentGeography != null) {
geography.setCurrentGeography(currentGeography); geography.setCurrentGeography(currentGeography);
toSave.add(geography); toSave.add(geography);
...@@ -338,7 +347,7 @@ public class UsdaGeographyUpdater { ...@@ -338,7 +347,7 @@ public class UsdaGeographyUpdater {
geography.setNote(geographyRow.note); geography.setNote(geographyRow.note);
geography.setChangedDate(geographyRow.changedDate); geography.setChangedDate(geographyRow.changedDate);
geography.setCreatedDate(geographyRow.createdDate); geography.setCreatedDate(geographyRow.createdDate);
geography.setModifiedDate(geographyRow.modifiedDate); // geography.setModifiedDate(geographyRow.modifiedDate); // Do not update @Versioned modifiedDate
toSave.add(geography); toSave.add(geography);
geoTheirsToOurs.put(geographyRow.geographyId, geography); geoTheirsToOurs.put(geographyRow.geographyId, geography);
......
...@@ -52,7 +52,6 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -52,7 +52,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.opencsv.CSVReader; import com.opencsv.CSVReader;
...@@ -90,7 +89,6 @@ public class UsdaTaxonomyUpdater { ...@@ -90,7 +89,6 @@ public class UsdaTaxonomyUpdater {
* @throws Exception * @throws Exception
*/ */
@PreAuthorize("hasAuthority('GROUP_ADMINS')") @PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Transactional
public void update() throws Exception { public void update() throws Exception {
LOG.info("Updating GRIN taxonomy database from folder {}", downloadFolder.getAbsolutePath()); LOG.info("Updating GRIN taxonomy database from folder {}", downloadFolder.getAbsolutePath());
downloadDataIfNeeded(downloadFolder); downloadDataIfNeeded(downloadFolder);
...@@ -481,7 +479,7 @@ public class UsdaTaxonomyUpdater { ...@@ -481,7 +479,7 @@ public class UsdaTaxonomyUpdater {
species.setAlternateName(speciesRow.getAlternateName()); species.setAlternateName(speciesRow.getAlternateName());
// species.setCreatedDate(speciesRow.getCreatedDate()); // species.setCreatedDate(speciesRow.getCreatedDate());
// species.setModifiedDate(speciesRow.getModifiedDate()); // species.setModifiedDate(speciesRow.getModifiedDate()); // Do not update @Versioned modifiedDate
toSave.add(species); toSave.add(species);
speTheirsToOurs.put(speciesRow.getSpeciesId(), species); speTheirsToOurs.put(speciesRow.getSpeciesId(), species);
...@@ -558,7 +556,7 @@ public class UsdaTaxonomyUpdater { ...@@ -558,7 +556,7 @@ public class UsdaTaxonomyUpdater {
genus.setNote(genusRow.getNote()); genus.setNote(genusRow.getNote());
// genus.setCreatedDate(genusRow.getCreatedDate()); // genus.setCreatedDate(genusRow.getCreatedDate());
// genus.setModifiedDate(genusRow.getModifiedDate()); // genus.setModifiedDate(genusRow.getModifiedDate()); // Do not update @Versioned modifiedDate
toSave.add(genus); toSave.add(genus);
genTheirsToOurs.put(genusRow.getGenusId(), genus); genTheirsToOurs.put(genusRow.getGenusId(), genus);
......
...@@ -947,6 +947,16 @@ public class AccessionServiceTest extends AbstractServicesTest { ...@@ -947,6 +947,16 @@ public class AccessionServiceTest extends AbstractServicesTest {
assertThat(loadedAccession.getSite().getId(), is(DEFAULT_SITE.getId())); assertThat(loadedAccession.getSite().getId(), is(DEFAULT_SITE.getId()));
} }
@Test
public void testAccessionDetails() throws Exception{
Accession accession = addAccessionToDB(addTaxonomySpeciesToDB(addTaxonomyGenusToDB("Genus"), TAXONOMY_SPECIES_NAME));
assertThat(accession, is(notNullValue()));
var res = accessionService.getAccessionDetails(accession);
assertThat(res.accession, equalTo(accession));
}
@Test @Test
public void testAccessionMCPD() throws Exception { public void testAccessionMCPD() throws Exception {
var sdf = new SimpleDateFormat("dd MMM yyyy"); var sdf = new SimpleDateFormat("dd MMM yyyy");
......
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