Search in sources :

Example 1 with UniqueMetadata

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

the class EntityMetadataBuilderImpl method customizeFieldMd.

private void customizeFieldMd(FieldMetadata fmd, EntityDto entity, FieldDto field, EntityType entityType, Class<?> definition) {
    setColumnParameters(fmd, field, definition);
    // Check whether the field is required and set appropriate metadata
    fmd.setNullValue(isFieldRequired(field, entityType) ? NullValue.EXCEPTION : NullValue.NONE);
    // Non DDE fields have controllable unique
    if (!field.isReadOnly() && entityType == EntityType.STANDARD && field.getBasic().isUnique()) {
        UniqueMetadata umd = fmd.newUniqueMetadata();
        // TODO: Move to KeyNames class (to be introduced in MOTECH-1991)
        umd.setName(KeyNames.uniqueKeyName(entity.getName(), getNameForMetadata(field)));
    }
}
Also used : UniqueMetadata(javax.jdo.metadata.UniqueMetadata)

Example 2 with UniqueMetadata

use of javax.jdo.metadata.UniqueMetadata 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)

Aggregations

UniqueMetadata (javax.jdo.metadata.UniqueMetadata)2 FieldMetadata (javax.jdo.metadata.FieldMetadata)1 Test (org.junit.Test)1 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)1 FieldDto (org.motechproject.mds.dto.FieldDto)1 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1