001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.dao.orm.hibernate;
016    
017    import org.hibernate.PropertyNotFoundException;
018    import org.hibernate.property.BasicPropertyAccessor;
019    import org.hibernate.property.Getter;
020    import org.hibernate.property.Setter;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     */
025    @SuppressWarnings("rawtypes")
026    public class CamelCasePropertyAccessor extends BasicPropertyAccessor {
027    
028            @Override
029            public Getter getGetter(Class clazz, String propertyName)
030                    throws PropertyNotFoundException {
031    
032                    propertyName = fixPropertyName(propertyName);
033    
034                    return super.getGetter(clazz, propertyName);
035            }
036    
037            @Override
038            public Setter getSetter(Class clazz, String propertyName)
039                    throws PropertyNotFoundException {
040    
041                    propertyName = fixPropertyName(propertyName);
042    
043                    return super.getSetter(clazz, propertyName);
044            }
045    
046            protected String fixPropertyName(String propertyName) {
047                    if (propertyName.length() < 3) {
048                            return propertyName;
049                    }
050    
051                    char[] chars = propertyName.toCharArray();
052    
053                    char c0 = chars[0];
054                    char c1 = chars[1];
055                    char c2 = chars[2];
056    
057                    if (Character.isLowerCase(c0) && Character.isUpperCase(c1) &&
058                            Character.isLowerCase(c2)) {
059    
060                            return Character.toUpperCase(c0) + propertyName.substring(1);
061                    }
062    
063                    return propertyName;
064            }
065    
066    }