Skip to content
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 @@ -187,26 +187,24 @@ public List<BrAPIGermplasm> getGermplasmByList(UUID programId, String listDbId)
// get list BrAPI germplasm variables
List<String> germplasmNames = listResponse.getResult().getData();
List<BrAPIGermplasm> germplasm = germplasmDAO.getGermplasmByRawName(germplasmNames, programId);
Map<String, BrAPIGermplasm> germplasmByName = new HashMap<>();
Map<String, BrAPIGermplasm> germplasmByGid = new HashMap<>();

for (BrAPIGermplasm g : germplasm) {
// set the list ID in the germplasm additional info
g.putAdditionalInfoItem(BrAPIAdditionalInfoFields.GERMPLASM_LIST_ID, listId);
// Add to map.
germplasmByName.put(g.getGermplasmName(), g);
germplasmByGid.put(g.getAccessionNumber(), g);
}

// Get the program key.
String programKey = programService.getById(programId)
.orElseThrow(ApiException::new)
.getKey();
// Extract gids from list names
List<String> gids = germplasmNames.stream().map(Utilities::extractGid).collect(Collectors.toList());

// Build list from BrAPI list that preserves ordering and duplicates and assigns sequential entry numbers.
List<BrAPIGermplasm> germplasmList = new ArrayList<>();
int entryNumber = 0;
for (String germplasmName : germplasmNames) {
for (String gid : gids) {
++entryNumber;
BrAPIGermplasm listEntry = cloneBrAPIGermplasm(germplasmByName.get(Utilities.removeProgramKeyAndUnknownAdditionalData(germplasmName, programKey)));
BrAPIGermplasm listEntry = cloneBrAPIGermplasm(germplasmByGid.get(gid));
// Set entry number.
listEntry.putAdditionalInfoItem(BrAPIAdditionalInfoFields.GERMPLASM_IMPORT_ENTRY_NUMBER, entryNumber);
germplasmList.add(listEntry);
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/breedinginsight/utilities/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.sql.Statement;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utilities {
Expand Down Expand Up @@ -183,6 +184,37 @@ public static String removeProgramKeyAndUnknownAdditionalData(String original, S
return stripped;
}

/**
* Extracts the germplasm identifier (GID) from a germplasm name string that contains
* a key in the format "[PROGKEY-NUMBER]".
*
* <p>This method searches for a pattern matching "[anything-digits]" in the input string
* and returns the numeric portion if found. The prefix before the hyphen can be any sequence
* of characters.</p>
*
* @param germplasmNameWithKey The germplasm name string containing the identifier in the format
* "[PROGKEY-NUMBER]", e.g., "TestDup [DEMO-12]"
* @return The numeric portion after the hyphen as a String if the pattern is found,
* or null if the pattern is not found in the input string
* @throws NullPointerException If the input string is null
*
* @example
* <pre>
* String gid = extractGid("TestDup [DEMO-12]"); // Returns "12"
* String gid = extractGid("Wheat [BRC-789]"); // Returns "789"
* String gid = extractGid("NoPattern"); // Returns null
* </pre>
*/
public static String extractGid(String germplasmNameWithKey) {
Pattern pattern = Pattern.compile("\\[(.*?)-(\\d+)\\]");
Matcher matcher = pattern.matcher(germplasmNameWithKey);

if (matcher.find()) {
return matcher.group(2);
}
return null;
}

public static String generateApiExceptionLogMessage(ApiException e) {
return new StringBuilder("BrAPI Exception: \n\t").append("message: ")
.append(e.getMessage())
Expand Down