Search in sources :

Example 1 with QueryParams

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

the class EmailAuditServiceBundleIT method shouldCountAndRetrieveEmailsBasedOnCriteria.

@Test
public void shouldCountAndRetrieveEmailsBasedOnCriteria() {
    emailRecordService.create(createEmailRecord("address@1.com", "something", DeliveryStatus.SENT));
    emailRecordService.create(createEmailRecord("address@2.com", "a subject", DeliveryStatus.SENT));
    emailRecordService.create(createEmailRecord("address3@1.com", "received this?", DeliveryStatus.RECEIVED));
    emailRecordService.create(createEmailRecord("something@1.com", "was sent", DeliveryStatus.SENT));
    emailRecordService.create(createEmailRecord("address@1.com", "error", DeliveryStatus.ERROR));
    QueryParams queryParams = new QueryParams(1, 10, new Order("subject", Order.Direction.DESC));
    EmailRecordSearchCriteria criteria = new EmailRecordSearchCriteria().withToAddress("address").withDeliveryStatuses(DeliveryStatus.SENT, DeliveryStatus.RECEIVED).withQueryParams(queryParams).withMessageTimeRange(new Range<DateTime>(null, null));
    assertEquals(3, emailAuditService.countEmailRecords(criteria));
    List<EmailRecord> records = emailAuditService.findEmailRecords(criteria);
    assertEquals(asList("something", "received this?", "a subject"), extract(records, on(EmailRecord.class).getSubject()));
    assertEquals(asList("address@1.com", "address3@1.com", "address@2.com"), extract(records, on(EmailRecord.class).getToAddress()));
    assertEquals(asList(DeliveryStatus.SENT, DeliveryStatus.RECEIVED, DeliveryStatus.SENT), extract(records, on(EmailRecord.class).getDeliveryStatus()));
}
Also used : Order(org.motechproject.mds.util.Order) EmailRecord(org.motechproject.email.domain.EmailRecord) QueryParams(org.motechproject.mds.query.QueryParams) EmailRecordSearchCriteria(org.motechproject.email.builder.EmailRecordSearchCriteria) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 2 with QueryParams

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

the class MdsDdeBundleIT method shouldRevertFromTrash.

@Test
public void shouldRevertFromTrash() {
    HomeAddress homeAddress1 = revertFromTrashService.create(new HomeAddress("street1", "city1"));
    HomeAddress homeAddress2 = revertFromTrashService.create(new HomeAddress("street2", "city2"));
    revertFromTrashService.delete(homeAddress1);
    revertFromTrashService.delete(homeAddress2);
    revertFromTrashService.revertFromTrash(homeAddress1.getId());
    QueryParams q = new QueryParams(1, 10);
    Collection instancesInTrash = trashService.getInstancesFromTrash(HomeAddress.class.getName(), q);
    Collection<HomeAddress> instancesOutsideTrash = revertFromTrashService.retrieveAll();
    assertEquals(1, instancesInTrash.size());
    assertEquals(1, instancesOutsideTrash.size());
}
Also used : HomeAddress(org.motechproject.mds.test.domain.revertFromTrash.HomeAddress) Collection(java.util.Collection) QueryParams(org.motechproject.mds.query.QueryParams) Test(org.junit.Test)

Example 3 with QueryParams

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

the class QueryParamsBuilder method buildQueryParams.

/**
 * Builds query params using the provided order list. The grid setting will be only used for pagination.
 * @param settings the grid settings received
 * @param orderList the order list that will be used for sorting
 * @return query params for this request
 */
public static QueryParams buildQueryParams(GridSettings settings, List<Order> orderList) {
    // just check if the page is set
    int page = (settings.getPage() == null) ? DEFAULT_PAGE : settings.getPage();
    int pageSize = (settings.getRows() == null) ? DEFAULT_PAGE_SIZE : settings.getRows();
    QueryParams queryParams = new QueryParams(page, pageSize, orderList);
    if (!queryParams.containsOrderOnField(Constants.Util.ID_FIELD_NAME)) {
        queryParams.addOrder(orderIdAsc());
    }
    return queryParams;
}
Also used : QueryParams(org.motechproject.mds.query.QueryParams)

Example 4 with QueryParams

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

the class MdsRestBundleIT method testBasicCrud.

@Test
public void testBasicCrud() throws Exception {
    final MotechDataService dataService = getDataService();
    // CREATE
    // create 11 records using REST
    getLogger().info("Creating instance via REST");
    for (int i = 0; i < 11; i++) {
        HttpPost post = new HttpPost(ENTITY_URL);
        post.setEntity(new StringEntity(recordJsonString("string" + i, i), ContentType.APPLICATION_JSON));
        HttpResponse response = getHttpClient().execute(post);
        assertNotNull(response);
        assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    }
    // UPDATE
    // update records 4 & 5
    // we add "updated" at the end of the string
    // GET object Ids
    // TODO: should we return ID in REST responses?
    // TODO: invoke this lookup through REST
    QueryParams queryParams = new QueryParams(new Order("intField", Order.Direction.ASC));
    List objList = (List) MethodUtils.invokeMethod(dataService, "byIntSet", new Object[] { new HashSet<>(asList(4, 5)), queryParams });
    // verify that the lookup works correctly just in case
    assertNotNull(objList);
    assertEquals(2, objList.size());
    for (Object record : objList) {
        HttpPut put = new HttpPut(ENTITY_URL);
        put.setEntity(new StringEntity(recordJsonString(PropertyUtil.getProperty(record, "strField") + "Updated", (int) PropertyUtil.getProperty(record, "intField"), (long) PropertyUtil.getProperty(record, "id"))));
        HttpResponse response = getHttpClient().execute(put);
        assertNotNull(response);
        assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    }
    // READ
    // read records from 5 to 1
    getLogger().info("Reading instances via REST");
    String body = getHttpClient().get(ENTITY_URL + "?page=2&pageSize=5&sort=intField&order=desc", new BasicResponseHandler());
    assertNotNull(body);
    RestResponse response = OBJECT_MAPPER.readValue(body, RestResponse.class);
    assertEquals(5, response.getData().size());
    for (int i = 0; i < 5; i++) {
        int expectedIntField = 5 - i;
        // we updated the int fields X 10 for these two records
        boolean fieldWasUpdated = expectedIntField == 5 || expectedIntField == 4;
        // and added "Updated" to the string field
        String expectedStrField = "string" + expectedIntField;
        if (fieldWasUpdated) {
            expectedStrField += "Updated";
        }
        Object record = response.getData().get(i);
        assertEquals(expectedStrField, PropertyUtils.getProperty(record, "strField"));
        assertEquals(expectedIntField, PropertyUtils.getProperty(record, "intField"));
    }
    for (Object rec1 : response.getData()) {
        String responseBody = getHttpClient().get(ENTITY_URL + "?id=" + PropertyUtils.getProperty(rec1, "id"), new BasicResponseHandler());
        assertNotNull(responseBody);
        RestResponse response2 = OBJECT_MAPPER.readValue(responseBody, RestResponse.class);
        assertEquals(response2.getData().get(0).get("strField"), PropertyUtils.getProperty(rec1, "strField"));
    }
    // DELETE
    // delete 5 records using REST
    getLogger().info("Delete instances via REST");
    List list2 = dataService.retrieveAll();
    for (int i = 0; i < 5; i++) {
        Object record2 = list2.get(i);
        long id = (long) PropertyUtils.getProperty(record2, "id");
        HttpDelete delete = new HttpDelete(ENTITY_URL + "/" + id);
        HttpResponse response2 = getHttpClient().execute(delete);
        assertNotNull(response2);
        assertEquals(HttpStatus.SC_OK, response2.getStatusLine().getStatusCode());
        assertNull(dataService.findById(id));
    }
    assertEquals(dataService.retrieveAll().size(), 6);
}
Also used : Order(org.motechproject.mds.util.Order) HttpPost(org.apache.http.client.methods.HttpPost) HttpDelete(org.apache.http.client.methods.HttpDelete) RestResponse(org.motechproject.mds.rest.RestResponse) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpResponse(org.apache.http.HttpResponse) MotechDataService(org.motechproject.mds.service.MotechDataService) HttpPut(org.apache.http.client.methods.HttpPut) StringEntity(org.apache.http.entity.StringEntity) QueryParams(org.motechproject.mds.query.QueryParams) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with QueryParams

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

the class InstanceController method getInstances.

@RequestMapping(value = "/entities/{entityId}/instances", method = RequestMethod.POST)
@ResponseBody
public Records<BasicEntityRecord> getInstances(@PathVariable Long entityId, GridSettings settings) throws IOException {
    String lookup = settings.getLookup();
    String filterStr = settings.getFilter();
    Map<String, Object> fieldMap = getFields(settings);
    QueryParams queryParams = QueryParamsBuilder.buildQueryParams(settings, fieldMap);
    List<BasicEntityRecord> entityRecords;
    long recordCount;
    if (StringUtils.isNotBlank(lookup)) {
        entityRecords = instanceService.getEntityRecordsFromLookup(entityId, lookup, fieldMap, queryParams);
        recordCount = instanceService.countRecordsByLookup(entityId, lookup, fieldMap);
    } else if (filterSet(filterStr)) {
        Filters filters = new Filters(objectMapper.readValue(filterStr, Filter[].class));
        filters.setMultiselect(instanceService.getEntityFields(entityId));
        entityRecords = instanceService.getEntityRecordsWithFilter(entityId, filters, queryParams);
        recordCount = instanceService.countRecordsWithFilters(entityId, filters);
    } else {
        entityRecords = instanceService.getEntityRecords(entityId, queryParams);
        recordCount = instanceService.countRecords(entityId);
    }
    int rowCount = (int) Math.ceil(recordCount / (double) queryParams.getPageSize());
    Records<BasicEntityRecord> records = new Records<>(queryParams.getPage(), rowCount, (int) recordCount, entityRecords);
    processFieldsForUI(records);
    return records;
}
Also used : Filters(org.motechproject.mds.filter.Filters) Filter(org.motechproject.mds.filter.Filter) JsonObject(com.google.gson.JsonObject) 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)

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