use of org.motechproject.mds.dto.LookupFieldDto in project motech by motech.
the class LookupExecutor method getLookupArgs.
private List<Object> getLookupArgs(Map<String, ?> paramMap) {
List<Object> args = new ArrayList<>();
for (LookupFieldDto lookupField : lookup.getLookupFields()) {
FieldDto field = fieldsByName.get(lookupField.getLookupFieldName());
if (field == null) {
throw new FieldNotFoundException(entityClass.getName(), lookupField.getName());
}
Object val = paramMap.get(lookupField.getLookupFieldName());
String typeClass = getTypeClass(field);
String genericType = getGenericTypeClass(field);
Object arg;
if (lookupField.getType() == LookupFieldType.RANGE) {
arg = TypeHelper.toRange(val, typeClass);
} else if (lookupField.getType() == LookupFieldType.SET) {
arg = TypeHelper.toSet(val, typeClass, classLoader);
} else {
arg = TypeHelper.parse(val, lookupField.isUseGenericParam() ? genericType : typeClass, classLoader);
}
args.add(arg);
}
return args;
}
use of org.motechproject.mds.dto.LookupFieldDto in project motech by motech.
the class Entity method addLookup.
public void addLookup(Lookup lookup) {
Lookup existing = getLookupByName(lookup.getLookupName());
if (existing == null) {
lookup.setEntity(this);
getLookups().add(lookup);
} else {
LookupDto lookupDto = lookup.toDto();
List<Field> lookupFields = new ArrayList<>();
for (LookupFieldDto lookupField : lookupDto.getLookupFields()) {
lookupFields.add(getField(lookupField.getId()));
}
existing.update(lookupDto, lookupFields);
}
}
use of org.motechproject.mds.dto.LookupFieldDto in project motech by motech.
the class Entity method updateIndexes.
public void updateIndexes(List<LookupDto> indexes) {
// deletion
Iterator<Lookup> it = getLookups().iterator();
while (it.hasNext()) {
Lookup lookup = it.next();
boolean inNewList = false;
for (LookupDto lookupDto : indexes) {
if (Objects.equals(lookup.getId(), lookupDto.getId())) {
inNewList = true;
break;
}
}
if (!inNewList) {
it.remove();
}
}
for (LookupDto lookupDto : indexes) {
Lookup lookup = getLookupById(lookupDto.getId());
List<Field> lookupFields = new ArrayList<>();
List<String> lookupFieldsOrder = new ArrayList<>();
for (LookupFieldDto lookupField : lookupDto.getLookupFields()) {
Field field = getField(lookupField.getId());
if (!lookupFields.contains(field)) {
lookupFields.add(field);
}
String lookupFieldName = LookupName.buildLookupFieldName(field.getName(), lookupField.getRelatedName());
lookupFieldsOrder.add(lookupFieldName);
lookupField.setName(lookupFieldName);
}
lookupDto.setFieldsOrder(lookupFieldsOrder);
if (lookup == null) {
Lookup newLookup = new Lookup(lookupDto, lookupFields);
addLookup(newLookup);
} else {
lookup.update(lookupDto, lookupFields);
}
}
}
use of org.motechproject.mds.dto.LookupFieldDto 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.LookupFieldDto 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());
}
Aggregations