Search in sources :

Example 11 with MetadataDto

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");
}
Also used : CollectionMetadata(javax.jdo.metadata.CollectionMetadata) FieldMetadata(javax.jdo.metadata.FieldMetadata) MetadataDto(org.motechproject.mds.dto.MetadataDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with MetadataDto

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;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 13 with MetadataDto

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);
}
Also used : ComboboxHolder(org.motechproject.mds.web.domain.ComboboxHolder) MDSClassLoader(org.motechproject.mds.util.MDSClassLoader) TypeDto(org.motechproject.mds.dto.TypeDto) Method(java.lang.reflect.Method) MetadataDto(org.motechproject.mds.dto.MetadataDto)

Example 14 with MetadataDto

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()));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) MetadataDto(org.motechproject.mds.dto.MetadataDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Test(org.junit.Test)

Example 15 with MetadataDto

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;
}
Also used : FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) TypeDto(org.motechproject.mds.dto.TypeDto) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldInfo(org.motechproject.mds.entityinfo.FieldInfo) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Aggregations

MetadataDto (org.motechproject.mds.dto.MetadataDto)28 FieldDto (org.motechproject.mds.dto.FieldDto)22 Test (org.junit.Test)9 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)8 TypeDto (org.motechproject.mds.dto.TypeDto)7 ArrayList (java.util.ArrayList)6 SettingDto (org.motechproject.mds.dto.SettingDto)6 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)5 LinkedList (java.util.LinkedList)3 FieldMetadata (javax.jdo.metadata.FieldMetadata)3 EntityDto (org.motechproject.mds.dto.EntityDto)3 FieldValidationDto (org.motechproject.mds.dto.FieldValidationDto)3 LookupDto (org.motechproject.mds.dto.LookupDto)3 Method (java.lang.reflect.Method)2 List (java.util.List)2 ForeignKeyMetadata (javax.jdo.metadata.ForeignKeyMetadata)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 ClassData (org.motechproject.mds.domain.ClassData)2 ValidationCriterionDto (org.motechproject.mds.dto.ValidationCriterionDto)2 ResultActions (org.springframework.test.web.server.ResultActions)2