Example usage for org.hibernate.cfg BinderHelper findPropertyByName

List of usage examples for org.hibernate.cfg BinderHelper findPropertyByName

Introduction

In this page you can find the example usage for org.hibernate.cfg BinderHelper findPropertyByName.

Prototype

public static Property findPropertyByName(Component component, String propertyName) 

Source Link

Document

Retrieve the property by path in a recursive way

Usage

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

@SuppressWarnings("unchecked")
protected String buildOrderByClause(String hqlOrderBy, PersistentClass associatedClass, String role,
        String defaultOrder) {//w ww.j  a v  a 2 s .c o m
    String orderByString = null;
    if (hqlOrderBy != null) {
        List<String> properties = new ArrayList<String>();
        List<String> ordering = new ArrayList<String>();
        StringBuilder orderByBuffer = new StringBuilder();
        if (hqlOrderBy.length() == 0) {
            //order by id
            Iterator<?> it = associatedClass.getIdentifier().getColumnIterator();
            while (it.hasNext()) {
                Selectable col = (Selectable) it.next();
                orderByBuffer.append(col.getText()).append(" asc").append(", ");
            }
        } else {
            StringTokenizer st = new StringTokenizer(hqlOrderBy, " ,", false);
            String currentOrdering = defaultOrder;
            //FIXME make this code decent
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                if (isNonPropertyToken(token)) {
                    if (currentOrdering != null) {
                        throw new GrailsDomainException(
                                "Error while parsing sort clause: " + hqlOrderBy + " (" + role + ")");
                    }
                    currentOrdering = token;
                } else {
                    //Add ordering of the previous
                    if (currentOrdering == null) {
                        //default ordering
                        ordering.add("asc");
                    } else {
                        ordering.add(currentOrdering);
                        currentOrdering = null;
                    }
                    properties.add(token);
                }
            }
            ordering.remove(0); //first one is the algorithm starter
            // add last one ordering
            if (currentOrdering == null) {
                //default ordering
                ordering.add(defaultOrder);
            } else {
                ordering.add(currentOrdering);
                currentOrdering = null;
            }
            int index = 0;

            for (String property : properties) {
                Property p = BinderHelper.findPropertyByName(associatedClass, property);
                if (p == null) {
                    throw new GrailsDomainException("property from sort clause not found: "
                            + associatedClass.getEntityName() + "." + property);
                }
                PersistentClass pc = p.getPersistentClass();
                String table;
                if (pc == null) {
                    table = "";
                }

                else if (pc == associatedClass || (associatedClass instanceof SingleTableSubclass
                        && pc.getMappedClass().isAssignableFrom(associatedClass.getMappedClass()))) {
                    table = "";
                } else {
                    table = pc.getTable().getQuotedName() + ".";
                }

                Iterator<?> propertyColumns = p.getColumnIterator();
                while (propertyColumns.hasNext()) {
                    Selectable column = (Selectable) propertyColumns.next();
                    orderByBuffer.append(table).append(column.getText()).append(" ").append(ordering.get(index))
                            .append(", ");
                }
                index++;
            }
        }
        orderByString = orderByBuffer.substring(0, orderByBuffer.length() - 2);
    }
    return orderByString;
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java

License:Apache License

@SuppressWarnings("unchecked")
private static String buildOrderByClause(String hqlOrderBy, PersistentClass associatedClass, String role,
        String defaultOrder) {/*from  w w w .ja v  a 2 s  .co  m*/
    String orderByString = null;
    if (hqlOrderBy != null) {
        List<String> properties = new ArrayList<String>();
        List<String> ordering = new ArrayList<String>();
        StringBuilder orderByBuffer = new StringBuilder();
        if (hqlOrderBy.length() == 0) {
            //order by id
            Iterator<?> it = associatedClass.getIdentifier().getColumnIterator();
            while (it.hasNext()) {
                Selectable col = (Selectable) it.next();
                orderByBuffer.append(col.getText()).append(" asc").append(", ");
            }
        } else {
            StringTokenizer st = new StringTokenizer(hqlOrderBy, " ,", false);
            String currentOrdering = defaultOrder;
            //FIXME make this code decent
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                if (isNonPropertyToken(token)) {
                    if (currentOrdering != null) {
                        throw new GrailsDomainException(
                                "Error while parsing sort clause: " + hqlOrderBy + " (" + role + ")");
                    }
                    currentOrdering = token;
                } else {
                    //Add ordering of the previous
                    if (currentOrdering == null) {
                        //default ordering
                        ordering.add("asc");
                    } else {
                        ordering.add(currentOrdering);
                        currentOrdering = null;
                    }
                    properties.add(token);
                }
            }
            ordering.remove(0); //first one is the algorithm starter
            // add last one ordering
            if (currentOrdering == null) {
                //default ordering
                ordering.add(defaultOrder);
            } else {
                ordering.add(currentOrdering);
                currentOrdering = null;
            }
            int index = 0;

            for (String property : properties) {
                Property p = BinderHelper.findPropertyByName(associatedClass, property);
                if (p == null) {
                    throw new GrailsDomainException("property from sort clause not found: "
                            + associatedClass.getEntityName() + "." + property);
                }
                PersistentClass pc = p.getPersistentClass();
                String table;
                if (pc == null) {
                    table = "";
                }

                else if (pc == associatedClass || (associatedClass instanceof SingleTableSubclass
                        && pc.getMappedClass().isAssignableFrom(associatedClass.getMappedClass()))) {
                    table = "";
                } else {
                    table = pc.getTable().getQuotedName() + ".";
                }

                Iterator<?> propertyColumns = p.getColumnIterator();
                while (propertyColumns.hasNext()) {
                    Selectable column = (Selectable) propertyColumns.next();
                    orderByBuffer.append(table).append(column.getText()).append(" ").append(ordering.get(index))
                            .append(", ");
                }
                index++;
            }
        }
        orderByString = orderByBuffer.substring(0, orderByBuffer.length() - 2);
    }
    return orderByString;
}

From source file:org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

@SuppressWarnings("unchecked")
protected String buildOrderByClause(String hqlOrderBy, PersistentClass associatedClass, String role,
        String defaultOrder) {//w  ww. ja va 2  s  .  c  o m
    String orderByString = null;
    if (hqlOrderBy != null) {
        List<String> properties = new ArrayList<String>();
        List<String> ordering = new ArrayList<String>();
        StringBuilder orderByBuffer = new StringBuilder();
        if (hqlOrderBy.length() == 0) {
            //order by id
            Iterator<?> it = associatedClass.getIdentifier().getColumnIterator();
            while (it.hasNext()) {
                Selectable col = (Selectable) it.next();
                orderByBuffer.append(col.getText()).append(" asc").append(", ");
            }
        } else {
            StringTokenizer st = new StringTokenizer(hqlOrderBy, " ,", false);
            String currentOrdering = defaultOrder;
            //FIXME make this code decent
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                if (isNonPropertyToken(token)) {
                    if (currentOrdering != null) {
                        throw new DatastoreConfigurationException(
                                "Error while parsing sort clause: " + hqlOrderBy + " (" + role + ")");
                    }
                    currentOrdering = token;
                } else {
                    //Add ordering of the previous
                    if (currentOrdering == null) {
                        //default ordering
                        ordering.add("asc");
                    } else {
                        ordering.add(currentOrdering);
                        currentOrdering = null;
                    }
                    properties.add(token);
                }
            }
            ordering.remove(0); //first one is the algorithm starter
            // add last one ordering
            if (currentOrdering == null) {
                //default ordering
                ordering.add(defaultOrder);
            } else {
                ordering.add(currentOrdering);
                currentOrdering = null;
            }
            int index = 0;

            for (String property : properties) {
                Property p = BinderHelper.findPropertyByName(associatedClass, property);
                if (p == null) {
                    throw new DatastoreConfigurationException("property from sort clause not found: "
                            + associatedClass.getEntityName() + "." + property);
                }
                PersistentClass pc = p.getPersistentClass();
                String table;
                if (pc == null) {
                    table = "";
                }

                else if (pc == associatedClass || (associatedClass instanceof SingleTableSubclass
                        && pc.getMappedClass().isAssignableFrom(associatedClass.getMappedClass()))) {
                    table = "";
                } else {
                    table = pc.getTable().getQuotedName() + ".";
                }

                Iterator<?> propertyColumns = p.getColumnIterator();
                while (propertyColumns.hasNext()) {
                    Selectable column = (Selectable) propertyColumns.next();
                    orderByBuffer.append(table).append(column.getText()).append(" ").append(ordering.get(index))
                            .append(", ");
                }
                index++;
            }
        }
        orderByString = orderByBuffer.substring(0, orderByBuffer.length() - 2);
    }
    return orderByString;
}