Example usage for org.hibernate.mapping PersistentClass getIdentifierMapper

List of usage examples for org.hibernate.mapping PersistentClass getIdentifierMapper

Introduction

In this page you can find the example usage for org.hibernate.mapping PersistentClass getIdentifierMapper.

Prototype

public Component getIdentifierMapper() 

Source Link

Usage

From source file:org.libreplan.business.common.LibrePlanClassValidator.java

License:Open Source License

/**
 * Retrieve the property by path in a recursive way, including IndetifierProperty in the loop
 * If propertyName is null or empty, the IdentifierProperty is returned
 *//* w  w  w. j av  a2s  .  c o  m*/
public static Property findPropertyByName(PersistentClass associatedClass, String propertyName) {
    Property property = null;
    Property idProperty = associatedClass.getIdentifierProperty();
    String idName = idProperty != null ? idProperty.getName() : null;
    try {
        if (propertyName == null || propertyName.length() == 0 || propertyName.equals(idName)) {
            //default to id
            property = idProperty;
        } else {
            if (propertyName.indexOf(idName + ".") == 0) {
                property = idProperty;
                propertyName = propertyName.substring(idName.length() + 1);
            }
            StringTokenizer st = new StringTokenizer(propertyName, ".", false);
            while (st.hasMoreElements()) {
                String element = (String) st.nextElement();
                if (property == null) {
                    property = associatedClass.getProperty(element);
                } else {
                    if (!property.isComposite())
                        return null;
                    property = ((Component) property.getValue()).getProperty(element);
                }
            }
        }
    } catch (MappingException e) {
        try {
            //if we do not find it try to check the identifier mapper
            if (associatedClass.getIdentifierMapper() == null)
                return null;
            StringTokenizer st = new StringTokenizer(propertyName, ".", false);
            while (st.hasMoreElements()) {
                String element = (String) st.nextElement();
                if (property == null) {
                    property = associatedClass.getIdentifierMapper().getProperty(element);
                } else {
                    if (!property.isComposite())
                        return null;
                    property = ((Component) property.getValue()).getProperty(element);
                }
            }
        } catch (MappingException ee) {
            return null;
        }
    }
    return property;
}