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

Startup: Assign missing accession and inventory numbers in background

parent a53c5505
......@@ -411,7 +411,7 @@ public class AccessionServiceImpl extends FilteredCRUDServiceImpl<Accession, Acc
lastPage = page.isLast();
pageNumber += 1;
LOG.warn("Updating acceNumbers for {} records", page.getNumberOfElements());
LOG.warn("Updating accessionNumber for {} records, page {}", page.getNumberOfElements(), pageNumber);
Lists.partition(page.getContent(), 100).forEach(batch -> TransactionHelper.executeInTransaction(false, () -> {
batch.forEach(a -> repository.setAccessionNumber(a.getId(), GGCE.accessionNumber(a)));
return true;
......@@ -425,19 +425,19 @@ public class AccessionServiceImpl extends FilteredCRUDServiceImpl<Accession, Acc
@PreAuthorize("hasRole('ADMINISTRATOR')")
public void assignMissingAccessionNumbers() {
boolean lastPage = false;
int counter = 0;
int pageNumber = 0;
do {
Page<Accession> page = repository.findAll(QAccession.accession.accessionNumber.isNull(), PageRequest.of(0, 1000));
lastPage = page.isLast();
pageNumber++;
LOG.warn("Assigning accessionNumber for {} records", page.getNumberOfElements());
LOG.warn("Assigning accessionNumber for {} records, page {}", page.getNumberOfElements(), pageNumber);
Lists.partition(page.getContent(), 100).forEach(batch -> TransactionHelper.executeInTransaction(false, () -> {
batch.forEach(a -> repository.setAccessionNumber(a.getId(), GGCE.accessionNumber(a)));
return true;
}));
counter++;
} while (!lastPage && counter < 100);
} while (!lastPage && pageNumber < 1000); // at most 1000 loops
LOG.info("Done.");
}
......
......@@ -523,7 +523,7 @@ public class InventoryServiceImpl extends FilteredCRUDServiceImpl<Inventory, Inv
lastPage = page.isLast();
pageNumber += 1;
LOG.warn("Updating inventoryNumbers for {} records", page.getNumberOfElements());
LOG.warn("Updating inventoryNumber for {} records, page {}", page.getNumberOfElements(), pageNumber);
Lists.partition(page.getContent(), 100).forEach(batch -> TransactionHelper.executeInTransaction(false, () -> {
batch.forEach(i -> repository.setInventoryNumber(i.getId(), GGCE.inventoryNumber(i)));
return true;
......@@ -537,19 +537,19 @@ public class InventoryServiceImpl extends FilteredCRUDServiceImpl<Inventory, Inv
@PreAuthorize("hasRole('ADMINISTRATOR')")
public void assignMissingInventoryNumbers() {
boolean lastPage = false;
int counter = 0;
int pageNumber = 0;
do {
Page<Inventory> page = repository.findAll(QInventory.inventory.inventoryNumber.isNull(), PageRequest.of(0, 1000));
lastPage = page.isLast();
pageNumber++;
LOG.warn("Assigning inventoryNumbers for {} records", page.getNumberOfElements());
LOG.warn("Assigning inventoryNumber for {} records, page {}", page.getNumberOfElements(), pageNumber);
Lists.partition(page.getContent(), 100).forEach(batch -> TransactionHelper.executeInTransaction(false, () -> {
batch.forEach(a -> repository.setInventoryNumber(a.getId(), GGCE.inventoryNumber(a)));
return true;
}));
counter++;
} while (!lastPage && counter < 100);
} while (!lastPage && pageNumber < 5000); // at most 5000 loops
LOG.info("Done.");
}
......
......@@ -15,7 +15,7 @@
*/
package org.gringlobal.spring;
import static org.gringlobal.service.LanguageService.MCPD_IETF_TAG;
import static org.gringlobal.service.LanguageService.*;
import java.io.InputStream;
import java.nio.file.Paths;
......@@ -60,6 +60,7 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.task.TaskExecutor;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
......@@ -125,6 +126,9 @@ public class ApplicationStartup implements InitializingBean {
@Autowired
private LanguageService languageService;
@Autowired
private TaskExecutor taskExecutor;
/**
* Things to run immediately
*/
......@@ -147,8 +151,7 @@ public class ApplicationStartup implements InitializingBean {
// makeSiteLocations();
ensureCommunityCodeValues();
ensureCommunityAppSettings();
ensureAccessionNumbers();
ensureInventoryNumbers();
ensureAccessionAndInventoryNumbers();
ensureSysLangMCPD();
// ensure GG-CE dataviews
......@@ -163,21 +166,19 @@ public class ApplicationStartup implements InitializingBean {
}
}
private void ensureInventoryNumbers() {
try {
asAdmin(() -> { inventoryService.assignMissingInventoryNumbers(); return true; });
} catch (Throwable e) {
LOG.error("Could assign missing inventory numbers: {}", e.getMessage(), e);
}
}
private void ensureAccessionNumbers() {
try {
asAdmin(() -> { accessionService.assignMissingAccessionNumbers(); return true; });
} catch (Throwable e) {
LOG.error("Could assign missing accession numbers: {}", e.getMessage(), e);
}
private void ensureAccessionAndInventoryNumbers() {
// Start the task in the background: This cuts down on startup time during upgrades of legacy GG databases
taskExecutor.execute(() -> {
try {
asAdmin(() -> {
accessionService.assignMissingAccessionNumbers();
inventoryService.assignMissingInventoryNumbers();
return true;
});
} catch (Throwable e) {
LOG.error("Could assign missing accession/inventory numbers: {}", e.getMessage(), e);
}
});
}
/**
......
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