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-4771 Refactor validation code #1810

Merged
merged 14 commits into from
Jan 21, 2025
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 @@ -19,7 +19,7 @@ private VocabularyConstants() {
public static final String AGM_RELATION_VOCABULARY = "agm_relation";
public static final String AGM_STR_RELATION_VOCABULARY_TERM_SET = "agm_str_relation";
public static final String AGM_ALLELE_RELATION_VOCABULARY_TERM_SET = "agm_allele_relation";
public static final String AGM_ALLELE_ASSOCIATION_VOCABULARY = "agm_allele_association_geno_terms";
public static final String AGM_ALLELE_GENOTYPE_TERMS_VOCABULARY = "agm_allele_association_geno_terms";
public static final String AGM_AGM_RELATION_VOCABULARY_TERM_SET = "agm_agm_relation";

public static final String PHENOTYPE_RELATION_VOCABULARY = "phenotype_relation";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
package org.alliancegenome.curation_api.dao.slotAnnotations.constructSlotAnnotations;

import org.alliancegenome.curation_api.dao.NoteDAO;
import org.alliancegenome.curation_api.dao.base.BaseSQLDAO;
import org.alliancegenome.curation_api.model.entities.slotAnnotations.constructSlotAnnotations.ConstructComponentSlotAnnotation;

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

@ApplicationScoped
public class ConstructComponentSlotAnnotationDAO extends BaseSQLDAO<ConstructComponentSlotAnnotation> {

@Inject
NoteDAO noteDAO;

protected ConstructComponentSlotAnnotationDAO() {
super(ConstructComponentSlotAnnotation.class);
}

public void deleteAttachedNote(Long id) {
Query jpqlQuery = entityManager.createNativeQuery("DELETE FROM slotannotation_note WHERE relatednotes_id = '" + id + "'");
jpqlQuery.executeUpdate();

noteDAO.remove(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonView;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Index;
import jakarta.persistence.JoinColumn;
Expand Down Expand Up @@ -73,9 +74,12 @@ public class ConstructComponentSlotAnnotation extends SlotAnnotation {
@EqualsAndHashCode.Include
protected String taxonText;

@IndexedEmbedded(includeDepth = 1)
@IndexedEmbedded(includePaths = {"freeText", "noteType.name", "references.curie",
"references.primaryCrossReferenceCurie", "freeText_keyword", "noteType.name_keyword", "references.curie_keyword",
"references.primaryCrossReferenceCurie_keyword"
})
@IndexingDependency(reindexOnUpdate = ReindexOnUpdate.SHALLOW)
@OneToMany
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JsonView({ View.FieldsAndLists.class, View.ConstructView.class })
@JoinTable(
joinColumns = @JoinColumn(name = "slotannotation_id"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ public Long getIdByModID(String modID) {
}

public Construct getShallowEntity(Long id) {
if (id == null) {
return null;
}
return constructDAO.getShallowEntity(Construct.class, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public Long getIdByModID(String modID) {
}

public GenomicEntity getShallowEntity(Long id) {
if (id == null) {
return null;
}
return genomicEntityDAO.getShallowEntity(GenomicEntity.class, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.alliancegenome.curation_api.response.ObjectResponse;
import org.alliancegenome.curation_api.response.SearchResponse;
import org.alliancegenome.curation_api.services.base.BaseEntityCrudService;
import org.alliancegenome.curation_api.services.validation.associations.SequenceTargetingReagentGeneAssociationFmsDTOValidator;
import org.alliancegenome.curation_api.services.validation.dto.fms.SequenceTargetingReagentGeneAssociationFmsDTOValidator;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.RequestScoped;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ public static void setObsoleteAndInternal(DiseaseAnnotationDTO dto, AuditedObjec
annotation.setInternal(dto.getInternal() != null && dto.getInternal());
}

public static <E extends AnnotationDTO, F extends Annotation> String setAnnotationID(E annotationDTO, F annotation, String uniqueId) {
public static <E extends AnnotationDTO, F extends Annotation> String setAnnotationIdentifiers(E annotationDTO, F annotation, String uniqueId) {
if (StringUtils.isNotBlank(annotationDTO.getPrimaryExternalId())) {
annotation.setPrimaryExternalId(annotationDTO.getPrimaryExternalId());
annotation.setModInternalId(null);
return annotationDTO.getPrimaryExternalId();
} else if (StringUtils.isNotBlank(annotationDTO.getModInternalId())) {
annotation.setModInternalId(annotationDTO.getModInternalId());
annotation.setPrimaryExternalId(null);
return annotationDTO.getModInternalId();
} else {
return uniqueId;
Expand All @@ -60,12 +62,14 @@ public static void setObsoleteAndInternal(SubmittedObjectDTO dto, SubmittedObjec
submittedObject.setInternal(dto.getInternal() != null && dto.getInternal());
}

public static <E extends SubmittedObjectDTO, F extends SubmittedObject> String setAnnotationID(E submittedObjectDTO, F submittedObject, String uniqueId) {
public static <E extends SubmittedObjectDTO, F extends SubmittedObject> String setSubmittedObjectIdentifiers(E submittedObjectDTO, F submittedObject, String uniqueId) {
if (StringUtils.isNotBlank(submittedObjectDTO.getPrimaryExternalId())) {
submittedObject.setPrimaryExternalId(submittedObjectDTO.getPrimaryExternalId());
submittedObject.setModInternalId(null);
return submittedObjectDTO.getPrimaryExternalId();
} else if (StringUtils.isNotBlank(submittedObjectDTO.getModInternalId())) {
submittedObject.setModInternalId(submittedObjectDTO.getModInternalId());
submittedObject.setPrimaryExternalId(null);
return submittedObjectDTO.getModInternalId();
} else {
return uniqueId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package org.alliancegenome.curation_api.services.validation;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.alliancegenome.curation_api.constants.ValidationConstants;
import org.alliancegenome.curation_api.constants.VocabularyConstants;
import org.alliancegenome.curation_api.dao.AGMDiseaseAnnotationDAO;
import org.alliancegenome.curation_api.dao.AffectedGenomicModelDAO;
Expand All @@ -16,9 +13,6 @@
import org.alliancegenome.curation_api.model.entities.Gene;
import org.alliancegenome.curation_api.model.entities.VocabularyTerm;
import org.alliancegenome.curation_api.response.ObjectResponse;
import org.alliancegenome.curation_api.services.VocabularyTermService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;

import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
Expand All @@ -30,8 +24,6 @@ public class AGMDiseaseAnnotationValidator extends DiseaseAnnotationValidator {

@Inject AGMDiseaseAnnotationDAO agmDiseaseAnnotationDAO;

@Inject VocabularyTermService vocabularyTermService;

@Inject AlleleDAO alleleDAO;

private String errorMessage;
Expand Down Expand Up @@ -67,22 +59,22 @@ public AGMDiseaseAnnotation validateAnnotationCreate(AGMDiseaseAnnotation uiEnti

public AGMDiseaseAnnotation validateAnnotation(AGMDiseaseAnnotation uiEntity, AGMDiseaseAnnotation dbEntity) {

AffectedGenomicModel subject = validateSubject(uiEntity, dbEntity);
AffectedGenomicModel subject = validateRequiredEntity(affectedGenomicModelDAO, "diseaseAnnotationSubject", uiEntity.getDiseaseAnnotationSubject(), dbEntity.getDiseaseAnnotationSubject());
dbEntity.setDiseaseAnnotationSubject(subject);

Gene inferredGene = validateInferredGene(uiEntity, dbEntity);
Gene inferredGene = validateEntity(geneDAO, "inferredGene", uiEntity.getInferredGene(), dbEntity.getInferredGene());
dbEntity.setInferredGene(inferredGene);

List<Gene> assertedGenes = validateAssertedGenes(uiEntity, dbEntity);
List<Gene> assertedGenes = validateEntities(geneDAO, "assertedGenes", uiEntity.getAssertedGenes(), dbEntity.getAssertedGenes());
dbEntity.setAssertedGenes(assertedGenes);

Allele inferredAllele = validateInferredAllele(uiEntity, dbEntity);
Allele inferredAllele = validateEntity(alleleDAO, "inferredAllele", uiEntity.getInferredAllele(), dbEntity.getInferredAllele());
dbEntity.setInferredAllele(inferredAllele);

Allele assertedAllele = validateAssertedAllele(uiEntity, dbEntity);
Allele assertedAllele = validateEntity(alleleDAO, "assertedAllele", uiEntity.getAssertedAllele(), dbEntity.getAssertedAllele());
dbEntity.setAssertedAllele(assertedAllele);

VocabularyTerm relation = validateDiseaseRelation(uiEntity, dbEntity);
VocabularyTerm relation = validateRequiredTermInVocabularyTermSet("relation", VocabularyConstants.AGM_DISEASE_RELATION_VOCABULARY_TERM_SET, uiEntity.getRelation(), dbEntity.getRelation());
dbEntity.setRelation(relation);

dbEntity = (AGMDiseaseAnnotation) validateCommonDiseaseAnnotationFields(uiEntity, dbEntity);
Expand All @@ -94,146 +86,4 @@ public AGMDiseaseAnnotation validateAnnotation(AGMDiseaseAnnotation uiEntity, AG

return dbEntity;
}

private AffectedGenomicModel validateSubject(AGMDiseaseAnnotation uiEntity, AGMDiseaseAnnotation dbEntity) {
String field = "diseaseAnnotationSubject";
if (ObjectUtils.isEmpty(uiEntity.getDiseaseAnnotationSubject())) {
addMessageResponse(field, ValidationConstants.REQUIRED_MESSAGE);
return null;
}

AffectedGenomicModel subjectEntity = null;
if (uiEntity.getDiseaseAnnotationSubject().getId() != null) {
subjectEntity = affectedGenomicModelDAO.find(uiEntity.getDiseaseAnnotationSubject().getId());
}
if (subjectEntity == null) {
addMessageResponse(field, ValidationConstants.INVALID_MESSAGE);
return null;
}

if (subjectEntity.getObsolete() && (dbEntity.getDiseaseAnnotationSubject() == null || !subjectEntity.getId().equals(dbEntity.getDiseaseAnnotationSubject().getId()))) {
addMessageResponse(field, ValidationConstants.OBSOLETE_MESSAGE);
return null;
}

return subjectEntity;

}

private Gene validateInferredGene(AGMDiseaseAnnotation uiEntity, AGMDiseaseAnnotation dbEntity) {
if (uiEntity.getInferredGene() == null) {
return null;
}

Gene inferredGene = null;
if (uiEntity.getInferredGene().getId() != null) {
inferredGene = geneDAO.find(uiEntity.getInferredGene().getId());
}
if (inferredGene == null) {
addMessageResponse("inferredGene", ValidationConstants.INVALID_MESSAGE);
return null;
}

if (inferredGene.getObsolete() && (dbEntity.getInferredGene() == null || !inferredGene.getId().equals(dbEntity.getInferredGene().getId()))) {
addMessageResponse("inferredGene", ValidationConstants.OBSOLETE_MESSAGE);
return null;
}

return inferredGene;
}

private List<Gene> validateAssertedGenes(AGMDiseaseAnnotation uiEntity, AGMDiseaseAnnotation dbEntity) {
if (CollectionUtils.isEmpty(uiEntity.getAssertedGenes())) {
return null;
}

List<Gene> assertedGenes = new ArrayList<Gene>();
List<Long> previousIds = new ArrayList<Long>();
if (CollectionUtils.isNotEmpty(dbEntity.getAssertedGenes())) {
previousIds = dbEntity.getAssertedGenes().stream().map(Gene::getId).collect(Collectors.toList());
}
for (Gene gene : uiEntity.getAssertedGenes()) {
Gene assertedGene = null;
if (gene.getId() != null) {
assertedGene = geneDAO.find(gene.getId());
}
if (assertedGene == null) {
addMessageResponse("assertedGenes", ValidationConstants.INVALID_MESSAGE);
return null;
}
if (assertedGene.getObsolete() && !previousIds.contains(assertedGene.getId())) {
addMessageResponse("assertedGenes", ValidationConstants.OBSOLETE_MESSAGE);
return null;
}
assertedGenes.add(assertedGene);
}

return assertedGenes;
}

private Allele validateInferredAllele(AGMDiseaseAnnotation uiEntity, AGMDiseaseAnnotation dbEntity) {
if (uiEntity.getInferredAllele() == null) {
return null;
}

Allele inferredAllele = null;
if (uiEntity.getInferredAllele().getId() != null) {
inferredAllele = alleleDAO.find(uiEntity.getInferredAllele().getId());
}
if (inferredAllele == null) {
addMessageResponse("inferredAllele", ValidationConstants.INVALID_MESSAGE);
return null;
}

if (inferredAllele.getObsolete() && (dbEntity.getInferredAllele() == null || !inferredAllele.getId().equals(dbEntity.getInferredAllele().getId()))) {
addMessageResponse("inferredAllele", ValidationConstants.OBSOLETE_MESSAGE);
return null;
}

return inferredAllele;
}

private Allele validateAssertedAllele(AGMDiseaseAnnotation uiEntity, AGMDiseaseAnnotation dbEntity) {
if (uiEntity.getAssertedAllele() == null) {
return null;
}

Allele assertedAllele = null;
if (uiEntity.getAssertedAllele().getId() != null) {
assertedAllele = alleleDAO.find(uiEntity.getAssertedAllele().getId());
}
if (assertedAllele == null) {
addMessageResponse("assertedAllele", ValidationConstants.INVALID_MESSAGE);
return null;
}

if (assertedAllele.getObsolete() && (dbEntity.getAssertedAllele() == null || !assertedAllele.getId().equals(dbEntity.getAssertedAllele().getId()))) {
addMessageResponse("assertedAllele", ValidationConstants.OBSOLETE_MESSAGE);
return null;
}

return assertedAllele;
}

private VocabularyTerm validateDiseaseRelation(AGMDiseaseAnnotation uiEntity, AGMDiseaseAnnotation dbEntity) {
String field = "relation";
if (uiEntity.getRelation() == null) {
addMessageResponse(field, ValidationConstants.REQUIRED_MESSAGE);
return null;
}

VocabularyTerm relation = vocabularyTermService.getTermInVocabularyTermSet(VocabularyConstants.AGM_DISEASE_RELATION_VOCABULARY_TERM_SET, uiEntity.getRelation().getName()).getEntity();

if (relation == null) {
addMessageResponse(field, ValidationConstants.INVALID_MESSAGE);
return null;
}

if (relation.getObsolete() && (dbEntity.getRelation() == null || !relation.getName().equals(dbEntity.getRelation().getName()))) {
addMessageResponse(field, ValidationConstants.OBSOLETE_MESSAGE);
return null;
}

return relation;
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
package org.alliancegenome.curation_api.services.validation;

import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import org.alliancegenome.curation_api.constants.ValidationConstants;
import org.alliancegenome.curation_api.constants.VocabularyConstants;
import org.alliancegenome.curation_api.dao.AffectedGenomicModelDAO;
import org.alliancegenome.curation_api.dao.CrossReferenceDAO;
import org.alliancegenome.curation_api.exceptions.ApiErrorException;
import org.alliancegenome.curation_api.model.entities.AffectedGenomicModel;
import org.alliancegenome.curation_api.model.entities.VocabularyTerm;
import org.alliancegenome.curation_api.response.ObjectResponse;
import org.alliancegenome.curation_api.services.VocabularyTermService;
import org.apache.commons.collections4.CollectionUtils;

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

@RequestScoped
public class AffectedGenomicModelValidator extends GenomicEntityValidator<AffectedGenomicModel> {

@Inject
AffectedGenomicModelDAO affectedGenomicModelDAO;
@Inject
VocabularyTermService vocabularyTermService;
@Inject
CrossReferenceDAO crossReferenceDAO;


private String errorMessage;

public AffectedGenomicModel validateAffectedGenomicModelUpdate(AffectedGenomicModel uiEntity) {
Expand Down Expand Up @@ -64,7 +59,7 @@ private AffectedGenomicModel validateAffectedGenomicModel(AffectedGenomicModel u
String name = handleStringField(uiEntity.getName());
dbEntity.setName(name);

VocabularyTerm subtype = validateSubtype(uiEntity, dbEntity);
VocabularyTerm subtype = validateRequiredTermInVocabulary("subtype", VocabularyConstants.AGM_SUBTYPE_VOCABULARY, uiEntity.getSubtype(), dbEntity.getSubtype());
dbEntity.setSubtype(subtype);

if (CollectionUtils.isNotEmpty(uiEntity.getSynonyms())) {
Expand All @@ -81,25 +76,4 @@ private AffectedGenomicModel validateAffectedGenomicModel(AffectedGenomicModel u
return dbEntity;
}

public VocabularyTerm validateSubtype(AffectedGenomicModel uiEntity, AffectedGenomicModel dbEntity) {
String field = "subtype";
if (uiEntity.getSubtype() == null) {
addMessageResponse(field, ValidationConstants.REQUIRED_MESSAGE);
return null;
}

VocabularyTerm subtype = vocabularyTermService.getTermInVocabulary(VocabularyConstants.AGM_SUBTYPE_VOCABULARY, uiEntity.getSubtype().getName()).getEntity();
if (subtype == null) {
addMessageResponse(field, ValidationConstants.INVALID_MESSAGE);
return null;
}

if (subtype.getObsolete() && (dbEntity.getSubtype() == null || !subtype.getName().equals(dbEntity.getSubtype().getName()))) {
addMessageResponse(field, ValidationConstants.OBSOLETE_MESSAGE);
return null;
}

return subtype;
}

}
Loading
Loading