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-3590-FieldMapModifications #896

Open
wants to merge 4 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 @@ -155,7 +155,7 @@ public List<FieldMapDatasetInfo> getFieldMapLabels(final int projectId) {
}

@SuppressWarnings("unchecked")
public List<FieldMapInfo> getAllFieldMapsInBlockByTrialInstanceId(final int datasetId, final int instanceId, final Integer blockId)
public List<FieldMapInfo> getAllFieldMapsInBlockByTrialInstanceId(final int datasetId, final int instanceId, final Integer locationId)
{
List<FieldMapInfo> fieldmaps = new ArrayList<>();

Expand Down Expand Up @@ -198,7 +198,7 @@ public List<FieldMapInfo> getAllFieldMapsInBlockByTrialInstanceId(final int data
.append(" AND gpSeason.type_id = ").append(TermId.SEASON_VAR.getId()).append(" ") // -- 8371 (2452)
.append(" WHERE blk.type_id = ").append(TermId.BLOCK_ID.getId());

if (blockId != null) {
if (locationId != null) {
sql.append(" AND blk.value = :blockId ");
} else {
sql.append(" AND blk.value IN (SELECT DISTINCT bval.value FROM nd_geolocationprop bval ")
Expand All @@ -217,8 +217,8 @@ public List<FieldMapInfo> getAllFieldMapsInBlockByTrialInstanceId(final int data
.addScalar("col").addScalar("blockId").addScalar("studyId").addScalar("trialInstance").addScalar("gid")
.addScalar("startDate").addScalar("season").addScalar("blockNo").addScalar("obsUnitId", Hibernate.STRING);

if (blockId != null) {
query.setParameter("blockId", blockId);
if (locationId != null) {
query.setParameter("blockId", locationId);
} else {
query.setParameter("datasetId", datasetId);
query.setParameter("instanceId", instanceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void saveFieldmapProperties(final List<FieldMapInfo> infos) throws Middle

if (trial.getLocationId() != null) {
this.saveOrUpdate(locationId, TermId.LOCATION_ID.getId(), trial.getLocationId().toString());
this.saveOrUpdate(locationId, TermId.TRIAL_LOCATION.getId(), trial.getLocationName());
}

if (trial.getBlockId() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,8 @@ public int addLocation(final String locationName, final Integer parentId, final
}

@Override
public List<FieldMapInfo> getAllFieldMapsInBlockByBlockId(final int blockId) {
return this.studyDataManager.getAllFieldMapsInBlockByBlockId(blockId);
public List<FieldMapInfo> getAllFieldMapsInBlockByLocationId(final int locationId) {
return this.studyDataManager.getAllFieldMapsInBlockByBlockId(locationId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,11 @@ List<StandardVariableReference> filterStandardVariablesByMode(List<Integer> stor
/**
* Get all field maps in the same block.
*
* @param blockId
* @param locationId
* the block id
* @return the field maps in the given block
*/
List<FieldMapInfo> getAllFieldMapsInBlockByBlockId(int blockId);
List<FieldMapInfo> getAllFieldMapsInBlockByLocationId(int locationId);

/**
* Fetch all the possible pairs of the treatment level variable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ public void testGetFieldMapLabels() {
}

@Test
public void testGetAllFieldMapsInBlockByTrialInstanceId() {
public void testGetAllFieldMapsTrialInstanceId() {

final Geolocation geolocation = this.testDataInitializer.createTestGeolocation("1", 101);
this.testDataInitializer.addGeolocationProp(geolocation, TermId.SEASON_VAR.getId(), "10101", 1);
this.testDataInitializer.addGeolocationProp(geolocation, TermId.TRIAL_LOCATION.getId(), "India", 2);
this.testDataInitializer.addGeolocationProp(geolocation, TermId.BLOCK_ID.getId(), "1234", 3);
this.testDataInitializer.addGeolocationProp(geolocation, TermId.BLOCK_ID.getId(), "0", 3);

final ExperimentModel experimentModel =
this.testDataInitializer.createTestExperiment(this.plot, geolocation, TermId.PLOT_EXPERIMENT.getId(), "1", null);
Expand All @@ -129,14 +129,14 @@ public void testGetAllFieldMapsInBlockByTrialInstanceId() {
this.sessionProvder.getSession().flush();

final List<FieldMapInfo> fieldMapInfos1 = this.experimentPropertyDao
.getAllFieldMapsInBlockByTrialInstanceId(this.study.getProjectId(), geolocation.getLocationId(), 1234);
.getAllFieldMapsInBlockByTrialInstanceId(this.plot.getProjectId(), geolocation.getLocationId(), 0);

assertEquals(1, fieldMapInfos1.size());
assertEquals(1, fieldMapInfos1.get(0).getDatasets().size());

final List<FieldMapInfo> fieldMapInfos2 = this.experimentPropertyDao
.getAllFieldMapsInBlockByTrialInstanceId(this.study.getProjectId(), geolocation.getLocationId(), 9999);
assertEquals(0, fieldMapInfos2.size());
.getAllFieldMapsInBlockByTrialInstanceId(this.study.getProjectId(), geolocation.getLocationId(), 0);
assertEquals(1, fieldMapInfos2.size());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import org.mockito.MockitoAnnotations;

public class ExperimentPropertyDaoTest {

@Mock
private Session mockSession;

@Mock
private SQLQuery mockQuery;

Expand All @@ -38,7 +38,7 @@ public class ExperimentPropertyDaoTest {
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);

this.dao = new ExperimentPropertyDao();
this.dao.setSession(this.mockSession);
Mockito.when(this.mockSession.createSQLQuery(Matchers.anyString())).thenReturn(this.mockQuery);
Expand All @@ -50,35 +50,20 @@ public void setUp() throws Exception {
public void testGetFieldMapLabels() {
final int projectId = 112;
this.dao.getFieldMapLabels(projectId);

final ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class);
Mockito.verify(this.mockSession).createSQLQuery(sqlCaptor.capture());
Assert.assertEquals(this.getFieldmapLabelsQuery(), sqlCaptor.getValue());
Mockito.verify(this.mockQuery).setParameter("projectId", projectId);
}

@Test
public void testGetAllFieldMapsInBlockByTrialInstanceId_WithBlockId() {
final int datasetId = 11;
final int instanceId = 22;
final int blockId = 33;
this.dao.getAllFieldMapsInBlockByTrialInstanceId(datasetId, instanceId, blockId);

final String expectedSql = this.getFieldmapsInBlockMainQuery() + " AND blk.value = :blockId ORDER BY e.nd_experiment_id ASC";
final ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class);
Mockito.verify(this.mockSession).createSQLQuery(sqlCaptor.capture());
Assert.assertEquals(expectedSql.replace(" ", ""), sqlCaptor.getValue().replace(" ", ""));
Mockito.verify(this.mockQuery).setParameter("blockId", blockId);
Mockito.verify(this.mockQuery, Mockito.never()).setParameter("datasetId", datasetId);
Mockito.verify(this.mockQuery, Mockito.never()).setParameter("instanceId", instanceId);
}



@Test
public void testGetAllFieldMapsInBlockByTrialInstanceId_WithNullBlockId() {
final int datasetId = 11;
final int geolocationId = 22;
this.dao.getAllFieldMapsInBlockByTrialInstanceId(datasetId, geolocationId, null);

final String expectedSql = this.getFieldmapsInBlockMainQuery() +
" AND blk.value IN (SELECT DISTINCT bval.value FROM nd_geolocationprop bval " +
" INNER JOIN nd_experiment bexp ON bexp.nd_geolocation_id = bval.nd_geolocation_id " +
Expand All @@ -92,7 +77,7 @@ public void testGetAllFieldMapsInBlockByTrialInstanceId_WithNullBlockId() {
Mockito.verify(this.mockQuery).setParameter("datasetId", datasetId);
Mockito.verify(this.mockQuery).setParameter("instanceId", geolocationId);
}

private String getFieldmapsInBlockMainQuery() {
return " SELECT p.project_id AS datasetId , p.name AS datasetName "
+ " , st.name AS studyName , e.nd_geolocation_id AS instanceId "
Expand Down Expand Up @@ -130,7 +115,7 @@ private String getFieldmapsInBlockMainQuery() {
+ " AND gpSeason.type_id = "+ TermId.SEASON_VAR.getId() + " "
+ " WHERE blk.type_id = "+ TermId.BLOCK_ID.getId();
}

private String getFieldmapLabelsQuery() {
return " SELECT " +
" nde.project_id AS datasetId " +
Expand Down