001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }