View Javadoc
1   /*
2    * Copyright 2010 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.service.data.impl;
20  
21  import java.util.Locale;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.hibernate.boot.model.naming.Identifier;
25  import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
26  import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
27  
28  
29  /**
30   * The Class LegacyNamingStrategy.
31   */
32  public final class LegacyNamingStrategy implements PhysicalNamingStrategy {
33  
34      private static final String REG_EXPR = "([a-z])([A-Z])";
35      private static final String REPLACEMENT_PATTERN = "$1_$2";
36  
37  
38      /**
39  	 * Instantiates a new legacy naming strategy.
40  	 */
41      public LegacyNamingStrategy() {
42  		super();
43  	}
44  
45      /**
46  	 * Convert.
47  	 *
48  	 * @param identifier
49  	 *            the identifier
50  	 * @return the identifier
51  	 */
52      private static Identifier convert(final Identifier identifier) {
53      	if (identifier == null || StringUtils.isBlank(identifier.getText())) {
54              return identifier;
55          } else {
56          	return Identifier.toIdentifier(identifier.getText().replaceAll(REG_EXPR, REPLACEMENT_PATTERN).toLowerCase(Locale.ENGLISH));
57          }
58      }
59  
60      @Override
61      public Identifier toPhysicalCatalogName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
62          return convert(identifier);
63      }
64  
65      @Override
66      public Identifier toPhysicalColumnName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
67          return convert(identifier);
68      }
69  
70      @Override
71      public Identifier toPhysicalSchemaName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
72          return convert(identifier);
73      }
74  
75      @Override
76      public Identifier toPhysicalSequenceName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
77          return convert(identifier);
78      }
79  
80      @Override
81      public Identifier toPhysicalTableName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
82          return convert(identifier);
83      }
84  
85  }