Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ibp 3981 variable type get by name deprecation #909

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -483,9 +484,11 @@ public Map<String, Object> generateMeasurements(@RequestBody final GenerateDesig
final Dataset dataset = (Dataset) SettingsUtil.convertPojoToXmlDataSet(this.fieldbookMiddlewareService, this.userSelection.getStudyName(), this.userSelection,
null, this.contextUtil.getCurrentProgramUUID());

final List<Term> 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);
Expand Down Expand Up @@ -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<Term> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -295,11 +295,13 @@ public ResponseEntity<String> submit(@RequestBody final TrialData data) {
final Dataset dataset = (Dataset) SettingsUtil.convertPojoToXmlDataSet(this.fieldbookMiddlewareService, name, this.userSelection,
data.getTreatmentFactors().getCurrentData(), this.contextUtil.getCurrentProgramUUID());

final List<Term> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -368,10 +369,12 @@ public Map<String, Object> submit(@RequestParam("replace") final int replace, @R

SettingsUtil.setConstantLabels(dataset, this.userSelection.getConstantsWithLabels());

final List<Term> 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());
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/com/efficio/fieldbook/web/util/SettingsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Term> variableTypes) {
return SettingsUtil.convertXmlDatasetToWorkbook(dataset, null, null, null, null, programUUID, variableTypes);
}

/**
Expand All @@ -530,14 +531,14 @@ public static Workbook convertXmlDatasetToWorkbook(final ParentDataset dataset,
public static Workbook convertXmlDatasetToWorkbook(
final ParentDataset dataset, final ExpDesignParameterUi param,
final List<Integer> variables, final org.generationcp.middleware.service.api.FieldbookService fieldbookMiddlewareService,
final List<MeasurementVariable> allExpDesignVariables, final String programUUID) {
final List<MeasurementVariable> allExpDesignVariables, final String programUUID, final List<Term> 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()));

Expand Down Expand Up @@ -816,11 +817,11 @@ private static TreatmentVariable convertTreatmentFactorToTreatmentVariable(final
* @param variates the variates
* @return the list
*/
private static List<MeasurementVariable> convertVariatesToMeasurementVariables(final List<Variate> variates) {
private static List<MeasurementVariable> convertVariatesToMeasurementVariables(final List<Variate> variates, final List<Term> variableTypes) {
final List<MeasurementVariable> 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;
Expand All @@ -832,13 +833,17 @@ private static List<MeasurementVariable> 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<Term> 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<String, Integer> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -113,6 +137,9 @@ public class OpenTrialControllerTest {
@Mock
protected FieldbookService fieldbookMiddlewareService;

@Mock
protected OntologyService ontologyService;

@Mock
protected com.efficio.fieldbook.service.api.FieldbookService fieldbookService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -1089,4 +1090,13 @@ public void testConvertConstantToMeasurementVariable() {

}

private List<Term> getVariableTypes() {
final List<Term> 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;
}

}