Example usage for org.hibernate Query getReturnAliases

List of usage examples for org.hibernate Query getReturnAliases

Introduction

In this page you can find the example usage for org.hibernate Query getReturnAliases.

Prototype

@Deprecated
String[] getReturnAliases();

Source Link

Usage

From source file:org.unitime.timetable.export.hql.SavedHqlExportToCSV.java

License:Open Source License

public static void execute(UserContext user, Printer out, SavedHQLInterface.Query query,
        List<SavedHQLInterface.IdValue> options, int fromRow, int maxRows)
        throws SavedHQLException, PageAccessException {
    try {/*  ww w .  j  av a 2  s  .  c  o  m*/
        String hql = query.getQuery();
        for (SavedHQL.Option o : SavedHQL.Option.values()) {
            if (hql.indexOf("%" + o.name() + "%") >= 0) {
                String value = null;
                for (SavedHQLInterface.IdValue v : options)
                    if (o.name().equals(v.getValue())) {
                        value = v.getText();
                        break;
                    }
                if (value == null || value.isEmpty()) {
                    Map<Long, String> vals = o.values(user);
                    if (vals == null || vals.isEmpty())
                        throw new SavedHQLException(MESSAGES.errorUnableToSetParameterNoValues(o.name()));
                    value = "";
                    for (Long id : vals.keySet()) {
                        if (!value.isEmpty())
                            value += ",";
                        value += id.toString();
                    }
                }
                hql = hql.replace("%" + o.name() + "%", "(" + value + ")");
            }
        }
        org.hibernate.Session hibSession = SavedHQLDAO.getInstance().getSession();
        org.hibernate.Query q = hibSession.createQuery(hql);
        if (maxRows > 0)
            q.setMaxResults(maxRows);
        if (fromRow > 0)
            q.setFirstResult(fromRow);
        q.setCacheable(true);
        int len = -1;
        for (Object o : q.list()) {
            if (len < 0) {
                len = length(o);
                String[] line = new String[len];
                header(line, o, q.getReturnAliases());
                if (line.length > 0 && line[0].startsWith("__"))
                    out.hideColumn(0);
                out.printHeader(line);
            }
            String[] line = new String[len];
            line(line, o, (SessionImplementor) hibSession);
            out.printLine(line);
            out.flush();
        }
    } catch (PageAccessException e) {
        throw e;
    } catch (SavedHQLException e) {
        throw e;
    } catch (Exception e) {
        sLog.error(e.getMessage(), e);
        throw new SavedHQLException(MESSAGES.failedExecution(
                e.getMessage() + (e.getCause() == null ? "" : " (" + e.getCause().getMessage() + ")")));
    }
}

From source file:org.unitime.timetable.export.hql.TestHqlExportToCSV.java

License:Apache License

public static void execute(UserContext user, Printer out, String hql, int fromRow, int maxRows)
        throws SavedHQLException, PageAccessException {
    try {//ww  w  .  ja v a  2 s. c  o  m
        org.hibernate.Session hibSession = SavedHQLDAO.getInstance().getSession();
        org.hibernate.Query q = hibSession.createQuery(hql);
        if (maxRows > 0)
            q.setMaxResults(maxRows);
        if (fromRow > 0)
            q.setFirstResult(fromRow);
        q.setCacheable(true);
        int len = -1;
        for (Object o : q.list()) {
            if (len < 0) {
                len = length(o);
                String[] line = new String[len];
                header(line, o, q.getReturnAliases());
                if (line.length > 0 && line[0].startsWith("__"))
                    out.hideColumn(0);
                out.printHeader(line);
            }
            String[] line = new String[len];
            line(line, o, (SessionImplementor) hibSession);
            out.printLine(line);
            out.flush();
        }
    } catch (PageAccessException e) {
        throw e;
    } catch (SavedHQLException e) {
        throw e;
    } catch (Exception e) {
        sLog.error(e.getMessage(), e);
        throw new SavedHQLException(MESSAGES.failedExecution(
                e.getMessage() + (e.getCause() == null ? "" : " (" + e.getCause().getMessage() + ")")));
    }
}