diff --git a/src/main/java/org/generationcp/middleware/api/brapi/StudyServiceBrapiImpl.java b/src/main/java/org/generationcp/middleware/api/brapi/StudyServiceBrapiImpl.java index 24b9df3e40..58cc106965 100644 --- a/src/main/java/org/generationcp/middleware/api/brapi/StudyServiceBrapiImpl.java +++ b/src/main/java/org/generationcp/middleware/api/brapi/StudyServiceBrapiImpl.java @@ -206,6 +206,8 @@ public List getStudyInstancesWithMetadata(final StudySearchFil environmentParameterVariables.addAll(this.createGeolocationVariables(environmentVariables, studyDbId)); + this.addEnvironmentParameterVariablesWithoutValues(environmentParameterVariables, environmentVariables); + final List environmentParameters = environmentParameterVariables.stream() .map(EnvironmentParameter::new).collect(Collectors.toList()); studyInstanceDto.setEnvironmentParameters(environmentParameters); @@ -599,4 +601,29 @@ private List getUsersForEnvironment(final Integer instanceId) { return Collections.emptyList(); } + private void addEnvironmentParameterVariablesWithoutValues(final List environmentParameterVariables, + final List environmentVariables) { + // Filter standard environment factors + final List filterVariables = Lists.newArrayList( + TermId.LOCATION_ID.getId(), + TermId.TRIAL_INSTANCE_FACTOR.getId(), + TermId.EXPERIMENT_DESIGN_FACTOR.getId()); + //filter the variables already added + filterVariables.addAll(environmentParameterVariables.stream().map(MeasurementVariable::getTermId).collect(Collectors.toList())); + + final List variables = environmentVariables + .stream() + .filter(measurementVariable -> !filterVariables.contains(measurementVariable.getTermId())) + .map(measurementVariable -> { + if (measurementVariable.getValue() == null) { + measurementVariable.setValue(""); + } + return measurementVariable; + }) + .collect(Collectors.toList()); + if (!CollectionUtils.isEmpty(variables)) { + environmentParameterVariables.addAll(variables); + } + } + } diff --git a/src/test/java/org/generationcp/middleware/brapi/StudyServiceBrapiImplTest.java b/src/test/java/org/generationcp/middleware/brapi/StudyServiceBrapiImplTest.java index 9ad2231e02..5e795484fa 100644 --- a/src/test/java/org/generationcp/middleware/brapi/StudyServiceBrapiImplTest.java +++ b/src/test/java/org/generationcp/middleware/brapi/StudyServiceBrapiImplTest.java @@ -27,11 +27,13 @@ import org.generationcp.middleware.service.api.study.EnvironmentParameter; import org.generationcp.middleware.service.api.study.StudyDetailsDto; import org.generationcp.middleware.service.api.study.StudyInstanceDto; +import org.generationcp.middleware.service.api.study.StudySearchFilter; import org.generationcp.middleware.utils.test.IntegrationTestDataInitializer; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.PageRequest; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; @@ -182,7 +184,11 @@ public void testSaveStudyInstances_WithValidVariableHavingInvalidVariableValue() .saveStudyInstances(this.cropType.getCropName(), Collections.singletonList(dto), this.testUser.getUserid()).get(0); Assert.assertEquals(dto.getTrialDbId(), savedInstance.getTrialDbId()); Assert.assertEquals(dto.getLocationDbId(), savedInstance.getLocationDbId()); - Assert.assertTrue(CollectionUtils.isEmpty(savedInstance.getEnvironmentParameters())); + + assertThat(savedInstance.getEnvironmentParameters(), hasSize(1)); + final EnvironmentParameter environmentParameter = savedInstance.getEnvironmentParameters().get(0); + assertThat(environmentParameter.getParameterPUI(), is(numericVariable.getCvTermId().toString())); + assertThat(environmentParameter.getValue(), is("")); } @Test