/*
 * Hibernate OGM, Domain model persistence for NoSQL datastores
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */
package org.hibernate.ogm.cfg.impl;

import org.hibernate.AssertionFailure;
import org.hibernate.cfg.EJB3NamingStrategy;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.ogm.util.impl.StringHelper;

/**
 * Implements the OGM naming strategy:
 *  - based of the JPA 2 naming strategy
 *  - column names in components defaults to the fully qualified path (ie address.city)
 *
 * @author Emmanuel Bernard
 */
public class OgmNamingStrategy extends EJB3NamingStrategy {

	public static final NamingStrategy INSTANCE = new OgmNamingStrategy();

	/**
	 * A pattern common to all property names used in element collections.
	 */
	private static final String ELEMENT_COLLECTION_NAME_PATTERN = "collection&&element";


	private String addPropertySeparator(String name) {
		//the . is already present, no need to replace it
		return name; //name.replace( '.', '_' ).toLowerCase();
	}

	@Override
	public String propertyToColumnName(String propertyName) {
		// for element collections just use the simple name
		if ( propertyName.contains( ELEMENT_COLLECTION_NAME_PATTERN ) ) {
			propertyName = propertyName.substring( propertyName.lastIndexOf( "." ) + 1 );
		}

		return addPropertySeparator( propertyName );
	}

	@Override
	public String collectionTableName(
			String ownerEntity, String ownerEntityTable, String associatedEntity, String associatedEntityTable,
			String propertyName
	) {
		return tableName(
				new StringBuilder( ownerEntityTable ).append( "_" )
						.append(
								associatedEntityTable != null ?
										associatedEntityTable :
										addPropertySeparator( propertyName )
						).toString()
		);
	}


	@Override
	public String foreignKeyColumnName(
			String propertyName, String propertyEntityName, String propertyTableName, String referencedColumnName
	) {
		String header = propertyName != null ? addPropertySeparator( propertyName ) : propertyTableName;
		if ( header == null ) {
			throw new AssertionFailure( "NamingStrategy not properly filled" );
		}
		return columnName( header + "_" + referencedColumnName );
	}

	@Override
	public String logicalColumnName(String columnName, String propertyName) {
		return StringHelper.isEmpty( columnName ) == false ? columnName : propertyName;
	}

	@Override
	public String logicalCollectionTableName(
			String tableName, String ownerEntityTable, String associatedEntityTable, String propertyName
	) {
		if ( tableName != null ) {
			return tableName;
		}
		else {
			//use of a stringbuffer to workaround a JDK bug
			return new StringBuffer( ownerEntityTable ).append( "_" )
					.append(
							associatedEntityTable != null ?
									associatedEntityTable :
									propertyName
					).toString();
		}

	}

	@Override
	public String logicalCollectionColumnName(String columnName, String propertyName, String referencedColumn) {
		return StringHelper.isEmpty( columnName ) == false ?
				columnName :
				propertyName + "_" + referencedColumn;
	}

}

@@ -960,16 +960,64 @@
 ent%22;%0A%0A%0A
+%09// noop method kept for documentation purposes%0A
 %09private
@@ -1024,19 +1024,23 @@
  String 
-add
+replace
 Property
@@ -1122,61 +1122,78 @@
 t%0A%09%09
-return name; //name.
+// Other strategies would typically 
 replace
-(
  '.'
-, '_' ).toLowerCase()
+ with '_'%0A%09%09return name
 ;%0A%09%7D
@@ -1481,19 +1481,23 @@
 %09return 
-add
+replace
 Property
@@ -1880,19 +1880,23 @@
 %09%09%09%09%09%09%09%09
-add
+replace
 Property
@@ -2149,19 +2149,23 @@
  null ? 
-add
+replace
 Property

Improve documentation of OgmNamingStrategy