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
@@ -0,0 +1,174 @@
package org.breedinginsight.api.v1.controller;

import io.micronaut.http.HttpResponse;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.*;
import io.micronaut.http.server.exceptions.InternalServerException;
import io.micronaut.security.annotation.Secured;
import io.micronaut.security.rules.SecurityRule;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.brapi.client.v2.model.exceptions.ApiException;
import org.breedinginsight.api.auth.*;
import org.breedinginsight.api.model.v1.response.DataResponse;
import org.breedinginsight.api.model.v1.response.Response;
import org.breedinginsight.api.model.v1.response.metadata.Metadata;
import org.breedinginsight.api.model.v1.response.metadata.Pagination;
import org.breedinginsight.api.model.v1.response.metadata.Status;
import org.breedinginsight.api.model.v1.response.metadata.StatusCode;
import org.breedinginsight.api.v1.controller.metadata.AddMetadata;
import org.breedinginsight.dao.db.tables.pojos.BreedingMethodEntity;
import org.breedinginsight.dao.db.tables.pojos.ProgramBreedingMethodEntity;
import org.breedinginsight.daos.BreedingMethodDAO;
import org.breedinginsight.services.BreedingMethodService;
import org.breedinginsight.services.exceptions.BadRequestException;

import javax.inject.Inject;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Slf4j
@Controller("/${micronaut.bi.api.version}")
public class BreedingMethodController {

private final SecurityService securityService;
private final BreedingMethodService breedingMethodService;

@Inject
public BreedingMethodController(SecurityService securityService, BreedingMethodService breedingMethodService) {
this.securityService = securityService;
this.breedingMethodService = breedingMethodService;
}

@Get("/breeding-methods")
@Produces(MediaType.APPLICATION_JSON)
@AddMetadata
@Secured(SecurityRule.IS_AUTHENTICATED)
public HttpResponse<Response<DataResponse<ProgramBreedingMethodEntity>>> getSystemBreedingMethods() {
log.debug("fetching system breeding methods");

try {
AuthenticatedUser actingUser = securityService.getUser();

List<ProgramBreedingMethodEntity> breedingMethods = breedingMethodService.getSystemBreedingMethods();
return buildResponse(breedingMethods);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new InternalServerException("Error fetching system breeding methods", e);
}
}

@Post("programs/{programId}/breeding-methods")
@Produces(MediaType.APPLICATION_JSON)
@ProgramSecured(roles = {ProgramSecuredRole.BREEDER})
public HttpResponse<Response<ProgramBreedingMethodEntity>> createProgramBreedingMethod(@PathVariable UUID programId, @Body ProgramBreedingMethodEntity breedingMethod) throws BadRequestException, ApiException {
log.debug("Saving new program breeding method");

try {
AuthenticatedUser user = securityService.getUser();
Response<ProgramBreedingMethodEntity> response = new Response<>();
response.result = breedingMethodService.createBreedingMethod(breedingMethod, programId, user.getId());

List<Status> metadataStatus = new ArrayList<>();
metadataStatus.add(new Status(StatusCode.INFO, "Successful Creation"));
Pagination pagination = new Pagination(1, 1, 1, 0);
Metadata metadata = new Metadata(pagination, metadataStatus);
response.metadata = metadata;

return HttpResponse.ok(response);
} catch (Exception e) {
log.error("Error creating program breeding method", e);
throw e;
}
}

@Get("programs/{programId}/breeding-methods{?inUse}")
@Produces(MediaType.APPLICATION_JSON)
@AddMetadata
@ProgramSecured(roleGroups = {ProgramSecuredRoleGroup.ALL})
public HttpResponse<Response<DataResponse<ProgramBreedingMethodEntity>>> getProgramBreedingMethods(@PathVariable UUID programId, @QueryValue(defaultValue = "false") Boolean inUse) {
log.debug(String.format("fetching breeding methods for program: %s", programId));

try {
AuthenticatedUser actingUser = securityService.getUser();
List<ProgramBreedingMethodEntity> breedingMethods;
if(inUse) {
breedingMethods = breedingMethodService.fetchBreedingMethodsInUse(programId);
} else {
breedingMethods = breedingMethodService.getBreedingMethods(programId);
}
return buildResponse(breedingMethods);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new InternalServerException("Error fetching breeding methods", e);
}
}

@Put("programs/{programId}/breeding-methods/{breedingMethodId}")
@Produces(MediaType.APPLICATION_JSON)
@ProgramSecured(roles = {ProgramSecuredRole.BREEDER})
public HttpResponse<Response<ProgramBreedingMethodEntity>> updateProgramBreedingMethod(@PathVariable UUID programId, @PathVariable UUID breedingMethodId, @Body ProgramBreedingMethodEntity breedingMethod) throws BadRequestException, ApiException {
log.debug("Saving new program breeding method");

try {
AuthenticatedUser user = securityService.getUser();

breedingMethod.setId(breedingMethodId);

Response<ProgramBreedingMethodEntity> response = new Response<>();
response.result = breedingMethodService.updateBreedingMethod(breedingMethod, programId, user.getId());

List<Status> metadataStatus = new ArrayList<>();
metadataStatus.add(new Status(StatusCode.INFO, "Successful Update"));
Pagination pagination = new Pagination(1, 1, 1, 0);
Metadata metadata = new Metadata(pagination, metadataStatus);
response.metadata = metadata;

return HttpResponse.ok(response);
} catch (Exception e) {
log.error("Error updating program breeding method", e);
throw e;
}
}

@Put("programs/{programId}/breeding-methods/enable")
@ProgramSecured(roles = {ProgramSecuredRole.BREEDER})
public HttpResponse enableSystemBreedingMethods(@PathVariable UUID programId, @Body List<UUID> systemBreedingMethodIds) throws ApiException, BadRequestException {
log.debug("enabling system breeding methods for program: "+programId);

try {
AuthenticatedUser user = securityService.getUser();

breedingMethodService.enableSystemMethods(systemBreedingMethodIds, programId, user.getId());
return HttpResponse.ok();
} catch (Exception e) {
log.error("Error enabling system breeding methods for program: "+programId, e);
throw e;
}
}

@Delete("programs/{programId}/breeding-methods/{breedingMethodId}")
@ProgramSecured(roles = {ProgramSecuredRole.BREEDER})
public HttpResponse deleteProgramBreedingMethod(@PathVariable UUID programId, @PathVariable UUID breedingMethodId) throws BadRequestException, ApiException {
try {
AuthenticatedUser user = securityService.getUser();
breedingMethodService.deleteBreedingMethod(programId, breedingMethodId);
return HttpResponse.ok();
} catch (Exception e) {
log.error("Error deleting breeding method.\n\tprogramId: " + programId + "\n\tbreedingMethodId: " + breedingMethodId);
throw e;
}
}

private HttpResponse<Response<DataResponse<ProgramBreedingMethodEntity>>> buildResponse(List<ProgramBreedingMethodEntity> breedingMethods) {
List<Status> metadataStatus = new ArrayList<>();
metadataStatus.add(new Status(StatusCode.INFO, "Successful Query"));
Pagination pagination = new Pagination(breedingMethods.size(), breedingMethods.size(), 1, 0);
Metadata metadata = new Metadata(pagination, metadataStatus);
Response<DataResponse<ProgramBreedingMethodEntity>> response = new Response<>(metadata, new DataResponse<>(breedingMethods));
return HttpResponse.ok(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +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;
import org.breedinginsight.utilities.Utilities;
Expand Down Expand Up @@ -143,7 +143,7 @@ public static String constructGermplasmListName(String listName, Program program
return String.format("%s [%s-germplasm]", listName, program.getKey());
}

public BrAPIGermplasm constructBrAPIGermplasm(BreedingMethodEntity breedingMethod, User user) {
private BrAPIGermplasm constructBrAPIGermplasm(ProgramBreedingMethodEntity breedingMethod, User user) {
BrAPIGermplasm germplasm = new BrAPIGermplasm();
germplasm.setGermplasmName(getGermplasmName());
germplasm.setDefaultDisplayName(getGermplasmName());
Expand All @@ -163,8 +163,8 @@ public BrAPIGermplasm constructBrAPIGermplasm(BreedingMethodEntity breedingMetho
germplasm.setCountryOfOriginCode(getCountryOfOrigin());
if (additionalInfos != null) {
additionalInfos.stream()
.filter(additionalInfo -> additionalInfo.getAdditionalInfoValue() != null)
.forEach(additionalInfo -> germplasm.putAdditionalInfoItem(additionalInfo.getAdditionalInfoName(), additionalInfo.getAdditionalInfoValue()));
.filter(additionalInfo -> additionalInfo.getAdditionalInfoValue() != null)
.forEach(additionalInfo -> germplasm.putAdditionalInfoItem(additionalInfo.getAdditionalInfoName(), additionalInfo.getAdditionalInfoValue()));
}

// Seed Source
Expand All @@ -183,8 +183,8 @@ public BrAPIGermplasm constructBrAPIGermplasm(BreedingMethodEntity breedingMetho
germplasm.externalReferences(new ArrayList<>());
if (externalReferences != null) {
List<BrAPIExternalReference> brAPIExternalReferences = externalReferences.stream()
.map(externalReference -> externalReference.constructBrAPIExternalReference())
.collect(Collectors.toList());
.map(externalReference -> externalReference.constructBrAPIExternalReference())
.collect(Collectors.toList());
if (uidExternalReference != null) {
brAPIExternalReferences.add(uidExternalReference);
}
Expand All @@ -203,9 +203,9 @@ public BrAPIGermplasm constructBrAPIGermplasm(BreedingMethodEntity breedingMetho
List<BrAPIGermplasmSynonyms> brapiSynonyms = new ArrayList<>();
if (synonyms != null) {
List<String> synonyms = Arrays.asList(blobSynonyms.split(";")).stream()
.map(synonym -> synonym.strip())
.distinct()
.collect(Collectors.toList());
.map(synonym -> synonym.strip())
.distinct()
.collect(Collectors.toList());
// Create synonym
for (String synonym: synonyms) {
BrAPIGermplasmSynonyms brapiSynonym = new BrAPIGermplasmSynonyms();
Expand Down Expand Up @@ -246,7 +246,7 @@ private void setBrAPIGermplasmCommitFields(BrAPIGermplasm germplasm, String prog
}
}

public BrAPIGermplasm constructBrAPIGermplasm(Program program, BreedingMethodEntity breedingMethod, User user, boolean commit, String referenceSource, Supplier<BigInteger> nextVal) {
public BrAPIGermplasm constructBrAPIGermplasm(Program program, ProgramBreedingMethodEntity breedingMethod, User user, boolean commit, String referenceSource, Supplier<BigInteger> nextVal) {
BrAPIGermplasm germplasm = constructBrAPIGermplasm(breedingMethod, user);
if (commit) {
setBrAPIGermplasmCommitFields(germplasm, program.getKey(), referenceSource, nextVal);
Expand Down
Loading