Skip to content

Commit

Permalink
[#3166]Add filters to rest service. Remove dpa loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
stellanl committed Sep 17, 2019
1 parent 4f72f50 commit e323012
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
54 changes: 27 additions & 27 deletions GAE/src/org/akvo/flow/dao/DataPointAssignmentDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,38 +37,38 @@ public DataPointAssignmentDao() {
super(DataPointAssignment.class);
}

public List<DataPointAssignment> listBySurvey(Long surveyId) {
return listByProperty("surveyId", surveyId, "Long");
}

public List<DataPointAssignment> listByDevice(Long deviceId) {
return listByProperty("deviceId", deviceId, "Long");
}

/**
* Return a set of data point ids for a specified device and survey (in any assignment)
* Return a set of data point assignments for a specified Device and surveyAssignment
*
* @return list of assignments
*/
public Set<Long> listDataPointIds(Long deviceId, Long surveyId) {
SurveyAssignmentDao saDao = new SurveyAssignmentDao();
Set<Long> result = new HashSet<>();
List<SurveyAssignment> assignments = saDao.listBySurveyGroup(surveyId);
for (SurveyAssignment sa: assignments) {
PersistenceManager pm = PersistenceFilter.getManager();
javax.jdo.Query query = pm.newQuery(User.class);
StringBuilder filterString = new StringBuilder();
StringBuilder paramString = new StringBuilder();
Map<String, Object> paramMap = null;
paramMap = new HashMap<String, Object>();
///TODO: add this index
appendNonNullParam("deviceId", filterString, paramString, "Long", deviceId, paramMap);
appendNonNullParam("surveyAssignmentId", filterString, paramString, "Long", sa.getKey().getId(), paramMap);
public List<DataPointAssignment> listByDeviceAndSurvey(Long deviceId, Long surveyAssignmentId) {
PersistenceManager pm = PersistenceFilter.getManager();
javax.jdo.Query query = pm.newQuery(DataPointAssignment.class);
StringBuilder filterString = new StringBuilder();
StringBuilder paramString = new StringBuilder();
Map<String, Object> paramMap = null;
paramMap = new HashMap<String, Object>();
appendNonNullParam("deviceId", filterString, paramString, "Long", deviceId, paramMap);
appendNonNullParam("surveyAssignmentId", filterString, paramString, "Long", surveyAssignmentId, paramMap);

if (filterString.length() > 0) {
query.setFilter(filterString.toString());
query.declareParameters(paramString.toString());
}
//TODO mandatory? Or do we want pagination?: prepareCursor(cursorString, query);
@SuppressWarnings("unchecked")
List<DataPointAssignment> selected = (List<DataPointAssignment>) query.executeWithMap(paramMap);

for (DataPointAssignment dpa:selected) {
result.addAll(dpa.getDataPointIds());
}
if (filterString.length() > 0) {
query.setFilter(filterString.toString());
query.declareParameters(paramString.toString());
}
return result;
@SuppressWarnings("unchecked")
List<DataPointAssignment> selected = (List<DataPointAssignment>) query.executeWithMap(paramMap);

return selected;
}


}
27 changes: 21 additions & 6 deletions GAE/src/org/akvo/flow/rest/DataPointAssignmentRestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.waterforpeople.mapping.app.web.dto.ApprovalStepDTO;
import org.waterforpeople.mapping.app.web.rest.ResourceNotFoundException;
import org.waterforpeople.mapping.app.web.rest.dto.RestStatusDto;

import com.gallatinsystems.common.Constants;
import com.gallatinsystems.survey.domain.ApprovalStep;
import com.google.appengine.api.datastore.KeyFactory;


Expand All @@ -48,14 +51,26 @@ public class DataPointAssignmentRestService {

@RequestMapping(method = RequestMethod.GET, value = "")
@ResponseBody
public Map<String, List<DataPointAssignmentDto>> listAll() {
public Map<String, List<DataPointAssignmentDto>> listSomeOrAll(
@RequestParam(value = "surveyId", required = false) Long surveyId,
@RequestParam(value = "deviceId", required = false) Long deviceId
) {
final HashMap<String, List<DataPointAssignmentDto>> response = new HashMap<String, List<DataPointAssignmentDto>>();
final List<DataPointAssignmentDto> results = new ArrayList<DataPointAssignmentDto>();

for (DataPointAssignment dpa : dataPointAssignmentDao.list(Constants.ALL_RESULTS)) {
results.add(marshallToDto(dpa));
if (surveyId != null) {
for (DataPointAssignment dpa : dataPointAssignmentDao.listBySurvey(surveyId)) {
results.add(marshallToDto(dpa));
}
} else if (deviceId != null) {
for (DataPointAssignment dpa : dataPointAssignmentDao.listByDevice(deviceId)) {
results.add(marshallToDto(dpa));
}
} else { //All of them
for (DataPointAssignment dpa : dataPointAssignmentDao.list(Constants.ALL_RESULTS)) {
results.add(marshallToDto(dpa));
}
}

response.put("data_point_assignments", results);
return response;
}
Expand Down Expand Up @@ -96,7 +111,7 @@ public Map<String, RestStatusDto> deleteById(@PathVariable("id")

@RequestMapping(method = RequestMethod.PUT, value = "/{id}")
@ResponseBody
public Map<String, DataPointAssignmentDto> updateSurveyAssignment(
public Map<String, DataPointAssignmentDto> updateDataPointAssignment(
@PathVariable("id")
Long id,
@RequestBody
Expand Down Expand Up @@ -124,7 +139,7 @@ public Map<String, DataPointAssignmentDto> updateSurveyAssignment(

@RequestMapping(method = RequestMethod.POST, value = "")
@ResponseBody
public Map<String, DataPointAssignmentDto> newSurveyAssignment(
public Map<String, DataPointAssignmentDto> newDataPointAssignment(
@RequestBody
DataPointAssignmentPayload payload) {

Expand Down

0 comments on commit e323012

Please # to comment.