Example usage for org.hibernate.hql.internal QueryExecutionRequestException getMessage

List of usage examples for org.hibernate.hql.internal QueryExecutionRequestException getMessage

Introduction

In this page you can find the example usage for org.hibernate.hql.internal QueryExecutionRequestException getMessage.

Prototype

@Override
    public String getMessage() 

Source Link

Usage

From source file:org.unitime.timetable.action.HibernateQueryTestAction.java

License:Open Source License

/** 
 * Method execute//w  w  w.  j av  a 2 s.c  o m
 * @param mapping
 * @param form
 * @param request
 * @param response
 * @return ActionForward
 */
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    sessionContext.checkPermission(Right.TestHQL);

    String op = request.getParameter("op");
    if (op == null || !op.equals("Submit")) {
        if ("Clear Cache".equals(op))
            HibernateUtil.clearCache();
        return mapping.findForward("displayQueryForm");
    }

    HibernateQueryTestForm frm = (HibernateQueryTestForm) form;
    ActionMessages errors = frm.validate(mapping, request);

    Logger sqlLog = Logger.getLogger("org.hibernate.SQL");
    if (iOriginalLevel == null)
        iOriginalLevel = sqlLog.getLevel();
    sqlLog.setLevel(Level.DEBUG);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Appender myAppender = new WriterAppender(new PatternLayout("%m%n"), out);
    sqlLog.addAppender(myAppender);

    if (errors.size() == 0) {
        try {
            int limit = ApplicationProperty.TestHQLMaxLines.intValue();
            String query = frm.getQuery();
            _RootDAO rdao = new _RootDAO();
            Session hibSession = rdao.getSession();
            Query q = hibSession.createQuery(query);
            try {
                List l = q.list();
                StringBuffer s = new StringBuffer();
                int line = 0;
                for (Iterator i = l.iterator(); i.hasNext(); line++) {
                    if (limit > 0 && line >= limit) {
                        s.append("<tr><td>...</td></tr>");
                        break;
                    }
                    Object o = i.next();
                    if (s.length() == 0)
                        printHeader(s, o);
                    printLine(s, o, (SessionImplementor) hibSession);
                }
                if (s.length() > 0) {
                    printFooter(s);
                    request.setAttribute("result", s.toString());
                }
                frm.setListSize(String.valueOf(l.size()));
            } catch (QueryExecutionRequestException e) {
                Transaction tx = null;
                try {
                    tx = hibSession.beginTransaction();
                    int i = q.executeUpdate();
                    request.setAttribute("result", i + " lines updated.");
                    frm.setListSize(String.valueOf(i));
                    tx.commit();
                } catch (Exception ex) {
                    if (tx != null && tx.isActive())
                        tx.rollback();
                    throw ex;
                }
                hibSession.flush();
                HibernateUtil.clearCache();
            }
        } catch (Exception e) {
            errors.add("query", new ActionMessage("errors.generic", e.getMessage()));
            Debug.error(e);
        }
    }

    sqlLog.removeAppender(myAppender);
    sqlLog.setLevel(iOriginalLevel == null ? Level.INFO : iOriginalLevel);
    out.flush();
    out.close();
    String sql = "";
    for (StringTokenizer stk = new StringTokenizer(new String(out.toByteArray()), "\n"); stk.hasMoreTokens();) {
        String line = (String) stk.nextToken();
        String comment = null;
        if (line.indexOf("/*") >= 0 && line.indexOf("/*") < line.indexOf("*/")) {
            comment = line.substring(line.indexOf("/*") + 2, line.indexOf("*/"));
            line = line.substring(0, line.indexOf("/*")) + line.substring(line.indexOf("*/") + 2);
        }
        if (sql.length() > 0)
            sql += "<br><br>";
        if (comment != null)
            sql += "<font color='gray'>-- " + comment + "</font>";
        Formatter f = new BasicFormatterImpl();
        sql += f.format(line).replaceAll("\n", "<br>").replaceAll(" ", "&nbsp;");
    }
    if (sql.length() > 0)
        request.setAttribute("sql", sql);

    saveErrors(request, errors);
    return mapping.findForward("displayQueryForm");

}