use of org.motechproject.mds.filter.Filter 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;
}
use of org.motechproject.mds.filter.Filter in project motech by motech.
the class EntityServiceImpl method findEntitiesByPackage.
@Override
@Transactional
public List<EntityDto> findEntitiesByPackage(String packageName) {
List<EntityDto> entities = new ArrayList<>();
FilterValue filterValue = new FilterValue() {
@Override
public Object valueForQuery() {
return super.getValue();
}
@Override
public String paramTypeForQuery() {
return String.class.getName();
}
@Override
public List<String> operatorForQueryFilter() {
return Arrays.asList(".startsWith(", ")");
}
};
filterValue.setValue(packageName);
Filter filter = new Filter("className", new FilterValue[] { filterValue });
for (Entity entity : allEntities.filter(new Filters(filter), null, null)) {
if (entity.isActualEntity()) {
entities.add(entity.toDto());
}
}
return entities;
}
use of org.motechproject.mds.filter.Filter in project motech by motech.
the class FilterContextIT method shouldDoFilters.
@Test
public void shouldDoFilters() {
MotechDataService service = getService();
Filter yearFilter = new Filter(DATE_FIELD, FilterValue.THIS_YEAR);
Filter falseFilter = new Filter(BOOL_FIELD, FilterValue.NO);
Filters filters = new Filters(new Filter[] { yearFilter, falseFilter });
try {
TimeFaker.fakeNow(NOW);
List<Object> list = service.filter(filters, null);
assertPresentByStrField(list, "eightDaysAgo", "notThisMonth");
assertEquals(2, service.countForFilters(filters));
} finally {
TimeFaker.stopFakingTime();
}
}
use of org.motechproject.mds.filter.Filter in project motech by motech.
the class FilterContextIT method shouldDoFilter.
@Test
public void shouldDoFilter() {
MotechDataService service = getService();
Filter trueFilter = new Filter(BOOL_FIELD, FilterValue.YES);
List<Object> list = service.filter(new Filters(trueFilter), null);
assertPresentByStrField(list, "now", "threeDaysAgo");
assertEquals(2, service.countForFilters(new Filters(trueFilter)));
Filter falseFilter = new Filter(BOOL_FIELD, FilterValue.NO);
list = service.filter(new Filters(falseFilter), null);
assertPresentByStrField(list, "eightDaysAgo", "notThisMonth", "notThisYear");
assertEquals(3, service.countForFilters(new Filters(falseFilter)));
try {
TimeFaker.fakeNow(NOW);
for (String field : asList(DATE_FIELD, DATETIME_FIELD)) {
Filter todayFilter = new Filter(field, FilterValue.TODAY);
list = service.filter(new Filters(todayFilter), null);
assertPresentByStrField(list, "now");
assertEquals(1, service.countForFilters(new Filters(todayFilter)));
Filter sevenDayFilter = new Filter(field, FilterValue.PAST_7_DAYS);
list = service.filter(new Filters(sevenDayFilter), null);
assertPresentByStrField(list, "now", "threeDaysAgo");
assertEquals(2, service.countForFilters(new Filters(sevenDayFilter)));
Filter monthFilter = new Filter(field, FilterValue.THIS_MONTH);
list = service.filter(new Filters(monthFilter), null);
assertPresentByStrField(list, "now", "threeDaysAgo", "eightDaysAgo");
assertEquals(3, service.countForFilters(new Filters(monthFilter)));
Filter yearFilter = new Filter(field, FilterValue.THIS_YEAR);
list = service.filter(new Filters(yearFilter), null);
assertPresentByStrField(list, "now", "threeDaysAgo", "eightDaysAgo", "notThisMonth");
assertEquals(4, service.countForFilters(new Filters(yearFilter)));
}
} finally {
TimeFaker.stopFakingTime();
}
}
Aggregations