Search in sources :

Example 81 with EntityDto

use of org.motechproject.mds.dto.EntityDto 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 82 with EntityDto

use of org.motechproject.mds.dto.EntityDto in project motech by motech.

the class LookupProcessor method setUseGenericParam.

private void setUseGenericParam(EntityDto entity, Class<?> methodParameterType, LookupFieldDto lookupField) {
    FieldDto field = findEntityFieldByName(entity.getClassName(), lookupField.getName());
    TypeDto fieldType = field.getType();
    EntityDto relatedEntity = null;
    if (fieldType.isRelationship()) {
        relatedEntity = findEntityByClassName(field.getMetadata(Constants.MetadataKeys.RELATED_CLASS).getValue());
        field = findEntityFieldByName(field.getMetadata(Constants.MetadataKeys.RELATED_CLASS).getValue(), lookupField.getRelatedName());
    }
    if (fieldType.isCombobox()) {
        ComboboxHolder holder = new ComboboxHolder(relatedEntity == null ? entity : relatedEntity, field);
        boolean isCollection = holder.isCollection();
        boolean isCollectionParam = Collection.class.isAssignableFrom(methodParameterType);
        lookupField.setUseGenericParam(isCollection && !isCollectionParam);
    }
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) TypeDto(org.motechproject.mds.dto.TypeDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 83 with EntityDto

use of org.motechproject.mds.dto.EntityDto in project motech by motech.

the class EntityInfrastructureBuilderTest method shouldCreateCodeForClassWithLookups.

@Test
public void shouldCreateCodeForClassWithLookups() throws Exception {
    MDSClassLoader mdsClassLoaderImpl = MDSClassLoader.getStandaloneInstance(getClass().getClassLoader());
    EntityDto entity = new EntityDto(SampleWithLookups.class.getName());
    entity.setMaxFetchDepth(-1);
    LookupDto lookup = new LookupDto();
    lookup.setLookupName("testLookup");
    lookup.setMethodName("testLookupMethod");
    FieldDto testField = fieldDto("TestField", "testDispName", String.class);
    FieldDto testField2 = fieldDto("TestField2", "DisplayName with space", String.class);
    FieldDto dateField = fieldDto("dateField", "Display names should not affect methods", DateTime.class);
    FieldDto timeField = fieldDto("timeField", Time.class);
    when(schemaHolder.getFieldByName(SampleWithLookups.class.getName(), "TestField")).thenReturn(testField);
    when(schemaHolder.getFieldByName(SampleWithLookups.class.getName(), "TestField2")).thenReturn(testField2);
    when(schemaHolder.getFieldByName(SampleWithLookups.class.getName(), "dateField")).thenReturn(dateField);
    when(schemaHolder.getFieldByName(SampleWithLookups.class.getName(), "timeField")).thenReturn(timeField);
    lookup.setFieldsOrder(asList("TestField", "TestField2", "dateField", "timeField"));
    lookup.setSingleObjectReturn(true);
    when(schemaHolder.getLookups(entity)).thenReturn(singletonList(lookup));
    List<LookupFieldDto> lookupFields = new ArrayList<>();
    lookupFields.add(new LookupFieldDto("TestField", LookupFieldType.VALUE));
    lookupFields.add(new LookupFieldDto("TestField2", LookupFieldType.VALUE));
    lookupFields.add(new LookupFieldDto("dateField", LookupFieldType.RANGE));
    lookupFields.add(new LookupFieldDto("timeField", LookupFieldType.SET));
    lookup.setLookupFields(lookupFields);
    List<ClassData> data = entityInfrastructureBuilder.buildInfrastructure(entity, schemaHolder);
    for (ClassData classData : data) {
        mdsClassLoaderImpl.safeDefineClass(classData.getClassName(), classData.getBytecode());
    }
    verifySingleLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_SERVICE));
    verifySingleLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_INTERFACE));
    verifyCountLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_SERVICE));
    verifyCountLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_INTERFACE));
    // lookup with multiple return
    lookup.setSingleObjectReturn(false);
    mdsClassLoaderImpl = MDSClassLoader.getStandaloneInstance(getClass().getClassLoader());
    data = entityInfrastructureBuilder.buildInfrastructure(entity, schemaHolder);
    for (ClassData classData : data) {
        mdsClassLoaderImpl.safeDefineClass(classData.getClassName(), classData.getBytecode());
    }
    verifyMultiReturnLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_SERVICE));
    verifyMultiReturnLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_INTERFACE));
    verifyCountLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_SERVICE));
    verifyCountLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_INTERFACE));
    verifyFetchDepthInRepository(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_REPOSITORY), -1);
}
Also used : MDSClassLoader(org.motechproject.mds.util.MDSClassLoader) EntityDto(org.motechproject.mds.dto.EntityDto) ClassData(org.motechproject.mds.domain.ClassData) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) SampleWithLookups(org.motechproject.mds.builder.SampleWithLookups) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 84 with EntityDto

use of org.motechproject.mds.dto.EntityDto in project motech by motech.

the class MdsBundleIT method clearEntities.

private void clearEntities() {
    getLogger().info("Cleaning up entities");
    for (EntityDto entity : entityService.listEntities()) {
        if (!entity.isDDE()) {
            userPreferencesService.removeUserPreferences(entity.getId(), USERNAME);
            entityService.deleteEntity(entity.getId());
        }
    }
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto)

Example 85 with EntityDto

use of org.motechproject.mds.dto.EntityDto in project motech by motech.

the class MdsBundleIT method testUserPreferences.

@Test
public void testUserPreferences() {
    EntityDto entityDto = createEntityForPreferencesTest();
    // first retrieve - should create default user preferences for entity
    UserPreferencesDto userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(new Integer(50), userPreferencesDto.getGridRowsNumber());
    assertEquals(3, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getVisibleFields().contains("someBoolean"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("someString"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("someInteger"));
    assertEquals(0, userPreferencesDto.getSelectedFields().size());
    assertEquals(0, userPreferencesDto.getUnselectedFields().size());
    userPreferencesService.updateGridSize(entityDto.getId(), USERNAME, 100);
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(new Integer(100), userPreferencesDto.getGridRowsNumber());
    // if null then default value from settings will be used
    userPreferencesService.updateGridSize(entityDto.getId(), USERNAME, null);
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(new Integer(50), userPreferencesDto.getGridRowsNumber());
    userPreferencesService.unselectField(entityDto.getId(), USERNAME, "someString");
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(2, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getVisibleFields().contains("someBoolean"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("someInteger"));
    assertEquals(0, userPreferencesDto.getSelectedFields().size());
    assertEquals(1, userPreferencesDto.getUnselectedFields().size());
    assertTrue(userPreferencesDto.getUnselectedFields().contains("someString"));
    userPreferencesService.selectField(entityDto.getId(), USERNAME, "otherInteger");
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(3, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getVisibleFields().contains("someBoolean"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("someInteger"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("otherInteger"));
    assertEquals(1, userPreferencesDto.getSelectedFields().size());
    assertTrue(userPreferencesDto.getSelectedFields().contains("otherInteger"));
    assertEquals(1, userPreferencesDto.getUnselectedFields().size());
    assertTrue(userPreferencesDto.getUnselectedFields().contains("someString"));
    userPreferencesService.selectField(entityDto.getId(), USERNAME, "someString");
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(4, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getVisibleFields().contains("someBoolean"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("otherInteger"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("otherInteger"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("someString"));
    assertEquals(2, userPreferencesDto.getSelectedFields().size());
    assertTrue(userPreferencesDto.getSelectedFields().contains("otherInteger"));
    assertTrue(userPreferencesDto.getSelectedFields().contains("someString"));
    assertEquals(0, userPreferencesDto.getUnselectedFields().size());
    userPreferencesService.unselectFields(entityDto.getId(), USERNAME);
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(0, userPreferencesDto.getVisibleFields().size());
    assertEquals(0, userPreferencesDto.getSelectedFields().size());
    assertEquals(10, userPreferencesDto.getUnselectedFields().size());
    userPreferencesService.selectFields(entityDto.getId(), USERNAME);
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(10, userPreferencesDto.getVisibleFields().size());
    assertEquals(10, userPreferencesDto.getSelectedFields().size());
    assertEquals(0, userPreferencesDto.getUnselectedFields().size());
    // if field will be removed from entity then it should be removed also from preferences (CASCADE)
    EntityDraft draft = entityService.getEntityDraft(entityDto.getId());
    List<FieldDto> fields1 = entityService.getEntityFields(draft.getId());
    Long someIntegerId = getFieldIdByName(fields1, "someInteger");
    DraftData draftData = DraftBuilder.forFieldRemoval(someIntegerId);
    entityService.saveDraftEntityChanges(entityDto.getId(), draftData);
    entityService.commitChanges(entityDto.getId());
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(9, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getSelectedFields().contains("someBoolean"));
    assertTrue(userPreferencesDto.getSelectedFields().contains("someString"));
    assertFalse(userPreferencesDto.getSelectedFields().contains("someInteger"));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) DraftData(org.motechproject.mds.dto.DraftData) UserPreferencesDto(org.motechproject.mds.dto.UserPreferencesDto) EntityDraft(org.motechproject.mds.domain.EntityDraft) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Aggregations

EntityDto (org.motechproject.mds.dto.EntityDto)136 Test (org.junit.Test)61 FieldDto (org.motechproject.mds.dto.FieldDto)53 ArrayList (java.util.ArrayList)34 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)32 MotechDataService (org.motechproject.mds.service.MotechDataService)26 LookupDto (org.motechproject.mds.dto.LookupDto)24 List (java.util.List)19 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)12 Method (java.lang.reflect.Method)11 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)11 Arrays.asList (java.util.Arrays.asList)9 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)9 EntityRecord (org.motechproject.mds.web.domain.EntityRecord)9 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)7 TypeDto (org.motechproject.mds.dto.TypeDto)7 HashMap (java.util.HashMap)6 SchemaHolder (org.motechproject.mds.dto.SchemaHolder)6 BasicFieldRecord (org.motechproject.mds.web.domain.BasicFieldRecord)6 FieldRecord (org.motechproject.mds.web.domain.FieldRecord)6