use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class EntityServiceImpl method addOrUpdateLookups.
private void addOrUpdateLookups(Entity entity, Collection<LookupDto> lookups) {
for (LookupDto lookupDto : lookups) {
Lookup lookup = entity.getLookupByName(lookupDto.getLookupName());
List<String> fieldsOrder = new ArrayList<>();
List<Field> lookupFields = new ArrayList<>();
for (LookupFieldDto lookupField : lookupDto.getLookupFields()) {
String fieldName = lookupField.getName();
Field field;
field = entity.getField(fieldName);
fieldsOrder.add(LookupName.buildLookupFieldName(lookupField.getName(), lookupField.getRelatedName()));
if (field == null) {
LOGGER.error("No field {} in entity {}", fieldName, entity.getClassName());
} else {
if (!lookupFields.contains(field)) {
lookupFields.add(field);
}
}
}
lookupDto.setFieldsOrder(fieldsOrder);
if (lookup == null) {
Lookup newLookup = new Lookup(lookupDto, lookupFields);
entity.addLookup(newLookup);
} else {
lookup.update(lookupDto, lookupFields);
}
}
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class MdsDataProviderTest method setUp.
@Before
public void setUp() {
when(entityService.getEntityByClassName(Record.class.getName())).thenReturn(entity);
when(entity.getId()).thenReturn(ENTITY_ID);
FieldDto fieldDto = FieldTestHelper.fieldDto(1L, "field", String.class.getName(), "disp", null);
when(entityService.getEntityFields(ENTITY_ID)).thenReturn(asList(fieldDto));
LookupFieldDto lookupField = FieldTestHelper.lookupFieldDto(1L, "field");
LookupDto singleLookup = new LookupDto("singleLookup", true, false, asList(lookupField), false);
LookupDto multiLookup = new LookupDto("multiLookup", false, false, asList(lookupField), false);
lookupField = FieldTestHelper.lookupFieldDto(2L, "related");
lookupField.setRelatedName("stringVar");
LookupDto relatedLookup = new LookupDto("relatedLookup", false, false, asList(lookupField), false);
when(entityService.getLookupByName(ENTITY_ID, "singleLookup")).thenReturn(singleLookup);
when(entityService.getLookupByName(ENTITY_ID, "multiLookup")).thenReturn(multiLookup);
when(entityService.getLookupByName(ENTITY_ID, "relatedLookup")).thenReturn(relatedLookup);
Map<String, FieldDto> mapping = new HashMap<>();
mapping.put("field", fieldDto);
when(entityService.getLookupFieldsMapping(ENTITY_ID, "singleLookup")).thenReturn(mapping);
when(entityService.getLookupFieldsMapping(ENTITY_ID, "multiLookup")).thenReturn(mapping);
mapping = new HashMap<>();
mapping.put("related.stringVar", fieldDto);
when(entityService.getLookupFieldsMapping(ENTITY_ID, "relatedLookup")).thenReturn(mapping);
when(bundleContext.getServiceReference(LookupService.class.getName())).thenReturn(serviceReference);
when(bundleContext.getService(serviceReference)).thenReturn(new LookupService());
dataProvider = new MDSDataProvider(resourceLoader);
dataProvider.setEntityService(entityService);
dataProvider.setBundleContext(bundleContext);
MotechClassPool.registerServiceInterface(Record.class.getName(), LookupService.class.getName());
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class SchemaComparatorTest method shouldCompareLookupsWithDifferentSize.
@Test
public void shouldCompareLookupsWithDifferentSize() {
LookupDto lookupDto = new LookupDto();
lookupDto.setReadOnly(true);
entityLookups = asList(lookupDto);
newLookups = asList(lookupDto, lookupDto);
when(entityService.getEntityLookups(ENTITY_ID)).thenReturn(entityLookups);
boolean lookupsDiffer = schemaComparator.lookupsDiffer(ENTITY_ID, newLookups);
assertTrue(lookupsDiffer);
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class SchemaComparatorTest method shouldCompareLookupsWithDifferentFields.
@Test
public void shouldCompareLookupsWithDifferentFields() {
LookupDto lookupDto = new LookupDto("lookupName", true, true, new ArrayList<LookupFieldDto>(), true, "methodName", new ArrayList<String>());
LookupFieldDto lookupFieldDto = new LookupFieldDto(1L, "name", LookupFieldType.RANGE, "customOperator", true, "relatedName");
lookupDto.setLookupFields(asList(lookupFieldDto));
LookupDto lookupDto2 = new LookupDto("lookupName", true, true, new ArrayList<LookupFieldDto>(), true, "methodName", new ArrayList<String>());
LookupFieldDto lookupFieldDto2 = new LookupFieldDto(1L, "name", LookupFieldType.RANGE, "customOperator", true, "differentRelatedName");
lookupDto2.setLookupFields(asList(lookupFieldDto2));
entityLookups = asList(lookupDto);
newLookups = asList(lookupDto2);
when(entityService.getEntityLookups(ENTITY_ID)).thenReturn(entityLookups);
boolean lookupsDiffer = schemaComparator.lookupsDiffer(ENTITY_ID, newLookups);
assertTrue(lookupsDiffer);
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class SchemaComparatorTest method shouldCompareTheSameLookups.
@Test
public void shouldCompareTheSameLookups() {
LookupDto lookupDto = new LookupDto("lookupName", true, true, new ArrayList<LookupFieldDto>(), true, "methodName", new ArrayList<String>());
entityLookups = asList(lookupDto);
newLookups = asList(lookupDto);
when(entityService.getEntityLookups(ENTITY_ID)).thenReturn(entityLookups);
boolean lookupsDiffer = schemaComparator.lookupsDiffer(ENTITY_ID, newLookups);
assertFalse(lookupsDiffer);
}
Aggregations