use of org.motechproject.mds.dto.EntityDto in project motech by motech.
the class InstanceServiceImpl method getSingleTrashRecord.
@Override
public EntityRecord getSingleTrashRecord(Long entityId, Long instanceId) {
EntityDto entity = getEntity(entityId);
validateCredentialsForReading(entity);
MotechDataService service = getServiceForEntity(entity);
List<FieldDto> fields = entityService.getEntityFieldsForUI(entityId);
Object instance = trashService.findTrashById(instanceId, entity.getClassName());
Map<String, List<FieldDto>> relatedEntitiesFields = getRelatedEntitiesFields(fields);
return instanceToRecord(instance, entity, fields, service, EntityType.TRASH, relatedEntitiesFields);
}
use of org.motechproject.mds.dto.EntityDto 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.EntityDto in project motech by motech.
the class EntitySorter method sortByInheritance.
/**
* Takes a list of entities and sorts them by the inheritance tree. The entities that extend
* the Object class or MdsEntity class will be moved to the beggining of the list. After that,
* the entites that are already present on the list will be added, up the inheritance tree.
*
* @param list Initial list of entities to sort
* @return List of entities, sorted by inheritance tree
*/
public static List<EntityDto> sortByInheritance(List<EntityDto> list) {
List<EntityDto> sorted = new ArrayList<>(list.size());
// firstly we add entities with base class equal to Object class or MdsEntity class
for (Iterator<EntityDto> iterator = list.iterator(); iterator.hasNext(); ) {
EntityDto entity = iterator.next();
if (entity.isBaseEntity()) {
sorted.add(entity);
iterator.remove();
}
}
// we do that after all entities will be added to sorted list
while (!list.isEmpty()) {
for (Iterator<EntityDto> iterator = list.iterator(); iterator.hasNext(); ) {
final EntityDto entity = iterator.next();
EntityDto superClass = (EntityDto) CollectionUtils.find(sorted, new Predicate() {
@Override
public boolean evaluate(Object object) {
return object instanceof EntityDto && ((EntityDto) object).getClassName().equals(entity.getSuperClass());
}
});
if (null != superClass) {
sorted.add(entity);
iterator.remove();
}
}
}
return sorted;
}
use of org.motechproject.mds.dto.EntityDto in project motech by motech.
the class LookupBuilder method build.
private CtMethod build(boolean body) throws CannotCompileException, NotFoundException {
Collection<String> paramCollection = new ArrayList<>();
List<String> fieldOrder = lookup.getFieldsOrder();
for (int i = 0; i < fieldOrder.size(); i++) {
String fieldName = fieldOrder.get(i);
FieldDto field = getLookupField(fieldName);
// don't use fieldName for fetching fields, as it can contain dots, etc.
LookupFieldDto lookupField = lookup.getLookupField(field.getBasic().getName());
FieldDto relationField = null;
EntityDto relatedEntity = null;
if (fieldOrder.get(i).contains(".")) {
relatedEntity = schemaHolder.getEntityByClassName(new RelationshipHolder(field).getRelatedClass());
relationField = schemaHolder.getFieldByName(relatedEntity, LookupName.getRelatedFieldName(fieldOrder.get(i)));
}
String type = getTypeForParam(i, resolveEntity(entity, relatedEntity), resolveField(field, relationField), lookupField);
String param = String.format("%s %s", type, fieldOrder.get(i).replace(".", ""));
paramCollection.add(param);
}
// query params at the end for ordering/paging
if (WITH_QUERY_PARAMS == lookupType) {
String queryParam = String.format("%s queryParams", QueryParams.class.getName());
paramCollection.add(queryParam);
}
String params = StringUtils.join(paramCollection, ", ");
String signature = String.format("public %s %s(%s)", returnType(), lookupName, params);
String methodAsString = body ? String.format("%s{%s}", signature, body()) : String.format("%s;", signature);
String generic = buildGenericSignature();
CtMethod method = CtNewMethod.make(methodAsString, definition);
method.setGenericSignature(generic);
return method;
}
use of org.motechproject.mds.dto.EntityDto in project motech by motech.
the class LookupBuilder method buildGenericSignature.
private String buildGenericSignature() throws NotFoundException {
// we must build generic signatures for lookup methods
// an example signature for the method signature
// List<org.motechproject.mds.Test> method(String p1, Integer p2)
// is
// cmt -- (Ljava/lang/String;Ljava/lang/Integer;)Ljava/util/List<Lorg/motechproject/mds/Test;>;
StringBuilder sb = new StringBuilder();
sb.append('(');
List<String> fieldsOrder = lookup.getFieldsOrder();
for (int i = 0; i < fieldsOrder.size(); ++i) {
String fieldName = fieldsOrder.get(i);
FieldDto field = getLookupField(fieldName);
// don't use fieldName for fetching fields, as it can contain dots, etc.
LookupFieldDto lookupField = lookup.getLookupField(field.getBasic().getName());
FieldDto relationField = null;
EntityDto relatedEntity = null;
if (fieldsOrder.get(i).contains(".")) {
relatedEntity = schemaHolder.getEntityByClassName(new RelationshipHolder(field).getRelatedClass());
relationField = schemaHolder.getFieldByName(relatedEntity, LookupName.getRelatedFieldName(fieldsOrder.get(i)));
}
String paramType = getTypeForParam(i, resolveEntity(entity, relatedEntity), resolveField(field, relationField), lookupField);
String genericType;
TypeDto type = resolveField(field, relationField).getType();
if (type.isCombobox()) {
ComboboxHolder holder = new ComboboxHolder(resolveEntity(entity, relatedEntity), resolveField(field, relationField));
genericType = holder.getUnderlyingType();
} else {
genericType = type.getTypeClass();
}
if (StringUtils.equals(paramType, genericType) || TypeHelper.isPrimitive(paramType)) {
// simple parameter
sb.append(JavassistUtil.toGenericParam(paramType));
} else {
// we wrap in a range/set or a different wrapper
sb.append(JavassistUtil.genericSignature(paramType, genericType));
}
}
sb.append(')');
if (lookup.isSingleObjectReturn()) {
sb.append(JavassistUtil.toGenericParam(className));
} else {
sb.append(JavassistUtil.genericSignature(List.class.getName(), className));
}
return sb.toString();
}
Aggregations