Search in sources :

Example 11 with FieldMetadata

use of javax.jdo.metadata.FieldMetadata in project motech by motech.

the class EntityMetadataBuilderTest method shouldMarkEudeFieldsAsUnique.

@Test
public void shouldMarkEudeFieldsAsUnique() {
    when(entity.getName()).thenReturn(ENTITY_NAME);
    when(jdoMetadata.newPackageMetadata(anyString())).thenReturn(packageMetadata);
    when(packageMetadata.newClassMetadata(anyString())).thenReturn(classMetadata);
    FieldDto eudeField = mock(FieldDto.class);
    FieldBasicDto eudeBasic = mock(FieldBasicDto.class);
    when(eudeField.getBasic()).thenReturn(eudeBasic);
    when(eudeBasic.getName()).thenReturn("uniqueField");
    when(eudeField.isReadOnly()).thenReturn(false);
    when(eudeBasic.isUnique()).thenReturn(true);
    when(eudeField.getType()).thenReturn(TypeDto.STRING);
    FieldDto ddeField = mock(FieldDto.class);
    FieldBasicDto ddeBasic = mock(FieldBasicDto.class);
    when(ddeField.getBasic()).thenReturn(ddeBasic);
    when(ddeBasic.getName()).thenReturn("uniqueField2");
    when(ddeField.isReadOnly()).thenReturn(true);
    when(ddeBasic.isUnique()).thenReturn(true);
    when(ddeField.getType()).thenReturn(TypeDto.STRING);
    when(schemaHolder.getFields(entity)).thenReturn(asList(ddeField, eudeField));
    FieldMetadata fmdEude = mock(FieldMetadata.class);
    when(fmdEude.getName()).thenReturn("uniqueField");
    when(classMetadata.newFieldMetadata("uniqueField")).thenReturn(fmdEude);
    FieldMetadata fmdDde = mock(FieldMetadata.class);
    when(fmdDde.getName()).thenReturn("uniqueField2");
    when(classMetadata.newFieldMetadata("uniqueField2")).thenReturn(fmdDde);
    UniqueMetadata umd = mock(UniqueMetadata.class);
    when(fmdEude.newUniqueMetadata()).thenReturn(umd);
    entityMetadataBuilder.addEntityMetadata(jdoMetadata, entity, Sample.class, schemaHolder);
    verify(fmdDde, never()).newUniqueMetadata();
    verify(fmdDde, never()).setUnique(anyBoolean());
    verify(fmdEude).newUniqueMetadata();
    verify(umd).setName("unq_Sample_uniqueField");
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) UniqueMetadata(javax.jdo.metadata.UniqueMetadata) 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 FieldMetadata

use of javax.jdo.metadata.FieldMetadata in project motech by motech.

the class EntityMetadataBuilderTest method shouldNotSetDefaultFetchGroupIfSpecified.

@Test
public void shouldNotSetDefaultFetchGroupIfSpecified() {
    when(entity.getName()).thenReturn(ENTITY_NAME);
    when(entity.getTableName()).thenReturn(TABLE_NAME);
    when(jdoMetadata.newPackageMetadata(anyString())).thenReturn(packageMetadata);
    when(packageMetadata.newClassMetadata(anyString())).thenReturn(classMetadata);
    FieldDto field = fieldDto("notInDefFg", OneToOneRelationship.class);
    when(schemaHolder.getFields(CLASS_NAME)).thenReturn(singletonList(field));
    FieldMetadata fmd = mock(FieldMetadata.class);
    when(fmd.getName()).thenReturn("notInDefFg");
    when(classMetadata.newFieldMetadata("notInDefFg")).thenReturn(fmd);
    entityMetadataBuilder.addEntityMetadata(jdoMetadata, entity, Sample.class, schemaHolder);
    verify(fmd, never()).setDefaultFetchGroup(anyBoolean());
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with FieldMetadata

use of javax.jdo.metadata.FieldMetadata in project motech by motech.

the class EntityMetadataBuilderImpl method setComboboxMetadata.

private FieldMetadata setComboboxMetadata(ClassMetadata cmd, EntityDto entity, FieldDto field, Class<?> definition, EntityType entityType) {
    ComboboxHolder holder = new ComboboxHolder(entity, field);
    String fieldName = getNameForMetadata(field);
    FieldMetadata fmd = cmd.newFieldMetadata(fieldName);
    if (holder.isCollection()) {
        addDefaultFetchGroupMetadata(fmd, definition);
        fmd.setTable(ClassTableName.getTableName(cmd.getTable(), fieldName));
        JoinMetadata jm = fmd.newJoinMetadata();
        ForeignKeyMetadata fkmd = getOrCreateFkMetadata(jm);
        fkmd.setName(KeyNames.cbForeignKeyName(entity.getName(), entity.getId(), fieldName, entityType));
        jm.setDeleteAction(ForeignKeyAction.CASCADE);
        jm.newColumnMetadata().setName(fieldName + "_OID");
    }
    return fmd;
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) ForeignKeyMetadata(javax.jdo.metadata.ForeignKeyMetadata) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) JoinMetadata(javax.jdo.metadata.JoinMetadata)

Example 14 with FieldMetadata

use of javax.jdo.metadata.FieldMetadata in project motech by motech.

the class EntityMetadataBuilderImpl method setRelationshipMetadata.

private FieldMetadata setRelationshipMetadata(ClassMetadata cmd, ClassData classData, EntityDto entity, FieldDto field, EntityType entityType, Class<?> definition) {
    RelationshipHolder holder = new RelationshipHolder(classData, field);
    FieldMetadata fmd = cmd.newFieldMetadata(getNameForMetadata(field));
    addDefaultFetchGroupMetadata(fmd, definition);
    if (entityType == EntityType.STANDARD) {
        processRelationship(fmd, holder, entity, field, definition);
    } else {
        processHistoryTrashRelationship(cmd, fmd, holder);
    }
    return fmd;
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder)

Example 15 with FieldMetadata

use of javax.jdo.metadata.FieldMetadata in project motech by motech.

the class EntityMetadataBuilderImpl method setMapMetadata.

private FieldMetadata setMapMetadata(ClassMetadata cmd, EntityDto entity, FieldDto field, Class<?> definition, EntityType entityType) {
    FieldMetadata fmd = cmd.newFieldMetadata(getNameForMetadata(field));
    MetadataDto keyMetadata = field.getMetadata(MAP_KEY_TYPE);
    MetadataDto valueMetadata = field.getMetadata(MAP_VALUE_TYPE);
    boolean serialized = shouldSerializeMap(keyMetadata, valueMetadata);
    // Depending on the types of key and value of the map we either serialize the map or create a separate table for it
    fmd.setSerialized(serialized);
    addDefaultFetchGroupMetadata(fmd, definition);
    MapMetadata mmd = fmd.newMapMetadata();
    if (serialized) {
        mmd.setSerializedKey(true);
        mmd.setSerializedValue(true);
    } else {
        mmd.setKeyType(keyMetadata.getValue());
        mmd.setValueType(valueMetadata.getValue());
        fmd.setTable(ClassTableName.getTableName(cmd.getTable(), getNameForMetadata(field)));
        JoinMetadata jmd = fmd.newJoinMetadata();
        ForeignKeyMetadata fkmd = getOrCreateFkMetadata(jmd);
        fkmd.setDeleteAction(ForeignKeyAction.CASCADE);
        fkmd.setName(KeyNames.mapForeignKeyName(entity.getName(), entity.getId(), field.getBasic().getName(), entityType));
    }
    return fmd;
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) ForeignKeyMetadata(javax.jdo.metadata.ForeignKeyMetadata) JoinMetadata(javax.jdo.metadata.JoinMetadata) MetadataDto(org.motechproject.mds.dto.MetadataDto) MapMetadata(javax.jdo.metadata.MapMetadata)

Aggregations

FieldMetadata (javax.jdo.metadata.FieldMetadata)16 Test (org.junit.Test)6 FieldDto (org.motechproject.mds.dto.FieldDto)6 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 ForeignKeyMetadata (javax.jdo.metadata.ForeignKeyMetadata)3 MetadataDto (org.motechproject.mds.dto.MetadataDto)3 CtClass (javassist.CtClass)2 IndexMetadata (javax.jdo.metadata.IndexMetadata)2 JoinMetadata (javax.jdo.metadata.JoinMetadata)2 Matchers.anyString (org.mockito.Matchers.anyString)2 AnotherSample (org.motechproject.mds.annotations.internal.samples.AnotherSample)2 Sample (org.motechproject.mds.builder.Sample)2 LookupDto (org.motechproject.mds.dto.LookupDto)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CtField (javassist.CtField)1 NotFoundException (javassist.NotFoundException)1 ClassMetadata (javax.jdo.metadata.ClassMetadata)1 CollectionMetadata (javax.jdo.metadata.CollectionMetadata)1