Search in sources :

Example 1 with EntityInfoReader

use of org.motechproject.mds.entityinfo.EntityInfoReader 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 2 with EntityInfoReader

use of org.motechproject.mds.entityinfo.EntityInfoReader in project motech by motech.

the class MdsScheduledJob method execute.

@Override
@SuppressWarnings("unchecked")
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
    LOGGER.info("Executing Trash Clean Up");
    try {
        SchedulerContext schedulerContext;
        try {
            schedulerContext = jobExecutionContext.getScheduler().getContext();
        } catch (SchedulerException e) {
            LOGGER.error("Can not execute job. Can not get Scheduler Context", e);
            return;
        }
        ApplicationContext applicationContext = (ApplicationContext) schedulerContext.get("applicationContext");
        BundleContext bundleContext = ((MotechOsgiConfigurableApplicationContext) applicationContext).getBundleContext();
        TrashService trashService = OSGiServiceUtils.findService(bundleContext, TrashService.class);
        EntityInfoReader entityInfoReader = OSGiServiceUtils.findService(bundleContext, EntityInfoReader.class);
        if (trashService != null) {
            if (entityInfoReader != null) {
                Collection<String> entitiesClassNames = entityInfoReader.getEntitiesClassNames();
                trashService.emptyTrash(entitiesClassNames);
            } else {
                LOGGER.warn("EntityInfoReader is unavailable, unable to empty trash");
            }
        } else {
            LOGGER.warn("TrashService is unavailable, unable to empty trash");
        }
    } catch (Exception e) {
        LOGGER.error("Job execution failed.", e);
    }
}
Also used : MotechOsgiConfigurableApplicationContext(org.motechproject.bundle.extender.MotechOsgiConfigurableApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) SchedulerException(org.quartz.SchedulerException) MotechOsgiConfigurableApplicationContext(org.motechproject.bundle.extender.MotechOsgiConfigurableApplicationContext) TrashService(org.motechproject.mds.service.TrashService) SchedulerContext(org.quartz.SchedulerContext) JobExecutionException(org.quartz.JobExecutionException) SchedulerException(org.quartz.SchedulerException) BundleContext(org.osgi.framework.BundleContext) EntityInfoReader(org.motechproject.mds.entityinfo.EntityInfoReader)

Aggregations

EntityInfoReader (org.motechproject.mds.entityinfo.EntityInfoReader)2 ArrayList (java.util.ArrayList)1 MotechOsgiConfigurableApplicationContext (org.motechproject.bundle.extender.MotechOsgiConfigurableApplicationContext)1 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)1 FieldDto (org.motechproject.mds.dto.FieldDto)1 EntityInfo (org.motechproject.mds.entityinfo.EntityInfo)1 FieldInfo (org.motechproject.mds.entityinfo.FieldInfo)1 DefaultMotechDataService (org.motechproject.mds.service.DefaultMotechDataService)1 MotechDataService (org.motechproject.mds.service.MotechDataService)1 TrashService (org.motechproject.mds.service.TrashService)1 BundleContext (org.osgi.framework.BundleContext)1 JobExecutionException (org.quartz.JobExecutionException)1 SchedulerContext (org.quartz.SchedulerContext)1 SchedulerException (org.quartz.SchedulerException)1 ApplicationContext (org.springframework.context.ApplicationContext)1