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