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