Search in sources :

Example 26 with QueryParams

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

the class ActivityController method getTaskActivities.

/**
 * Returns the list of activities for task with the given ID.
 *
 * @param taskId  the ID of the task
 * @return  the list of activities
 */
@RequestMapping(value = "/activity/{taskId}", method = RequestMethod.GET)
@ResponseBody
public TaskActivityRecords getTaskActivities(@PathVariable Long taskId, GridSettings settings) {
    if (settings != null) {
        QueryParams params = getParams(settings);
        Set<TaskActivityType> types = settings.getTypesFromString();
        Range<DateTime> dateTimeRange = settings.convertToDateRange(settings.getDateTimeFrom(), settings.getDateTimeTo());
        List<TaskActivityDto> activities;
        long count;
        if (dateTimeRange != null) {
            activities = taskWebService.getTaskActivities(taskId, types, dateTimeRange, params, settings.isLastExecution());
            count = activityService.getTaskActivitiesCount(taskId, types, dateTimeRange, settings.isLastExecution());
        } else {
            activities = taskWebService.getTaskActivities(taskId, types, params, settings.isLastExecution());
            count = activityService.getTaskActivitiesCount(taskId, types, settings.isLastExecution());
        }
        int totalPages = (int) Math.ceil((double) count / settings.getRows());
        return new TaskActivityRecords(settings.getPage(), totalPages, count, activities);
    } else {
        return null;
    }
}
Also used : TaskActivityType(org.motechproject.tasks.domain.enums.TaskActivityType) QueryParams(org.motechproject.mds.query.QueryParams) TaskActivityDto(org.motechproject.tasks.dto.TaskActivityDto) DateTime(org.joda.time.DateTime) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 27 with QueryParams

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

the class LookupBuilder method build.

private CtMethod build(boolean body) throws CannotCompileException, NotFoundException {
    Collection<String> paramCollection = new ArrayList<>();
    List<String> fieldOrder = lookup.getFieldsOrder();
    for (int i = 0; i < fieldOrder.size(); i++) {
        String fieldName = fieldOrder.get(i);
        FieldDto field = getLookupField(fieldName);
        // don't use fieldName for fetching fields, as it can contain dots, etc.
        LookupFieldDto lookupField = lookup.getLookupField(field.getBasic().getName());
        FieldDto relationField = null;
        EntityDto relatedEntity = null;
        if (fieldOrder.get(i).contains(".")) {
            relatedEntity = schemaHolder.getEntityByClassName(new RelationshipHolder(field).getRelatedClass());
            relationField = schemaHolder.getFieldByName(relatedEntity, LookupName.getRelatedFieldName(fieldOrder.get(i)));
        }
        String type = getTypeForParam(i, resolveEntity(entity, relatedEntity), resolveField(field, relationField), lookupField);
        String param = String.format("%s %s", type, fieldOrder.get(i).replace(".", ""));
        paramCollection.add(param);
    }
    // query params at the end for ordering/paging
    if (WITH_QUERY_PARAMS == lookupType) {
        String queryParam = String.format("%s queryParams", QueryParams.class.getName());
        paramCollection.add(queryParam);
    }
    String params = StringUtils.join(paramCollection, ", ");
    String signature = String.format("public %s %s(%s)", returnType(), lookupName, params);
    String methodAsString = body ? String.format("%s{%s}", signature, body()) : String.format("%s;", signature);
    String generic = buildGenericSignature();
    CtMethod method = CtNewMethod.make(methodAsString, definition);
    method.setGenericSignature(generic);
    return method;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) ArrayList(java.util.ArrayList) QueryParams(org.motechproject.mds.query.QueryParams) CtMethod(javassist.CtMethod) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 28 with QueryParams

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

the class MdsBundleIT method verifyCsvImport.

private void verifyCsvImport() throws Exception {
    getLogger().info("Verifying CSV Import");
    CsvImportExportService csvImportExportService = ServiceRetriever.getService(bundleContext, CsvImportExportService.class);
    try (InputStream in = new ClassPathResource("csv/import.csv").getInputStream()) {
        Reader reader = new InputStreamReader(in);
        CsvImportResults results = csvImportExportService.importCsv(FOO_CLASS, reader, "import.csv", false);
        assertNotNull(results);
        assertEquals(2, results.totalNumberOfImportedInstances());
        assertEquals(2, results.newInstanceCount());
        assertEquals(0, results.updatedInstanceCount());
    }
    assertEquals(7, service.count());
    // get the imported instances through a lookup
    QueryParams queryParams = new QueryParams(new Order("someTime", Order.Direction.DESC));
    List list = (List) MethodUtils.invokeExactMethod(service, "matchesOperator", new Object[] { "fromCsv", queryParams });
    assertNotNull(list);
    assertEquals(2, list.size());
    assertInstance(list.get(0), false, "fromCsv2", "Capital CSV", Collections.emptyList(), null, new LocalDate(2012, 10, 14), null, new Period(2, 0, 0, 0, 0, 0, 0, 0), null, new DateTime(2014, 12, 2, 16, 13, 40, 0, DateTimeZone.UTC).toDate(), null, new Time(20, 20), null, null, null, null);
    assertInstance(list.get(1), true, "fromCsv1", "Capital CSV", new ArrayList(asList("one", "two")), new DateTime(2014, 12, 2, 13, 10, 40, 0, DateTimeZone.UTC).withZone(DateTimeZone.getDefault()), new LocalDate(2012, 10, 15), null, new Period(1, 0, 0, 0, 0, 0, 0, 0), null, new DateTime(2014, 12, 2, 13, 13, 40, 0, DateTimeZone.UTC).toDate(), null, new Time(10, 30), null, null, null, null);
}
Also used : Order(org.motechproject.mds.util.Order) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) Period(org.joda.time.Period) Time(org.motechproject.commons.date.model.Time) LocalDateTime(java.time.LocalDateTime) DateTime(org.joda.time.DateTime) LocalDate(org.joda.time.LocalDate) ClassPathResource(org.springframework.core.io.ClassPathResource) LocalDateTime(java.time.LocalDateTime) DateTime(org.joda.time.DateTime) CsvImportResults(org.motechproject.mds.dto.CsvImportResults) CsvImportExportService(org.motechproject.mds.service.CsvImportExportService) QueryParams(org.motechproject.mds.query.QueryParams) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 29 with QueryParams

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

the class MdsBundleIT method verifyInstanceCreatingAndRetrieving.

private void verifyInstanceCreatingAndRetrieving(Class<?> loadedClass) throws Exception {
    getLogger().info("Verifying instance creation and retrieval");
    Object instance = loadedClass.newInstance();
    Object instance2 = loadedClass.newInstance();
    Object instance3 = loadedClass.newInstance();
    Object instance4 = loadedClass.newInstance();
    Object instance5 = loadedClass.newInstance();
    // instance 1
    updateInstance(instance, true, "trueNow", "trueNowCp", new ArrayList(asList("1", "2", "3")), NOW, LD_NOW, TEST_MAP, TEST_PERIOD, BYTE_ARRAY_VALUE, DATE_NOW, DOUBLE_VALUE_1, MORNING_TIME, 1, toEnum(loadedClass, "one"), JAVA_LD_NOW, JAVA_NOW);
    // instance 2
    updateInstance(instance2, true, "trueInRange", "trueInRangeCp", new ArrayList(asList("2", "4")), NOW.plusHours(1), LD_NOW.plusDays(1), TEST_MAP, TEST_PERIOD, BYTE_ARRAY_VALUE, DATE_NOW, DOUBLE_VALUE_1, MORNING_TIME, 2, toEnum(loadedClass, "two"), JAVA_LD_NOW.plusDays(1), JAVA_NOW.plusHours(1));
    // instance 3
    updateInstance(instance3, false, "falseInRange", "falseInRangeCp", null, NOW.plusHours(2), LD_NOW.plusDays(1), null, TEST_PERIOD, BYTE_ARRAY_VALUE, DATE_TOMORROW, DOUBLE_VALUE_2, NIGHT_TIME, 2, toEnum(loadedClass, "three"), JAVA_LD_NOW.plusDays(2), JAVA_NOW.plusHours(2));
    // instance 4
    updateInstance(instance4, true, "trueOutOfRange", "trueOutOfRangeCp", null, NOW.plusHours(3), LD_NOW.plusDays(10), null, TEST_PERIOD, BYTE_ARRAY_VALUE, DATE_TOMORROW, DOUBLE_VALUE_2, NIGHT_TIME, 3, toEnum(loadedClass, "one"), JAVA_LD_NOW.plusDays(3), JAVA_NOW.plusHours(3));
    // instance 5
    updateInstance(instance5, true, "notInSet", "notInSetCp", null, NOW.plusHours(4), LD_NOW, null, TEST_PERIOD, BYTE_ARRAY_VALUE, DATE_NOW, DOUBLE_VALUE_2, MORNING_TIME, 4, toEnum(loadedClass, "two"), JAVA_LD_NOW.plusDays(4), JAVA_NOW.plusHours(4));
    // Single object return lookup should return 0 if there are no instances
    Long emptyCount = (Long) MethodUtils.invokeMethod(service, "countByUniqueString", "trueNow");
    assertEquals(emptyCount, (Long) 0L);
    service.create(instance);
    Object retrieved = service.retrieveAll(QueryParams.ascOrder("someDateTime")).get(0);
    // Single object return lookup should return 1 if there is instance with unique value in field
    Long count = (Long) MethodUtils.invokeMethod(service, "countByUniqueString", "trueNow");
    assertEquals(count, (Long) 1L);
    assertInstanceOne(retrieved, loadedClass);
    assertEquals(1, service.retrieveAll().size());
    service.create(instance2);
    service.create(instance3);
    service.create(instance4);
    service.create(instance5);
    assertEquals(INSTANCE_COUNT, service.retrieveAll().size());
    // verify double order
    QueryParams queryParams = new QueryParams(asList(new Order("someLocalDate", Order.Direction.DESC), new Order("someString", Order.Direction.ASC)));
    List<Object> result = service.retrieveAll(queryParams);
    assertNotNull(result);
    assertEquals(5, result.size());
    assertInstanceFour(result.get(0), loadedClass);
    assertInstanceThree(result.get(1), loadedClass);
    assertInstanceTwo(result.get(2), loadedClass);
    assertInstanceFive(result.get(3), loadedClass);
    assertInstanceOne(result.get(4), loadedClass);
}
Also used : Order(org.motechproject.mds.util.Order) ArrayList(java.util.ArrayList) QueryParams(org.motechproject.mds.query.QueryParams)

Example 30 with QueryParams

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

the class HistoryServiceImpl method getLatestRevision.

private Object getLatestRevision(Class<?> historyClass, Long instanceId) {
    Query query = initQuery(historyClass);
    QueryUtil.setQueryParams(query, new QueryParams(1, 1, new Order(ID_FIELD_NAME, Order.Direction.DESC)));
    query.setUnique(true);
    return query.execute(instanceId);
}
Also used : Order(org.motechproject.mds.util.Order) Query(javax.jdo.Query) QueryParams(org.motechproject.mds.query.QueryParams)

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