001: package net.sourceforge.squirrel_sql.plugins.hibernate;
002:
003: import net.sourceforge.squirrel_sql.client.session.ISession;
004: import net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter;
005: import net.sourceforge.squirrel_sql.fw.util.StringManager;
006: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
007: import net.sourceforge.squirrel_sql.fw.util.Utilities;
008: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
009: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
010: import net.sourceforge.squirrel_sql.fw.sql.QueryTokenizer;
011:
012: import javax.swing.*;
013: import java.awt.*;
014: import java.awt.event.ActionEvent;
015: import java.awt.event.KeyEvent;
016: import java.util.ArrayList;
017:
018: public class HQLPanelController {
019: private static final StringManager s_stringMgr = StringManagerFactory
020: .getStringManager(HQLPanelController.class);
021:
022: private static ILogger s_log = LoggerController
023: .createLogger(HQLPanelController.class);
024:
025: private IHibernateTabController _hibernateTabController;
026: private ISession _sess;
027: private HibernateConnection _con;
028: private AbstractAction _convertToSQL;
029: private HQLEntryPanelManager _hqlEntryPanelManager;
030:
031: public HQLPanelController(
032: IHibernateTabController hibernateTabController,
033: ISession sess, HibernatePluginResources resource) {
034: _hibernateTabController = hibernateTabController;
035: _sess = sess;
036:
037: _hqlEntryPanelManager = new HQLEntryPanelManager(_sess,
038: resource, hibernateTabController
039: .getHibernateConnectionProvider());
040:
041: }
042:
043: void initActions() {
044: _convertToSQL = new AbstractAction() {
045: public void actionPerformed(ActionEvent e) {
046: onConvertToSQL();
047: }
048: };
049:
050: // i18n[hibernate.hqlToSqlLong=HQL to SQL]
051: _convertToSQL.putValue(AbstractAction.NAME, s_stringMgr
052: .getString("hibernate.hqlToSqlLong"));
053:
054: // i18n[hibernate.hqlToSqlShort=Convert HQL to SQL (ctrl + enter)]
055: _convertToSQL.putValue(AbstractAction.SHORT_DESCRIPTION,
056: s_stringMgr.getString("hibernate.hqlToSqlShort"));
057:
058: _convertToSQL.setEnabled(false);
059:
060: _hibernateTabController.addToToolbar(_convertToSQL);
061:
062: KeyStroke ctrlEnter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
063: Event.CTRL_MASK);
064: _hqlEntryPanelManager.registerKeyboardAction(_convertToSQL,
065: ctrlEnter);
066: }
067:
068: private void onConvertToSQL() {
069: try {
070: if (false == _convertToSQL.isEnabled()) {
071: return;
072: }
073:
074: String hql = _hqlEntryPanelManager.getEntryPanel()
075: .getSQLToBeExecuted();
076:
077: if (null == hql || 0 == hql.trim().length()) {
078: return;
079: }
080:
081: String statementSeparator = _sess.getProperties()
082: .getSQLStatementSeparator();
083: String startOfLineComment = _sess.getProperties()
084: .getStartOfLineComment();
085: QueryTokenizer qt = new QueryTokenizer(statementSeparator,
086: startOfLineComment, true);
087: qt.setScriptToTokenize(hql);
088:
089: if (false == qt.hasQuery()) {
090: return;
091: }
092:
093: ArrayList<String> list;
094:
095: long begin = System.currentTimeMillis();
096: long duration;
097: try {
098: list = _con.generateSQL(qt.nextQuery());
099: duration = System.currentTimeMillis() - begin;
100: } catch (Exception e) {
101: Throwable t = Utilities.getDeepestThrowable(e);
102: ExceptionFormatter formatter = _sess
103: .getExceptionFormatter();
104: String message = formatter.format(t);
105: _sess.showErrorMessage(message);
106:
107: if (_sess.getProperties().getWriteSQLErrorsToLog()
108: || (-1 == t.getClass().getName().toLowerCase()
109: .indexOf("hibernate") && -1 == t
110: .getClass().getName().toLowerCase()
111: .indexOf("antlr"))) {
112: // If this is not a hibernate error we write a log entry
113: s_log.error(t);
114: }
115:
116: return;
117: }
118:
119: _hibernateTabController.displaySqls(list);
120:
121: // i18n[HQLPanelController.hqlToSqlSuccess=Generated {0} SQL(s) in {1} milliseconds.]
122: _sess.getApplication().getMessageHandler().showMessage(
123: s_stringMgr.getString(
124: "SQLPanelController.hqlToSqlSuccess", list
125: .size(), duration));
126: } catch (Exception e) {
127: throw new RuntimeException(e);
128: }
129: }
130:
131: public void setConnection(HibernateConnection con) {
132: _con = con;
133:
134: if (null == _con) {
135: _convertToSQL.setEnabled(false);
136: } else {
137: _convertToSQL.setEnabled(true);
138: }
139: }
140:
141: public JComponent getComponent() {
142: return _hqlEntryPanelManager.getComponent();
143: }
144:
145: public void requestFocus() {
146: _hqlEntryPanelManager.requestFocus();
147: }
148: }
|