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