diff --git a/src/main/java/com/efficio/fieldbook/web/importdesign/controller/DesignImportController.java b/src/main/java/com/efficio/fieldbook/web/importdesign/controller/DesignImportController.java index 7bbf77128a..c6b225a668 100644 --- a/src/main/java/com/efficio/fieldbook/web/importdesign/controller/DesignImportController.java +++ b/src/main/java/com/efficio/fieldbook/web/importdesign/controller/DesignImportController.java @@ -11,15 +11,15 @@ import com.efficio.fieldbook.web.common.form.ImportDesignForm; import com.efficio.fieldbook.web.importdesign.service.DesignImportService; import com.efficio.fieldbook.web.importdesign.validator.DesignImportValidator; -import com.efficio.fieldbook.web.trial.controller.SettingsController; +import com.efficio.fieldbook.web.trial.bean.ExpDesignParameterUi; import com.efficio.fieldbook.web.trial.bean.Instance; import com.efficio.fieldbook.web.trial.bean.InstanceInfo; -import com.efficio.fieldbook.web.trial.bean.ExpDesignParameterUi; -import org.generationcp.commons.constant.AppConstants; +import com.efficio.fieldbook.web.trial.controller.SettingsController; import com.efficio.fieldbook.web.util.ExpDesignUtil; import com.efficio.fieldbook.web.util.SettingsUtil; import com.efficio.fieldbook.web.util.WorkbookUtil; import com.efficio.fieldbook.web.util.parsing.DesignImportParser; +import org.generationcp.commons.constant.AppConstants; import org.generationcp.commons.parsing.FileParsingException; import org.generationcp.middleware.domain.dms.ExperimentDesignType; import org.generationcp.middleware.domain.dms.PhenotypicType; @@ -31,6 +31,7 @@ import org.generationcp.middleware.domain.etl.MeasurementVariable; import org.generationcp.middleware.domain.etl.StudyDetails; import org.generationcp.middleware.domain.etl.Workbook; +import org.generationcp.middleware.domain.oms.CvId; import org.generationcp.middleware.domain.oms.Term; import org.generationcp.middleware.domain.oms.TermId; import org.generationcp.middleware.domain.ontology.VariableType; @@ -483,9 +484,11 @@ public Map generateMeasurements(@RequestBody final GenerateDesig final Dataset dataset = (Dataset) SettingsUtil.convertPojoToXmlDataSet(this.fieldbookMiddlewareService, this.userSelection.getStudyName(), this.userSelection, null, this.contextUtil.getCurrentProgramUUID()); + final List variableTypes = this.ontologyDataManager.getAllTermsByCvId(CvId.VARIABLE_TYPE); + final Workbook workbook = SettingsUtil.convertXmlDatasetToWorkbook(dataset, this.userSelection.getExpDesignParams(), this.userSelection.getExpDesignVariables(), this.fieldbookMiddlewareService, this.userSelection.getExperimentalDesignVariables(), - this.contextUtil.getCurrentProgramUUID()); + this.contextUtil.getCurrentProgramUUID(), variableTypes); workbook.setStudyDetails(this.userSelection.getWorkbook().getStudyDetails()); this.userSelection.setMeasurementRowList(null); @@ -758,7 +761,9 @@ public void initializeTemporaryWorkbook() { null, null, this.userSelection.getStudyConditions(), this.contextUtil.getCurrentProgramUUID(), description, startDate, endDate, studyUpdate); - workbook = SettingsUtil.convertXmlDatasetToWorkbook(dataset, this.contextUtil.getCurrentProgramUUID()); + final List variableTypes = this.ontologyDataManager.getAllTermsByCvId(CvId.VARIABLE_TYPE); + + workbook = SettingsUtil.convertXmlDatasetToWorkbook(dataset, this.contextUtil.getCurrentProgramUUID(), variableTypes); //TODO evaluate if we can get this info from userSelection if needed. By now it is not used anywhere // details.setStudyType(studyDataManager.getStudyTypeByName(studyType)); diff --git a/src/main/java/com/efficio/fieldbook/web/trial/controller/CreateTrialController.java b/src/main/java/com/efficio/fieldbook/web/trial/controller/CreateTrialController.java index d2fbe3200d..31e2288a87 100644 --- a/src/main/java/com/efficio/fieldbook/web/trial/controller/CreateTrialController.java +++ b/src/main/java/com/efficio/fieldbook/web/trial/controller/CreateTrialController.java @@ -11,20 +11,27 @@ package com.efficio.fieldbook.web.trial.controller; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.annotation.Resource; -import javax.servlet.http.HttpSession; - +import com.efficio.fieldbook.service.api.ErrorHandlerService; +import com.efficio.fieldbook.web.common.bean.SettingDetail; +import com.efficio.fieldbook.web.trial.bean.BasicDetails; import com.efficio.fieldbook.web.trial.bean.Instance; +import com.efficio.fieldbook.web.trial.bean.InstanceInfo; +import com.efficio.fieldbook.web.trial.bean.TabInfo; +import com.efficio.fieldbook.web.trial.bean.TrialData; +import com.efficio.fieldbook.web.trial.bean.TrialSettingsBean; +import com.efficio.fieldbook.web.trial.form.CreateTrialForm; +import com.efficio.fieldbook.web.trial.form.ImportGermplasmListForm; +import com.efficio.fieldbook.web.util.SessionUtility; +import com.efficio.fieldbook.web.util.SettingsUtil; +import com.efficio.fieldbook.web.util.WorkbookUtil; +import org.generationcp.commons.constant.AppConstants; import org.generationcp.commons.context.ContextInfo; import org.generationcp.middleware.domain.etl.MeasurementRow; import org.generationcp.middleware.domain.etl.MeasurementVariable; import org.generationcp.middleware.domain.etl.TreatmentVariable; import org.generationcp.middleware.domain.etl.Workbook; +import org.generationcp.middleware.domain.oms.CvId; +import org.generationcp.middleware.domain.oms.Term; import org.generationcp.middleware.domain.oms.TermId; import org.generationcp.middleware.domain.ontology.VariableType; import org.generationcp.middleware.exceptions.MiddlewareException; @@ -45,19 +52,12 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; -import com.efficio.fieldbook.service.api.ErrorHandlerService; -import com.efficio.fieldbook.web.common.bean.SettingDetail; -import com.efficio.fieldbook.web.trial.bean.BasicDetails; -import com.efficio.fieldbook.web.trial.bean.InstanceInfo; -import com.efficio.fieldbook.web.trial.bean.TabInfo; -import com.efficio.fieldbook.web.trial.bean.TrialData; -import com.efficio.fieldbook.web.trial.bean.TrialSettingsBean; -import com.efficio.fieldbook.web.trial.form.CreateTrialForm; -import com.efficio.fieldbook.web.trial.form.ImportGermplasmListForm; -import org.generationcp.commons.constant.AppConstants; -import com.efficio.fieldbook.web.util.SessionUtility; -import com.efficio.fieldbook.web.util.SettingsUtil; -import com.efficio.fieldbook.web.util.WorkbookUtil; +import javax.annotation.Resource; +import javax.servlet.http.HttpSession; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * The Class CreateTrialController. @@ -295,11 +295,13 @@ public ResponseEntity submit(@RequestBody final TrialData data) { final Dataset dataset = (Dataset) SettingsUtil.convertPojoToXmlDataSet(this.fieldbookMiddlewareService, name, this.userSelection, data.getTreatmentFactors().getCurrentData(), this.contextUtil.getCurrentProgramUUID()); + final List variableTypes = this.ontologyService.getAllTermsByCvId(CvId.VARIABLE_TYPE); + SettingsUtil.setConstantLabels(dataset, this.userSelection.getConstantsWithLabels()); final Workbook workbook = SettingsUtil .convertXmlDatasetToWorkbook(dataset, this.userSelection.getExpDesignParams(), this.userSelection.getExpDesignVariables(), this.fieldbookMiddlewareService, this.userSelection.getExperimentalDesignVariables(), - this.contextUtil.getCurrentProgramUUID()); + this.contextUtil.getCurrentProgramUUID(), variableTypes); if (this.userSelection.getTemporaryWorkbook() != null) { this.addMeasurementVariablesToTrialObservationIfNecessary(data.getInstanceInfo().getInstances(), workbook, diff --git a/src/main/java/com/efficio/fieldbook/web/trial/controller/OpenTrialController.java b/src/main/java/com/efficio/fieldbook/web/trial/controller/OpenTrialController.java index 3cd97abf8c..6863e6f62f 100644 --- a/src/main/java/com/efficio/fieldbook/web/trial/controller/OpenTrialController.java +++ b/src/main/java/com/efficio/fieldbook/web/trial/controller/OpenTrialController.java @@ -22,6 +22,7 @@ import org.generationcp.middleware.domain.etl.MeasurementRow; import org.generationcp.middleware.domain.etl.MeasurementVariable; import org.generationcp.middleware.domain.etl.Workbook; +import org.generationcp.middleware.domain.oms.CvId; import org.generationcp.middleware.domain.oms.Term; import org.generationcp.middleware.domain.ontology.VariableType; import org.generationcp.middleware.domain.samplelist.SampleListDTO; @@ -368,10 +369,12 @@ public Map submit(@RequestParam("replace") final int replace, @R SettingsUtil.setConstantLabels(dataset, this.userSelection.getConstantsWithLabels()); + final List variableTypes = this.ontologyService.getAllTermsByCvId(CvId.VARIABLE_TYPE); + final Workbook workbook = SettingsUtil .convertXmlDatasetToWorkbook(dataset, this.userSelection.getExpDesignParams(), this.userSelection.getExpDesignVariables(), this.fieldbookMiddlewareService, this.userSelection.getExperimentalDesignVariables(), - this.contextUtil.getCurrentProgramUUID()); + this.contextUtil.getCurrentProgramUUID(), variableTypes); workbook.setOriginalObservations(this.userSelection.getWorkbook().getOriginalObservations()); workbook.setTrialObservations(this.userSelection.getWorkbook().getTrialObservations()); diff --git a/src/main/java/com/efficio/fieldbook/web/util/SettingsUtil.java b/src/main/java/com/efficio/fieldbook/web/util/SettingsUtil.java index 8a5179539d..e59c6c4376 100644 --- a/src/main/java/com/efficio/fieldbook/web/util/SettingsUtil.java +++ b/src/main/java/com/efficio/fieldbook/web/util/SettingsUtil.java @@ -37,6 +37,7 @@ import org.generationcp.middleware.domain.etl.MeasurementVariable; import org.generationcp.middleware.domain.etl.TreatmentVariable; import org.generationcp.middleware.domain.etl.Workbook; +import org.generationcp.middleware.domain.oms.Term; import org.generationcp.middleware.domain.oms.TermId; import org.generationcp.middleware.domain.ontology.VariableType; import org.generationcp.middleware.exceptions.MiddlewareQueryException; @@ -517,8 +518,8 @@ public static boolean inHideVariableFields(final Integer stdVarId, final String return inList; } - public static Workbook convertXmlDatasetToWorkbook(final ParentDataset dataset, final String programUUID) { - return SettingsUtil.convertXmlDatasetToWorkbook(dataset, null, null, null, null, programUUID); + public static Workbook convertXmlDatasetToWorkbook(final ParentDataset dataset, final String programUUID, final List variableTypes) { + return SettingsUtil.convertXmlDatasetToWorkbook(dataset, null, null, null, null, programUUID, variableTypes); } /** @@ -530,14 +531,14 @@ public static Workbook convertXmlDatasetToWorkbook(final ParentDataset dataset, public static Workbook convertXmlDatasetToWorkbook( final ParentDataset dataset, final ExpDesignParameterUi param, final List variables, final org.generationcp.middleware.service.api.FieldbookService fieldbookMiddlewareService, - final List allExpDesignVariables, final String programUUID) { + final List allExpDesignVariables, final String programUUID, final List variableTypes) { final Workbook workbook = new Workbook(); final Dataset studyDataSet = (Dataset) dataset; workbook.setConditions(SettingsUtil.convertConditionsToMeasurementVariables(studyDataSet.getConditions())); workbook.setFactors(SettingsUtil.convertFactorsToMeasurementVariables(studyDataSet.getFactors())); - workbook.setVariates(SettingsUtil.convertVariatesToMeasurementVariables(studyDataSet.getVariates())); + workbook.setVariates(SettingsUtil.convertVariatesToMeasurementVariables(studyDataSet.getVariates(), variableTypes)); workbook.getConditions().addAll(SettingsUtil.convertFactorsToMeasurementVariables(studyDataSet.getTrialLevelFactor())); workbook.setConstants(SettingsUtil.convertConstantsToMeasurementVariables(studyDataSet.getConstants())); @@ -816,11 +817,11 @@ private static TreatmentVariable convertTreatmentFactorToTreatmentVariable(final * @param variates the variates * @return the list */ - private static List convertVariatesToMeasurementVariables(final List variates) { + private static List convertVariatesToMeasurementVariables(final List variates, final List variableTypes) { final List list = new ArrayList<>(); if (variates != null && !variates.isEmpty()) { for (final Variate variate : variates) { - list.add(SettingsUtil.convertVariateToMeasurementVariable(variate)); + list.add(SettingsUtil.convertVariateToMeasurementVariable(variate, variableTypes)); } } return list; @@ -832,13 +833,17 @@ private static List convertVariatesToMeasurementVariables(f * @param variate the variate * @return the measurement variable */ - private static MeasurementVariable convertVariateToMeasurementVariable(final Variate variate) { + private static MeasurementVariable convertVariateToMeasurementVariable(final Variate variate, final List variableTypes) { // because variates are mostly PLOT variables final MeasurementVariable mvar = new MeasurementVariable(variate.getName(), variate.getDescription(), variate.getScale(), variate.getMethod(), variate.getProperty(), variate.getDatatype(), null, PhenotypicType.TRIAL_DESIGN.getLabelList().get(0), variate.getMinRange(), variate.getMaxRange(), PhenotypicType.getPhenotypicTypeByName(variate.getRole())); if (variate.getVariableType() != null) { - mvar.setVariableType(VariableType.getByName(variate.getVariableType())); + final Map variableTypesByName = new HashMap<>(); + for (final Term term: variableTypes) { + variableTypesByName.put(term.getName(), term.getId()); + } + mvar.setVariableType(VariableType.getById(variableTypesByName.get(variate.getVariableType()))); } mvar.setOperation(variate.getOperation()); mvar.setTermId(variate.getId()); diff --git a/src/test/java/com/efficio/fieldbook/web/trial/controller/OpenTrialControllerTest.java b/src/test/java/com/efficio/fieldbook/web/trial/controller/OpenTrialControllerTest.java index a4066a8cfc..0c71fdd7b5 100644 --- a/src/test/java/com/efficio/fieldbook/web/trial/controller/OpenTrialControllerTest.java +++ b/src/test/java/com/efficio/fieldbook/web/trial/controller/OpenTrialControllerTest.java @@ -9,7 +9,12 @@ import com.efficio.fieldbook.web.common.bean.UserSelection; import com.efficio.fieldbook.web.data.initializer.DesignImportTestDataInitializer; import com.efficio.fieldbook.web.trial.TestDataHelper; -import com.efficio.fieldbook.web.trial.bean.*; +import com.efficio.fieldbook.web.trial.bean.BasicDetails; +import com.efficio.fieldbook.web.trial.bean.ExpDesignParameterUi; +import com.efficio.fieldbook.web.trial.bean.TabInfo; +import com.efficio.fieldbook.web.trial.bean.TreatmentFactorTabBean; +import com.efficio.fieldbook.web.trial.bean.TrialData; +import com.efficio.fieldbook.web.trial.bean.TrialSettingsBean; import com.efficio.fieldbook.web.trial.form.CreateTrialForm; import com.efficio.fieldbook.web.util.SessionUtility; import com.efficio.fieldbook.web.util.SettingsUtil; @@ -24,8 +29,17 @@ import org.generationcp.middleware.data.initializer.MeasurementVariableTestDataInitializer; import org.generationcp.middleware.data.initializer.StandardVariableTestDataInitializer; import org.generationcp.middleware.data.initializer.WorkbookTestDataInitializer; -import org.generationcp.middleware.domain.dms.*; -import org.generationcp.middleware.domain.etl.*; +import org.generationcp.middleware.domain.dms.DMSVariableType; +import org.generationcp.middleware.domain.dms.ExperimentDesignType; +import org.generationcp.middleware.domain.dms.PhenotypicType; +import org.generationcp.middleware.domain.dms.StandardVariable; +import org.generationcp.middleware.domain.dms.Study; +import org.generationcp.middleware.domain.dms.VariableTypeList; +import org.generationcp.middleware.domain.etl.MeasurementData; +import org.generationcp.middleware.domain.etl.MeasurementRow; +import org.generationcp.middleware.domain.etl.MeasurementVariable; +import org.generationcp.middleware.domain.etl.StudyDetails; +import org.generationcp.middleware.domain.etl.Workbook; import org.generationcp.middleware.domain.gms.SystemDefinedEntryType; import org.generationcp.middleware.domain.oms.Term; import org.generationcp.middleware.domain.oms.TermId; @@ -46,6 +60,7 @@ import org.generationcp.middleware.pojos.dms.StudyType; import org.generationcp.middleware.pojos.workbench.Project; import org.generationcp.middleware.service.api.FieldbookService; +import org.generationcp.middleware.service.api.OntologyService; import org.generationcp.middleware.service.api.SampleListService; import org.generationcp.middleware.service.api.dataset.DatasetService; import org.generationcp.middleware.service.api.dataset.DatasetTypeService; @@ -57,7 +72,11 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.*; +import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatchers; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.springframework.mock.web.MockHttpSession; import org.springframework.ui.ExtendedModelMap; @@ -67,7 +86,12 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.text.ParseException; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; @RunWith(MockitoJUnitRunner.class) public class OpenTrialControllerTest { @@ -113,6 +137,9 @@ public class OpenTrialControllerTest { @Mock protected FieldbookService fieldbookMiddlewareService; + @Mock + protected OntologyService ontologyService; + @Mock protected com.efficio.fieldbook.service.api.FieldbookService fieldbookService; diff --git a/src/test/java/com/efficio/fieldbook/web/util/SettingsUtilTest.java b/src/test/java/com/efficio/fieldbook/web/util/SettingsUtilTest.java index b2f510c100..98fe77cfb7 100644 --- a/src/test/java/com/efficio/fieldbook/web/util/SettingsUtilTest.java +++ b/src/test/java/com/efficio/fieldbook/web/util/SettingsUtilTest.java @@ -53,6 +53,7 @@ public class SettingsUtilTest { public static final String NFERT_KG = "NFERT_KG"; public static final int NFERT_NO_ID = 1001; public static final int NFERT_KG_ID = 1002; + @Mock private org.generationcp.middleware.service.api.FieldbookService fieldbookMiddlewareService; @@ -98,7 +99,7 @@ public void testConvertXmlDatasetToWorkbookAndBack() { variate.setVariableType(VariableType.TRAIT.getName()); dataset.getVariates().add(variate); - final Workbook workbook = SettingsUtil.convertXmlDatasetToWorkbook(dataset, SettingsUtilTest.PROGRAM_UUID); + final Workbook workbook = SettingsUtil.convertXmlDatasetToWorkbook(dataset, SettingsUtilTest.PROGRAM_UUID, this.getVariableTypes()); final Dataset newDataset = (Dataset) SettingsUtil.convertWorkbookToXmlDataset(workbook); Assert.assertEquals(dataset.getConditions().get(0).getName(), newDataset.getConditions().get(0).getName()); @@ -1089,4 +1090,13 @@ public void testConvertConstantToMeasurementVariable() { } + private List getVariableTypes() { + final List terms = new ArrayList<>(); + for (final VariableType variableType: VariableType.values()) { + final Term term = new Term(variableType.getId(), variableType.getName(), variableType.getDescription()); + terms.add(term); + } + return terms; + } + }