use of org.motechproject.mds.domain.RelationshipHolder in project motech by motech.
the class InstancesReader method readRelationshipProperty.
private void readRelationshipProperty(Field field, Object instance) throws IOException {
RelationshipHolder relationshipHolder = new RelationshipHolder(field);
if (relationshipHolder.isManyToOne() || relationshipHolder.isOneToOne()) {
Long refId = objectReader.readLong();
if (null != refId) {
Object relatedInstance = importContext.getInstanceOfEntity(relationshipHolder.getRelatedClass(), refId);
PropertyUtil.safeSetProperty(instance, field.getName(), relatedInstance);
}
} else if (relationshipHolder.isManyToMany() || relationshipHolder.isOneToMany()) {
List<Long> refIds = objectReader.readLongArray();
if (null != refIds) {
Collection<Object> relatedInstances = importContext.getInstancesOfEntity(relationshipHolder.getRelatedClass(), refIds);
PropertyUtil.safeSetCollectionProperty(instance, field.getName(), relatedInstances);
}
}
}
use of org.motechproject.mds.domain.RelationshipHolder in project motech by motech.
the class EntityMetadataBuilderImpl method fixCollectionMetadata.
private void fixCollectionMetadata(CollectionMetadata collMd, FieldDto field) {
String elementType = collMd.getElementType();
RelationshipHolder holder = new RelationshipHolder(field);
if (null != MotechClassPool.getEnhancedClassData(elementType)) {
collMd.setEmbeddedElement(false);
}
if (holder.isOneToMany() && holder.getRelatedField() == null) {
collMd.setDependentElement(holder.isCascadeDelete());
}
}
use of org.motechproject.mds.domain.RelationshipHolder 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.domain.RelationshipHolder 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();
}
use of org.motechproject.mds.domain.RelationshipHolder in project motech by motech.
the class ActionHandlerServiceImpl method setRelationshipInstanceProperty.
private void setRelationshipInstanceProperty(Object instance, Field field, Object value) throws ActionHandlerException {
RelationshipHolder relationshipHolder = new RelationshipHolder(field);
try {
if (null != value) {
String relatedClassName = relationshipHolder.getRelatedClass();
MotechDataService relatedClassDataService = getEntityDataService(relatedClassName);
if (relationshipHolder.isManyToOne() || relationshipHolder.isOneToOne()) {
Object relatedInstance = getRelatedInstance(relatedClassDataService, value);
PropertyUtil.safeSetProperty(instance, field.getName(), relatedInstance);
} else if (relationshipHolder.isManyToMany() || relationshipHolder.isOneToMany()) {
List<Object> relatedInstances = getRelatedInstances(relatedClassDataService, value);
PropertyUtil.safeSetCollectionProperty(instance, field.getName(), relatedInstances);
}
} else {
PropertyUtil.safeSetProperty(instance, field.getName(), null);
}
} catch (RuntimeException e) {
throw new ActionHandlerException("Cannot set instance property " + field.getName() + " with value " + value, e);
}
}
Aggregations