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()));
}
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());
}
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;
}
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);
}
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;
}
Aggregations