use of org.motechproject.mds.service.MotechDataService 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.service.MotechDataService 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();
}
}
use of org.motechproject.mds.service.MotechDataService in project motech by motech.
the class FilterContextIT method setUpTestData.
private void setUpTestData() throws Exception {
Class<?> clazz = getEntityClass();
DateTime threeDaysAgo = NOW.minusDays(3);
DateTime eightDaysAgo = NOW.minusDays(8);
DateTime notThisMonth = NOW.minusMonths(2);
DateTime notThisYear = NOW.minusYears(2);
MotechDataService service = getService();
final Object instance1 = objectInstance(clazz, true, NOW.toDate(), NOW, "now");
final Object instance2 = objectInstance(clazz, true, threeDaysAgo.toDate(), threeDaysAgo, "threeDaysAgo");
final Object instance3 = objectInstance(clazz, false, eightDaysAgo.toDate(), eightDaysAgo, "eightDaysAgo");
final Object instance4 = objectInstance(clazz, false, notThisMonth.toDate(), notThisMonth, "notThisMonth");
final Object instance5 = objectInstance(clazz, false, notThisYear.toDate(), notThisYear, "notThisYear");
service.doInTransaction(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
service.create(instance1);
service.create(instance2);
service.create(instance3);
service.create(instance4);
service.create(instance5);
}
});
assertEquals("There were issues creating test data", 5, service.count());
}
use of org.motechproject.mds.service.MotechDataService in project motech by motech.
the class BaseInstanceIT method createService.
private MotechDataService createService() throws Exception {
Object repository = MDSClassLoader.getInstance().loadClass(getRepositoryClass()).newInstance();
Object service = MDSClassLoader.getInstance().loadClass(getServiceClass()).newInstance();
EntityInfoReader entityInfoReader = new EntityInfoReader() {
@Override
public EntityInfo getEntityInfo(String entityClassName) {
return buildEntityInfo();
}
@Override
public EntityInfo getEntityInfo(Long entityId) {
return buildEntityInfo();
}
@Override
public Collection<String> getEntitiesClassNames() {
List<String> classNames = new ArrayList<>();
classNames.add(entity.getClassName());
return classNames;
}
private EntityInfo buildEntityInfo() {
EntityInfo info = new EntityInfo();
info.setEntity(entity);
info.setAdvancedSettings(new AdvancedSettingsDto());
List<FieldInfo> fieldInfos = new ArrayList<>();
for (FieldDto fieldDto : getEntityFields()) {
FieldInfo fieldInfo = new FieldInfo();
fieldInfo.setField(fieldDto);
fieldInfos.add(fieldInfo);
}
info.setFieldsInfo(fieldInfos);
return info;
}
};
PropertyUtil.safeSetProperty(repository, "persistenceManagerFactory", getDataPersistenceManagerFactory());
PropertyUtil.safeSetProperty(service, "transactionManager", getDataTransactionManager());
PropertyUtil.safeSetProperty(service, "repository", repository);
PropertyUtil.safeSetProperty(service, "entityInfoReader", entityInfoReader);
PropertyUtil.safeSetProperty(service, "historyService", getHistoryService());
PropertyUtil.safeSetProperty(service, "trashService", getTrashService());
PropertyUtil.safeSetProperty(service, "osgiEventProxy", getOsgiEventProxy());
MotechDataService mds = (MotechDataService) service;
((DefaultMotechDataService) mds).init();
return mds;
}
use of org.motechproject.mds.service.MotechDataService in project motech by motech.
the class MdsDummyDataGeneratorImpl method generateDummyInstances.
@Override
public void generateDummyInstances(Long entityId, int numberOfInstances, int numberOfHistoricalRevisions, int numberOfTrashInstances) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Bundle entitiesBundle = OsgiBundleUtils.findBundleBySymbolicName(bundleContext, MDS_ENTITIES_SYMBOLIC_NAME);
MotechDataService service = getService(entitiesBundle.getBundleContext(), getEntityClassName(entityId));
makeInstances(service, entityId, numberOfInstances, numberOfHistoricalRevisions, numberOfTrashInstances);
}
Aggregations