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)));
}
}
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");
}
Aggregations