Search in sources :

Example 6 with QueryParams

use of org.motechproject.mds.query.QueryParams in project motech by motech.

the class InstanceController method getTrash.

@RequestMapping(value = "/entities/{entityId}/trash", method = RequestMethod.GET)
@ResponseBody
public Records<BasicEntityRecord> getTrash(@PathVariable Long entityId, GridSettings settings) {
    QueryParams queryParams = QueryParamsBuilder.buildQueryParams(settings);
    List<BasicEntityRecord> trashRecordsList = instanceService.getTrashRecords(entityId, queryParams);
    long recordCount = instanceService.countTrashRecords(entityId);
    int rowCount = (int) Math.ceil(recordCount / (double) queryParams.getPageSize());
    Records<BasicEntityRecord> records = new Records<>(queryParams.getPage(), rowCount, (int) recordCount, trashRecordsList);
    processFieldsForUI(records);
    return records;
}
Also used : QueryParams(org.motechproject.mds.query.QueryParams) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) Records(org.motechproject.mds.web.domain.Records) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 7 with QueryParams

use of org.motechproject.mds.query.QueryParams in project motech by motech.

the class InstanceController method getHistory.

@RequestMapping(value = "/instances/{entityId}/{instanceId}/history", method = RequestMethod.GET)
@ResponseBody
public Records<BasicHistoryRecord> getHistory(@PathVariable Long entityId, @PathVariable Long instanceId, GridSettings settings) {
    QueryParams queryParams = QueryParamsBuilder.buildQueryParams(settings);
    List<BasicHistoryRecord> historyRecordsList = instanceService.getInstanceHistory(entityId, instanceId, queryParams);
    long recordCount = instanceService.countHistoryRecords(entityId, instanceId);
    int rowCount = (int) Math.ceil(recordCount / (double) queryParams.getPageSize());
    Records<BasicHistoryRecord> records = new Records<>(queryParams.getPage(), rowCount, (int) recordCount, historyRecordsList);
    processFieldsForUIinHistoryRecords(records);
    return records;
}
Also used : QueryParams(org.motechproject.mds.query.QueryParams) BasicHistoryRecord(org.motechproject.mds.web.domain.BasicHistoryRecord) Records(org.motechproject.mds.web.domain.Records) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 8 with QueryParams

use of org.motechproject.mds.query.QueryParams in project motech by motech.

the class MdsRestControllerTest method verifyLookupExecution.

private void verifyLookupExecution() {
    ArgumentCaptor<Map> lookupMapCaptor = ArgumentCaptor.forClass(Map.class);
    ArgumentCaptor<QueryParams> queryParamsCaptor = ArgumentCaptor.forClass(QueryParams.class);
    verify(restFacade).executeLookup(eq(LOOKUP_NAME), lookupMapCaptor.capture(), queryParamsCaptor.capture(), anyBoolean());
    Map lookupMap = lookupMapCaptor.getValue();
    assertEquals("something", lookupMap.get("strField"));
    assertEquals("3", lookupMap.get("intField"));
    verifyQueryParams(queryParamsCaptor.getValue());
}
Also used : QueryParams(org.motechproject.mds.query.QueryParams) Map(java.util.Map)

Example 9 with QueryParams

use of org.motechproject.mds.query.QueryParams in project motech by motech.

the class MdsRestControllerTest method testListReturnLookup.

private void testListReturnLookup(String entityName, String moduleName, String namespace, boolean lookupNameInPath) throws Exception {
    final TestRecord record1 = new TestRecord("T1", 5);
    final TestRecord record2 = new TestRecord("T2", 5);
    final List<TestRecord> records = asList(record1, record2);
    List<String> fields = new ArrayList<>();
    fields.add(NAME_FIELD);
    fields.add(VAL_FIELD);
    final RestResponse response = new RestResponse(entityName, CLASSNAME, moduleName, namespace, 2l, new QueryParams(5, 14), RestProjection.createProjectionCollection(records, fields, new ArrayList<String>()));
    when(restFacadeRetriever.getRestFacade(entityName, moduleName, namespace)).thenReturn(restFacade);
    when(restFacade.executeLookup(eq(LOOKUP_NAME), any(Map.class), any(QueryParams.class), anyBoolean())).thenReturn(response);
    String url;
    if (lookupNameInPath) {
        url = buildUrl(entityName, moduleName, namespace, LOOKUP_NAME) + "?" + LOOKUP_PAGINATION_STR;
    } else {
        url = buildUrl(entityName, moduleName, namespace) + "?lookup=" + LOOKUP_NAME + "&" + LOOKUP_PAGINATION_STR;
    }
    mockMvc.perform(get(url)).andExpect(status().isOk()).andExpect(content().string(objectMapper.writeValueAsString(response)));
    verifyLookupExecution();
}
Also used : RestResponse(org.motechproject.mds.rest.RestResponse) ArrayList(java.util.ArrayList) QueryParams(org.motechproject.mds.query.QueryParams) Map(java.util.Map)

Example 10 with QueryParams

use of org.motechproject.mds.query.QueryParams in project motech by motech.

the class ParamParserTest method shouldBuildQueryParams.

@Test
public void shouldBuildQueryParams() {
    Map<String, String> requestParams = new HashMap<>();
    requestParams.put("page", "14");
    requestParams.put("pageSize", "120");
    requestParams.put("sort", "someColumn");
    requestParams.put("order", "desc");
    QueryParams queryParams = ParamParser.buildQueryParams(requestParams);
    assertEquals(Integer.valueOf(14), queryParams.getPage());
    assertEquals(Integer.valueOf(120), queryParams.getPageSize());
    assertNotNull(queryParams.getOrderList());
    assertEquals(1, queryParams.getOrderList().size());
    assertEquals("someColumn", queryParams.getOrderList().get(0).getField());
    assertEquals(Order.Direction.DESC, queryParams.getOrderList().get(0).getDirection());
    // null order
    requestParams.remove("sort");
    requestParams.remove("order");
    queryParams = ParamParser.buildQueryParams(requestParams);
    assertTrue(queryParams.getOrderList().isEmpty());
    // default order direction
    requestParams.put("sort", "anotherColumn");
    queryParams = ParamParser.buildQueryParams(requestParams);
    assertNotNull(queryParams.getOrderList());
    assertEquals(1, queryParams.getOrderList().size());
    assertEquals("anotherColumn", queryParams.getOrderList().get(0).getField());
    assertEquals(Order.Direction.ASC, queryParams.getOrderList().get(0).getDirection());
}
Also used : HashMap(java.util.HashMap) QueryParams(org.motechproject.mds.query.QueryParams) Test(org.junit.Test)

Aggregations

QueryParams (org.motechproject.mds.query.QueryParams)48 Test (org.junit.Test)27 Order (org.motechproject.mds.util.Order)14 ArrayList (java.util.ArrayList)8 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 DateTime (org.joda.time.DateTime)6 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 Arrays.asList (java.util.Arrays.asList)5 List (java.util.List)5 Map (java.util.Map)4 Records (org.motechproject.mds.web.domain.Records)4 TaskActivityType (org.motechproject.tasks.domain.enums.TaskActivityType)4 HashSet (java.util.HashSet)3 Matchers.anyString (org.mockito.Matchers.anyString)3 EntityDto (org.motechproject.mds.dto.EntityDto)3 RestResponse (org.motechproject.mds.rest.RestResponse)3 TaskActivity (org.motechproject.tasks.domain.mds.task.TaskActivity)3 TaskActivityDto (org.motechproject.tasks.dto.TaskActivityDto)3 JsonObject (com.google.gson.JsonObject)2