Search in sources :

Example 36 with MotechDataService

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();
    }
}
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 37 with MotechDataService

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();
    }
}
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 38 with MotechDataService

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());
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) MotechDataService(org.motechproject.mds.service.MotechDataService) DateTime(org.joda.time.DateTime) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 39 with MotechDataService

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;
}
Also used : ArrayList(java.util.ArrayList) DefaultMotechDataService(org.motechproject.mds.service.DefaultMotechDataService) MotechDataService(org.motechproject.mds.service.MotechDataService) EntityInfoReader(org.motechproject.mds.entityinfo.EntityInfoReader) EntityInfo(org.motechproject.mds.entityinfo.EntityInfo) AdvancedSettingsDto(org.motechproject.mds.dto.AdvancedSettingsDto) DefaultMotechDataService(org.motechproject.mds.service.DefaultMotechDataService) FieldInfo(org.motechproject.mds.entityinfo.FieldInfo) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 40 with MotechDataService

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);
}
Also used : Bundle(org.osgi.framework.Bundle) MotechDataService(org.motechproject.mds.service.MotechDataService)

Aggregations

MotechDataService (org.motechproject.mds.service.MotechDataService)52 EntityDto (org.motechproject.mds.dto.EntityDto)26 FieldDto (org.motechproject.mds.dto.FieldDto)14 ArrayList (java.util.ArrayList)13 List (java.util.List)9 Test (org.junit.Test)8 Entity (org.motechproject.mds.domain.Entity)4 LookupDto (org.motechproject.mds.dto.LookupDto)4 ObjectNotFoundException (org.motechproject.mds.exception.object.ObjectNotFoundException)4 LookupExecutor (org.motechproject.mds.lookup.LookupExecutor)4 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)4 Bundle (org.osgi.framework.Bundle)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Collection (java.util.Collection)3 LookupExecutionException (org.motechproject.mds.exception.lookup.LookupExecutionException)3 ObjectReadException (org.motechproject.mds.exception.object.ObjectReadException)3 DefaultMotechDataService (org.motechproject.mds.service.DefaultMotechDataService)3 EntityRecord (org.motechproject.mds.web.domain.EntityRecord)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2