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
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) {
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());
InventoryMaintenancePolicy inventoryMaintPolicy = inventoryMaintenancePolicyRepository.getOne(acquisitionBatch.inventoryMaintenancePolicyId);
InventoryMaintenancePolicy inventoryMaintPolicy = inventoryMaintenancePolicyRepository.getById(acquisitionBatch.inventoryMaintenancePolicyId);
LOG.info("Using policy {}", inventoryMaintPolicy.getMaintenanceName());
// Make accessions
......
......@@ -80,7 +80,7 @@ public abstract class BaseAttachmentSupport<T extends CooperatorOwnedModel, A ex
@Override
@Transactional
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);
LOG.info("Upload file {} to path {}", file.getOriginalFilename(), repositoryPath);
......@@ -114,7 +114,7 @@ public abstract class BaseAttachmentSupport<T extends CooperatorOwnedModel, A ex
@Override
@Transactional
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)))
.orElseThrow(() -> new NotFoundElement("No such attach for provided entity"));
......
......@@ -92,7 +92,7 @@ public abstract class BaseTranslationSupport<T extends TranslatedCooperatorOwned
@Override
public Translation<T> getTranslated(T input) {
T savedEntity = owningEntityRepository.getOne(input.getId());
T savedEntity = owningEntityRepository.getById(input.getId());
SysLang targetLanguage = languageService.getLanguage(LocaleContextHolder.getLocale());
......
......@@ -110,8 +110,9 @@ public abstract class CRUDServiceImpl<T extends EmptyModel, R extends JpaReposit
T current = get(source.getId());
if (current instanceof AuditedModel) {
AuditedModel audited = (AuditedModel) current;
AuditedModel sourceAudited = (AuditedModel) 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());
}
}
......
......@@ -94,7 +94,7 @@ public class CropServiceImpl extends FilteredCRUDServiceImpl<Crop, CropFilter, C
@Override
protected Path createRepositoryPath(Crop crop) {
crop = owningEntityRepository.getOne(crop.getId());
crop = owningEntityRepository.getById(crop.getId());
return Paths.get("/crop/" + crop.getName());
}
......
......@@ -110,7 +110,7 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe
@Override
protected Path createRepositoryPath(CropTrait cropTrait) {
cropTrait = owningEntityRepository.getOne(cropTrait.getId());
cropTrait = owningEntityRepository.getById(cropTrait.getId());
Hibernate.initialize(cropTrait.getCrop());
Crop crop = cropTrait.getCrop();
......@@ -155,7 +155,7 @@ public class CropTraitServiceImpl extends CRUDServiceImpl<CropTrait, CropTraitRe
@Override
protected Path createRepositoryPath(CropTraitCode cropTraitCode) {
cropTraitCode = owningEntityRepository.getOne(cropTraitCode.getId());
cropTraitCode = owningEntityRepository.getById(cropTraitCode.getId());
Hibernate.initialize(cropTraitCode.getCropTrait());
CropTrait cropTrait = cropTraitCode.getCropTrait();
......
......@@ -130,7 +130,7 @@ public class InventoryServiceImpl extends FilteredCRUDServiceImpl<Inventory, Inv
*/
@Override
protected Path createRepositoryPath(Inventory inventory) {
inventory = owningEntityRepository.getOne(inventory.getId());
inventory = owningEntityRepository.getById(inventory.getId());
Hibernate.initialize(inventory.getAccession());
Accession accession = inventory.getAccession();
......
......@@ -73,7 +73,7 @@ public class InventoryViabilityRuleServiceImpl extends FilteredCRUDServiceImpl<I
@Override
@Transactional
public InventoryViabilityRuleMap removeInventoryViabilityRuleMap(long id) {
var viabilityRuleMap = viabilityRuleMapRepository.getOne(id);
var viabilityRuleMap = viabilityRuleMapRepository.getById(id);
viabilityRuleMapRepository.delete(viabilityRuleMap);
return viabilityRuleMap;
}
......
......@@ -226,7 +226,7 @@ public class InventoryViabilityServiceImpl extends FilteredCRUDServiceImpl<Inven
@PostAuthorize("@ggceSec.actionAllowed('ViabilityTest', 'CREATE', returnObject.inventory.site)")
public InventoryViability create(InventoryViability source) {
assert(source.getId() == null);
source.setInventory(inventoryRepository.getOne(source.getInventory().getId()));
source.setInventory(inventoryRepository.getById(source.getInventory().getId()));
var rule = source.getInventoryViabilityRule();
if (rule != null) {
......@@ -356,7 +356,7 @@ public class InventoryViabilityServiceImpl extends FilteredCRUDServiceImpl<Inven
public InventoryViabilityData create(InventoryViabilityData source) {
assert(source.getId() == null);
// 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);
// Validate sum counts
......
......@@ -119,7 +119,7 @@ public class OrderRequestItemServiceImpl extends FilteredCRUDServiceImpl<OrderRe
OrderRequestItemActionRequest request = new OrderRequestItemActionRequest();
if (CommunityCodeValues.ORDER_REQUEST_ACTION_RETRIEVE.is(actionCode)) {
orderRequest = orderRequestRepository.getOne(orderRequest.getId());
orderRequest = orderRequestRepository.getById(orderRequest.getId());
// Update item status to PENDING
List<OrderRequestItem> items = orderRequest.getOrderRequestItems().stream()
// filter for allowed codes
......@@ -129,7 +129,7 @@ public class OrderRequestItemServiceImpl extends FilteredCRUDServiceImpl<OrderRe
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()
// filter for allowed codes
.filter((ori) -> CommunityCodeValues.ORDER_REQUEST_ITEM_STATUS_PENDING.is(ori.getStatusCode()))
......
......@@ -17,7 +17,6 @@
package org.gringlobal.service.impl;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
......
......@@ -19,7 +19,7 @@ import java.util.List;
import java.util.concurrent.Callable;
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.gringlobal.model.kpi.Execution;
import org.gringlobal.persistence.kpi.ExecutionRepository;
......
......@@ -49,7 +49,6 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Lists;
import com.opencsv.CSVReader;
......@@ -87,7 +86,6 @@ public class UsdaGeographyUpdater {
* @throws Exception
*/
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Transactional
public void update() throws Exception {
LOG.info("Updating Region and Geography from USDA GRIN folder {}", downloadFolder.getAbsolutePath());
UsdaTaxonomyUpdater.downloadDataIfNeeded(downloadFolder);
......@@ -109,6 +107,7 @@ public class UsdaGeographyUpdater {
LOG.info("Loading region.txt");
Map<Long, Region> regTheirsToOurs = 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();
......@@ -156,7 +155,7 @@ public class UsdaGeographyUpdater {
region.setNote(regionRow.note);
region.setCreatedDate(regionRow.createdDate);
region.setModifiedDate(regionRow.modifiedDate);
// region.setModifiedDate(regionRow.modifiedDate); // Do not update @Versioned modifiedDate
region = regionRepository.save(region);
regTheirsToOurs.put(regionRow.regionId, region);
......@@ -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;
});
});
......@@ -224,7 +230,8 @@ public class UsdaGeographyUpdater {
Thread.sleep(100);
}
}
LOG.warn("Updating current geography references for {} records", ggGeo.size());
// Update currentGeography
......@@ -234,7 +241,9 @@ public class UsdaGeographyUpdater {
List<Geography> toSave = new ArrayList<>();
for (GeographyRow geographyRow : batch) {
Geography geography = geoTheirsToOurs.get(geographyRow.geographyId);
geography = geoOldToUpdated.get(geography);
Geography currentGeography = geoTheirsToOurs.get(geographyRow.currentGeographyId);
currentGeography = geoOldToUpdated.get(currentGeography);
if (currentGeography != null) {
geography.setCurrentGeography(currentGeography);
toSave.add(geography);
......@@ -338,7 +347,7 @@ public class UsdaGeographyUpdater {
geography.setNote(geographyRow.note);
geography.setChangedDate(geographyRow.changedDate);
geography.setCreatedDate(geographyRow.createdDate);
geography.setModifiedDate(geographyRow.modifiedDate);
// geography.setModifiedDate(geographyRow.modifiedDate); // Do not update @Versioned modifiedDate
toSave.add(geography);
geoTheirsToOurs.put(geographyRow.geographyId, geography);
......
......@@ -52,7 +52,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Lists;
import com.opencsv.CSVReader;
......@@ -90,7 +89,6 @@ public class UsdaTaxonomyUpdater {
* @throws Exception
*/
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Transactional
public void update() throws Exception {
LOG.info("Updating GRIN taxonomy database from folder {}", downloadFolder.getAbsolutePath());
downloadDataIfNeeded(downloadFolder);
......@@ -481,7 +479,7 @@ public class UsdaTaxonomyUpdater {
species.setAlternateName(speciesRow.getAlternateName());
// species.setCreatedDate(speciesRow.getCreatedDate());
// species.setModifiedDate(speciesRow.getModifiedDate());
// species.setModifiedDate(speciesRow.getModifiedDate()); // Do not update @Versioned modifiedDate
toSave.add(species);
speTheirsToOurs.put(speciesRow.getSpeciesId(), species);
......@@ -558,7 +556,7 @@ public class UsdaTaxonomyUpdater {
genus.setNote(genusRow.getNote());
// genus.setCreatedDate(genusRow.getCreatedDate());
// genus.setModifiedDate(genusRow.getModifiedDate());
// genus.setModifiedDate(genusRow.getModifiedDate()); // Do not update @Versioned modifiedDate
toSave.add(genus);
genTheirsToOurs.put(genusRow.getGenusId(), genus);
......
......@@ -947,6 +947,16 @@ public class AccessionServiceTest extends AbstractServicesTest {
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
public void testAccessionMCPD() throws Exception {
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