Search in sources :

Example 46 with EntityDto

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

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

the class EntityServiceContextIT method shouldNotAddFieldsWithTheSameNameToAnEntity.

@Test
public void shouldNotAddFieldsWithTheSameNameToAnEntity() throws IOException {
    EntityDto entityDto = new EntityDto();
    entityDto.setName("SameFieldTest");
    entityDto = entityService.createEntity(entityDto);
    FieldDto field1 = new FieldDto();
    field1.setBasic(new FieldBasicDto("disp", "name"));
    field1.setType(typeService.findType(Boolean.class));
    FieldDto field2 = new FieldDto();
    field2.setBasic(new FieldBasicDto("dispName2", "name"));
    field2.setType(typeService.findType(Integer.class));
    entityService.addFields(entityDto, asList(field1, field2));
    List<FieldDto> fieldsFromDb = entityService.getFields(entityDto.getId());
    assertNotNull(fieldsFromDb);
    assertEquals(7, fieldsFromDb.size());
    assertEquals(asList("Id", "Created By", "Owner", "Modified By", "Creation Date", "Modification Date", "dispName2"), extract(fieldsFromDb, on(FieldDto.class).getBasic().getDisplayName()));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 48 with EntityDto

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

the class EntityServiceContextIT method shouldRetrieveAllWorkInProgress.

@Test
public void shouldRetrieveAllWorkInProgress() throws IOException {
    EntityDto entityDto = new EntityDto();
    entityDto.setName("WIP1");
    EntityDto result1 = entityService.createEntity(entityDto);
    entityDto.setClassName(null);
    entityDto.setName("WIP2");
    EntityDto result2 = entityService.createEntity(entityDto);
    entityService.saveDraftEntityChanges(result1.getId(), DraftBuilder.forNewField("disp", "name", Long.class.getName()));
    entityService.saveDraftEntityChanges(result2.getId(), DraftBuilder.forNewField("disp", "name", Long.class.getName()));
    List<EntityDto> wip = entityService.listWorkInProgress();
    assertNotNull(wip);
    List<String> result = extract(wip, on(EntityDto.class).getName());
    Collections.sort(result);
    assertEquals(asList("WIP1", "WIP2"), result);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) Test(org.junit.Test)

Example 49 with EntityDto

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

the class EntityServiceContextIT method testDraftWorkflow.

@Test
public void testDraftWorkflow() throws IOException {
    EntityDto entityDto = new EntityDto();
    entityDto.setName("DraftTest");
    entityDto = entityService.createEntity(entityDto);
    final Long entityId = entityDto.getId();
    entityService.saveDraftEntityChanges(entityId, DraftBuilder.forNewField("disp", "f1name", Long.class.getName()));
    List<FieldDto> fields = entityService.getFields(entityId);
    assertNotNull(fields);
    assertEquals(asList("id", "creator", "owner", "modifiedBy", "creationDate", "modificationDate", "f1name"), extract(fields, on(FieldDto.class).getBasic().getName()));
    // check if abandoning works
    entityService.abandonChanges(entityId);
    fields = entityService.getFields(entityId);
    assertNotNull(fields);
    assertEquals(asList("id", "creator", "owner", "modifiedBy", "creationDate", "modificationDate"), extract(fields, on(FieldDto.class).getBasic().getName()));
    // check add-edit-commit
    entityService.saveDraftEntityChanges(entityId, DraftBuilder.forNewField("disp", "f1name", Long.class.getName()));
    fields = entityService.getFields(entityId);
    assertNotNull(fields);
    assertEquals(7, fields.size());
    FieldDto field = selectFirst(fields, having(on(FieldDto.class).getBasic().getName(), equalTo("f1name")));
    entityService.saveDraftEntityChanges(entityDto.getId(), DraftBuilder.forFieldEdit(field.getId(), "basic.displayName", "newDisp"));
    fields = entityService.getFields(entityId);
    assertNotNull(fields);
    assertEquals(asList("Id", "Created By", "Owner", "Modified By", "Creation Date", "Modification Date", "newDisp"), extract(fields, on(FieldDto.class).getBasic().getDisplayName()));
    entityService.commitChanges(entityId);
    // check if changes were persisted in db
    Entity entityFromDb = getEntities().get(0);
    assertEquals(7, entityFromDb.getFields().size());
    Field fieldFromDb = entityFromDb.getField("f1name");
    assertNotNull(fieldFromDb);
    assertEquals("newDisp", fieldFromDb.getDisplayName());
    // no drafts in db
    assertTrue(entityService.listWorkInProgress().isEmpty());
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) Entity(org.motechproject.mds.domain.Entity) Field(org.motechproject.mds.domain.Field) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 50 with EntityDto

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

the class CsvImportExportServiceImpl method importCsv.

@Override
public CsvImportResults importCsv(long entityId, Reader reader, String fileName, CsvImportCustomizer importCustomizer, boolean continueOnError, boolean clearData) {
    LOGGER.debug("Importing instances of entity with ID: {}", entityId);
    CsvImportResults importResults;
    try {
        importResults = csvImporterExporter.importCsv(entityId, reader, importCustomizer, continueOnError, clearData);
    } catch (RuntimeException e) {
        EntityDto entity = entityService.getEntity(entityId);
        sendImportFailureEvent(entity, fileName, e);
        throw e;
    }
    sendImportSuccessEvent(importResults, fileName);
    return importResults;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) CsvImportResults(org.motechproject.mds.dto.CsvImportResults)

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