use of org.motechproject.mds.service.MotechDataService in project motech by motech.
the class MdsDummyDataGeneratorImpl method getService.
@Override
public MotechDataService getService(BundleContext bundleContext, String className) {
String interfaceName = MotechClassPool.getInterfaceName(className);
ServiceReference ref = bundleContext.getServiceReference(interfaceName);
if (ref == null) {
throw new ServiceNotFoundException(interfaceName);
}
return (MotechDataService) bundleContext.getService(ref);
}
use of org.motechproject.mds.service.MotechDataService in project motech by motech.
the class MdsDummyDataGeneratorIT method verifyEUDEInstancesAreCreated.
private void verifyEUDEInstancesAreCreated() throws IllegalAccessException, ClassNotFoundException, InstantiationException {
EntityDto entityDto = entityService.getEntityByClassName(Constants.Packages.ENTITY.concat(".").concat(generator.getEntityPrefix()).concat("0"));
Assert.assertNotNull(entityDto);
generator.generateDummyInstances(entityDto.getId(), INSTANCES);
Bundle entitiesBundle = OsgiBundleUtils.findBundleBySymbolicName(bundleContext, MDS_ENTITIES_SYMBOLIC_NAME);
assertNotNull(entitiesBundle);
final String serviceName = ClassName.getInterfaceName(entityDto.getName());
MotechDataService service = (MotechDataService) ServiceRetriever.getService(entitiesBundle.getBundleContext(), serviceName);
Assert.assertEquals(service.retrieveAll().size(), INSTANCES);
}
use of org.motechproject.mds.service.MotechDataService in project motech by motech.
the class ActionHandlerServiceImpl method delete.
@Override
public void delete(Map<String, Object> parameters) throws ActionHandlerException {
LOGGER.debug("Action DELETE: params: {}", parameters);
String entityClassName = getEntityClassName(parameters);
MotechDataService dataService = getEntityDataService(entityClassName);
Long instanceId = getInstanceId(parameters, true);
Object instance = retrieveEntityInstance(dataService, instanceId);
dataService.delete(instance);
}
use of org.motechproject.mds.service.MotechDataService in project motech by motech.
the class ActionHandlerServiceImpl method queryAndUpdate.
@Override
public void queryAndUpdate(Map<String, Object> parameters) throws ActionHandlerException {
LOGGER.debug("Action QUERY AND UPDATE: params {}", parameters);
String entityClassName = getEntityClassName(parameters);
MotechDataService dataService = getEntityDataService(entityClassName);
Entity entity = getEntity(entityClassName);
List<?> instances = dataService.retrieveAll();
for (Object instance : instances) {
setInstancePropertiesForQuery(instance, entity.getFields(), parameters);
dataService.update(instance);
}
}
use of org.motechproject.mds.service.MotechDataService in project motech by motech.
the class CsvImporterExporter method importCsv.
private CsvImportResults importCsv(final EntityInfo entityInfo, final Reader reader, CsvImportCustomizer importCustomizer, boolean continueOnError, boolean clearData) {
final MotechDataService dataService = DataServiceHelper.getDataService(getBundleContext(), entityInfo.getClassName());
Map<String, FieldDto> fieldCacheMap = new HashMap<>();
if (clearData) {
dataService.deleteAll();
}
try (CsvMapReader csvMapReader = new CsvMapReader(reader, CsvPreference.STANDARD_PREFERENCE)) {
List<Long> newInstanceIDs = new ArrayList<>();
List<Long> updatedInstanceIDs = new ArrayList<>();
Map<Integer, String> exceptions = new HashMap<>();
Map<String, String> row;
int rowNum = 0;
final String[] headers = csvMapReader.getHeader(true);
while ((row = csvMapReader.read(headers)) != null) {
rowNum++;
try {
// import a row
RowImportResult rowImportResult = importInstanceFromRow(entityInfo.getEntity(), row, headers, fieldCacheMap, entityInfo.getFieldDtos(), dataService, importCustomizer);
Long id = rowImportResult.getId();
// put its ID in the correct list
if (rowImportResult.isNewInstance()) {
newInstanceIDs.add(id);
} else {
updatedInstanceIDs.add(id);
}
} catch (RuntimeException e) {
if (continueOnError) {
exceptions.put(rowNum, e.getMessage());
} else {
throw e;
}
}
}
return new CsvImportResults(entityInfo.getEntity(), newInstanceIDs, updatedInstanceIDs, exceptions);
} catch (IOException e) {
throw new CsvImportException("IO Error when importing CSV", e);
}
}
Aggregations