Search in sources :

Example 41 with MotechDataService

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

Example 42 with MotechDataService

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

Example 43 with MotechDataService

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

Example 44 with MotechDataService

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

Example 45 with MotechDataService

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);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MotechDataService(org.motechproject.mds.service.MotechDataService) CsvImportResults(org.motechproject.mds.dto.CsvImportResults) CsvImportException(org.motechproject.mds.exception.csv.CsvImportException) CsvMapReader(org.supercsv.io.CsvMapReader) FieldDto(org.motechproject.mds.dto.FieldDto)

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