Search in sources :

Example 11 with SchemaHolder

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

the class MdsBundleIT method verifyComboboxDataMigration.

private void verifyComboboxDataMigration() throws NoSuchFieldException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    Long entityId = entityService.getEntityByClassName(FOO_CLASS).getId();
    Long fieldId = getFieldIdByName(entityService.getFields(entityId), "someEnum");
    DraftData draft = DraftBuilder.forFieldEdit(fieldId, "settings.2.value", true);
    entityService.saveDraftEntityChanges(entityId, draft);
    entityService.commitChanges(entityId);
    SchemaHolder schemaHolder = entityService.getSchema();
    generator.regenerateMdsDataBundle(schemaHolder, true);
    service = (MotechDataService) ServiceRetriever.getService(bundleContext, ClassName.getInterfaceName(FOO_CLASS), true);
    assertValuesEqual(getExpectedComboboxValues(), getValues(service.detachedCopyAll(service.retrieveAll())));
}
Also used : DraftData(org.motechproject.mds.dto.DraftData) SchemaHolder(org.motechproject.mds.dto.SchemaHolder)

Example 12 with SchemaHolder

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

the class MdsDataProviderBuilderImplTest method shouldGenerateJson.

@Test
public void shouldGenerateJson() {
    String json = "{\n" + "    \"name\": \"data-services\",\n" + "    \"objects\": [         {\n" + "            \"displayName\": \"TestEntity\",\n" + "            \"type\": \"org.motechproject.TestEntity\",\n" + "            \"lookupFields\": [                 {\n" + "                    \"displayName\": \"TestLookupName\",\n" + "                    \"fields\": [\n" + "                         \"TestFieldName\"                      ]\n" + "                },                {\n" + "                    \"displayName\": \"mds.dataprovider.byinstanceid\",\n" + "                    \"fields\": [\n" + "                        \"mds.dataprovider.instanceid\"\n" + "                    ]\n" + "                }\n" + "            ],\n" + "            \"fields\": [\n" + "                                {\n" + "                    \"displayName\": \"TestFieldDisplayName\",\n" + "                    \"fieldKey\": \"TestFieldName\"\n" + "                }              ]\n" + "        }      ]\n" + "}\n";
    EntityDto entity = new EntityDto();
    entity.setId(Long.valueOf("1"));
    entity.setName("TestEntity");
    entity.setClassName("org.motechproject.TestEntity");
    FieldDto field = new FieldDto();
    FieldBasicDto fieldBasicDto = new FieldBasicDto();
    fieldBasicDto.setName("TestFieldName");
    fieldBasicDto.setDisplayName("TestFieldDisplayName");
    field.setBasic(fieldBasicDto);
    fieldList.add(field);
    LookupDto lookup = new LookupDto();
    lookup.setLookupName("TestLookupName");
    List<LookupFieldDto> lookupFields = new LinkedList<>();
    lookupFields.add(FieldTestHelper.lookupFieldDto("TestFieldName"));
    lookup.setLookupFields(lookupFields);
    lookupList.add(lookup);
    AdvancedSettingsDto advancedSettings = new AdvancedSettingsDto();
    advancedSettings.setIndexes(lookupList);
    SchemaHolder schema = new SchemaHolder();
    schema.addEntity(entity, advancedSettings, fieldList);
    String generatedJson = mdsDataProviderBuilder.generateDataProvider(schema);
    assertEquals(json, generatedJson.replace("\r\n", "\n"));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) LookupDto(org.motechproject.mds.dto.LookupDto) SchemaHolder(org.motechproject.mds.dto.SchemaHolder) AdvancedSettingsDto(org.motechproject.mds.dto.AdvancedSettingsDto) LinkedList(java.util.LinkedList) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Test(org.junit.Test)

Example 13 with SchemaHolder

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

the class MdsDataProviderBuilderImplTest method shouldGenerateEmptyJson.

@Test
public void shouldGenerateEmptyJson() {
    String generatedJson = mdsDataProviderBuilder.generateDataProvider(new SchemaHolder());
    assertEquals("", generatedJson);
}
Also used : SchemaHolder(org.motechproject.mds.dto.SchemaHolder) Test(org.junit.Test)

Example 14 with SchemaHolder

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

the class EntityServiceContextIT method shouldCreateEntity.

@Test
public void shouldCreateEntity() throws Exception {
    // given
    EntityDto entityDto = new EntityDto();
    entityDto.setName(SIMPLE_NAME);
    // when
    entityService.createEntity(entityDto);
    setProperty(monitor, "bundleStarted", true);
    setProperty(monitor, "bundleInstalled", true);
    setProperty(monitor, "contextInitialized", true);
    SchemaHolder schemaHolder = entityService.getSchema();
    jarGeneratorService.regenerateMdsDataBundle(schemaHolder);
    // then
    // 1. new entry in db should be added
    String className = String.format("%s.%s", Constants.PackagesGenerated.ENTITY, "Test");
    assertTrue(String.format("Not found %s in database", className), containsEntity(className));
    // 2. there should be ability to create a new instance of created entity
    Class<?> clazz = MDSClassLoader.getInstance().loadClass(className);
    Object instance = clazz.newInstance();
    assertNotNull(instance);
    ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        // We want to use the current factory for persisting so this hack is required
        // Normally these classes live in a separate bundle
        Thread.currentThread().setContextClassLoader(MDSClassLoader.getInstance());
        getPersistenceManagerFactory().registerMetadata(metadataHolder.getJdoMetadata());
        getPersistenceManager().makePersistent(instance);
        List<?> list = cast(clazz, (Collection) getPersistenceManager().newQuery(clazz).execute());
        assertNotNull(list);
        assertFalse("The instance of entity should be saved in database", list.isEmpty());
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) MDSClassLoader(org.motechproject.mds.util.MDSClassLoader) SchemaHolder(org.motechproject.mds.dto.SchemaHolder) Test(org.junit.Test)

Example 15 with SchemaHolder

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

the class BaseInstanceIT method setUpEntity.

private void setUpEntity() throws Exception {
    String entityClass = getEntityClassName();
    entity = new EntityDto(entityClass);
    entity.setRecordHistory(true);
    entity = entityService.createEntity(entity);
    entityService.addFields(entity, getEntityFields());
    TrackingDto tracking = entityService.getAdvancedSettings(entity.getId(), true).getTracking();
    tracking.setAllowCreateEvent(false);
    tracking.setAllowUpdateEvent(false);
    tracking.setAllowDeleteEvent(false);
    entityService.updateTracking(entity.getId(), tracking);
    SchemaHolder schemaHolder = entityService.getSchema();
    mdsConstructor.constructEntities(schemaHolder);
    PersistenceManagerFactory factory = getDataPersistenceManagerFactory();
    if (null == factory.getMetadata(entityClass)) {
        factory.registerMetadata(metadataHolder.getJdoMetadata());
    }
    CtClass ctClass = MotechClassPool.getDefault().get(getRepositoryClass());
    MDSClassLoader.getInstance().safeDefineClass(getRepositoryClass(), ctClass.toBytecode());
    ctClass = MotechClassPool.getDefault().get(getInterfaceClass());
    MDSClassLoader.getInstance().safeDefineClass(getInterfaceClass(), ctClass.toBytecode());
    ctClass = MotechClassPool.getDefault().get(getServiceClass());
    MDSClassLoader.getInstance().safeDefineClass(getServiceClass(), ctClass.toBytecode());
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) CtClass(javassist.CtClass) SchemaHolder(org.motechproject.mds.dto.SchemaHolder) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) TrackingDto(org.motechproject.mds.dto.TrackingDto)

Aggregations

SchemaHolder (org.motechproject.mds.dto.SchemaHolder)24 EntityDto (org.motechproject.mds.dto.EntityDto)7 FieldDto (org.motechproject.mds.dto.FieldDto)5 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)5 Test (org.junit.Test)4 StopWatch (org.apache.commons.lang.time.StopWatch)3 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Before (org.junit.Before)2 MDSProcessorOutput (org.motechproject.mds.annotations.internal.MDSProcessorOutput)2 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)2 DraftData (org.motechproject.mds.dto.DraftData)2 LookupDto (org.motechproject.mds.dto.LookupDto)2 SettingDto (org.motechproject.mds.dto.SettingDto)2