{
writer.startElement( "field" );
StoreFieldMetadata storeMetadata = (StoreFieldMetadata) modelField.getMetadata( StoreFieldMetadata.ID );
JPoxFieldMetadata jpoxMetadata = (JPoxFieldMetadata) modelField.getMetadata( JPoxFieldMetadata.ID );
writer.addAttribute( "name", modelField.getName() );
if ( !storeMetadata.isStorable() )
{
writer.addAttribute( "persistence-modifier", "none" );
}
else if ( StringUtils.isNotEmpty( jpoxMetadata.getPersistenceModifier() ) )
{
writer.addAttribute( "persistence-modifier", jpoxMetadata.getPersistenceModifier() );
}
if ( modelField.isRequired() )
{
writer.addAttribute( "null-value", "exception" );
}
else if ( jpoxMetadata.getNullValue() != null )
{
writer.addAttribute( "null-value", jpoxMetadata.getNullValue() );
}
String columnName = getColumnName( modelField, jpoxMetadata );
if ( StringUtils.isNotEmpty( jpoxMetadata.getJoinTableName() ) )
{
writer.addAttribute( "table", getJoinTableName( modelField, jpoxMetadata ) );
}
if ( jpoxMetadata.isPrimaryKey() )
{
writer.addAttribute( "primary-key", "true" );
// value-strategy is only useful when you have a primary-key defined for the field.
if ( StringUtils.isNotEmpty( valueStrategyOverride ) )
{
writeValueStrategy( valueStrategyOverride, writer );
}
else if ( StringUtils.isNotEmpty( jpoxMetadata.getValueStrategy() ) )
{
writeValueStrategy( jpoxMetadata.getValueStrategy(), writer );
}
}
if ( StringUtils.isNotEmpty( jpoxMetadata.getIndexed() ) )
{
writer.addAttribute( "indexed", jpoxMetadata.getIndexed() );
}
if ( StringUtils.isNotEmpty( jpoxMetadata.getMappedBy() ) )
{
writer.addAttribute( "mapped-by", jpoxMetadata.getMappedBy() );
}
if ( modelField instanceof ModelAssociation )
{
writeAssociation( writer, (ModelAssociation) modelField );
}
else
{
if ( modelField.isPrimitiveArray() )
{
writer.startElement( "array" );
writer.endElement();
}
// Work out potential <column> subelement
// Store potential column properties.
Properties columnProps = new Properties();
if ( !StringUtils.equalsIgnoreCase( columnName, modelField.getName() ) )
{
columnProps.setProperty( "name", columnName );
}
if ( storeMetadata.getMaxSize() > 0 )
{
columnProps.setProperty( "length", String.valueOf( storeMetadata.getMaxSize() ) );
}
if ( StringUtils.equals( jpoxMetadata.getNullValue(), "default" ) )
{
columnProps.setProperty( "default-value", modelField.getDefaultValue() );
}
// Now write the column sub element (if it has properties)
if ( !columnProps.isEmpty() )
{
writer.startElement( "column" );
for ( Enumeration en = columnProps.propertyNames(); en.hasMoreElements(); )
{
String attributeName = (String) en.nextElement();
String attributeValue = columnProps.getProperty( attributeName );
writer.addAttribute( attributeName, attributeValue );
}
writer.endElement();
}
// Work out potential <unique> subelement.
if ( jpoxMetadata.isUnique() )
{
writer.startElement( "unique" );
writer.addAttribute( "name", columnName.toUpperCase() + "_UNIQUE_CONSTRAINT" );
writer.endElement();
}
// Work out potential <foreign-key> subelement
if ( jpoxMetadata.isForeignKey() )
{
writer.startElement( "foreign-key" );
writer.addAttribute( "name", columnName.toUpperCase() + "_FK" );
if ( StringUtils.isNotEmpty( jpoxMetadata.getForeignKeyDeferred() ) )
{
writer.addAttribute( "deferred", jpoxMetadata.getForeignKeyDeferred() );
}
if ( StringUtils.isNotEmpty( jpoxMetadata.getForeignKeyDeleteAction() ) )
{
writer.addAttribute( "delete-action", jpoxMetadata.getForeignKeyDeleteAction() );
}
if ( StringUtils.isNotEmpty( jpoxMetadata.getForeignKeyUpdateAction() ) )
{
writer.addAttribute( "update-action", jpoxMetadata.getForeignKeyUpdateAction() );
}
writer.endElement();
}
}