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 @@ -106,10 +106,10 @@ public HttpResponse<Response<DataResponse<List<BrAPIGermplasm>>>> getGermplasm(
@ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.ALL})
public HttpResponse<Response<DataResponse<List<BrAPIGermplasm>>>> getGermplasmListRecords(
@PathVariable("programId") UUID programId,
@PathVariable("listDbId") String listId,
@PathVariable("listDbId") String listDbId,
@QueryValue @QueryValid(using = GermplasmQueryMapper.class) @Valid GermplasmQuery queryParams) {
try {
List<BrAPIGermplasm> germplasm = germplasmService.getGermplasmByList(programId, listId);
List<BrAPIGermplasm> germplasm = germplasmService.getGermplasmByList(programId, listDbId);
SearchRequest searchRequest = queryParams.constructSearchRequest();
return ResponseUtils.getBrapiQueryResponse(germplasm, germplasmQueryMapper, queryParams, searchRequest);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package org.breedinginsight.brapi.v2.constants;

public final class BrAPIAdditionalInfoFields {
public static final String GERMPLASM_LIST_ENTRY_NUMBERS = "listEntryNumbers";
public static final String GERMPLASM_LIST_ID = "listId";
public static final String GERMPLASM_RAW_PEDIGREE = "rawPedigree";
public static final String GERMPLASM_PEDIGREE_BY_NAME = "pedigreeByName";
public static final String GERMPLASM_PEDIGREE_BY_UUID = "pedigreeByUUID";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import lombok.extern.slf4j.Slf4j;
import org.brapi.v2.model.core.BrAPIListSummary;
import org.brapi.v2.model.core.BrAPIListTypes;
import org.brapi.v2.model.core.request.BrAPIListNewRequest;
import org.brapi.v2.model.core.response.BrAPIListDetails;
import org.brapi.v2.model.core.response.BrAPIListsListResponse;
import org.brapi.v2.model.core.response.BrAPIListsSingleResponse;
import org.brapi.v2.model.germ.BrAPIGermplasm;
import org.brapi.v2.model.germ.BrAPIGermplasmSynonyms;
import org.breedinginsight.brapi.v2.constants.BrAPIAdditionalInfoFields;
import org.breedinginsight.brapps.importer.daos.BrAPIListDAO;
import org.breedinginsight.brapps.importer.model.exports.FileType;
Expand All @@ -35,6 +34,7 @@
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.ToIntFunction;
import java.util.stream.Collectors;

@Slf4j
Expand Down Expand Up @@ -112,18 +112,25 @@ public List<BrAPIListSummary> getGermplasmListsByProgramId(UUID programId, HttpR
}
}

public List<Map<String, Object>> processData(List<BrAPIGermplasm> germplasm){
public List<Map<String, Object>> processListData(List<BrAPIGermplasm> germplasm, UUID germplasmListId){
List<Map<String, Object>> processedData = new ArrayList<>();

for (BrAPIGermplasm germplasmEntry: germplasm) {
HashMap<String, Object> row = new HashMap<>();
row.put("GID", Integer.valueOf(germplasmEntry.getAccessionNumber()));
row.put("Name", germplasmEntry.getGermplasmName());
row.put("Entry No", germplasmEntry.getAdditionalInfo().get(BrAPIAdditionalInfoFields.GERMPLASM_IMPORT_ENTRY_NUMBER).getAsInt());
row.put("Breeding Method", germplasmEntry.getAdditionalInfo().get(BrAPIAdditionalInfoFields.GERMPLASM_BREEDING_METHOD).getAsString());
String source = germplasmEntry.getSeedSource();
row.put("Source", source);

// Use the entry number in the list map if generated
if(new UUID(0,0).compareTo(germplasmListId) == 0) {
row.put("Entry No", germplasmEntry.getAdditionalInfo().get(BrAPIAdditionalInfoFields.GERMPLASM_IMPORT_ENTRY_NUMBER).getAsInt());
} else {
row.put("Entry No", germplasmEntry.getAdditionalInfo()
.getAsJsonObject(BrAPIAdditionalInfoFields.GERMPLASM_LIST_ENTRY_NUMBERS).get(germplasmListId.toString()).getAsInt());
}

//If germplasm was imported with an external UID, it will be stored in external reference with same source as seed source
List<BrAPIExternalReference> externalReferences = germplasmEntry.getExternalReferences();
for (BrAPIExternalReference reference: externalReferences){
Expand Down Expand Up @@ -156,17 +163,24 @@ public List<Map<String, Object>> processData(List<BrAPIGermplasm> germplasm){
return processedData;
}

public List<BrAPIGermplasm> getGermplasmByList(UUID programId, String listId) throws ApiException {
public List<BrAPIGermplasm> getGermplasmByList(UUID programId, String listDbId) throws ApiException {
// get list germplasm names
BrAPIListsSingleResponse listResponse = brAPIListDAO.getListById(listId, programId);
BrAPIListsSingleResponse listResponse = brAPIListDAO.getListById(listDbId, programId);
if(Objects.nonNull(listResponse) && Objects.nonNull(listResponse.getResult())) {
List<String> germplasmNames = listResponse.getResult().getData();

// get the list ID stored in the list external references
UUID listId = getGermplasmListId(listResponse.getResult());

// get list BrAPI germplasm variables
return germplasmDAO.getGermplasmByRawName(germplasmNames, programId);
List<String> germplasmNames = listResponse.getResult().getData();
List<BrAPIGermplasm> germplasm = germplasmDAO.getGermplasmByRawName(germplasmNames, programId);

// set the list ID in the germplasm additional info
germplasm.forEach(g -> g.putAdditionalInfoItem(BrAPIAdditionalInfoFields.GERMPLASM_LIST_ID, listId));
return germplasm;
} else throw new ApiException();
}
public DownloadFile exportGermplasmList(UUID programId, String listId, FileType fileExtension) throws ApiException, IOException {
public DownloadFile exportGermplasmList(UUID programId, String listId, FileType fileExtension) throws IllegalArgumentException, ApiException, IOException {
List<Column> columns = GermplasmFileColumns.getOrderedColumns();

//Retrieve germplasm list data
Expand All @@ -175,8 +189,10 @@ public DownloadFile exportGermplasmList(UUID programId, String listId, FileType
//Retrieve germplasm data
List<String> germplasmNames = listData.getData();
List<BrAPIGermplasm> germplasm = germplasmDAO.getGermplasmByRawName(germplasmNames, programId);

//processGermplasmForDisplay, numbers
germplasm.sort(Comparator.comparingInt(g -> g.getAdditionalInfo().get(BrAPIAdditionalInfoFields.GERMPLASM_IMPORT_ENTRY_NUMBER).getAsInt()));
UUID germplasmListId = getGermplasmListId(listData);
germplasm.sort(Comparator.comparingInt(getEntryNumber(germplasmListId)));

String listName = listData.getListName();
Optional<Program> optionalProgram = programService.getById(programId);
Expand All @@ -187,7 +203,7 @@ public DownloadFile exportGermplasmList(UUID programId, String listId, FileType
String fileName = createFileName(listData, listName);
StreamedFile downloadFile;
//Convert list data to List<Map<String, Object>> data to pass into file writer
List<Map<String, Object>> processedData = processData(germplasm);
List<Map<String, Object>> processedData = processListData(germplasm, germplasmListId);

if (fileExtension == FileType.CSV){
downloadFile = CSVWriter.writeToDownload(columns, processedData, fileExtension);
Expand All @@ -198,6 +214,57 @@ public DownloadFile exportGermplasmList(UUID programId, String listId, FileType
return new DownloadFile(fileName, downloadFile);
}

public UUID getGermplasmListId(BrAPIListDetails listData) {
if(Objects.nonNull(listData.getExternalReferences()) && hasListExternalReference(listData.getExternalReferences())) {
return UUID.fromString(listData.getExternalReferences().stream()
.filter(e -> referenceSource.concat("/lists").equals(e.getReferenceSource()))
.map(e -> e.getReferenceID()).findAny().orElse("00000000-0000-0000-000000000000"));
} else {
return new UUID(0,0);
}
}

public UUID getGermplasmListId(BrAPIListNewRequest importList) {
if(Objects.nonNull(importList.getExternalReferences()) && hasListExternalReference(importList.getExternalReferences())) {
return UUID.fromString(importList.getExternalReferences().stream()
.filter(e -> referenceSource.concat("/lists").equals(e.getReferenceSource()))
.map(e -> e.getReferenceID()).findAny().orElse("00000000-0000-0000-000000000000"));
} else {
return new UUID(0,0);
}
}

private boolean hasListExternalReference(List<BrAPIExternalReference> refs) throws IllegalArgumentException {
if (refs == null) throw new IllegalArgumentException();
return refs.stream().anyMatch(e -> referenceSource.concat("/lists").equals(e.getReferenceSource()));
}

private ToIntFunction<BrAPIGermplasm> getEntryNumber(UUID germplasmListId) throws IllegalArgumentException {
if(germplasmListId.compareTo(new UUID(0,0)) == 0) {
return this::getImportEntryNumber;
} else {
return g -> getGermplasmListEntryNumber(g, germplasmListId);
}
}

private Integer getImportEntryNumber(BrAPIGermplasm g) throws IllegalArgumentException {
if(Objects.nonNull(g.getAdditionalInfo()) &&
g.getAdditionalInfo().has(BrAPIAdditionalInfoFields.GERMPLASM_IMPORT_ENTRY_NUMBER)) {
return g.getAdditionalInfo().get(BrAPIAdditionalInfoFields.GERMPLASM_IMPORT_ENTRY_NUMBER).getAsInt();
} else {
throw new IllegalArgumentException();
}
}
private Integer getGermplasmListEntryNumber(BrAPIGermplasm g, UUID germplasmListId) throws IllegalArgumentException {
if(Objects.nonNull(g.getAdditionalInfo()) &&
g.getAdditionalInfo().has(BrAPIAdditionalInfoFields.GERMPLASM_LIST_ENTRY_NUMBERS)) {
return g.getAdditionalInfo().getAsJsonObject(BrAPIAdditionalInfoFields.GERMPLASM_LIST_ENTRY_NUMBERS)
.get(germplasmListId.toString()).getAsInt();
} else {
throw new IllegalArgumentException();
}
}

private String createFileName(BrAPIListDetails listData, String listName) {
//TODO change timestamp to edit date when editing functionality is added
String timestamp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.brapi.v2.model.germ.BrAPIGermplasmSynonyms;
import org.breedinginsight.brapi.v2.constants.BrAPIAdditionalInfoFields;
import org.breedinginsight.brapps.importer.model.config.*;
import org.breedinginsight.dao.db.tables.pojos.BreedingMethodEntity;
import org.breedinginsight.dao.db.tables.pojos.ProgramBreedingMethodEntity;
import org.breedinginsight.model.Program;
import org.breedinginsight.model.User;
Expand Down Expand Up @@ -131,25 +132,29 @@ public BrAPIListNewRequest constructBrAPIList(Program program, String referenceS
brapiList.setListName(constructGermplasmListName(listName, program));
brapiList.setListDescription(this.listDescription);
brapiList.listType(BrAPIListTypes.GERMPLASM);
// Set external reference
BrAPIExternalReference reference = new BrAPIExternalReference();
reference.setReferenceSource(String.format("%s/programs", referenceSource));
reference.setReferenceID(program.getId().toString());
brapiList.setExternalReferences(List.of(reference));

// Set external references
BrAPIExternalReference programReference = new BrAPIExternalReference();
programReference.setReferenceSource(String.format("%s/programs", referenceSource));
programReference.setReferenceID(program.getId().toString());
BrAPIExternalReference listReference = new BrAPIExternalReference();
listReference.setReferenceSource(String.format("%s/lists", referenceSource));
listReference.setReferenceID(UUID.randomUUID().toString());
brapiList.setExternalReferences(List.of(programReference, listReference));

return brapiList;
}

public static String constructGermplasmListName(String listName, Program program) {
return String.format("%s [%s-germplasm]", listName, program.getKey());
}

private BrAPIGermplasm constructBrAPIGermplasm(ProgramBreedingMethodEntity breedingMethod, User user) {
public BrAPIGermplasm constructBrAPIGermplasm(ProgramBreedingMethodEntity breedingMethod, User user, UUID listId) {
BrAPIGermplasm germplasm = new BrAPIGermplasm();
germplasm.setGermplasmName(getGermplasmName());
germplasm.setDefaultDisplayName(getGermplasmName());
germplasm.setGermplasmPUI(getGermplasmPUI());
germplasm.setCollection(getCollection());
germplasm.putAdditionalInfoItem(BrAPIAdditionalInfoFields.GERMPLASM_IMPORT_ENTRY_NUMBER, entryNo);
germplasm.putAdditionalInfoItem(BrAPIAdditionalInfoFields.GERMPLASM_FEMALE_PARENT_GID, getFemaleParentDBID());
germplasm.putAdditionalInfoItem(BrAPIAdditionalInfoFields.GERMPLASM_MALE_PARENT_GID, getMaleParentDBID());
germplasm.putAdditionalInfoItem(BrAPIAdditionalInfoFields.GERMPLASM_FEMALE_PARENT_ENTRY_NO, getFemaleParentEntryNo());
Expand All @@ -158,6 +163,9 @@ private BrAPIGermplasm constructBrAPIGermplasm(ProgramBreedingMethodEntity breed
createdBy.put(BrAPIAdditionalInfoFields.CREATED_BY_USER_ID, user.getId().toString());
createdBy.put(BrAPIAdditionalInfoFields.CREATED_BY_USER_NAME, user.getName());
germplasm.putAdditionalInfoItem(BrAPIAdditionalInfoFields.CREATED_BY, createdBy);
Map<UUID, String> listEntryNumbers = new HashMap<>();
listEntryNumbers.put(listId, entryNo);
germplasm.putAdditionalInfoItem(BrAPIAdditionalInfoFields.GERMPLASM_LIST_ENTRY_NUMBERS, listEntryNumbers);
//TODO: Need to check that the acquisition date it in date format
//brAPIGermplasm.setAcquisitionDate(pedigreeImport.getGermplasm().getAcquisitionDate());
germplasm.setCountryOfOriginCode(getCountryOfOrigin());
Expand Down Expand Up @@ -246,8 +254,8 @@ private void setBrAPIGermplasmCommitFields(BrAPIGermplasm germplasm, String prog
}
}

public BrAPIGermplasm constructBrAPIGermplasm(Program program, ProgramBreedingMethodEntity breedingMethod, User user, boolean commit, String referenceSource, Supplier<BigInteger> nextVal) {
BrAPIGermplasm germplasm = constructBrAPIGermplasm(breedingMethod, user);
public BrAPIGermplasm constructBrAPIGermplasm(Program program, ProgramBreedingMethodEntity breedingMethod, User user, boolean commit, String referenceSource, Supplier<BigInteger> nextVal, UUID listId) {
BrAPIGermplasm germplasm = constructBrAPIGermplasm(breedingMethod, user, listId);
if (commit) {
setBrAPIGermplasmCommitFields(germplasm, program.getKey(), referenceSource, nextVal);
}
Expand All @@ -261,4 +269,4 @@ public BrAPIGermplasm constructBrAPIGermplasm(Program program, ProgramBreedingMe

return germplasm;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ public Map<String, ImportPreviewStatistics> process(List<BrAPIImport> importRows
entryNumberCounts.put(germplasm.getEntryNo(),
entryNumberCounts.containsKey(germplasm.getEntryNo()) ? entryNumberCounts.get(germplasm.getEntryNo()) + 1 : 1);

BrAPIGermplasm newGermplasm = germplasm.constructBrAPIGermplasm(program, breedingMethod, user, commit, BRAPI_REFERENCE_SOURCE, nextVal);
UUID importListId = brAPIGermplasmService.getGermplasmListId(importList);

BrAPIGermplasm newGermplasm = germplasm.constructBrAPIGermplasm(program, breedingMethod, user, commit, BRAPI_REFERENCE_SOURCE, nextVal, importListId);

newGermplasmList.add(newGermplasm);
// Assign status of the germplasm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.JsonObject;
import lombok.Getter;
import org.brapi.v2.model.core.response.BrAPIListsSingleResponse;
import org.brapi.v2.model.germ.BrAPIGermplasm;
import org.breedinginsight.api.v1.controller.metadata.SortOrder;
import org.breedinginsight.brapi.v1.model.ObservationVariable;
Expand All @@ -24,10 +25,24 @@ public class GermplasmQueryMapper extends AbstractQueryMapper {

public GermplasmQueryMapper() {
fields = Map.ofEntries(
Map.entry("importEntryNumber", (germplasm) ->
germplasm.getAdditionalInfo() != null && germplasm.getAdditionalInfo().has(BrAPIAdditionalInfoFields.GERMPLASM_IMPORT_ENTRY_NUMBER) ?
germplasm.getAdditionalInfo().get(BrAPIAdditionalInfoFields.GERMPLASM_IMPORT_ENTRY_NUMBER).getAsString() :
null),
Map.entry("importEntryNumber", (germplasm) ->{
String entryNumber = null;
if (germplasm.getAdditionalInfo() != null) {
// if additionalInfo contains the importEntryNumber key then return the value
if (germplasm.getAdditionalInfo().has(BrAPIAdditionalInfoFields.GERMPLASM_IMPORT_ENTRY_NUMBER)) {
entryNumber = germplasm.getAdditionalInfo().get(BrAPIAdditionalInfoFields.GERMPLASM_IMPORT_ENTRY_NUMBER).getAsString();
}

// if additionalInfo has both listEntryNumbers and listId keys then return the entry number
// mapped to the listId
if (germplasm.getAdditionalInfo().has(BrAPIAdditionalInfoFields.GERMPLASM_LIST_ENTRY_NUMBERS)
&& germplasm.getAdditionalInfo().has(BrAPIAdditionalInfoFields.GERMPLASM_LIST_ID)) {
String listId = germplasm.getAdditionalInfo().get(BrAPIAdditionalInfoFields.GERMPLASM_LIST_ID).getAsString();
entryNumber = germplasm.getAdditionalInfo().getAsJsonObject(BrAPIAdditionalInfoFields.GERMPLASM_LIST_ENTRY_NUMBERS).get(listId).getAsString();
}
}
return entryNumber;
}),
Map.entry("accessionNumber", BrAPIGermplasm::getAccessionNumber),
Map.entry("defaultDisplayName", BrAPIGermplasm::getDefaultDisplayName),
Map.entry("breedingMethod", (germplasm) ->
Expand Down
Loading