Search in sources :

Example 31 with QueryParams

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

the class TriggerEventServiceImplTest method shouldGetStaticTriggers.

@Test
public void shouldGetStaticTriggers() throws Exception {
    int page = 1;
    int pageSize = 10;
    List<TriggerEvent> expectedTriggers = prepareTriggers();
    QueryParams params = new QueryParams(page, pageSize);
    when(triggerEventsDataService.byChannelModuleName(MODULE_NAME, params)).thenReturn(expectedTriggers);
    List<TriggerEvent> triggers = triggerEventService.getStaticTriggers(MODULE_NAME, page, pageSize);
    verify(triggerEventsDataService, times(1)).byChannelModuleName(eq(MODULE_NAME), eq(params));
    assertEquals(expectedTriggers, triggers);
}
Also used : TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) QueryParams(org.motechproject.mds.query.QueryParams) Test(org.junit.Test)

Example 32 with QueryParams

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

the class ActivityControllerTest method setup.

@Before
public void setup() throws Exception {
    initMocks(this);
    controller = new ActivityController(activityService, taskTriggerHandler, taskWebService);
    params = new HashMap<String, Object>();
    params.put("errorKey", "errorValue");
    expected = new ArrayList<>();
    expected.add(new TaskActivityDto(SUCCESS.getValue(), TASK_ID, SUCCESS));
    expected.add(new TaskActivityDto(WARNING.getValue(), TASK_ID, WARNING));
    expected.add(new TaskActivityDto(ERROR.getValue(), TASK_ID, ERROR));
    expected.add(new TaskActivityDto(ACTIVITY_ID, ERROR.getValue(), TASK_ID, new ArrayList<>(), null, ERROR, null, params));
    activityTypes = new HashSet<>();
    activityTypes.addAll(Arrays.asList(TaskActivityType.values()));
    queryParams = new QueryParams(page, pageSize);
    task = new Task();
    task.setId(TASK_ID);
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) ArrayList(java.util.ArrayList) TaskActivityDto(org.motechproject.tasks.dto.TaskActivityDto) QueryParams(org.motechproject.mds.query.QueryParams) Before(org.junit.Before)

Example 33 with QueryParams

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

the class MdsRestFacadeImpl method executeLookup.

@Override
@Transactional
public Object executeLookup(String lookupName, Map<String, String> lookupMap, QueryParams queryParams, boolean includeBlob) {
    if (lookupExecutors.containsKey(lookupName)) {
        LookupExecutor executor = lookupExecutors.get(lookupName);
        Object result = executor.execute(lookupMap, queryParams);
        if (result instanceof Collection) {
            if (includeBlob) {
                for (T value : ((Collection<T>) result)) {
                    getBlobs(value);
                }
            }
            return new RestResponse(entityName, entityClass.getName(), moduleName, namespace, executor.executeCount(lookupMap), queryParams, RestProjection.createProjectionCollection((Collection) result, restFields, blobFields));
        } else {
            if (result == null) {
                throw new RestNoLookupResultException("No result for lookup:" + lookupName);
            }
            if (includeBlob) {
                getBlobs((T) result);
            }
            return new RestResponse(entityName, entityClass.getName(), moduleName, namespace, 1l, new QueryParams(1, 1), RestProjection.createProjection(result, restFields, blobFields));
        }
    } else if (forbiddenLookupMethodNames.contains(lookupName)) {
        throw new RestLookupExecutionForbiddenException(lookupName);
    } else {
        throw new RestLookupNotFoundException(lookupName);
    }
}
Also used : RestNoLookupResultException(org.motechproject.mds.exception.rest.RestNoLookupResultException) RestLookupExecutionForbiddenException(org.motechproject.mds.exception.rest.RestLookupExecutionForbiddenException) RestLookupNotFoundException(org.motechproject.mds.exception.rest.RestLookupNotFoundException) Collection(java.util.Collection) QueryParams(org.motechproject.mds.query.QueryParams) LookupExecutor(org.motechproject.mds.lookup.LookupExecutor) Transactional(org.springframework.transaction.annotation.Transactional)

Example 34 with QueryParams

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

the class ParamParser method buildQueryParams.

public static QueryParams buildQueryParams(Map<String, String> requestParams) {
    Integer page;
    Integer pageSize;
    String sortBy;
    String orderDir;
    Order order;
    try {
        page = getInteger(requestParams, PAGE, 1);
    } catch (NumberFormatException e) {
        throw new InvalidParameterException("Given page is not a number!", e);
    }
    try {
        pageSize = getInteger(requestParams, PAGE_SIZE, 20);
    } catch (NumberFormatException e) {
        throw new InvalidParameterException("Given page size is not a number!", e);
    }
    sortBy = requestParams.get(SORT_BY);
    orderDir = requestParams.get(ORDER_DIR);
    order = buildOrder(sortBy, orderDir);
    return new QueryParams(page, pageSize, order);
}
Also used : Order(org.motechproject.mds.util.Order) InvalidParameterException(org.motechproject.mds.web.exception.InvalidParameterException) QueryParams(org.motechproject.mds.query.QueryParams)

Example 35 with QueryParams

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

the class InstanceController method exportEntityInstances.

@RequestMapping(value = "/entities/{entityId}/exportInstances", method = RequestMethod.GET)
public void exportEntityInstances(@PathVariable Long entityId, GridSettings settings, @RequestParam String exportRecords, @RequestParam String outputFormat, HttpServletResponse response) throws IOException {
    if (!Constants.ExportFormat.isValidFormat(outputFormat)) {
        throw new IllegalArgumentException("Invalid export format: " + outputFormat);
    }
    instanceService.verifyEntityAccess(entityId);
    final String fileName = "Entity_" + entityId + "_instances";
    response.setContentType("text/csv");
    response.setCharacterEncoding(UTF_8);
    response.setHeader("Content-Disposition", "attachment; filename=" + fileName + "." + outputFormat.toLowerCase());
    final Integer pageSize = StringUtils.equalsIgnoreCase(exportRecords, "all") ? null : Integer.valueOf(exportRecords);
    final Map<String, Object> fieldMap = getFields(settings);
    QueryParams queryParams = new QueryParams(1, pageSize, QueryParamsBuilder.buildOrderList(settings, fieldMap));
    if (Constants.ExportFormat.PDF.equals(outputFormat)) {
        csvImportExportService.exportPdf(entityId, response.getOutputStream(), settings.getLookup(), queryParams, settings.getSelectedFields(), fieldMap);
    } else {
        csvImportExportService.exportCsv(entityId, response.getWriter(), settings.getLookup(), queryParams, settings.getSelectedFields(), fieldMap);
    }
}
Also used : JsonObject(com.google.gson.JsonObject) QueryParams(org.motechproject.mds.query.QueryParams) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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