use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class EntityServiceContextIT method shouldAddLookupWithRelatedField.
@Test
public void shouldAddLookupWithRelatedField() {
EntityDto entityWithLookup = new EntityDto();
entityWithLookup.setName("entityWithLookup");
entityWithLookup = entityService.createEntity(entityWithLookup);
EntityDto relatedEntity = new EntityDto();
relatedEntity.setName("relatedEntity");
relatedEntity = entityService.createEntity(relatedEntity);
FieldDto stringField = FieldTestHelper.fieldDto("stringField", String.class);
FieldDto nameField = FieldTestHelper.fieldDto("nameField", String.class);
FieldDto lengthField = FieldTestHelper.fieldDto("lengthField", Long.class);
FieldDto relationField = FieldTestHelper.fieldDto("relatedField", TypeDto.ONE_TO_MANY_RELATIONSHIP.getTypeClass());
relationField.addMetadata(new MetadataDto(RELATED_CLASS, relatedEntity.getClassName()));
entityService.addFields(entityWithLookup, asList(stringField, relationField));
entityService.addFields(relatedEntity, asList(nameField, lengthField));
List<LookupFieldDto> lookupFieldDtos = lookupFieldsFromNames("relatedField.nameField", "relatedField.lengthField", "stringField");
LookupDto lookup = new LookupDto("lookup", false, false, lookupFieldDtos, true);
entityService.addLookups(entityWithLookup.getId(), lookup);
LookupDto lookupFromDb = entityService.getLookupByName(entityWithLookup.getId(), "lookup");
assertNotNull(lookupFromDb);
assertEquals(asList("relatedField.nameField", "relatedField.lengthField", "stringField"), lookupFromDb.getFieldsOrder());
assertEquals(asList("relatedField", "relatedField", "stringField"), extract(lookupFromDb.getLookupFields(), on(LookupFieldDto.class).getName()));
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class EntityServiceContextIT method shouldAddNewFieldForLookupAndSaveEntity.
@Test
public void shouldAddNewFieldForLookupAndSaveEntity() throws IOException {
EntityDto entityDto = new EntityDto();
entityDto.setName("myEntity");
entityDto = entityService.createEntity(entityDto);
// add a new field to draft
EntityDraft entityDraft = entityService.getEntityDraft(entityDto.getId());
entityService.saveDraftEntityChanges(entityDraft.getId(), DraftBuilder.forNewField("disp", "testFieldName", Long.class.getName()));
FieldDto field = selectFirst(entityService.getFields(entityDraft.getId()), having(on(FieldDto.class).getBasic().getName(), equalTo("testFieldName")));
LookupDto lookup = new LookupDto("lookup", false, false, null, true);
entityService.addLookups(entityDraft.getId(), Collections.singletonList(lookup));
Map<String, Object> values = new HashMap<>();
values.put("path", "indexes.0.$addField");
values.put("advanced", true);
values.put("value", Collections.singletonList(field.getId()));
DraftData draftData = new DraftData();
draftData.setEdit(true);
draftData.setValues(values);
entityService.saveDraftEntityChanges(entityDraft.getId(), draftData);
entityService.commitChanges(entityDto.getId());
assertNotNull(entityService.getLookupByName(entityDto.getId(), "lookup").getLookupField("testFieldName"));
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class MdsRestFacadeTest method setUp.
@Before
public void setUp() {
when(dataService.getClassType()).thenReturn(Record.class);
when(entityInfoReader.getEntityInfo(Record.class.getName())).thenReturn(entity);
when(entity.getName()).thenReturn(ENTITY_NAME);
when(entity.getModule()).thenReturn(TEST_MODULE);
when(entity.getClassName()).thenReturn(Record.class.getName());
when(entity.getNamespace()).thenReturn(NAMESPACE);
when(entity.getAdvancedSettings()).thenReturn(advancedSettingsDto);
when(advancedSettingsDto.getRestOptions()).thenReturn(restOptions);
// set up rest fields
FieldDto valueField = FieldTestHelper.fieldDto(3L, VALUE_FIELD, String.class.getName(), VALUE_FIELD, null);
FieldDto dateField = FieldTestHelper.fieldDto(4L, DATE_FIELD, Date.class.getName(), DATE_FIELD, null);
FieldDto blobField = FieldTestHelper.fieldDto(5L, BLOB_FIELD, Byte[].class.getName(), BLOB_FIELD, null);
blobField.setType(new TypeDto("mds.field.blob", StringUtils.EMPTY, BLOB_FIELD, Byte[].class.getName()));
when(restOptions.getFieldNames()).thenReturn(Arrays.asList(VALUE_FIELD, DATE_FIELD, BLOB_FIELD));
// set up lookups
FieldDto strField = FieldTestHelper.fieldDto(1L, STR_FIELD, String.class.getName(), STR_FIELD, null);
FieldDto intField = FieldTestHelper.fieldDto(2L, INT_FIELD, Integer.class.getName(), INT_FIELD, null);
when(entity.getFieldDtos()).thenReturn(asList(intField, strField, valueField, dateField, blobField));
when(entity.getField(STR_FIELD)).thenReturn(FieldTestHelper.fieldInfo(STR_FIELD, String.class, false, true));
when(entity.getField(INT_FIELD)).thenReturn(FieldTestHelper.fieldInfo(INT_FIELD, Integer.class, false, true));
LookupDto forbiddenLookup = new LookupDto(FORBIDDEN_LOOKUP_NAME, true, false, asList(FieldTestHelper.lookupFieldDto(1L, STR_FIELD), FieldTestHelper.lookupFieldDto(2L, INT_FIELD)), true);
LookupDto supportedLookup = new LookupDto(SUPPORTED_LOOKUP_NAME, false, true, asList(FieldTestHelper.lookupFieldDto(1L, STR_FIELD), FieldTestHelper.lookupFieldDto(2L, INT_FIELD)), true);
when(entity.getLookups()).thenReturn(asList(forbiddenLookup, supportedLookup));
// set up record
recordOne = testRecord();
// set up data service
when(dataService.retrieveAll(any(QueryParams.class))).thenReturn(asList(recordOne));
when(dataService.retrieveAll()).thenReturn(asList(recordOne));
when(dataService.findById(1l)).thenReturn(recordOne);
when(dataService.create(recordOne)).thenReturn(recordOne);
when(dataService.getDetachedField(recordOne, BLOB_FIELD)).thenReturn(blobFieldValue);
// do the initialization, normally called by Spring as @PostConstruct
mdsRestFacade.init();
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class MdsLookupServiceTest method setUp.
@Before
public void setUp() {
MotechClassPool.registerServiceInterface(Record.class.getName(), TestDataService.class.getName());
EntityDto entity = new EntityDto(ENTITY_ID, ENTITY_CLASS_NAME);
FieldDto strField = FieldTestHelper.fieldDto(STR_FIELD_ID, STR_PARAM, String.class.getName(), "strDisp", null);
FieldDto intField = FieldTestHelper.fieldDto(INT_FIELD_ID, INT_PARAM, Integer.class.getName(), "strDisp", null);
LookupFieldDto strLookupField = new LookupFieldDto(STR_FIELD_ID, STR_PARAM, LookupFieldType.VALUE);
LookupFieldDto intLookupField = new LookupFieldDto(INT_FIELD_ID, INT_PARAM, LookupFieldType.VALUE);
LookupDto firstLookup = new LookupDto(FIRST_LOOKUP_NAME, true, false, asList(strLookupField, intLookupField), false);
LookupDto secondLookup = new LookupDto(SECOND_LOOKUP_NAME, false, false, asList(strLookupField, intLookupField), false);
when(entityService.getEntityByClassName(ENTITY_CLASS_NAME)).thenReturn(entity);
when(entityService.getEntityFields(ENTITY_ID)).thenReturn(asList(intField, strField));
when(entityService.getLookupByName(ENTITY_ID, FIRST_LOOKUP_NAME)).thenReturn(firstLookup);
when(entityService.getLookupByName(ENTITY_ID, SECOND_LOOKUP_NAME)).thenReturn(secondLookup);
Map<String, FieldDto> mapping = new HashMap<>();
mapping.put(STR_PARAM, strField);
mapping.put(INT_PARAM, intField);
when(entityService.getLookupFieldsMapping(ENTITY_ID, FIRST_LOOKUP_NAME)).thenReturn(mapping);
when(entityService.getLookupFieldsMapping(ENTITY_ID, SECOND_LOOKUP_NAME)).thenReturn(mapping);
when(bundleContext.getServiceReference(TestDataService.class.getName())).thenReturn(serviceReference);
when(bundleContext.getService(serviceReference)).thenReturn(dataService);
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class LookupExecutorTest method setUp.
@Before
public void setUp() {
LookupDto lookupDto = new LookupDto(LOOKUP_NAME, false, false, asList(new LookupFieldDto(1L, STR_FIELD_NAME, LookupFieldType.VALUE), new LookupFieldDto(2L, INT_FIELD_NAME, LookupFieldType.VALUE), new LookupFieldDto(3L, TEXTAREA_FIELD_NAME, LookupFieldType.VALUE)), false, LOOKUP_METHOD_NAME_1, asList(STR_FIELD_NAME, INT_FIELD_NAME, TEXTAREA_FIELD_NAME));
List<FieldDto> fields = Arrays.asList(FieldTestHelper.fieldDto(1L, STR_FIELD_NAME, String.class.getName(), "strFieldDisp", null), FieldTestHelper.fieldDto(2L, INT_FIELD_NAME, Integer.class.getName(), "intFieldDisp", null), FieldTestHelper.fieldDto(3L, TEXTAREA_FIELD_NAME, "mds.field.textArea", "textAreaFieldDisp", null));
lookupExecutor1 = new LookupExecutor(dataService, lookupDto, DtoHelper.asFieldMapByName(fields));
lookupDto = new LookupDto(LOOKUP_NAME, false, false, asList(new LookupFieldDto(1l, STR_FIELD_NAME, LookupFieldType.VALUE, null, false, null), new LookupFieldDto(3l, RELATED_FIELD_NAME, LookupFieldType.VALUE, null, false, "id"), new LookupFieldDto(2l, INT_FIELD_NAME, LookupFieldType.VALUE, null, false, null), new LookupFieldDto(4l, RELATED_FIELD_NAME, LookupFieldType.VALUE, null, false, "name")), false, LOOKUP_METHOD_NAME_2, asList(STR_FIELD_NAME, LookupName.buildLookupFieldName(RELATED_FIELD_NAME, ID), INT_FIELD_NAME, LookupName.buildLookupFieldName(RELATED_FIELD_NAME, NAME)));
lookupExecutor2 = new LookupExecutor(dataService, lookupDto, getFieldMapping());
}
Aggregations