Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCRUM-4185 updated executor, service, and validator #1591

Merged
merged 6 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected void init() {

@Override
public APIResponse updateSequenceTargetingReagent(String dataProvider, SequenceTargetingReagentIngestFmsDTO sqtrFmsDTO) {
return sqtrExecutor.runLoadApi(sqtrService, dataProvider, sqtrFmsDTO.getData());
return sqtrExecutor.runLoadApi(dataProvider, sqtrFmsDTO.getData());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.alliancegenome.curation_api.dao.associations;

import org.alliancegenome.curation_api.dao.base.BaseSQLDAO;
import org.alliancegenome.curation_api.model.entities.associations.sequenceTargetingReagentAssociations.SequenceTargetingReagentGeneAssociation;

import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class SequenceTargetingReagentGeneAssociationDAO extends BaseSQLDAO<SequenceTargetingReagentGeneAssociation> {

protected SequenceTargetingReagentGeneAssociationDAO() {
super(SequenceTargetingReagentGeneAssociation.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,37 @@

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;

import org.alliancegenome.curation_api.enums.BackendBulkDataProvider;
import org.alliancegenome.curation_api.exceptions.ObjectUpdateException;
import org.alliancegenome.curation_api.exceptions.ObjectUpdateException.ObjectUpdateExceptionData;
import org.alliancegenome.curation_api.interfaces.AGRCurationSchemaVersion;
import org.alliancegenome.curation_api.model.entities.SequenceTargetingReagent;
import org.alliancegenome.curation_api.model.entities.bulkloads.BulkFMSLoad;
import org.alliancegenome.curation_api.model.entities.bulkloads.BulkLoadFile;
import org.alliancegenome.curation_api.model.entities.bulkloads.BulkLoadFileHistory;
import org.alliancegenome.curation_api.model.ingest.dto.fms.SequenceTargetingReagentFmsDTO;
import org.alliancegenome.curation_api.model.ingest.dto.fms.SequenceTargetingReagentIngestFmsDTO;
import org.alliancegenome.curation_api.response.APIResponse;
import org.alliancegenome.curation_api.response.LoadHistoryResponce;
import org.alliancegenome.curation_api.services.SequenceTargetingReagentService;
import org.alliancegenome.curation_api.services.associations.SequenceTargetingReagentGeneAssociationService;
import org.alliancegenome.curation_api.util.ProcessDisplayHelper;
import org.apache.commons.lang3.StringUtils;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

@ApplicationScoped
public class SequenceTargetingReagentExecutor extends LoadFileExecutor {
@Inject SequenceTargetingReagentService sqtrService;
@Inject
SequenceTargetingReagentService sqtrService;
@Inject
SequenceTargetingReagentGeneAssociationService sqtrGeneAssociationService;

public void execLoad(BulkLoadFile bulkLoadFile) {

Expand All @@ -29,38 +41,119 @@ public void execLoad(BulkLoadFile bulkLoadFile) {
BulkFMSLoad fms = (BulkFMSLoad) bulkLoadFile.getBulkLoad();

SequenceTargetingReagentIngestFmsDTO sqtrIngestFmsDTO = mapper.readValue(
new GZIPInputStream(new FileInputStream(bulkLoadFile.getLocalFilePath())), SequenceTargetingReagentIngestFmsDTO.class);
new GZIPInputStream(new FileInputStream(bulkLoadFile.getLocalFilePath())),
SequenceTargetingReagentIngestFmsDTO.class);
bulkLoadFile.setRecordCount(sqtrIngestFmsDTO.getData().size());

AGRCurationSchemaVersion version = SequenceTargetingReagent.class.getAnnotation(AGRCurationSchemaVersion.class);
AGRCurationSchemaVersion version = SequenceTargetingReagent.class
.getAnnotation(AGRCurationSchemaVersion.class);
bulkLoadFile.setLinkMLSchemaVersion(version.max());

if (sqtrIngestFmsDTO.getMetaData() != null && StringUtils.isNotBlank(sqtrIngestFmsDTO.getMetaData().getRelease())) {
if (sqtrIngestFmsDTO.getMetaData() != null
&& StringUtils.isNotBlank(sqtrIngestFmsDTO.getMetaData().getRelease())) {
bulkLoadFile.setAllianceMemberReleaseVersion(sqtrIngestFmsDTO.getMetaData().getRelease());
}

BackendBulkDataProvider dataProvider = BackendBulkDataProvider.valueOf(fms.getFmsDataSubType());

List<Long> sqtrIdsLoaded = new ArrayList<>();
List<Long> sqtrIdsBefore = new ArrayList<>();
Map<String, List<Long>> idsAdded = new HashMap<String, List<Long>>();
idsAdded.put("SQTR", new ArrayList<Long>());
idsAdded.put("SQTRGeneAssociation", new ArrayList<Long>());

Map<String, List<Long>> previousIds = getPreviouslyLoadedIds(dataProvider);

bulkLoadFileDAO.merge(bulkLoadFile);

BulkLoadFileHistory history = new BulkLoadFileHistory(sqtrIngestFmsDTO.getData().size());
BulkLoadFileHistory history = new BulkLoadFileHistory(sqtrIngestFmsDTO.getData().size() * 2);

boolean success = runLoad(sqtrService, history, dataProvider, sqtrIngestFmsDTO.getData(), sqtrIdsLoaded);
runLoad(history, dataProvider, sqtrIngestFmsDTO.getData(), idsAdded.get("SQTR"), idsAdded.get("SQTRGeneAssociation"));

runCleanup(sqtrService, history, dataProvider.name(), previousIds.get("SQTR"), idsAdded.get("SQTR"), "SQTR", bulkLoadFile.getMd5Sum());
runCleanup(sqtrService, history, dataProvider.name(), previousIds.get("SQTRGeneAssociation"), idsAdded.get("SQTRGeneAssociation"), "SQTR Gene Associations", bulkLoadFile.getMd5Sum());

if (success) {
runCleanup(sqtrService, history, dataProvider.name(), sqtrIdsBefore, sqtrIdsLoaded, "SQTR", bulkLoadFile.getMd5Sum());
}

history.finishLoad();

updateHistory(history);
} catch (Exception e) {
failLoad(bulkLoadFile, e);
e.printStackTrace();
}
}

private Map<String, List<Long>> getPreviouslyLoadedIds(BackendBulkDataProvider dataProvider) {
Map<String, List<Long>> previousIds = new HashMap<>();

previousIds.put("SQTR", sqtrService.getIdsByDataProvider(dataProvider.name()));
previousIds.put("SQTRGeneAssociation", sqtrGeneAssociationService.getIdsByDataProvider(dataProvider.name()));

return previousIds;
}

public APIResponse runLoadApi(String dataProviderName, List<SequenceTargetingReagentFmsDTO> sqtrDTOs) {
List<Long> sqtrIdsLoaded = new ArrayList<>();
List<Long> sqtrGeneAssociationIdsLoaded = new ArrayList<>();

BulkLoadFileHistory history = new BulkLoadFileHistory(sqtrDTOs.size() * 2);
BackendBulkDataProvider dataProvider = BackendBulkDataProvider.valueOf(dataProviderName);
runLoad(history, dataProvider, sqtrDTOs, sqtrIdsLoaded, sqtrGeneAssociationIdsLoaded);
history.finishLoad();

return new LoadHistoryResponce(history);
}

private void runLoad(BulkLoadFileHistory history, BackendBulkDataProvider dataProvider, List<SequenceTargetingReagentFmsDTO> sqtrs, List<Long> sqtrIdsLoaded, List<Long> sqtrGeneAssociationIdsLoaded) {
adamgibs marked this conversation as resolved.
Show resolved Hide resolved
ProcessDisplayHelper ph = new ProcessDisplayHelper();
ph.addDisplayHandler(loadProcessDisplayService);
ph.startProcess("Sequence Targeting Reagent DTO Update for " + dataProvider.name(), sqtrs.size() * 2);

loadSequenceTargetingReagents(history, sqtrs, sqtrIdsLoaded, dataProvider, ph);
loadSequenceTargetingReagentGeneAssociations(history, sqtrs, sqtrGeneAssociationIdsLoaded, dataProvider, ph);

ph.finishProcess();

}

private void loadSequenceTargetingReagents(BulkLoadFileHistory history, List<SequenceTargetingReagentFmsDTO> sqtrs,
List<Long> idsLoaded, BackendBulkDataProvider dataProvider, ProcessDisplayHelper ph) {
for (SequenceTargetingReagentFmsDTO dto : sqtrs) {
try {
SequenceTargetingReagent dbObject = sqtrService.upsert(dto, dataProvider);
history.incrementCompleted();
if (idsLoaded != null) {
idsLoaded.add(dbObject.getId());
}
} catch (ObjectUpdateException e) {
history.incrementFailed();
addException(history, e.getData());
} catch (Exception e) {
history.incrementFailed();
addException(history, new ObjectUpdateExceptionData(dto, e.getMessage(), e.getStackTrace()));
}
updateHistory(history);
ph.progressProcess();
}
}

private void loadSequenceTargetingReagentGeneAssociations(BulkLoadFileHistory history,
List<SequenceTargetingReagentFmsDTO> sqtrs, List<Long> idsLoaded, BackendBulkDataProvider dataProvider,
ProcessDisplayHelper ph) {

for (SequenceTargetingReagentFmsDTO dto : sqtrs) {
try {
List<Long> associationIds = sqtrGeneAssociationService.loadGeneAssociations(dto, dataProvider);
history.incrementCompleted();
if (idsLoaded != null) {
idsLoaded.addAll(associationIds);
}
} catch (ObjectUpdateException e) {
history.incrementFailed();
addException(history, e.getData());
} catch (Exception e) {
history.incrementFailed();
addException(history, new ObjectUpdateExceptionData(dto, e.getMessage(), e.getStackTrace()));
}
updateHistory(history);
ph.progressProcess();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package org.alliancegenome.curation_api.services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.alliancegenome.curation_api.constants.EntityFieldConstants;
import org.alliancegenome.curation_api.dao.SequenceTargetingReagentDAO;
import org.alliancegenome.curation_api.enums.BackendBulkDataProvider;
import org.alliancegenome.curation_api.exceptions.ObjectUpdateException;
Expand All @@ -25,10 +31,18 @@ public class SequenceTargetingReagentService extends BaseEntityCrudService<Seque
protected void init() {
setSQLDao(sqtrDAO);
}

@Transactional
public SequenceTargetingReagent upsert(SequenceTargetingReagentFmsDTO dto, BackendBulkDataProvider dataProvider) throws ObjectUpdateException {
SequenceTargetingReagent sqtr = sqtrDtoValidator.validateSQTRFmsDTO(dto, dataProvider);
return sqtrDAO.persist(sqtr);
}

public List<Long> getIdsByDataProvider(String dataProvider) {
Map<String, Object> params = new HashMap<>();
params.put(EntityFieldConstants.DATA_PROVIDER, dataProvider);
List<Long> ids = sqtrDAO.findIdsByParams(params);
ids.removeIf(Objects::isNull);
return ids;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.alliancegenome.curation_api.services.associations;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.alliancegenome.curation_api.constants.EntityFieldConstants;
import org.alliancegenome.curation_api.dao.associations.SequenceTargetingReagentGeneAssociationDAO;
import org.alliancegenome.curation_api.enums.BackendBulkDataProvider;
import org.alliancegenome.curation_api.exceptions.ObjectUpdateException;
import org.alliancegenome.curation_api.model.entities.Gene;
import org.alliancegenome.curation_api.model.entities.SequenceTargetingReagent;
import org.alliancegenome.curation_api.model.entities.associations.sequenceTargetingReagentAssociations.SequenceTargetingReagentGeneAssociation;
import org.alliancegenome.curation_api.model.ingest.dto.fms.SequenceTargetingReagentFmsDTO;
import org.alliancegenome.curation_api.services.validation.associations.SequenceTargetingReagentGeneAssociationFmsDTOValidator;

import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;

@RequestScoped
public class SequenceTargetingReagentGeneAssociationService {

@Inject
SequenceTargetingReagentGeneAssociationDAO sequenceTargetingReagentGeneAssociationDAO;
@Inject
SequenceTargetingReagentGeneAssociationFmsDTOValidator sequenceTargetingReagentGeneAssociationFmsDTOValidator;

@Transactional
public List<Long> loadGeneAssociations(SequenceTargetingReagentFmsDTO dto, BackendBulkDataProvider dataProvider) throws ObjectUpdateException {

List<SequenceTargetingReagentGeneAssociation> associations = sequenceTargetingReagentGeneAssociationFmsDTOValidator
.validateSQTRGeneAssociationFmsDTO(dto, dataProvider);

for (SequenceTargetingReagentGeneAssociation association : associations) {
if (association != null) {
addAssociationToSQTR(association);
addAssociationToGene(association);
}
}

return associations.stream().map(SequenceTargetingReagentGeneAssociation::getId)
.collect(Collectors.toList());
}

private void addAssociationToSQTR(SequenceTargetingReagentGeneAssociation association) {
SequenceTargetingReagent sqtr = association.getSequenceTargetingReagentAssociationSubject();
List<SequenceTargetingReagentGeneAssociation> currentAssociations = sqtr
.getSequenceTargetingReagentGeneAssociations();
if (currentAssociations == null) {
currentAssociations = new ArrayList<>();
sqtr.setSequenceTargetingReagentGeneAssociations(currentAssociations);
}

List<Long> currentAssociationIds = new ArrayList<>();
for (SequenceTargetingReagentGeneAssociation sqtrga : currentAssociations) {
currentAssociationIds.add(sqtrga.getId());
}

if (!currentAssociationIds.contains(association.getId())) {
currentAssociations.add(association);
}
}

private void addAssociationToGene(SequenceTargetingReagentGeneAssociation association) {
Gene gene = association.getSequenceTargetingReagentGeneAssociationObject();
List<SequenceTargetingReagentGeneAssociation> currentAssociations = gene
.getSequenceTargetingReagentGeneAssociations();
if (currentAssociations == null) {
currentAssociations = new ArrayList<>();
gene.setSequenceTargetingReagentGeneAssociations(currentAssociations);
}

List<Long> currentAssociationIds = new ArrayList<>();
for (SequenceTargetingReagentGeneAssociation sqtrga : currentAssociations) {
currentAssociationIds.add(sqtrga.getId());
}

if (!currentAssociationIds.contains(association.getId())) {
currentAssociations.add(association);
}

}

public List<Long> getIdsByDataProvider(String dataProvider) {
Map<String, Object> params = new HashMap<>();
params.put(EntityFieldConstants.DATA_PROVIDER, dataProvider);
List<Long> ids = sequenceTargetingReagentGeneAssociationDAO.findIdsByParams(params);
ids.removeIf(Objects::isNull);
return ids;
}
}
Loading
Loading