Search in sources :

Example 1 with Filter

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

Example 2 with Filter

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;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) Filters(org.motechproject.mds.filter.Filters) Filter(org.motechproject.mds.filter.Filter) ArrayList(java.util.ArrayList) FilterValue(org.motechproject.mds.filter.FilterValue) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with Filter

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();
    }
}
Also used : Filters(org.motechproject.mds.filter.Filters) Filter(org.motechproject.mds.filter.Filter) MotechDataService(org.motechproject.mds.service.MotechDataService) Test(org.junit.Test)

Example 4 with Filter

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();
    }
}
Also used : Filters(org.motechproject.mds.filter.Filters) Filter(org.motechproject.mds.filter.Filter) MotechDataService(org.motechproject.mds.service.MotechDataService) Test(org.junit.Test)

Aggregations

Filter (org.motechproject.mds.filter.Filter)4 Filters (org.motechproject.mds.filter.Filters)4 Test (org.junit.Test)2 MotechDataService (org.motechproject.mds.service.MotechDataService)2 JsonObject (com.google.gson.JsonObject)1 ArrayList (java.util.ArrayList)1 Entity (org.motechproject.mds.domain.Entity)1 MdsEntity (org.motechproject.mds.domain.MdsEntity)1 MdsVersionedEntity (org.motechproject.mds.domain.MdsVersionedEntity)1 EntityDto (org.motechproject.mds.dto.EntityDto)1 FilterValue (org.motechproject.mds.filter.FilterValue)1 QueryParams (org.motechproject.mds.query.QueryParams)1 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)1 Records (org.motechproject.mds.web.domain.Records)1 Transactional (org.springframework.transaction.annotation.Transactional)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1