001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.backend.list.helper.naming;
016:
017: import org.araneaframework.uilib.util.NameUtil;
018:
019: /**
020: * Standard naming conventions between list fields and database columns.
021: * <p>
022: * Field names are transformed into database column names as following:<br/>
023: * <ul>
024: * <li>The full name is split into prefix and suffix (following to the last dot).
025: * <li>All dots in the prefix and suffix are converted into underscores.
026: * <li>Before each upper case letter followed by lower case an underscore is inserted.
027: * </ul>
028: * E.g:
029: * <pre>
030: * firstName -> first_name
031: * location.address -> location.address
032: * client.group.id -> client_group.id
033: * </pre>
034: * Field names are transformed into database column aliases as following:<br/><br/>
035: * <ul>
036: * <li>All dots are converted into underscores.
037: * <li>Before each upper case letter followed by lower case an underscore is inserted.
038: * </ul>
039: * E.g:
040: * <pre>
041: * lastName -> last_name
042: * location.address -> location_address
043: * client.group.id -> client_group_id
044: * </pre>
045: * </p>
046: *
047: * @author Rein Raudjärv
048: *
049: * @since 1.1
050: */
051: public class StandardNamingStrategy implements NamingStrategy {
052:
053: // --- Column Name ---
054:
055: public String fieldToColumnName(String fieldName) {
056: String prefix = resolvePrefix(extractPrefix(fieldName));
057: String suffix = resolveSuffix(extractSuffix(fieldName));
058: return concatFullname(prefix, suffix);
059: }
060:
061: protected String extractPrefix(String varName) {
062: return NameUtil.getLongestPrefix(varName);
063: }
064:
065: protected String extractSuffix(String varName) {
066: return NameUtil.getShortestSuffix(varName);
067: }
068:
069: protected String resolvePrefix(String fieldNamePrefix) {
070: return addUnderscores(fieldNamePrefix);
071: }
072:
073: protected String resolveSuffix(String fieldNameSuffix) {
074: return addUnderscores(fieldNameSuffix);
075: }
076:
077: protected String concatFullname(String prefix, String suffix) {
078: return NameUtil.getFullName(prefix, suffix);
079: }
080:
081: // --- Column Alias ---
082:
083: public String fieldToColumnAlias(String fieldName) {
084: return addUnderscores(fieldName);
085: }
086:
087: // --- Utils ---
088:
089: /**
090: * Replace all dots with underscores
091: * and add an underscore before upper-case letters
092: * followed by a non-upper-case letter.
093: * <p>
094: * E.g.
095: * <pre>
096: * "a" -> "a"
097: * "a.b" -> "a_b"
098: * "aBc" -> "a_bc"
099: * "aB" -> "aB"
100: * </pre>
101: */
102: public static String addUnderscores(String name) {
103: if (name == null) {
104: return null;
105: }
106:
107: StringBuffer buf = new StringBuffer(name.replace('.', '_'));
108: for (int i = 1; i < buf.length() - 1; i++) {
109: if ('_' != buf.charAt(i - 1)
110: && Character.isUpperCase(buf.charAt(i))
111: && !Character.isUpperCase(buf.charAt(i + 1))) {
112: buf.insert(i++, '_');
113: }
114: }
115: return buf.toString().toLowerCase();
116: }
117:
118: }
|