diff --git a/src/main/java/org/breedinginsight/brapi/v2/services/BrAPIGermplasmService.java b/src/main/java/org/breedinginsight/brapi/v2/services/BrAPIGermplasmService.java index 7e4a01e67..9159e3e47 100644 --- a/src/main/java/org/breedinginsight/brapi/v2/services/BrAPIGermplasmService.java +++ b/src/main/java/org/breedinginsight/brapi/v2/services/BrAPIGermplasmService.java @@ -350,16 +350,16 @@ public List getRawGermplasmByAccessionNumber(ArrayList g } return resultGermplasm; } - - public List getGermplasmByDisplayName(List germplasmDisplayNames, UUID programId) throws ApiException { - List allGermplasm = getGermplasm(programId); - HashSet requestedNames = new HashSet<>(germplasmDisplayNames); - List matchingGermplasm = new ArrayList<>(); - for (BrAPIGermplasm germplasm: allGermplasm) { - if (requestedNames.contains(germplasm.getDefaultDisplayName())) { - matchingGermplasm.add(germplasm); - } + +public List getGermplasmByGID(List germplasmGIDs, UUID programId) throws ApiException { + List allGermplasm = getGermplasm(programId); + HashSet requestedGIDs = new HashSet<>(germplasmGIDs); + List matchingGermplasm = new ArrayList<>(); + for (BrAPIGermplasm germplasm: allGermplasm) { + if (requestedGIDs.contains(germplasm.getAccessionNumber())) { + matchingGermplasm.add(germplasm); } - return matchingGermplasm; } + return matchingGermplasm; +} } 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 1cb15a4b3..3bfe55b5f 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 @@ -17,7 +17,6 @@ package org.breedinginsight.brapps.importer.services.processors.germplasm; import com.google.gson.Gson; -import com.google.gson.JsonElement; import io.micronaut.context.annotation.Property; import io.micronaut.context.annotation.Prototype; import io.micronaut.http.HttpStatus; @@ -68,7 +67,7 @@ public class GermplasmProcessor implements Processor { @Property(name = "brapi.server.reference-source") private String BRAPI_REFERENCE_SOURCE; - private BrAPIGermplasmService brAPIGermplasmService; + private final BrAPIGermplasmService brAPIGermplasmService; private final BreedingMethodDAO breedingMethodDAO; private final BrAPIListDAO brAPIListDAO; private final DSLContext dsl; @@ -76,7 +75,8 @@ public class GermplasmProcessor implements Processor { private final Gson gson = new Gson(); Map> germplasmByAccessionNumber = new HashMap<>(); - Map fileGermplasmByName = new HashMap<>(); + Map fileGermplasmByGID = new HashMap<>(); + Map dbGermplasmByName = new HashMap<>(); Map dbGermplasmByAccessionNo = new HashMap<>(); Map germplasmIndexByEntryNo = new HashMap<>(); @@ -110,7 +110,6 @@ public class GermplasmProcessor implements Processor { @Inject public GermplasmProcessor(BrAPIGermplasmService brAPIGermplasmService, DSLContext dsl, BreedingMethodDAO breedingMethodDAO, BrAPIListDAO brAPIListDAO, BrAPIGermplasmDAO brAPIGermplasmDAO) { - this.brAPIGermplasmService = brAPIGermplasmService; this.dsl = dsl; this.breedingMethodDAO = breedingMethodDAO; this.brAPIGermplasmDAO = brAPIGermplasmDAO; @@ -146,30 +145,29 @@ public void getExistingBrapiData(List importRows, Program program) germplasmIndexByEntryNo.put(germplasm.getEntryNo(), i); } - Integer count = fileGermplasmByName.getOrDefault(germplasm.getGermplasmName(), 0); - fileGermplasmByName.put(germplasm.getGermplasmName(), count+1); + Integer count = fileGermplasmByGID.getOrDefault(germplasm.getAccessionNumber(), 0); + fileGermplasmByGID.put(germplasm.getAccessionNumber(), count+1); } } // Get existing germplasm names - List dbGermplasm = brAPIGermplasmService.getGermplasmByDisplayName(new ArrayList<>(fileGermplasmByName.keySet()), program.getId()); + List dbGermplasm = brAPIGermplasmService.getGermplasmByGID(new ArrayList<>(fileGermplasmByGID.keySet()), program.getId()); dbGermplasm.forEach(germplasm -> { dbGermplasmByName.put(germplasm.getDefaultDisplayName(), germplasm); dbGermplasmByAccessionNo.put(germplasm.getAccessionNumber(), germplasm); }); // Get parental accession nos in file - for (int i = 0; i < importRows.size(); i++) { - BrAPIImport germplasmImport = importRows.get(i); + for (BrAPIImport germplasmImport : importRows) { Germplasm germplasm = germplasmImport.getGermplasm(); if (germplasm != null) { // 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 (germplasm.getFemaleParentAccessionNumber() != null) { + germplasmAccessionNumbers.put(germplasm.getFemaleParentAccessionNumber(), true); + } + if (germplasm.getMaleParentAccessionNumber() != null) { + germplasmAccessionNumbers.put(germplasm.getMaleParentAccessionNumber(), true); + } } } @@ -177,18 +175,16 @@ public void getExistingBrapiData(List importRows, Program program) existingGermplasm = new ArrayList<>(); List missingParentalAccessionNumbers = germplasmAccessionNumbers.entrySet().stream().filter(Map.Entry::getValue).map(Map.Entry::getKey).collect(Collectors.toList()); List missingAccessionNumbers = germplasmAccessionNumbers.entrySet().stream().filter(entry -> !entry.getValue()).map(Map.Entry::getKey).collect(Collectors.toList()); - if (germplasmAccessionNumbers.size() > 0) { + if (!germplasmAccessionNumbers.isEmpty()) { try { existingGermplasm = brAPIGermplasmService.getRawGermplasmByAccessionNumber(new ArrayList<>(germplasmAccessionNumbers.keySet()), program.getId()); List existingAccessionNumbers = existingGermplasm.stream() - .map(germplasm -> germplasm.getAccessionNumber()) + .map(BrAPIGermplasm::getAccessionNumber) .collect(Collectors.toList()); missingParentalAccessionNumbers.removeAll(existingAccessionNumbers); missingAccessionNumbers.removeAll(existingAccessionNumbers); - existingGermplasm.forEach(existingGermplasm -> { - germplasmByAccessionNumber.put(existingGermplasm.getAccessionNumber(), new PendingImportObject<>(ImportObjectState.EXISTING, existingGermplasm)); - }); + existingGermplasm.forEach(existingGermplasm -> germplasmByAccessionNumber.put(existingGermplasm.getAccessionNumber(), new PendingImportObject<>(ImportObjectState.EXISTING, existingGermplasm))); } catch (ApiException e) { // We shouldn't get an error back from our services. If we do, nothing the user can do about it throw new InternalServerException(e.toString(), e); @@ -196,8 +192,8 @@ public void getExistingBrapiData(List importRows, Program program) } // Check for existing germplasm lists - Boolean listNameDup = false; - if (importRows.size() > 0 && importRows.get(0).getGermplasm().getListName() != null) { + boolean listNameDup = false; + if (!importRows.isEmpty() && importRows.get(0).getGermplasm().getListName() != null) { try { Germplasm row = importRows.get(0).getGermplasm(); String listName = Germplasm.constructGermplasmListName(row.getListName(), program); @@ -205,6 +201,7 @@ public void getExistingBrapiData(List importRows, Program program) for (BrAPIListSummary existingList: existingLists) { if (existingList.getListName().equals(listName)) { listNameDup = true; + break; } } } catch (ApiException e) { @@ -217,14 +214,14 @@ public void getExistingBrapiData(List importRows, Program program) missingAccessionNumbers.remove("0"); // Parent reference checks - if (missingParentalAccessionNumbers.size() > 0) { + if (!missingParentalAccessionNumbers.isEmpty()) { throw new HttpStatusException(HttpStatus.UNPROCESSABLE_ENTITY, String.format(missingParentalGIDsMsg, arrayOfStringFormatter.apply(missingParentalAccessionNumbers))); } //GID existence check - if (missingAccessionNumbers.size() > 0) { + if (!missingAccessionNumbers.isEmpty()) { throw new HttpStatusException(HttpStatus.UNPROCESSABLE_ENTITY, String.format(missingGIDsMsg, arrayOfStringFormatter.apply(missingAccessionNumbers))); @@ -247,7 +244,7 @@ public void getExistingBrapiData(List importRows, Program program) } } } - if (missingEntryNumbers.size() > 0) { + if (!missingEntryNumbers.isEmpty()) { throw new HttpStatusException(HttpStatus.UNPROCESSABLE_ENTITY, String.format(missingParentalEntryNoMsg, arrayOfStringFormatter.apply(missingEntryNumbers))); @@ -284,7 +281,6 @@ public Map process(ImportUpload upload, List
(); updatedGermplasmList = new ArrayList<>(); Map breedingMethods = new HashMap<>(); - Boolean nullEntryNotFound = false; List badBreedingMethods = new ArrayList<>(); Map entryNumberCounts = new HashMap<>(); List userProvidedEntryNumbers = new ArrayList<>(); @@ -325,7 +321,7 @@ public Map process(ImportUpload upload, List
0 && userProvidedEntryNumbers.size() < importRows.size()) { + if (!userProvidedEntryNumbers.isEmpty() && userProvidedEntryNumbers.size() < importRows.size()) { throw new HttpStatusException(HttpStatus.UNPROCESSABLE_ENTITY, missingEntryNumbersMsg); } @@ -357,7 +353,7 @@ private void processNewGermplasm(Germplasm germplasm, ValidationErrors validatio breedingMethod = breedingMethods.get(germplasm.getBreedingMethod()); } else { List breedingMethodResults = breedingMethodDAO.findByNameOrAbbreviation(germplasm.getBreedingMethod(), program.getId()); - if (breedingMethodResults.size() > 0) { + if (!breedingMethodResults.isEmpty()) { breedingMethods.put(germplasm.getBreedingMethod(), breedingMethodResults.get(0)); breedingMethod = breedingMethods.get(germplasm.getBreedingMethod()); } else { @@ -379,7 +375,7 @@ private void processNewGermplasm(Germplasm germplasm, ValidationErrors validatio newGermplasmList.add(newGermplasm); // Assign status of the germplasm - if (fileGermplasmByName.get(newGermplasm.getDefaultDisplayName()) > 1 || dbGermplasmByName.containsKey(newGermplasm.getDefaultDisplayName())) { + if (fileGermplasmByGID.get(newGermplasm.getAccessionNumber()) > 1 || dbGermplasmByAccessionNo.containsKey(newGermplasm.getAccessionNumber())) { mappedImportRow.setGermplasm(new PendingImportObject<>(ImportObjectState.EXISTING, newGermplasm)); } else { mappedImportRow.setGermplasm(new PendingImportObject<>(ImportObjectState.NEW, newGermplasm)); @@ -468,16 +464,6 @@ 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 - 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.

@@ -527,7 +513,7 @@ private boolean arePedigreesEqual(BrAPIGermplasm existingGermplasm, Germplasm ge germplasmPedigreeGIDString.append(existingMalePedigree); } - return existingPedigreeGIDString.toString().equals(germplasmPedigreeGIDString.toString()); + return existingPedigreeGIDString.toString().contentEquals(germplasmPedigreeGIDString); } else { return true; } @@ -656,7 +642,7 @@ private void createPostOrder() { } totalRecorded += createList.size(); - if (createList.size() > 0) { + if (!createList.isEmpty()) { created.addAll(createList.stream().map(GermplasmImportIdUtils::getImportId).collect(Collectors.toList())); postOrder.add(createList); } else if (totalRecorded < newGermplasmList.size()) { @@ -667,7 +653,7 @@ private void createPostOrder() { } @Override - public void validateDependencies(Map mappedBrAPIImport) throws ValidatorException { + public void validateDependencies(Map mappedBrAPIImport) { // TODO } @@ -706,9 +692,7 @@ public void postBrapiData(Map mappedBrAPIImport, Program // Update our records with what is returned Map createdGermplasmMap = new HashMap<>(); - createdGermplasm.forEach(germplasm -> { - createdGermplasmMap.put(germplasm.getGermplasmName(), germplasm); - }); + createdGermplasm.forEach(germplasm -> createdGermplasmMap.put(germplasm.getGermplasmName(), germplasm)); for (Map.Entry entry : mappedBrAPIImport.entrySet()) { String germplasmName = entry.getValue().getGermplasm().getBrAPIObject().getGermplasmName(); if (createdGermplasmMap.containsKey(germplasmName)) {