Search in sources :

Example 31 with MotechDataService

use of org.motechproject.mds.service.MotechDataService in project motech by motech.

the class InstanceServiceImpl method getEntityRecordsFromLookup.

@Override
public List<BasicEntityRecord> getEntityRecordsFromLookup(Long entityId, String lookupName, Map<String, Object> lookupMap, QueryParams queryParams) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
    LookupDto lookup = getLookupByName(entityId, lookupName);
    List<FieldDto> fields = entityService.getEntityFieldsForUI(entityId);
    Map<String, FieldDto> fieldMap = entityService.getLookupFieldsMapping(entityId, lookupName);
    MotechDataService service = getServiceForEntity(entity);
    try {
        LookupExecutor lookupExecutor = new LookupExecutor(service, lookup, fieldMap);
        Object result = lookupExecutor.execute(lookupMap, queryParams);
        if (lookup.isSingleObjectReturn()) {
            BasicEntityRecord record = instanceToBasicRecord(result, entity, fields, service, EntityType.STANDARD, getRelatedEntitiesFields(fields));
            return (record == null) ? new ArrayList<BasicEntityRecord>() : Collections.singletonList(record);
        } else {
            List instances = (List) result;
            return instancesToBasicRecords(instances, entity, fields, service, EntityType.STANDARD);
        }
    } catch (LookupExecutorException e) {
        if (e.getMessageKey() != null) {
            throw new LookupExecutionException(e, e.getMessageKey());
        } else {
            throw new LookupExecutionException(e, LOOKUP_EXCEPTION_MESSAGE_KEY);
        }
    } catch (RuntimeException e) {
        throw new LookupExecutionException(e, LOOKUP_EXCEPTION_MESSAGE_KEY);
    }
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) LookupExecutor(org.motechproject.mds.lookup.LookupExecutor) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) MotechDataService(org.motechproject.mds.service.MotechDataService) EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) List(java.util.List) LookupExecutionException(org.motechproject.mds.exception.lookup.LookupExecutionException) FieldDto(org.motechproject.mds.dto.FieldDto) LookupExecutorException(org.motechproject.mds.exception.lookup.LookupExecutorException)

Example 32 with MotechDataService

use of org.motechproject.mds.service.MotechDataService in project motech by motech.

the class InstanceServiceImpl method deleteSelectedInstances.

@Override
public void deleteSelectedInstances(Long entityId, List<Long> instanceIds) {
    EntityDto entity = getEntity(entityId);
    validateCredentials(entity);
    validateNonEditableProperty(entity);
    MotechDataService service = getServiceForEntity(entity);
    for (Long instanceId : instanceIds) {
        service.delete(ID_FIELD_NAME, instanceId);
    }
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 33 with MotechDataService

use of org.motechproject.mds.service.MotechDataService in project motech by motech.

the class InstanceServiceImpl method countRecords.

@Override
public long countRecords(Long entityId) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
    MotechDataService service = getServiceForEntity(entity);
    return service.count();
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 34 with MotechDataService

use of org.motechproject.mds.service.MotechDataService in project motech by motech.

the class MdsDiskSpaceUsageIT method testEudeDiskSpaceUsage.

@Test
public void testEudeDiskSpaceUsage() throws IOException, IllegalAccessException, ClassNotFoundException, InstantiationException, SQLException {
    LOGGER.info("Creating entity");
    generator.generateDummyEntities(ENTITIES, FIELDS, LOOKUPS, true);
    EntityDto entityDto = entityService.getEntityByClassName(Constants.Packages.ENTITY.concat(".").concat(generator.getEntityPrefix()).concat("0"));
    LOGGER.info("Creating {} instances for entity", INSTANCES);
    generator.generateDummyInstances(entityDto.getId(), INSTANCES);
    WebApplicationContext context = ServiceRetriever.getWebAppContext(bundleContext, MDS_BUNDLE_SYMBOLIC_NAME);
    LocalPersistenceManagerFactoryBean dataPersistenceManagerFactoryBean = (LocalPersistenceManagerFactoryBean) context.getBean(BeanFactory.FACTORY_BEAN_PREFIX + "dataPersistenceManagerFactoryBean");
    LocalPersistenceManagerFactoryBean schemaPersistenceManagerFactoryBean = (LocalPersistenceManagerFactoryBean) context.getBean(BeanFactory.FACTORY_BEAN_PREFIX + "persistenceManagerFactoryBean");
    PersistenceManagerFactory dataPersistenceManagerFactory = dataPersistenceManagerFactoryBean.getObject();
    PersistenceManagerFactory schemaPersistenceManagerFactory = schemaPersistenceManagerFactoryBean.getObject();
    JDOConnection dataCon = dataPersistenceManagerFactory.getPersistenceManager().getDataStoreConnection();
    JDOConnection schemaCon = schemaPersistenceManagerFactory.getPersistenceManager().getDataStoreConnection();
    Connection dataNativeCon = (Connection) dataCon.getNativeConnection();
    Connection schemaNativeCon = (Connection) schemaCon.getNativeConnection();
    Statement dataStmt = dataNativeCon.createStatement();
    Statement schemaStmt = schemaNativeCon.createStatement();
    ResultSet dataResultSet = dataStmt.executeQuery(String.format(SQLQUERY, "motechdata"));
    dataResultSet.absolute(1);
    double spaceUsage = dataResultSet.getDouble("MB");
    ResultSet schemaResultSet = schemaStmt.executeQuery(String.format(SQLQUERY, "motechschema"));
    schemaResultSet.absolute(1);
    spaceUsage += schemaResultSet.getDouble("MB");
    LOGGER.info("Disk space usage of Motech Data Services database after creating {} instances is {} MB", INSTANCES, spaceUsage);
    logToFile(spaceUsage);
    Bundle entitiesBundle = OsgiBundleUtils.findBundleBySymbolicName(bundleContext, MDS_ENTITIES_SYMBOLIC_NAME);
    MotechDataService service = generator.getService(entitiesBundle.getBundleContext(), entityDto.getClassName());
    service.deleteAll();
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) Statement(java.sql.Statement) Bundle(org.osgi.framework.Bundle) LocalPersistenceManagerFactoryBean(org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean) Connection(java.sql.Connection) JDOConnection(javax.jdo.datastore.JDOConnection) ResultSet(java.sql.ResultSet) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOConnection(javax.jdo.datastore.JDOConnection) MotechDataService(org.motechproject.mds.service.MotechDataService) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 35 with MotechDataService

use of org.motechproject.mds.service.MotechDataService in project motech by motech.

the class MdsStressIT method testPerformance.

@Test
public void testPerformance() throws NotFoundException, CannotCompileException, IOException, InvalidSyntaxException, InterruptedException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
    final String serviceName = ClassName.getInterfaceName(FOO_CLASS);
    generator.generateDummyEntities(1, 2, 0, true);
    Bundle entitiesBundle = OsgiBundleUtils.findBundleBySymbolicName(bundleContext, MDS_ENTITIES_SYMBOLIC_NAME);
    assertNotNull(entitiesBundle);
    MotechDataService service = (MotechDataService) ServiceRetriever.getService(entitiesBundle.getBundleContext(), serviceName, true);
    Class<?> objectClass = entitiesBundle.loadClass(FOO_CLASS);
    LOGGER.info("Loaded class: " + objectClass.getName());
    stressTestCreating(service, objectClass);
    stressTestRetrieval(service);
    stressTestUpdating(service);
    stressTestDeleting(service);
}
Also used : Bundle(org.osgi.framework.Bundle) MotechDataService(org.motechproject.mds.service.MotechDataService) Test(org.junit.Test)

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