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