use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class InstanceServiceImpl method countRecordsByLookup.
@Override
public long countRecordsByLookup(Long entityId, String lookupName, Map<String, Object> lookupMap) {
EntityDto entity = getEntity(entityId);
validateCredentialsForReading(entity);
LookupDto lookup = getLookupByName(entityId, lookupName);
Map<String, FieldDto> fieldMap = entityService.getLookupFieldsMapping(entityId, lookupName);
MotechDataService service = getServiceForEntity(entity);
try {
LookupExecutor lookupExecutor = new LookupExecutor(service, lookup, fieldMap);
return lookupExecutor.executeCount(lookupMap);
} catch (RuntimeException e) {
throw new LookupExecutionException(e, LOOKUP_EXCEPTION_MESSAGE_KEY);
}
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class InstanceServiceImpl method getEntityRecordsFromLookup.
@Override
public List<BasicEntityRecord> getEntityRecordsFromLookup(Long entityId, String lookupName, Map<String, Object> lookupMap, QueryParams queryParams) {
EntityDto entity = getEntity(entityId);
validateCredentialsForReading(entity);
LookupDto lookup = getLookupByName(entityId, lookupName);
List<FieldDto> fields = entityService.getEntityFieldsForUI(entityId);
Map<String, FieldDto> fieldMap = entityService.getLookupFieldsMapping(entityId, lookupName);
MotechDataService service = getServiceForEntity(entity);
try {
LookupExecutor lookupExecutor = new LookupExecutor(service, lookup, fieldMap);
Object result = lookupExecutor.execute(lookupMap, queryParams);
if (lookup.isSingleObjectReturn()) {
BasicEntityRecord record = instanceToBasicRecord(result, entity, fields, service, EntityType.STANDARD, getRelatedEntitiesFields(fields));
return (record == null) ? new ArrayList<BasicEntityRecord>() : Collections.singletonList(record);
} else {
List instances = (List) result;
return instancesToBasicRecords(instances, entity, fields, service, EntityType.STANDARD);
}
} catch (LookupExecutorException e) {
if (e.getMessageKey() != null) {
throw new LookupExecutionException(e, e.getMessageKey());
} else {
throw new LookupExecutionException(e, LOOKUP_EXCEPTION_MESSAGE_KEY);
}
} catch (RuntimeException e) {
throw new LookupExecutionException(e, LOOKUP_EXCEPTION_MESSAGE_KEY);
}
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class MdsDdeBundleIT method shouldLoadLookupsFromFile.
@Test
public void shouldLoadLookupsFromFile() {
Entry entryOne = new Entry();
entryOne.setValue("someValue");
entryDataService.create(entryOne);
Entry entryTwo = new Entry();
entryTwo.setValue("someValueTwo");
entryDataService.create(entryTwo);
EntityDto entity = entityService.getEntityByClassName("org.motechproject.mds.test.domain.editablelookups.Entry");
List<LookupDto> lookups = entityService.getEntityLookups(entity.getId());
assertEquals(1, lookups.size());
assertEquals("Find by Value", lookups.get(0).getLookupName());
Map<String, String> params = new HashMap<>();
params.put("value", entryOne.getValue());
List<Entry> lookupResult = lookupService.findMany(entity.getClassName(), lookups.get(0).getLookupName(), params);
assertEquals(1, lookupResult.size());
assertEquals(entryOne.getValue(), lookupResult.get(0).getValue());
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class SchemaComparator method lookupsDiffer.
/**
* Returns true if lookups are different, false otherwise.
* It compares only read-only lookups, that created
* in the code by developer.
*
* @param entityId the id of entity
* @param newLookups the newLookups defined in the code
* @return true if lookups are different, false otherwise.
*/
public boolean lookupsDiffer(Long entityId, List<LookupDto> newLookups) {
List<LookupDto> existingLookups = entityService.getEntityLookups(entityId);
Map<String, LookupDto> entityLookupsMappings = new HashMap<>();
for (LookupDto entityLookup : existingLookups) {
if (entityLookup.isReadOnly()) {
entityLookupsMappings.put(entityLookup.getLookupName(), entityLookup);
}
}
if (entityLookupsMappings.size() != newLookups.size()) {
return true;
}
for (LookupDto lookup : newLookups) {
if (!lookupEquals(lookup, entityLookupsMappings.get(lookup.getLookupName()))) {
return true;
}
}
return false;
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class EntityInfrastructureBuilderTest method shouldCreateCodeForClassWithLookups.
@Test
public void shouldCreateCodeForClassWithLookups() throws Exception {
MDSClassLoader mdsClassLoaderImpl = MDSClassLoader.getStandaloneInstance(getClass().getClassLoader());
EntityDto entity = new EntityDto(SampleWithLookups.class.getName());
entity.setMaxFetchDepth(-1);
LookupDto lookup = new LookupDto();
lookup.setLookupName("testLookup");
lookup.setMethodName("testLookupMethod");
FieldDto testField = fieldDto("TestField", "testDispName", String.class);
FieldDto testField2 = fieldDto("TestField2", "DisplayName with space", String.class);
FieldDto dateField = fieldDto("dateField", "Display names should not affect methods", DateTime.class);
FieldDto timeField = fieldDto("timeField", Time.class);
when(schemaHolder.getFieldByName(SampleWithLookups.class.getName(), "TestField")).thenReturn(testField);
when(schemaHolder.getFieldByName(SampleWithLookups.class.getName(), "TestField2")).thenReturn(testField2);
when(schemaHolder.getFieldByName(SampleWithLookups.class.getName(), "dateField")).thenReturn(dateField);
when(schemaHolder.getFieldByName(SampleWithLookups.class.getName(), "timeField")).thenReturn(timeField);
lookup.setFieldsOrder(asList("TestField", "TestField2", "dateField", "timeField"));
lookup.setSingleObjectReturn(true);
when(schemaHolder.getLookups(entity)).thenReturn(singletonList(lookup));
List<LookupFieldDto> lookupFields = new ArrayList<>();
lookupFields.add(new LookupFieldDto("TestField", LookupFieldType.VALUE));
lookupFields.add(new LookupFieldDto("TestField2", LookupFieldType.VALUE));
lookupFields.add(new LookupFieldDto("dateField", LookupFieldType.RANGE));
lookupFields.add(new LookupFieldDto("timeField", LookupFieldType.SET));
lookup.setLookupFields(lookupFields);
List<ClassData> data = entityInfrastructureBuilder.buildInfrastructure(entity, schemaHolder);
for (ClassData classData : data) {
mdsClassLoaderImpl.safeDefineClass(classData.getClassName(), classData.getBytecode());
}
verifySingleLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_SERVICE));
verifySingleLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_INTERFACE));
verifyCountLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_SERVICE));
verifyCountLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_INTERFACE));
// lookup with multiple return
lookup.setSingleObjectReturn(false);
mdsClassLoaderImpl = MDSClassLoader.getStandaloneInstance(getClass().getClassLoader());
data = entityInfrastructureBuilder.buildInfrastructure(entity, schemaHolder);
for (ClassData classData : data) {
mdsClassLoaderImpl.safeDefineClass(classData.getClassName(), classData.getBytecode());
}
verifyMultiReturnLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_SERVICE));
verifyMultiReturnLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_INTERFACE));
verifyCountLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_SERVICE));
verifyCountLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_INTERFACE));
verifyFetchDepthInRepository(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_REPOSITORY), -1);
}
Aggregations