Author: dgeraskov
Date: 2008-01-25 09:59:42 -0500 (Fri, 25 Jan 2008)
New Revision: 5965
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/CriteriaEditorAction.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/HQLScratchpadAction.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenQueryEditorAction.java
Log:
http://jira.jboss.com/jira/browse/JBIDE-1620
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/CriteriaEditorAction.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/CriteriaEditorAction.java 2008-01-25
14:22:35 UTC (rev 5964)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/CriteriaEditorAction.java 2008-01-25
14:59:42 UTC (rev 5965)
@@ -21,10 +21,14 @@
*/
package org.hibernate.eclipse.console.actions;
+import org.eclipse.jface.viewers.TreePath;
+import org.eclipse.osgi.util.NLS;
import org.hibernate.console.ConsoleConfiguration;
import org.hibernate.console.ImageConstants;
import org.hibernate.eclipse.console.HibernateConsolePlugin;
import org.hibernate.eclipse.console.utils.EclipseImages;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.Property;
public class CriteriaEditorAction extends OpenQueryEditorAction {
public CriteriaEditorAction() {
@@ -36,6 +40,56 @@
protected void openQueryEditor(ConsoleConfiguration config, String query) {
HibernateConsolePlugin.getDefault().openCriteriaEditor(
config==null?null:config.getName(), query );
-
- }
+ }
+
+ /* (non-Javadoc)
+ * @see
org.hibernate.eclipse.console.actions.OpenQueryEditorAction#generateQuery(org.eclipse.jface.viewers.TreePath)
+ */
+ protected String generateQuery(TreePath path) {
+ final String criteria = ".createCriteria({0})";
+ final String alias = "\n.createCriteria(\"{0}\",
\"{1}\")";
+ final String projection = "\n.setProjection(
Property.forName(\"{0}\").as(\"{0}\"))";
+ final String sess = "session";
+ String enCriteria = "";
+ String propCriteria = "";
+ String enName = "";
+ Object node = path.getLastSegment();
+ if (node instanceof PersistentClass){
+ enName = ((PersistentClass)node).getEntityName();
+ enName = enName.substring(enName.lastIndexOf('.') + 1);
+ } else if (node instanceof Property){
+ Property prop = (Property)node;
+ String prName = prop.getName();
+ PersistentClass pClass = ((Property)node).getPersistentClass();
+ if (pClass != null){
+ enName = pClass.getEntityName();
+ enName = enName.substring(enName.lastIndexOf('.') + 1);
+ if (prop.getValue().isSimpleValue()) {
+ propCriteria = NLS.bind(projection, prName);
+ } else {
+ propCriteria = NLS.bind(alias, prName, prName.charAt(0));
+ }
+ } else {
+ // Generate script for Component property
+ for (int i = path.getSegmentCount() - 1; i > 0; i--) {
+ if (path.getSegment(i) instanceof PersistentClass){
+ enName = ((PersistentClass)path.getSegment(i)).getEntityName();
+ enName = enName.substring(enName.lastIndexOf('.') + 1);
+ } else if (path.getSegment(i) instanceof Property){
+ prName = ((Property)path.getSegment(i)).getName();
+ if (prop.getValue().isSimpleValue()) {
+ propCriteria += NLS.bind(projection, prName);
+ } else {
+ propCriteria += NLS.bind(alias, prName, prName.charAt(0));
+ }
+ //propCriteria += NLS.bind(projection, prName);
+ }
+ }
+ }
+ } else {
+ return "";
+ }
+ enCriteria = NLS.bind(criteria, enName + ".class");
+ return sess + enCriteria + propCriteria +
"\n.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP)";
+ }
}
\ No newline at end of file
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/HQLScratchpadAction.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/HQLScratchpadAction.java 2008-01-25
14:22:35 UTC (rev 5964)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/HQLScratchpadAction.java 2008-01-25
14:59:42 UTC (rev 5965)
@@ -21,10 +21,13 @@
*/
package org.hibernate.eclipse.console.actions;
+import org.eclipse.jface.viewers.TreePath;
import org.hibernate.console.ConsoleConfiguration;
import org.hibernate.console.ImageConstants;
import org.hibernate.eclipse.console.HibernateConsolePlugin;
import org.hibernate.eclipse.console.utils.EclipseImages;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.Property;
public class HQLScratchpadAction extends OpenQueryEditorAction {
public HQLScratchpadAction() {
@@ -38,4 +41,34 @@
HibernateConsolePlugin.getDefault().openScratchHQLEditor(config==null?null:config.getName(),
query);
}
+ /* (non-Javadoc)
+ * @see
org.hibernate.eclipse.console.actions.OpenQueryEditorAction#generateQuery(org.eclipse.jface.viewers.TreePath)
+ */
+ protected String generateQuery(TreePath path) {
+ Object node = path.getLastSegment();
+ if (node instanceof PersistentClass){
+ String name = ((PersistentClass)node).getEntityName();
+ return "from " + name;
+ } else if (node instanceof Property){
+ String prName = ((Property)node).getName();
+ PersistentClass pClass = ((Property)node).getPersistentClass();
+ String enName = "";
+ if (pClass != null){
+ enName = pClass.getEntityName();
+ enName = enName.substring(enName.lastIndexOf('.') + 1);
+ } else {
+ // Generate script for Component property
+ for (int i = path.getSegmentCount() - 2; i > 0; i--) {
+ if (path.getSegment(i) instanceof PersistentClass){
+ enName = ((PersistentClass)path.getSegment(i)).getEntityName();
+ enName = enName.substring(enName.lastIndexOf('.') + 1);
+ } else if (path.getSegment(i) instanceof Property){
+ prName = ((Property)path.getSegment(i)).getName() + "." + prName;
+ }
+ }
+ }
+ return "select o." + prName + " from " + enName + " o";
+ }
+ return "";
+ }
}
\ No newline at end of file
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenQueryEditorAction.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenQueryEditorAction.java 2008-01-25
14:22:35 UTC (rev 5964)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenQueryEditorAction.java 2008-01-25
14:59:42 UTC (rev 5965)
@@ -1,18 +1,15 @@
package org.hibernate.eclipse.console.actions;
-import java.io.FileNotFoundException;
-import java.util.Iterator;
-
-import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.swt.widgets.Event;
-import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.SelectionListenerAction;
import org.hibernate.HibernateException;
import org.hibernate.console.ConsoleConfiguration;
import org.hibernate.eclipse.console.HibernateConsolePlugin;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.Property;
public abstract class OpenQueryEditorAction extends SelectionListenerAction {
@@ -21,30 +18,38 @@
}
public void runWithEvent(Event event) {
- doRun();
- }
-
- protected void doRun() {
boolean showed = false;
IStructuredSelection sel = getStructuredSelection();
if (sel instanceof TreeSelection){
TreePath[] paths = ((TreeSelection)sel).getPaths();
- for (int i = 0; i < paths.length; i++) {
- TreePath path = paths[i];
- ConsoleConfiguration config = (ConsoleConfiguration) path.getSegment(0);
- try {
- openQueryEditor( config, "" );
- } catch(HibernateException he) {
- HibernateConsolePlugin.getDefault().showError(null, "Exception while trying to
open HQL editor", he);
- }
- showed = true;
- }
+ showed = doRun(paths);
}
-
if(!showed) {
openQueryEditor( null, "" );
- }
+ }
}
+ protected boolean doRun(TreePath[] paths) {
+ boolean showed = false;
+ for (int i = 0; i < paths.length; i++) {
+ TreePath path = paths[i];
+ ConsoleConfiguration config = (ConsoleConfiguration) path.getSegment(0);
+ try {
+ openQueryEditor( config, generateQuery(path) );
+ showed = true;
+ } catch(HibernateException he) {
+ HibernateConsolePlugin.getDefault().showError(null, "Exception while trying to
open HQL editor", he);
+ }
+ }
+ return showed;
+ }
+
protected abstract void openQueryEditor(final ConsoleConfiguration config, String
query);
+
+ /**
+ * Generates default query for selected element.
+ * @param selection
+ * @return
+ */
+ protected abstract String generateQuery(TreePath path);
}