use of org.motechproject.mds.dto.MetadataDto in project motech by motech.
the class EntityMetadataBuilderTest method shouldAddOneToManyRelationshipMetadata.
@Test
public void shouldAddOneToManyRelationshipMetadata() {
FieldDto oneToManyField = fieldDto("oneToManyName", OneToManyRelationship.class);
oneToManyField.addMetadata(new MetadataDto(RELATED_CLASS, "org.motechproject.test.MyClass"));
FieldMetadata fmd = mock(FieldMetadata.class);
CollectionMetadata collMd = mock(CollectionMetadata.class);
when(entity.getName()).thenReturn(ENTITY_NAME);
when(entity.getId()).thenReturn(2L);
when(schemaHolder.getFields(entity)).thenReturn(singletonList(oneToManyField));
when(entity.getTableName()).thenReturn(TABLE_NAME);
when(jdoMetadata.newPackageMetadata(PACKAGE)).thenReturn(packageMetadata);
when(packageMetadata.newClassMetadata(ENTITY_NAME)).thenReturn(classMetadata);
when(classMetadata.newFieldMetadata("oneToManyName")).thenReturn(fmd);
when(fmd.getCollectionMetadata()).thenReturn(collMd);
when(fmd.getName()).thenReturn("oneToManyName");
entityMetadataBuilder.addEntityMetadata(jdoMetadata, entity, Sample.class, schemaHolder);
verifyCommonClassMetadata();
verify(fmd).setDefaultFetchGroup(true);
verify(collMd).setEmbeddedElement(false);
verify(collMd).setSerializedElement(false);
verify(collMd).setElementType("org.motechproject.test.MyClass");
}
use of org.motechproject.mds.dto.MetadataDto in project motech by motech.
the class InstanceServiceImpl method getRelatedEntitiesFields.
private Map<String, List<FieldDto>> getRelatedEntitiesFields(List<FieldDto> fields) {
Map<String, List<FieldDto>> relatedEntitiesFields = new HashMap<>();
for (FieldDto field : fields) {
MetadataDto entityMetadata = field.getMetadata(Constants.MetadataKeys.RELATED_CLASS);
if (entityMetadata != null) {
String className = entityMetadata.getValue();
relatedEntitiesFields.put(className, getEntityFieldsByClassName(className));
}
}
return relatedEntitiesFields;
}
use of org.motechproject.mds.dto.MetadataDto in project motech by motech.
the class InstanceServiceImpl method setProperty.
private void setProperty(Object instance, FieldRecord fieldRecord, MotechDataService service, Long deleteValueFieldId, boolean retainId) throws NoSuchMethodException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
String fieldName = fieldRecord.getName();
TypeDto type = getType(fieldRecord);
String methodName = "set" + StringUtils.capitalize(fieldName);
ComboboxHolder holder = type.isCombobox() ? new ComboboxHolder(instance, fieldRecord) : null;
String methodParameterType = getMethodParameterType(type, holder);
ClassLoader classLoader = instance.getClass().getClassLoader();
Class<?> parameterType;
Object parsedValue;
if (Byte[].class.getName().equals(methodParameterType) || byte[].class.getName().equals(methodParameterType)) {
parameterType = getCorrectByteArrayType(methodParameterType);
parsedValue = parseBlobValue(fieldRecord, service, fieldName, deleteValueFieldId, instance);
} else {
parameterType = classLoader.loadClass(methodParameterType);
parsedValue = parseValue(holder, methodParameterType, fieldRecord, classLoader);
}
MetadataDto versionMetadata = fieldRecord.getMetadata(Constants.MetadataKeys.VERSION_FIELD);
validateNonEditableField(fieldRecord, instance, parsedValue, versionMetadata);
Method method = MethodUtils.getAccessibleMethod(instance.getClass(), methodName, parameterType);
if (method == null && TypeHelper.hasPrimitive(parameterType)) {
method = MethodUtils.getAccessibleMethod(instance.getClass(), methodName, TypeHelper.getPrimitive(parameterType));
// if the setter is for a primitive, but we have a null, we leave the default
if (method != null && parsedValue == null) {
return;
}
}
invokeMethod(method, instance, parsedValue, methodName, fieldName);
setTransactionVersion(instance, fieldRecord, retainId, versionMetadata);
}
use of org.motechproject.mds.dto.MetadataDto in project motech by motech.
the class EntityServiceContextIT method shouldAddLookupWithRelatedField.
@Test
public void shouldAddLookupWithRelatedField() {
EntityDto entityWithLookup = new EntityDto();
entityWithLookup.setName("entityWithLookup");
entityWithLookup = entityService.createEntity(entityWithLookup);
EntityDto relatedEntity = new EntityDto();
relatedEntity.setName("relatedEntity");
relatedEntity = entityService.createEntity(relatedEntity);
FieldDto stringField = FieldTestHelper.fieldDto("stringField", String.class);
FieldDto nameField = FieldTestHelper.fieldDto("nameField", String.class);
FieldDto lengthField = FieldTestHelper.fieldDto("lengthField", Long.class);
FieldDto relationField = FieldTestHelper.fieldDto("relatedField", TypeDto.ONE_TO_MANY_RELATIONSHIP.getTypeClass());
relationField.addMetadata(new MetadataDto(RELATED_CLASS, relatedEntity.getClassName()));
entityService.addFields(entityWithLookup, asList(stringField, relationField));
entityService.addFields(relatedEntity, asList(nameField, lengthField));
List<LookupFieldDto> lookupFieldDtos = lookupFieldsFromNames("relatedField.nameField", "relatedField.lengthField", "stringField");
LookupDto lookup = new LookupDto("lookup", false, false, lookupFieldDtos, true);
entityService.addLookups(entityWithLookup.getId(), lookup);
LookupDto lookupFromDb = entityService.getLookupByName(entityWithLookup.getId(), "lookup");
assertNotNull(lookupFromDb);
assertEquals(asList("relatedField.nameField", "relatedField.lengthField", "stringField"), lookupFromDb.getFieldsOrder());
assertEquals(asList("relatedField", "relatedField", "stringField"), extract(lookupFromDb.getLookupFields(), on(LookupFieldDto.class).getName()));
}
use of org.motechproject.mds.dto.MetadataDto in project motech by motech.
the class FieldTestHelper method fieldInfo.
public static FieldInfo fieldInfo(String name, Class typeClass, boolean required, boolean restExposed, boolean autoGenerated) {
FieldInfo field = new FieldInfo();
FieldDto fieldDto = new FieldDto();
fieldDto.setBasic(new FieldBasicDto());
fieldDto.getBasic().setName(name);
fieldDto.getBasic().setDisplayName(name + " disp");
fieldDto.getBasic().setRequired(required);
TypeDto type = new TypeDto();
type.setTypeClass(typeClass.getName());
fieldDto.setType(type);
field.setRestExposed(restExposed);
if (autoGenerated) {
fieldDto.addMetadata(new MetadataDto(Constants.Util.AUTO_GENERATED, Constants.Util.TRUE));
}
field.setField(fieldDto);
field.getTypeInfo().setType(typeClass.getName());
return field;
}
Aggregations