diff --git a/src/main/java/org/breedinginsight/brapps/importer/services/processors/germplasm/GermplasmProcessor.java b/src/main/java/org/breedinginsight/brapps/importer/services/processors/germplasm/GermplasmProcessor.java index 50ab5bafd..bfb2ceaa9 100644 --- a/src/main/java/org/breedinginsight/brapps/importer/services/processors/germplasm/GermplasmProcessor.java +++ b/src/main/java/org/breedinginsight/brapps/importer/services/processors/germplasm/GermplasmProcessor.java @@ -91,7 +91,7 @@ public class GermplasmProcessor implements Processor { public static String missingGIDsMsg = "The following GIDs were not found in the database: %s"; public static String missingParentalGIDsMsg = "The following parental GIDs were not found in the database: %s"; - public static String missingParentalEntryNoMsg = "The following parental entry numbers were not found in the database: %s"; + public static String missingParentalEntryNoMsg = "The following parental entry numbers were not found in the file: %s"; public static String badBreedMethodsMsg = "Invalid breeding method"; public static String badGermplasmNameMsg = "Germplasm name cannot contain /"; public static String missingEntryNumbersMsg = "Either all or none of the germplasm must have entry numbers"; @@ -137,14 +137,17 @@ public void getExistingBrapiData(List importRows, Program program) BrAPIImport germplasmImport = importRows.get(i); Germplasm germplasm = germplasmImport.getGermplasm(); if (germplasm != null) { - + //Ignore this if germplasm already has a pedigree in the database // Retrieve parent accession numbers to assess if already in db - if (germplasm.getFemaleParentAccessionNumber() != null) { - germplasmAccessionNumbers.put(germplasm.getFemaleParentAccessionNumber(), true); - } - if (germplasm.getMaleParentAccessionNumber() != null) { - germplasmAccessionNumbers.put(germplasm.getMaleParentAccessionNumber(), true); + if (!databaseGermplasmHasPedigree(germplasm)) { + if (germplasm.getFemaleParentAccessionNumber() != null) { + germplasmAccessionNumbers.put(germplasm.getFemaleParentAccessionNumber(), true); + } + if (germplasm.getMaleParentAccessionNumber() != null) { + germplasmAccessionNumbers.put(germplasm.getMaleParentAccessionNumber(), true); + } } + if (germplasm.getAccessionNumber() != null) { germplasmAccessionNumbers.put(germplasm.getAccessionNumber(), false); } @@ -223,27 +226,31 @@ public void getExistingBrapiData(List importRows, Program program) arrayOfStringFormatter.apply(missingAccessionNumbers))); } - List missingEntryNumbers = new ArrayList<>(); - for (BrAPIImport importRow: importRows) { - Germplasm germplasm = importRow.getGermplasm(); - // Check Female Parent - if (germplasm.getFemaleParentEntryNo() != null) { - if ((!germplasmIndexByEntryNo.containsKey(germplasm.getFemaleParentEntryNo())) && !(germplasm.getFemaleParentEntryNo().equals("0"))) { - missingEntryNumbers.add(germplasm.getFemaleParentEntryNo()); + List missingEntryNumbers = new ArrayList<>(); + for (BrAPIImport importRow : importRows) { + Germplasm germplasm = importRow.getGermplasm(); + + //If germplasm already has a pedigree, pedigree cannot be overwritten and file values for pedigree will be ignored + boolean pedigreeExists = databaseGermplasmHasPedigree(germplasm); + + // Check Female Parent + if (germplasm.getFemaleParentEntryNo() != null && !pedigreeExists) { + if ((!germplasmIndexByEntryNo.containsKey(germplasm.getFemaleParentEntryNo())) && !(germplasm.getFemaleParentEntryNo().equals("0"))) { + missingEntryNumbers.add(germplasm.getFemaleParentEntryNo()); + } } - } - // Check Male Parent - if (germplasm.getMaleParentEntryNo() != null) { - if ((!germplasmIndexByEntryNo.containsKey(germplasm.getMaleParentEntryNo())) && !(germplasm.getMaleParentEntryNo().equals("0"))) { - missingEntryNumbers.add(germplasm.getMaleParentEntryNo()); + // Check Male Parent + if (germplasm.getMaleParentEntryNo() != null && !pedigreeExists) { + if ((!germplasmIndexByEntryNo.containsKey(germplasm.getMaleParentEntryNo())) && !(germplasm.getMaleParentEntryNo().equals("0"))) { + missingEntryNumbers.add(germplasm.getMaleParentEntryNo()); + } } } - } - if (missingEntryNumbers.size() > 0) { - throw new HttpStatusException(HttpStatus.UNPROCESSABLE_ENTITY, - String.format(missingParentalEntryNoMsg, - arrayOfStringFormatter.apply(missingEntryNumbers))); - } + if (missingEntryNumbers.size() > 0) { + throw new HttpStatusException(HttpStatus.UNPROCESSABLE_ENTITY, + String.format(missingParentalEntryNoMsg, + arrayOfStringFormatter.apply(missingEntryNumbers))); + } if (listNameDup) { throw new HttpStatusException(HttpStatus.UNPROCESSABLE_ENTITY, listNameAlreadyExists); @@ -405,21 +412,8 @@ private boolean processExistingGermplasm(Germplasm germplasm, ValidationErrors v return false; } - // Error conditions: - // has existing pedigree and file pedigree is different and not empty - // Valid conditions: - // no existing pedigree and file different pedigree - // existing pedigree and file pedigree same - // existing pedigree and file pedigree empty - if(hasPedigree(existingGermplasm) && germplasm.pedigreeExists()) { - if(!arePedigreesEqual(existingGermplasm, germplasm, importRows)) { - ValidationError ve = new ValidationError("Pedigree", pedigreeAlreadyExists, HttpStatus.UNPROCESSABLE_ENTITY); - validationErrors.addError(rowIndex + 2, ve); // +2 instead of +1 to account for the column header row. - return false; - } - } - // if no existing pedigree and file has pedigree then validate and update + // if pedigree exists, file pedigree information should be ignored if(germplasm.pedigreeExists() && !hasPedigree(existingGermplasm)) { validatePedigree(germplasm, rowIndex + 2, validationErrors); updatePedigree = true; @@ -460,6 +454,16 @@ private boolean hasPedigree(BrAPIGermplasm germplasm) { germplasm.getAdditionalInfo().get(BrAPIAdditionalInfoFields.MALE_PARENT_UNKNOWN).getAsBoolean()); } + //Used to check if germplasm already has a pedigree in the database, if so, pedigree information in file should be ignored + private boolean databaseGermplasmHasPedigree(Germplasm germplasm) { + if (germplasm.getAccessionNumber() == null || dbGermplasmByAccessionNo.get(germplasm.getAccessionNumber()) == null) { + return false; + } else { + BrAPIGermplasm dbGermplasm = dbGermplasmByAccessionNo.get(germplasm.getAccessionNumber()); + return hasPedigree(dbGermplasm); + } + } + /** * Compare an existing germplasm's pedigree to the incoming germplasm's pedigree to ensure they are the same.

* Assumes that an empty value for a given parent in the incoming germplasm is equal to the existing germplasm.

@@ -793,6 +797,8 @@ else if (germplasmIndexByEntryNo.containsKey(germplasm.getFemaleParentEntryNo()) if (commit) { if (femaleParentFound) { brAPIGermplasm.putAdditionalInfoItem(BrAPIAdditionalInfoFields.GERMPLASM_FEMALE_PARENT_GID, femaleParent.getAccessionNumber()); + //entry number no longer needed for figuring out parentage, can remove (since same germplasm can have different entry numbers across multiple lists) + brAPIGermplasm.putAdditionalInfoItem(BrAPIAdditionalInfoFields.GERMPLASM_FEMALE_PARENT_ENTRY_NO, null); // Add femaleParentUUID to additionalInfo. Optional femaleParentUUID = Utilities.getExternalReference(femaleParent.getExternalReferences(), BRAPI_REFERENCE_SOURCE); if (femaleParentUUID.isPresent()) { @@ -802,6 +808,8 @@ else if (germplasmIndexByEntryNo.containsKey(germplasm.getFemaleParentEntryNo()) if (maleParent != null) { brAPIGermplasm.putAdditionalInfoItem(BrAPIAdditionalInfoFields.GERMPLASM_MALE_PARENT_GID, maleParent.getAccessionNumber()); + //entry number no longer needed for figuring out parentage, can remove (since same germplasm can have different entry numbers across multiple lists) + brAPIGermplasm.putAdditionalInfoItem(BrAPIAdditionalInfoFields.GERMPLASM_MALE_PARENT_ENTRY_NO, null); // Add maleParentUUID to additionalInfo. Optional maleParentUUID = Utilities.getExternalReference(maleParent.getExternalReferences(), BRAPI_REFERENCE_SOURCE); if (maleParentUUID.isPresent()) {