use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class LookupProcessor method process.
@Override
protected void process(AnnotatedElement annotatedElement) {
Method method = (Method) annotatedElement;
Class returnType = method.getReturnType();
String returnClassName = returnType.getName();
boolean singleObjectReturn = true;
if (returnType.isArray() || Collection.class.isAssignableFrom(returnType)) {
singleObjectReturn = false;
returnClassName = determineGenericClass(method.getGenericReturnType().toString());
}
EntityDto entity = findEntityByClassName(returnClassName);
if (entity == null) {
LOGGER.error("There's no matching entity for the resolved return type of the lookup" + "method: {}; Resolved return type: {}", method.getName(), returnClassName);
return;
}
LOGGER.debug("Found entity class by the return type of lookup method: {}", entity.getName());
Lookup annotation = ReflectionsUtil.findAnnotation(method, Lookup.class);
String lookupName = generateLookupName(annotation.name(), method.getName());
List<LookupFieldDto> lookupFields = findLookupFields(method, entity);
boolean restExposed = processRestExposed(method);
boolean indexRequired = annotation.indexRequired();
verifyLookupParameters(method, returnClassName, lookupName, lookupFields, method.getParameterTypes());
LookupDto lookup = new LookupDto();
lookup.setSingleObjectReturn(singleObjectReturn);
lookup.setLookupName(lookupName);
lookup.setLookupFields(lookupFields);
lookup.setReadOnly(true);
lookup.setMethodName(method.getName());
lookup.setIndexRequired(indexRequired);
if (!restOptionsModifiedByUser(entity)) {
lookup.setExposedViaRest(restExposed);
}
if (!getElements().containsKey(returnClassName)) {
put(returnClassName, new ArrayList<>());
}
getElement(returnClassName).add(lookup);
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class EntityMetadataBuilderImpl method processField.
public void processField(ClassMetadata cmd, ClassData classData, EntityDto entity, EntityType entityType, Class<?> definition, String fieldName, FieldDto field, SchemaHolder schemaHolder) {
// Metadata for ID field has been added earlier in addIdField() method
if (!fieldName.equals(ID_FIELD_NAME)) {
FieldMetadata fmd = null;
if (isFieldNotInherited(fieldName, entity, schemaHolder)) {
fmd = setFieldMetadata(cmd, classData, entity, entityType, field, definition);
}
// when field is in Lookup, we set field metadata indexed to retrieve instance faster
if (!field.getLookups().isEmpty() && entityType.equals(EntityType.STANDARD)) {
if (fmd == null) {
String inheritedFieldName = ClassName.getSimpleName(entity.getSuperClass()) + "." + fieldName;
fmd = cmd.newFieldMetadata(inheritedFieldName);
}
if (!isBlobOrClob(field)) {
IndexMetadata imd = getOrCreateIndexMetadata(fmd);
imd.setName(KeyNames.lookupIndexKeyName(entity.getName(), entity.getId(), fieldName, entityType));
}
fmd.setIndexed(false);
for (LookupDto lookupDto : field.getLookups()) {
if (lookupDto.isIndexRequired()) {
fmd.setIndexed(true);
break;
}
}
}
if (fmd != null) {
customizeFieldMd(fmd, entity, field, entityType, definition);
}
}
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class EntityInfrastructureBuilderImpl method getServiceCode.
private byte[] getServiceCode(String serviceClassName, String interfaceClassName, String className, EntityDto entity, SchemaHolder schemaHolder) {
try {
CtClass superClass = classPool.getCtClass(TransactionalMotechDataService.class.getName());
superClass.setGenericSignature(getGenericSignature(className));
CtClass serviceInterface = classPool.getCtClass(interfaceClassName);
CtClass serviceClass = createOrRetrieveClass(serviceClassName, superClass);
// add the interface if its not already there
if (!JavassistUtil.hasInterface(serviceClass, serviceInterface)) {
serviceClass.addInterface(serviceInterface);
}
List<CtMethod> methods = new ArrayList<>();
// a count method for the lookup
if (null != entity) {
List<LookupDto> lookups = schemaHolder.getLookups(entity);
for (LookupDto lookup : lookups) {
for (LookupType lookupType : LookupType.values()) {
LookupBuilder lookupBuilder = new LookupBuilder(entity, lookup, serviceClass, lookupType, schemaHolder);
methods.add(lookupBuilder.buildMethod());
}
}
}
// clear lookup methods before adding the new ones
removeExistingMethods(serviceClass);
for (CtMethod method : methods) {
serviceClass.addMethod(method);
}
return serviceClass.toBytecode();
} catch (NotFoundException | IOException | CannotCompileException e) {
throw new EntityInfrastructureException(serviceClassName, e);
}
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class Lookup method toDto.
public LookupDto toDto() {
List<LookupFieldDto> lookupFields = new ArrayList<>();
for (String lookupFieldName : getFieldsOrder()) {
Field field = getLookupFieldByName(LookupName.getFieldName(lookupFieldName));
LookupFieldType lookupFieldType = LookupFieldType.VALUE;
if (isRangeParam(lookupFieldName)) {
lookupFieldType = LookupFieldType.RANGE;
} else if (isSetParam(lookupFieldName)) {
lookupFieldType = LookupFieldType.SET;
}
String customOperator = getCustomOperators().get(lookupFieldName);
boolean useGenericParam = toBoolean(getUseGenericParams().get(lookupFieldName));
if (field != null) {
LookupFieldDto lookupField = new LookupFieldDto(field.getId(), field.getName(), lookupFieldType, customOperator, useGenericParam, LookupName.getRelatedFieldName(lookupFieldName));
lookupFields.add(lookupField);
} else {
throw new LookupWrongFieldNameException(String.format("Can't create LookupFieldDto. Field with given name %s does not exist in %s lookup", lookupFieldName, lookupName));
}
}
return new LookupDto(id, lookupName, singleObjectReturn, exposedViaRest, lookupFields, readOnly, methodName, fieldsOrder, indexRequired);
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class Field method toDto.
public FieldDto toDto() {
FieldBasicDto basic = new FieldBasicDto(displayName, name, required, unique, parseDefaultValue(), tooltip, placeholder);
TypeDto typeDto = null;
List<MetadataDto> metaDto = new ArrayList<>();
for (FieldMetadata meta : metadata) {
metaDto.add(meta.toDto());
}
FieldValidationDto validationDto = null;
if (CollectionUtils.isNotEmpty(validations)) {
validationDto = new FieldValidationDto();
for (FieldValidation validation : validations) {
validationDto.addCriterion(validation.toDto());
}
}
List<SettingDto> settingsDto = new ArrayList<>();
for (FieldSetting setting : settings) {
settingsDto.add(setting.toDto());
}
List<LookupDto> lookupDtos = new ArrayList<>();
for (Lookup lookup : getLookups()) {
lookupDtos.add(lookup.toDto());
}
if (typeDto == null && type != null) {
typeDto = type.toDto();
}
return new FieldDto(id, entity == null ? null : entity.getId(), typeDto, basic, readOnly, nonEditable, nonDisplayable, uiFilterable, uiChanged, metaDto, validationDto, settingsDto, lookupDtos);
}
Aggregations