use of org.motechproject.mds.dto.FieldDto in project motech by motech.
the class MdsDummyDataGeneratorImpl method makeDummyInstance.
@Override
public Object makeDummyInstance(Long entityId) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
String className = entityService.getEntity(entityId).getClassName();
Class objectClass = MDSClassLoader.getInstance().loadClass(className);
Object obj = objectClass.newInstance();
for (FieldDto fieldDto : entityService.getEntityFields(entityId)) {
String fieldName = fieldDto.getBasic().getName();
if (!ArrayUtils.contains(GENERATED_FIELD_NAMES, fieldName)) {
PropertyUtil.safeSetProperty(obj, fieldName, randomValueFor(fieldDto.getType()));
}
}
return obj;
}
use of org.motechproject.mds.dto.FieldDto in project motech by motech.
the class MdsDummyDataGeneratorImpl method prepareDummyEntity.
private void prepareDummyEntity(int number, int fieldsPerEntity, int lookupsPerEntity) throws IOException {
EntityDto entityDto = new EntityDto(Long.valueOf(number), entityPrefix.concat(String.valueOf(number)));
entityDto = entityService.createEntity(entityDto);
List<FieldDto> fields = new ArrayList<>();
for (int i = 0; i < fieldsPerEntity; i++) {
TypeDto type = pickRandomFieldType();
fields.add(new FieldDto(null, entityDto.getId(), type, new FieldBasicDto(fieldPrefix.concat(String.valueOf(i)), fieldPrefix.concat(String.valueOf(i))), false, null, null, settingsFor(type), null));
}
entityService.addFields(entityDto, fields);
List<LookupDto> lookups = new ArrayList<>();
for (int i = 0; i < lookupsPerEntity; i++) {
List<LookupFieldDto> lookupFields = new ArrayList<>();
List<FieldDto> entityFields = entityService.getFields(entityDto.getId());
int amountOfFields = RAND.nextInt(entityFields.size());
for (int j = 0; j < amountOfFields; j++) {
lookupFields.add(new LookupFieldDto(null, entityFields.get(j).getBasic().getName(), LookupFieldType.VALUE));
}
lookups.add(new LookupDto(lookupPrefix.concat(String.valueOf(i)), RAND.nextBoolean(), RAND.nextBoolean(), lookupFields, false));
}
entityService.addLookups(entityDto.getId(), lookups);
}
use of org.motechproject.mds.dto.FieldDto in project motech by motech.
the class FieldRecordTest method shouldHandleSingleSelectComboBoxes.
@Test
public void shouldHandleSingleSelectComboBoxes() {
FieldDto fieldDto = FieldTestHelper.fieldDto(1L, "name", List.class.getName(), "disp", "[one, two]");
fieldDto.setSettings(asList(new SettingDto(COMBOBOX_VALUES, asList("one", "two", "three"), null, null)));
FieldRecord fieldRecord = new FieldRecord(fieldDto);
fieldRecord.setValue("[two]");
assertEquals("two", fieldRecord.getValue());
fieldRecord.setValue("test");
assertEquals("test", fieldRecord.getValue());
// test with enum
fieldRecord.setValue(TestEnum.TWO);
assertEquals("TWO", fieldRecord.getValue());
fieldRecord.setValue(asList(TestEnum.ONE));
assertEquals("ONE", fieldRecord.getValue());
fieldRecord.setValue(null);
assertNull(fieldRecord.getValue());
}
use of org.motechproject.mds.dto.FieldDto in project motech by motech.
the class EntityControllerTest method shouldSaveTemporaryChange.
@Test
public void shouldSaveTemporaryChange() throws Exception {
DraftData data = new DraftData();
data.setEdit(true);
data.setValues(new HashMap<>());
data.getValues().put(DraftData.PATH, "basic.displayName");
data.getValues().put(DraftData.FIELD_ID, "2");
data.getValues().put(DraftData.VALUE, singletonList("test"));
ResultActions actions = controller.perform(get("/entities/9007")).andExpect(status().isOk());
EntityDto entity = new ObjectMapper().readValue(actions.andReturn().getResponse().getContentAsByteArray(), EntityDto.class);
actions = controller.perform(get("/entities/9007/fields")).andExpect(status().isOk());
List<FieldDto> fields = new ObjectMapper().readValue(actions.andReturn().getResponse().getContentAsByteArray(), new TypeReference<List<FieldDto>>() {
});
FieldDto fieldDto = findFieldById(fields, 12L);
// before change
assertFalse(entity.isModified());
assertNotNull(fieldDto);
assertEquals("ID", fieldDto.getBasic().getDisplayName());
// change
controller.perform(post("/entities/9007/draft").body(new ObjectMapper().writeValueAsBytes(data)).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
verify(entityService).saveDraftEntityChanges(eq(9007L), draftDataCaptor.capture());
DraftData captured = draftDataCaptor.getValue();
assertTrue(captured.isEdit());
assertEquals("basic.displayName", captured.getPath());
}
use of org.motechproject.mds.dto.FieldDto in project motech by motech.
the class InstanceServiceTest method shouldNotAutoPopulateOwnerAndCreatorForNonEditableFields.
@Test
public void shouldNotAutoPopulateOwnerAndCreatorForNonEditableFields() {
FieldDto ownerField = FieldTestHelper.fieldDto(1L, "owner", String.class.getName(), "String field", null);
ownerField.setNonEditable(true);
FieldDto creatorField = FieldTestHelper.fieldDto(1L, "creator", String.class.getName(), "String field", null);
creatorField.setNonEditable(true);
when(entityService.getEntityFieldsForUI(ENTITY_ID)).thenReturn(asList(ownerField, creatorField));
mockEntity();
setUpSecurityContext();
EntityRecord record = instanceService.newInstance(ENTITY_ID);
List<FieldRecord> fieldRecords = record.getFields();
assertEquals(asList(null, null), extract(fieldRecords, on(FieldRecord.class).getValue()));
}
Aggregations