Author: snjeza
Date: 2008-06-11 20:23:32 -0400 (Wed, 11 Jun 2008)
New Revision: 8732
Added:
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/IOdaSessionFactory.java
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/ConsoleConfigurationOdaSessionFactory.java
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateOdaQuery.java
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/ServerOdaSessionFactory.java
Removed:
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateQuery.java
Modified:
trunk/birt/plugins/org.jboss.tools.birt.oda/META-INF/MANIFEST.MF
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/Activator.java
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateConnection.java
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateResultSet.java
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateResultSetMetaData.java
Log:
JBIDE-2220 JBoss BIRT Integration
Modified: trunk/birt/plugins/org.jboss.tools.birt.oda/META-INF/MANIFEST.MF
===================================================================
--- trunk/birt/plugins/org.jboss.tools.birt.oda/META-INF/MANIFEST.MF 2008-06-12 00:23:18
UTC (rev 8731)
+++ trunk/birt/plugins/org.jboss.tools.birt.oda/META-INF/MANIFEST.MF 2008-06-12 00:23:32
UTC (rev 8732)
@@ -7,10 +7,10 @@
Bundle-Activator: org.jboss.tools.birt.oda.Activator
Bundle-Vendor: RedHat, Inc.
Bundle-Localization: plugin
-Export-Package: org.jboss.tools.birt.oda.impl
+Export-Package: org.jboss.tools.birt.oda,
+ org.jboss.tools.birt.oda.impl
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.datatools.connectivity.oda;bundle-version="[3.1.0,4.0.0)",
org.eclipse.datatools.connectivity.oda.profile;bundle-version="[3.0.4,4.0.0)",
- org.hibernate.eclipse.console;visibility:=reexport,
- org.hibernate.eclipse;visibility:=reexport
+ org.hibernate.eclipse;resolution:=optional;visibility:=reexport
Eclipse-LazyStart: true
Modified:
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/Activator.java
===================================================================
---
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/Activator.java 2008-06-12
00:23:18 UTC (rev 8731)
+++
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/Activator.java 2008-06-12
00:23:32 UTC (rev 8732)
@@ -57,4 +57,15 @@
return plugin;
}
+ public static Class classForName(String name, Class caller) throws
ClassNotFoundException {
+ try {
+ ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
+ if ( contextClassLoader != null ) {
+ return contextClassLoader.loadClass( name );
+ }
+ }
+ catch ( Throwable e ) {
+ }
+ return Class.forName( name, true, caller.getClassLoader() );
+ }
}
Added:
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/IOdaSessionFactory.java
===================================================================
---
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/IOdaSessionFactory.java
(rev 0)
+++
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/IOdaSessionFactory.java 2008-06-12
00:23:32 UTC (rev 8732)
@@ -0,0 +1,41 @@
+/*************************************************************************************
+ * Copyright (c) 2008 JBoss, a division of Red Hat and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ *
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * JBoss, a division of Red Hat - Initial implementation.
+ ************************************************************************************/
+package org.jboss.tools.birt.oda;
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.datatools.connectivity.oda.OdaException;
+import org.jboss.tools.birt.oda.impl.HibernateOdaQuery;
+import org.jboss.tools.birt.oda.impl.HibernateResultSetMetaData;
+
+/**
+ *
+ * @author snjeza
+ *
+ */
+public interface IOdaSessionFactory {
+
+ public static final String MAX_ROWS = "maxRows";
+ public static final String CONFIGURATION = "configuration";
+ public static final String ORG_HIBERNATE_ECLIPSE_BUNDLE_ID =
"org.hibernate.eclipse";
+
+ void close();
+ boolean isOpen();
+ HibernateResultSetMetaData prepare(String queryText) throws OdaException;
+ void setMaxRows(int max);
+ int getMaxRows();
+ void executeQuery(HibernateOdaQuery query) throws OdaException;
+ Iterator getIterator();
+ List getResult();
+ Object getResult(int rstcol) throws OdaException;
+ void next();
+}
Added:
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/ConsoleConfigurationOdaSessionFactory.java
===================================================================
---
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/ConsoleConfigurationOdaSessionFactory.java
(rev 0)
+++
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/ConsoleConfigurationOdaSessionFactory.java 2008-06-12
00:23:32 UTC (rev 8732)
@@ -0,0 +1,268 @@
+/*************************************************************************************
+ * Copyright (c) 2008 JBoss, a division of Red Hat and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ *
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * JBoss, a division of Red Hat - Initial implementation.
+ ************************************************************************************/
+package org.jboss.tools.birt.oda.impl;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+import java.util.StringTokenizer;
+
+import org.eclipse.datatools.connectivity.oda.IResultSetMetaData;
+import org.eclipse.datatools.connectivity.oda.OdaException;
+import org.hibernate.EntityMode;
+import org.hibernate.HibernateException;
+import org.hibernate.Query;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.console.ConsoleConfiguration;
+import org.hibernate.console.KnownConfigurations;
+import org.hibernate.metadata.ClassMetadata;
+import org.hibernate.type.Type;
+import org.jboss.tools.birt.oda.IOdaSessionFactory;
+
+/**
+ *
+ * @author snjeza
+ *
+ */
+public class ConsoleConfigurationOdaSessionFactory implements
+ IOdaSessionFactory {
+
+ private ConsoleConfiguration consoleConfiguration;
+ private SessionFactory sessionFactory;
+ private int maxRows;
+ private List result;
+ private Iterator iterator;
+ private Type[] queryReturnTypes;
+ private Object currentRow;
+ private HibernateOdaQuery query;
+ private org.hibernate.classic.Session session;
+ private String queryText;
+
+ public ConsoleConfigurationOdaSessionFactory(Properties properties) throws OdaException
{
+ getSessionFactory(properties);
+ String maxRowString = properties.getProperty(IOdaSessionFactory.MAX_ROWS);
+ try {
+ maxRows = new Integer(maxRowString);
+ } catch (NumberFormatException e) {
+ // ignore
+ }
+ }
+
+ public SessionFactory getSessionFactory(Properties properties) throws OdaException {
+ String configurationName = properties.getProperty(CONFIGURATION);
+ ConsoleConfiguration[] configurations =
KnownConfigurations.getInstance().getConfigurations();
+ for (int i = 0; i < configurations.length; i++) {
+ if (configurations[i].getName().equals(configurationName)) {
+ consoleConfiguration=configurations[i];
+ break;
+ }
+ }
+ if (isOpen()) {
+ try {
+ sessionFactory = consoleConfiguration.getSessionFactory();
+ if (sessionFactory == null) {
+ consoleConfiguration.build();
+ consoleConfiguration.buildSessionFactory();
+ sessionFactory = consoleConfiguration.getSessionFactory();
+ }
+ } catch (HibernateException e) {
+ throw new OdaException(e.getLocalizedMessage());
+ }
+ } else {
+ throw new OdaException("Invalid configuration '" +
configurationName + "'");
+ }
+ return sessionFactory;
+ }
+
+ public void close() {
+ consoleConfiguration = null;
+ sessionFactory = null;
+ if (session != null) {
+ session.close();
+ session = null;
+ }
+ }
+
+ public boolean isOpen() {
+ return consoleConfiguration != null;
+ }
+
+ public SessionFactory getSessionFactory() {
+ return sessionFactory;
+ }
+
+ public HibernateResultSetMetaData prepare(String queryText) throws OdaException {
+ this.queryText = queryText;
+ List arColsType = new ArrayList();
+ List arCols = new ArrayList();
+ List arColClass = new ArrayList();
+ String[] props = null;
+ Session session = null;
+ try {
+ session = getSessionFactory().openSession();
+ Query query = session.createQuery(queryText);
+ int maxRows = getMaxRows();
+ if (maxRows > 0) {
+ query.setFirstResult(0);
+ query.setMaxResults(maxRows);
+ }
+ this.maxRows = maxRows;
+ Type[] qryReturnTypes = query.getReturnTypes();
+ if (qryReturnTypes.length > 0 && qryReturnTypes[0].isEntityType()) {
+ for (int j = 0; j < qryReturnTypes.length; j++) {
+ String clsName = qryReturnTypes[j].getName();
+ props = getHibernateProp(clsName);
+ for (int x = 0; x < props.length; x++) {
+ String propType = getHibernatePropTypes(clsName,
+ props[x]);
+ if (DataTypes.isValidType(propType)) {
+ arColsType.add(propType);
+ arCols.add(props[x]);
+ arColClass.add(clsName);
+ } else {
+ //throw new OdaException("Data Type is Not Valid");
+ arColsType.add(DataTypes.UNKNOWN);
+ arCols.add(props[x]);
+ arColClass.add("java.lang.String");
+ }
+ }
+ }
+ } else {
+ props = extractColumns(query.getQueryString());
+ for (int t = 0; t < qryReturnTypes.length; t++) {
+ if (DataTypes.isValidType(qryReturnTypes[t].getName())) {
+ arColsType.add(qryReturnTypes[t].getName());
+ arCols.add(props[t]);
+ } else {
+ throw new OdaException("'"
+ + qryReturnTypes[t].getName()
+ + "' is not a valid type.");
+ }
+ }
+
+ }
+ String[] arLabels = (String[]) arCols.toArray(new String[arCols
+ .size()]);
+ for (int j = 0; j < arLabels.length; j++) {
+ arLabels[j] = arLabels[j].replace('.', ':');
+ }
+
+ return new HibernateResultSetMetaData(arLabels,
+ (String[]) arColsType
+ .toArray(new String[arColsType.size()]), arLabels,
+ (String[]) arColClass
+ .toArray(new String[arColClass.size()]));
+ } catch (Exception e) {
+ throw new OdaException(e.getLocalizedMessage());
+ } finally {
+ if (session != null) {
+ session.close();
+ }
+ }
+ }
+
+ private static String[] extractColumns(final String query) {
+ int fromPosition = query.toLowerCase().indexOf("from");
+ int selectPosition = query.toLowerCase().indexOf("select");
+ if (selectPosition >= 0) {
+ String columns = query.substring(selectPosition + 6, fromPosition);
+ StringTokenizer st = new StringTokenizer(columns, ",");
+ List columnList = new ArrayList();
+ while (st.hasMoreTokens()) {
+ columnList.add(st.nextToken().trim());
+ }
+ return (String[]) columnList.toArray(new String[0]);
+ } else {
+ return null;
+ }
+ }
+
+ private String[] getHibernateProp(String className){
+ SessionFactory sf = getSessionFactory();
+ String[] properties = sf.getClassMetadata(className).getPropertyNames();
+ return( properties);
+ }
+
+ private String getHibernatePropTypes(String className, String property){
+ SessionFactory sf = getSessionFactory();
+ Type hibClassProps = sf.getClassMetadata(className).getPropertyType(property);
+ return(hibClassProps.getName());
+
+ }
+
+ public int getMaxRows() {
+ return maxRows;
+ }
+
+ public void setMaxRows(int maxRows) {
+ this.maxRows = maxRows;
+ }
+
+ public void executeQuery(HibernateOdaQuery query) throws OdaException {
+ this.query = query;
+ try {
+ session = getSessionFactory().openSession();
+ Query q = session.createQuery(queryText);
+ result = q.list();
+ iterator = result.iterator();
+ this.queryReturnTypes = q.getReturnTypes();
+ } catch (HibernateException e) {
+ throw new OdaException(e.getLocalizedMessage());
+ }
+ }
+
+ public Iterator getIterator() {
+ return iterator;
+ }
+
+ public List getResult() {
+ return result;
+ }
+
+ public Object getResult(int rstcol) throws OdaException {
+ Object obj = this.currentRow;
+ Object value = null;
+ try {
+ if (queryReturnTypes.length > 0
+ && queryReturnTypes[0].isEntityType()) {
+ String checkClass = ((HibernateResultSetMetaData) getMetaData())
+ .getColumnClass(rstcol);
+ SessionFactory sf = getSessionFactory();
+ ClassMetadata metadata = sf.getClassMetadata(checkClass);
+ if (metadata == null) {
+ metadata = sf.getClassMetadata(obj.getClass());
+ }
+ value = metadata.getPropertyValue(obj, getMetaData()
+ .getColumnName(rstcol), EntityMode.POJO);
+ } else {
+ if (getMetaData().getColumnCount() == 1) {
+ value = obj;
+ } else {
+ Object[] values = (Object[]) obj;
+ value = values[rstcol - 1];
+ }
+ }
+ } catch (Exception e) {
+ throw new OdaException(e.getLocalizedMessage());
+ }
+ return (value);
+ }
+
+ private IResultSetMetaData getMetaData() throws OdaException {
+ return query.getMetaData();
+ }
+
+ public void next() {
+ currentRow = getIterator().next();
+ }
+}
Modified:
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateConnection.java
===================================================================
---
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateConnection.java 2008-06-12
00:23:18 UTC (rev 8731)
+++
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateConnection.java 2008-06-12
00:23:32 UTC (rev 8732)
@@ -8,16 +8,16 @@
******************************************************************************/
package org.jboss.tools.birt.oda.impl;
+import java.util.Map;
import java.util.Properties;
+import org.eclipse.core.runtime.Platform;
import org.eclipse.datatools.connectivity.oda.IConnection;
import org.eclipse.datatools.connectivity.oda.IDataSetMetaData;
import org.eclipse.datatools.connectivity.oda.IQuery;
import org.eclipse.datatools.connectivity.oda.OdaException;
-import org.hibernate.HibernateException;
-import org.hibernate.SessionFactory;
-import org.hibernate.console.ConsoleConfiguration;
-import org.hibernate.console.KnownConfigurations;
+import org.jboss.tools.birt.oda.IOdaSessionFactory;
+import org.osgi.framework.Bundle;
/**
* Implementation class of IConnection for an ODA runtime driver.
@@ -26,47 +26,22 @@
*/
public class HibernateConnection implements IConnection
{
- public static final String MAX_ROWS = "maxRows";
- public static final String CONFIGURATION = "configuration";
- private boolean isOpen = false;
- private ConsoleConfiguration consoleConfiguration;
- private SessionFactory sessionFactory;
- private Integer maxRows = -1;
+ private IOdaSessionFactory odaSessionFactory;
+ private Map appContext;
/*
* @see org.eclipse.datatools.connectivity.oda.IConnection#open(java.util.Properties)
*/
public void open( Properties connProperties ) throws OdaException
{
- String configurationName = connProperties.getProperty(CONFIGURATION);
- ConsoleConfiguration[] configurations =
KnownConfigurations.getInstance().getConfigurations();
- for (int i = 0; i < configurations.length; i++) {
- if (configurations[i].getName().equals(configurationName)) {
- consoleConfiguration=configurations[i];
- break;
- }
+ Bundle bundle =
Platform.getBundle(IOdaSessionFactory.ORG_HIBERNATE_ECLIPSE_BUNDLE_ID);
+ if (bundle != null) {
+ odaSessionFactory = new ConsoleConfigurationOdaSessionFactory(connProperties);
+ } else {
+ //parentClassLoader = appContext.get(key);
+ // FIXME
+ odaSessionFactory = new ServerOdaSessionFactory(connProperties);
}
- isOpen = (consoleConfiguration != null);
- if (isOpen) {
- try {
- sessionFactory = consoleConfiguration.getSessionFactory();
- if (sessionFactory == null) {
- consoleConfiguration.build();
- consoleConfiguration.buildSessionFactory();
- sessionFactory = consoleConfiguration.getSessionFactory();
- }
- } catch (HibernateException e) {
- throw new OdaException(e.getLocalizedMessage());
- }
- } else {
- throw new OdaException("Invalid configuration '" +
configurationName + "'");
- }
- String maxRowString = connProperties.getProperty(MAX_ROWS);
- try {
- maxRows = new Integer(maxRowString);
- } catch (NumberFormatException e) {
- // ignore
- }
}
/*
@@ -74,7 +49,10 @@
*/
public void setAppContext( Object context ) throws OdaException
{
- // do nothing; assumes no support for pass-through context
+ if (!(context instanceof Map)) {
+ throw new OdaException("Invalid AppContext");
+ }
+ this.appContext = (Map) context;
}
/*
@@ -82,9 +60,7 @@
*/
public void close() throws OdaException
{
- consoleConfiguration = null;
- sessionFactory = null;
- isOpen = false;
+ odaSessionFactory.close();
}
/*
@@ -92,7 +68,7 @@
*/
public boolean isOpen() throws OdaException
{
- return isOpen;
+ return odaSessionFactory != null && odaSessionFactory.isOpen();
}
/*
@@ -112,7 +88,7 @@
{
// assumes that this driver supports only one type of data set,
// ignores the specified dataSetType
- return new HibernateQuery(this);
+ return new HibernateOdaQuery(this);
}
/*
@@ -139,12 +115,12 @@
// do nothing; assumes no transaction support needed
}
- public Integer getMaxRows() {
- return maxRows;
+ /*public SessionFactory getSessionFactory() {
+ return odaSessionFactory.getSessionFactory();
+ }*/
+
+ public IOdaSessionFactory getOdaSessionFactory() {
+ return odaSessionFactory;
}
-
- public SessionFactory getSessionFactory() {
- return sessionFactory;
- }
}
Copied:
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateOdaQuery.java
(from rev 8158,
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateQuery.java)
===================================================================
---
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateOdaQuery.java
(rev 0)
+++
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateOdaQuery.java 2008-06-12
00:23:32 UTC (rev 8732)
@@ -0,0 +1,323 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ ******************************************************************************/
+package org.jboss.tools.birt.oda.impl;
+
+import java.math.BigDecimal;
+import java.sql.Date;
+import java.sql.Time;
+import java.sql.Timestamp;
+
+import org.eclipse.datatools.connectivity.oda.IParameterMetaData;
+import org.eclipse.datatools.connectivity.oda.IQuery;
+import org.eclipse.datatools.connectivity.oda.IResultSet;
+import org.eclipse.datatools.connectivity.oda.IResultSetMetaData;
+import org.eclipse.datatools.connectivity.oda.OdaException;
+import org.eclipse.datatools.connectivity.oda.SortSpec;
+
+/**
+ * Implementation class of IQuery for an ODA runtime driver.
+ *
+ * @author snjeza
+ */
+public class HibernateOdaQuery implements IQuery
+{
+ private HibernateConnection connection;
+ private HibernateResultSetMetaData resultSetMetaData;
+
+ public HibernateOdaQuery(HibernateConnection connection) {
+ this.connection = connection;
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#prepare(java.lang.String)
+ */
+ public void prepare(String queryText) throws OdaException {
+ this.resultSetMetaData = getConnection().getOdaSessionFactory().prepare(queryText);
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setAppContext(java.lang.Object)
+ */
+ public void setAppContext( Object context ) throws OdaException
+ {
+ // do nothing; assumes no support for pass-through context
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#close()
+ */
+ public void close() throws OdaException
+ {
+ // TODO Auto-generated method stub
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#getMetaData()
+ */
+ public IResultSetMetaData getMetaData() throws OdaException
+ {
+ return this.resultSetMetaData;
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#executeQuery()
+ */
+ public IResultSet executeQuery() throws OdaException
+ {
+ try {
+ return new HibernateResultSet(this);
+ } catch (Exception e) {
+ throw new OdaException(e.getLocalizedMessage());
+ }
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setProperty(java.lang.String,
java.lang.String)
+ */
+ public void setProperty( String name, String value ) throws OdaException
+ {
+ // do nothing; assumes no data set query property
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setMaxRows(int)
+ */
+ public void setMaxRows( int max ) throws OdaException
+ {
+ getConnection().getOdaSessionFactory().setMaxRows(max);
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#getMaxRows()
+ */
+ public int getMaxRows() throws OdaException
+ {
+ return getConnection().getOdaSessionFactory().getMaxRows();
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#clearInParameters()
+ */
+ public void clearInParameters() throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to input parameter
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setInt(java.lang.String, int)
+ */
+ public void setInt( String parameterName, int value ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to named input parameter
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setInt(int, int)
+ */
+ public void setInt( int parameterId, int value ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to input parameter
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setDouble(java.lang.String,
double)
+ */
+ public void setDouble( String parameterName, double value ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to named input parameter
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setDouble(int, double)
+ */
+ public void setDouble( int parameterId, double value ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to input parameter
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setBigDecimal(java.lang.String,
java.math.BigDecimal)
+ */
+ public void setBigDecimal( String parameterName, BigDecimal value ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to named input parameter
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setBigDecimal(int,
java.math.BigDecimal)
+ */
+ public void setBigDecimal( int parameterId, BigDecimal value ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to input parameter
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setString(java.lang.String,
java.lang.String)
+ */
+ public void setString( String parameterName, String value ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to named input parameter
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setString(int, java.lang.String)
+ */
+ public void setString( int parameterId, String value ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to input parameter
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setDate(java.lang.String,
java.sql.Date)
+ */
+ public void setDate( String parameterName, Date value ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to named input parameter
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setDate(int, java.sql.Date)
+ */
+ public void setDate( int parameterId, Date value ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to input parameter
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setTime(java.lang.String,
java.sql.Time)
+ */
+ public void setTime( String parameterName, Time value ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to named input parameter
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setTime(int, java.sql.Time)
+ */
+ public void setTime( int parameterId, Time value ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to input parameter
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setTimestamp(java.lang.String,
java.sql.Timestamp)
+ */
+ public void setTimestamp( String parameterName, Timestamp value ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to named input parameter
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setTimestamp(int,
java.sql.Timestamp)
+ */
+ public void setTimestamp( int parameterId, Timestamp value ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to input parameter
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setBoolean(java.lang.String,
boolean)
+ */
+ public void setBoolean( String parameterName, boolean value )
+ throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to named input parameter
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setBoolean(int, boolean)
+ */
+ public void setBoolean( int parameterId, boolean value )
+ throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to input parameter
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setNull(java.lang.String)
+ */
+ public void setNull( String parameterName ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to named input parameter
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#setNull(int)
+ */
+ public void setNull( int parameterId ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to input parameter
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#findInParameter(java.lang.String)
+ */
+ public int findInParameter( String parameterName ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to named input parameter
+ return 0;
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#getParameterMetaData()
+ */
+ public IParameterMetaData getParameterMetaData() throws OdaException
+ {
+ /* TODO Auto-generated method stub
+ * Replace with implementation to return an instance
+ * based on this prepared query.
+ */
+ return new HibernateParameterMetaData();
+ }
+
+ /*
+ * @see
org.eclipse.datatools.connectivity.oda.IQuery#setSortSpec(org.eclipse.datatools.connectivity.oda.SortSpec)
+ */
+ public void setSortSpec( SortSpec sortBy ) throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to sorting, assumes not supported
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * @see org.eclipse.datatools.connectivity.oda.IQuery#getSortSpec()
+ */
+ public SortSpec getSortSpec() throws OdaException
+ {
+ // TODO Auto-generated method stub
+ // only applies to sorting
+ return null;
+ }
+
+ public HibernateConnection getConnection() {
+ return connection;
+ }
+
+}
Deleted:
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateQuery.java
===================================================================
---
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateQuery.java 2008-06-12
00:23:18 UTC (rev 8731)
+++
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateQuery.java 2008-06-12
00:23:32 UTC (rev 8732)
@@ -1,430 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2007 Red Hat, Inc.
- * Distributed under license by Red Hat, Inc. All rights reserved.
- * This program is made available under the terms of the
- * Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at
http://www.eclipse.org/legal/epl-v10.html
- *
- ******************************************************************************/
-package org.jboss.tools.birt.oda.impl;
-
-import java.math.BigDecimal;
-import java.sql.Date;
-import java.sql.Time;
-import java.sql.Timestamp;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.StringTokenizer;
-
-import org.eclipse.datatools.connectivity.oda.IParameterMetaData;
-import org.eclipse.datatools.connectivity.oda.IResultSet;
-import org.eclipse.datatools.connectivity.oda.IResultSetMetaData;
-import org.eclipse.datatools.connectivity.oda.IQuery;
-import org.eclipse.datatools.connectivity.oda.OdaException;
-import org.eclipse.datatools.connectivity.oda.SortSpec;
-import org.hibernate.Query;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-import org.hibernate.type.Type;
-
-/**
- * Implementation class of IQuery for an ODA runtime driver.
- *
- * @author snjeza
- */
-public class HibernateQuery implements IQuery
-{
- private Integer _maxRows;
- private HibernateConnection connection;
- private HibernateResultSetMetaData resultSetMetaData;
- private String queryText;
-
- public HibernateQuery(HibernateConnection connection) {
- this.connection = connection;
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#prepare(java.lang.String)
- */
- public void prepare(String queryText) throws OdaException {
- this.queryText = queryText;
- List arColsType = new ArrayList();
- List arCols = new ArrayList();
- List arColClass = new ArrayList();
- String[] props = null;
- Session session = null;
- try {
- session = connection.getSessionFactory().openSession();
- Query query = session.createQuery(queryText);
- int maxRows = connection.getMaxRows();
- if (maxRows > 0) {
- query.setFirstResult(0);
- query.setMaxResults(maxRows);
- }
- _maxRows = maxRows;
- Type[] qryReturnTypes = query.getReturnTypes();
- if (qryReturnTypes.length > 0 && qryReturnTypes[0].isEntityType()) {
- for (int j = 0; j < qryReturnTypes.length; j++) {
- String clsName = qryReturnTypes[j].getName();
- props = getHibernateProp(clsName);
- for (int x = 0; x < props.length; x++) {
- String propType = getHibernatePropTypes(clsName,
- props[x]);
- if (DataTypes.isValidType(propType)) {
- arColsType.add(propType);
- arCols.add(props[x]);
- arColClass.add(clsName);
- } else {
- //throw new OdaException("Data Type is Not Valid");
- arColsType.add(DataTypes.UNKNOWN);
- arCols.add(props[x]);
- arColClass.add("java.lang.String");
- }
- }
- }
- } else {
- props = extractColumns(query.getQueryString());
- for (int t = 0; t < qryReturnTypes.length; t++) {
- if (DataTypes.isValidType(qryReturnTypes[t].getName())) {
- arColsType.add(qryReturnTypes[t].getName());
- arCols.add(props[t]);
- } else {
- throw new OdaException("'"
- + qryReturnTypes[t].getName()
- + "' is not a valid type.");
- }
- }
-
- }
- String[] arLabels = (String[]) arCols.toArray(new String[arCols
- .size()]);
- for (int j = 0; j < arLabels.length; j++) {
- arLabels[j] = arLabels[j].replace('.', ':');
- }
-
- this.resultSetMetaData = new HibernateResultSetMetaData(arLabels,
- (String[]) arColsType
- .toArray(new String[arColsType.size()]), arLabels,
- (String[]) arColClass
- .toArray(new String[arColClass.size()]));
- } catch (Exception e) {
- throw new OdaException(e.getLocalizedMessage());
- } finally {
- if (session != null) {
- session.close();
- }
- }
- }
-
- private static String[] extractColumns(final String query) {
- int fromPosition = query.toLowerCase().indexOf("from");
- int selectPosition = query.toLowerCase().indexOf("select");
- if (selectPosition >= 0) {
- String columns = query.substring(selectPosition + 6, fromPosition);
- StringTokenizer st = new StringTokenizer(columns, ",");
- List columnList = new ArrayList();
- while (st.hasMoreTokens()) {
- columnList.add(st.nextToken().trim());
- }
- return (String[]) columnList.toArray(new String[0]);
- } else {
- return null;
- }
- }
-
- private String[] getHibernateProp(String className){
- SessionFactory sf = connection.getSessionFactory();
- String[] properties = sf.getClassMetadata(className).getPropertyNames();
- return( properties);
- }
-
- private String getHibernatePropTypes(String className, String property){
- SessionFactory sf = connection.getSessionFactory();
- Type hibClassProps = sf.getClassMetadata(className).getPropertyType(property);
- return(hibClassProps.getName());
-
- }
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setAppContext(java.lang.Object)
- */
- public void setAppContext( Object context ) throws OdaException
- {
- // do nothing; assumes no support for pass-through context
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#close()
- */
- public void close() throws OdaException
- {
- // TODO Auto-generated method stub
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#getMetaData()
- */
- public IResultSetMetaData getMetaData() throws OdaException
- {
- return this.resultSetMetaData;
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#executeQuery()
- */
- public IResultSet executeQuery() throws OdaException
- {
- try {
- return new HibernateResultSet(this);
- } catch (Exception e) {
- throw new OdaException(e.getLocalizedMessage());
- }
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setProperty(java.lang.String,
java.lang.String)
- */
- public void setProperty( String name, String value ) throws OdaException
- {
- // do nothing; assumes no data set query property
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setMaxRows(int)
- */
- public void setMaxRows( int max ) throws OdaException
- {
- _maxRows = max;
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#getMaxRows()
- */
- public int getMaxRows() throws OdaException
- {
- return _maxRows;
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#clearInParameters()
- */
- public void clearInParameters() throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to input parameter
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setInt(java.lang.String, int)
- */
- public void setInt( String parameterName, int value ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to named input parameter
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setInt(int, int)
- */
- public void setInt( int parameterId, int value ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to input parameter
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setDouble(java.lang.String,
double)
- */
- public void setDouble( String parameterName, double value ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to named input parameter
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setDouble(int, double)
- */
- public void setDouble( int parameterId, double value ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to input parameter
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setBigDecimal(java.lang.String,
java.math.BigDecimal)
- */
- public void setBigDecimal( String parameterName, BigDecimal value ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to named input parameter
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setBigDecimal(int,
java.math.BigDecimal)
- */
- public void setBigDecimal( int parameterId, BigDecimal value ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to input parameter
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setString(java.lang.String,
java.lang.String)
- */
- public void setString( String parameterName, String value ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to named input parameter
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setString(int, java.lang.String)
- */
- public void setString( int parameterId, String value ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to input parameter
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setDate(java.lang.String,
java.sql.Date)
- */
- public void setDate( String parameterName, Date value ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to named input parameter
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setDate(int, java.sql.Date)
- */
- public void setDate( int parameterId, Date value ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to input parameter
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setTime(java.lang.String,
java.sql.Time)
- */
- public void setTime( String parameterName, Time value ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to named input parameter
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setTime(int, java.sql.Time)
- */
- public void setTime( int parameterId, Time value ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to input parameter
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setTimestamp(java.lang.String,
java.sql.Timestamp)
- */
- public void setTimestamp( String parameterName, Timestamp value ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to named input parameter
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setTimestamp(int,
java.sql.Timestamp)
- */
- public void setTimestamp( int parameterId, Timestamp value ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to input parameter
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setBoolean(java.lang.String,
boolean)
- */
- public void setBoolean( String parameterName, boolean value )
- throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to named input parameter
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setBoolean(int, boolean)
- */
- public void setBoolean( int parameterId, boolean value )
- throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to input parameter
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setNull(java.lang.String)
- */
- public void setNull( String parameterName ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to named input parameter
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.datatools.connectivity.oda.IQuery#setNull(int)
- */
- public void setNull( int parameterId ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to input parameter
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#findInParameter(java.lang.String)
- */
- public int findInParameter( String parameterName ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to named input parameter
- return 0;
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#getParameterMetaData()
- */
- public IParameterMetaData getParameterMetaData() throws OdaException
- {
- /* TODO Auto-generated method stub
- * Replace with implementation to return an instance
- * based on this prepared query.
- */
- return new HibernateParameterMetaData();
- }
-
- /*
- * @see
org.eclipse.datatools.connectivity.oda.IQuery#setSortSpec(org.eclipse.datatools.connectivity.oda.SortSpec)
- */
- public void setSortSpec( SortSpec sortBy ) throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to sorting, assumes not supported
- throw new UnsupportedOperationException();
- }
-
- /*
- * @see org.eclipse.datatools.connectivity.oda.IQuery#getSortSpec()
- */
- public SortSpec getSortSpec() throws OdaException
- {
- // TODO Auto-generated method stub
- // only applies to sorting
- return null;
- }
-
- public HibernateConnection getConnection() {
- return connection;
- }
-
- public String getQueryText() {
- return queryText;
- }
-
-}
Modified:
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateResultSet.java
===================================================================
---
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateResultSet.java 2008-06-12
00:23:18 UTC (rev 8731)
+++
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateResultSet.java 2008-06-12
00:23:32 UTC (rev 8732)
@@ -24,13 +24,6 @@
import org.eclipse.datatools.connectivity.oda.IResultSet;
import org.eclipse.datatools.connectivity.oda.IResultSetMetaData;
import org.eclipse.datatools.connectivity.oda.OdaException;
-import org.hibernate.EntityMode;
-import org.hibernate.HibernateException;
-import org.hibernate.Query;
-import org.hibernate.SessionFactory;
-import org.hibernate.classic.Session;
-import org.hibernate.metadata.ClassMetadata;
-import org.hibernate.type.Type;
/**
* Implementation class of IResultSet for an ODA runtime driver.
@@ -39,32 +32,16 @@
*/
public class HibernateResultSet implements IResultSet {
private int _maxRows;
- private HibernateQuery query;
- private List result;
- private Iterator iterator;
- private Object currentRow;
+ private HibernateOdaQuery query;
+
private int rowNumber = -1;
private boolean wasNull;
- private Type[] queryReturnTypes;
- private static DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
- private Session session;
+
+ private static DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
- public HibernateResultSet(HibernateQuery query) throws OdaException {
+ public HibernateResultSet(HibernateOdaQuery query) throws OdaException {
this.query = query;
- try {
- _maxRows = query.getMaxRows();
- } catch (OdaException e) {
- // ignore
- }
- try {
- session = query.getConnection().getSessionFactory().openSession();
- Query q = session.createQuery(query.getQueryText());
- result = q.list();
- iterator = result.iterator();
- this.queryReturnTypes = q.getReturnTypes();
- } catch (HibernateException e) {
- throw new OdaException(e.getLocalizedMessage());
- }
+ query.getConnection().getOdaSessionFactory().executeQuery(query);
}
/*
@@ -95,8 +72,8 @@
* @see org.eclipse.datatools.connectivity.oda.IResultSet#next()
*/
public boolean next() throws OdaException {
- if (iterator.hasNext()) {
- currentRow = iterator.next();
+ if (getIterator().hasNext()) {
+ query.getConnection().getOdaSessionFactory().next();
rowNumber++;
return true;
}
@@ -104,17 +81,25 @@
}
+ private Iterator getIterator() {
+ return query.getConnection().getOdaSessionFactory().getIterator();
+ }
+
/*
* @see org.eclipse.datatools.connectivity.oda.IResultSet#close()
*/
public void close() throws OdaException {
+ List result = getResult();
if (result != null)
result.clear();
- if (session != null)
- session.close();
+ query.getConnection().getOdaSessionFactory().close();
result = null;
}
+ private List getResult() {
+ return query.getConnection().getOdaSessionFactory().getResult();
+ }
+
/*
* @see org.eclipse.datatools.connectivity.oda.IResultSet#getRow()
*/
@@ -124,32 +109,7 @@
}
private Object getResult(int rstcol) throws OdaException {
- Object obj = this.currentRow;
- Object value = null;
- try {
- if (queryReturnTypes.length > 0
- && queryReturnTypes[0].isEntityType()) {
- String checkClass = ((HibernateResultSetMetaData) getMetaData())
- .getColumnClass(rstcol);
- SessionFactory sf = query.getConnection().getSessionFactory();
- ClassMetadata metadata = sf.getClassMetadata(checkClass);
- if (metadata == null) {
- metadata = sf.getClassMetadata(obj.getClass());
- }
- value = metadata.getPropertyValue(obj, getMetaData()
- .getColumnName(rstcol), EntityMode.POJO);
- } else {
- if (getMetaData().getColumnCount() == 1) {
- value = obj;
- } else {
- Object[] values = (Object[]) obj;
- value = values[rstcol - 1];
- }
- }
- } catch (Exception e) {
- throw new OdaException(e.getLocalizedMessage());
- }
- return (value);
+ return query.getConnection().getOdaSessionFactory().getResult(rstcol);
}
/*
Modified:
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateResultSetMetaData.java
===================================================================
---
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateResultSetMetaData.java 2008-06-12
00:23:18 UTC (rev 8731)
+++
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/HibernateResultSetMetaData.java 2008-06-12
00:23:32 UTC (rev 8732)
@@ -13,8 +13,6 @@
import org.eclipse.datatools.connectivity.oda.IResultSetMetaData;
import org.eclipse.datatools.connectivity.oda.OdaException;
-import org.hibernate.Query;
-import org.hibernate.type.Type;
/**
* Implementation class of IResultSetMetaData for an ODA runtime driver.
@@ -28,7 +26,7 @@
private String[] columnType = null;
private String[] columnLabel = null;
private String[] columnClass = null;
- private HibernateQuery query;
+ //private HibernateOdaQuery query;
HibernateResultSetMetaData( String[] cName, String[] cType, String[] cLabel, String[]
cClass )
throws OdaException
Added:
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/ServerOdaSessionFactory.java
===================================================================
---
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/ServerOdaSessionFactory.java
(rev 0)
+++
trunk/birt/plugins/org.jboss.tools.birt.oda/src/org/jboss/tools/birt/oda/impl/ServerOdaSessionFactory.java 2008-06-12
00:23:32 UTC (rev 8732)
@@ -0,0 +1,333 @@
+/*************************************************************************************
+ * Copyright (c) 2008 JBoss, a division of Red Hat and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ *
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * JBoss, a division of Red Hat - Initial implementation.
+ ************************************************************************************/
+package org.jboss.tools.birt.oda.impl;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+import java.util.StringTokenizer;
+
+import javax.naming.InitialContext;
+
+import org.eclipse.datatools.connectivity.oda.IResultSetMetaData;
+import org.eclipse.datatools.connectivity.oda.OdaException;
+import org.jboss.tools.birt.oda.Activator;
+import org.jboss.tools.birt.oda.IOdaSessionFactory;
+
+/**
+ *
+ * @author snjeza
+ *
+ */
+public class ServerOdaSessionFactory implements IOdaSessionFactory {
+
+ private static final Integer INTZERO = new Integer(0);
+ private static Class[] emptyClassArg = new Class[0];
+ private static Object[] emptyObjectArg = new Object[0];
+ private Object sessionFactory;
+ private Integer maxRows;
+ private String queryText;
+ private List result;
+ private Iterator iterator;
+ private Object currentRow;
+ private HibernateOdaQuery query;
+ private Object session;
+ private Object[] queryReturnTypes;
+
+ public ServerOdaSessionFactory(Properties properties) throws OdaException {
+ getSessionFactory(properties);
+ String maxRowString = properties.getProperty(IOdaSessionFactory.MAX_ROWS);
+ try {
+ maxRows = new Integer(maxRowString);
+ } catch (NumberFormatException e) {
+ // ignore
+ }
+ }
+
+ public Object getSessionFactory(Properties properties)
+ throws OdaException {
+ String configurationName = properties.getProperty(CONFIGURATION);
+ if (sessionFactory == null) {
+ InitialContext ctx = null;
+ try {
+ ctx = new InitialContext();
+ Object obj = ctx.lookup("java:/" + configurationName);
+ sessionFactory = obj;
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new OdaException(
+ "Cannot create Hibernate session factory");
+ }
+
+ }
+ return sessionFactory;
+ }
+
+ public Object getSessionFactory() {
+ return sessionFactory;
+ }
+
+ public void close() {
+ closeSession(session);
+ }
+
+ public boolean isOpen() {
+ if (sessionFactory == null)
+ return false;
+ boolean isClosed = true;
+ try {
+ Method method = sessionFactory.getClass().getMethod("isClosed", new
Class[0]);
+ if (method != null) {
+ Object closed = method.invoke(sessionFactory, new Object[0]);
+ isClosed = ((Boolean) closed).booleanValue();
+ }
+ } catch (Exception e) {
+ // ignore
+ }
+ return !isClosed;
+ }
+
+ public int getMaxRows() {
+ return maxRows;
+ }
+
+ public HibernateResultSetMetaData prepare(String queryText)
+ throws OdaException {
+ this.queryText = queryText;
+ List arColsType = new ArrayList();
+ List arCols = new ArrayList();
+ List arColClass = new ArrayList();
+ String[] props = null;
+ Object session = null;
+
+ try {
+ session = openSession();
+ Object query = createQuery(session, queryText);
+ if (maxRows > 0) {
+ Method setFirstResult = query.getClass().getMethod("setFirstResult", new
Class[] {Integer.TYPE});
+ setFirstResult.invoke(query, new Object[] {INTZERO});
+ Method setMaxResults = query.getClass().getMethod("setMaxResults", new
Class[] {Integer.TYPE});
+ setMaxResults.invoke(query, new Object[] {new Integer(maxRows)});
+ }
+ Method getReturnTypes = query.getClass().getMethod("getReturnTypes",
emptyClassArg);
+ Object returnType = getReturnTypes.invoke(query, emptyObjectArg);
+ Object[] qryReturnTypes = (Object[]) returnType;
+ if (checkEntityType(qryReturnTypes)) {
+ for (int j = 0; j < qryReturnTypes.length; j++) {
+ String clsName = getReturnTypeName(qryReturnTypes[j]);
+ props = getHibernateProp(clsName);
+ for (int x = 0; x < props.length; x++) {
+ String propType = getHibernatePropTypes(clsName,
+ props[x]);
+ if (DataTypes.isValidType(propType)) {
+ arColsType.add(propType);
+ arCols.add(props[x]);
+ arColClass.add(clsName);
+ } else {
+ arColsType.add(DataTypes.UNKNOWN);
+ arCols.add(props[x]);
+ arColClass.add("java.lang.String");
+ }
+ }
+ }
+ } else {
+ props = extractColumns(queryText);
+ for (int t = 0; t < qryReturnTypes.length; t++) {
+ String typeName = getReturnTypeName(qryReturnTypes[t]);
+ if (DataTypes.isValidType(typeName)) {
+ arColsType.add(typeName);
+ arCols.add(props[t]);
+ } else {
+ throw new OdaException("'"
+ + typeName
+ + "' is not a valid type.");
+ }
+ }
+ }
+ String[] arLabels = (String[]) arCols.toArray(new String[arCols
+ .size()]);
+ for (int j = 0; j < arLabels.length; j++) {
+ arLabels[j] = arLabels[j].replace('.', ':');
+ }
+
+ return new HibernateResultSetMetaData(arLabels,
+ (String[]) arColsType
+ .toArray(new String[arColsType.size()]), arLabels,
+ (String[]) arColClass
+ .toArray(new String[arColClass.size()]));
+ } catch (Exception e) {
+ throw new OdaException(e.getLocalizedMessage());
+ } finally {
+ closeSession(session);
+ }
+ }
+
+ private void closeSession(Object session) {
+ if (session != null) {
+ try {
+ Method close = session.getClass().getMethod("close", emptyClassArg);
+ close.invoke(session, emptyObjectArg);
+ session = null;
+ } catch (Exception e) {
+ // ignore
+ }
+ }
+ }
+
+ private boolean checkEntityType(Object[] qryReturnTypes) throws SecurityException,
NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
+ Object first = qryReturnTypes[0];
+ Method isEntityType = first.getClass().getMethod("isEntityType",
emptyClassArg);
+ Object ret = isEntityType.invoke(first, emptyObjectArg);
+ boolean isEntity = ((Boolean)ret).booleanValue();
+ return qryReturnTypes.length > 0 && isEntity;
+ }
+
+ private Object createQuery(Object session, String queryText)
+ throws NoSuchMethodException, IllegalAccessException,
+ InvocationTargetException {
+ Method createQuery = session.getClass().getMethod("createQuery", new Class[]
{String.class});
+ Object query = createQuery.invoke(session, new Object[] {queryText});
+ return query;
+ }
+
+ private Object openSession() throws NoSuchMethodException,
+ IllegalAccessException, InvocationTargetException {
+ Object session;
+ Method openSession = sessionFactory.getClass().getMethod("openSession",
emptyClassArg);
+ session = openSession.invoke(sessionFactory, emptyObjectArg);
+ return session;
+ }
+
+ private static String[] extractColumns(final String query) {
+ int fromPosition = query.toLowerCase().indexOf("from");
+ int selectPosition = query.toLowerCase().indexOf("select");
+ if (selectPosition >= 0) {
+ String columns = query.substring(selectPosition + 6, fromPosition);
+ StringTokenizer st = new StringTokenizer(columns, ",");
+ List columnList = new ArrayList();
+ while (st.hasMoreTokens()) {
+ columnList.add(st.nextToken().trim());
+ }
+ return (String[]) columnList.toArray(new String[0]);
+ } else {
+ return null;
+ }
+ }
+
+ private String getReturnTypeName(Object returnType) throws SecurityException,
NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
+ Method getName = returnType.getClass().getMethod("getName", emptyClassArg);
+ Object name = getName.invoke(returnType, emptyObjectArg);
+ return (String) name;
+ }
+
+ private String[] getHibernateProp(String className) throws SecurityException,
NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
InvocationTargetException{
+ Object classMetadata = getClassMetadata(className);
+ Method getPropertyNames =
classMetadata.getClass().getMethod("getPropertyNames", emptyClassArg);
+ String[] properties = (String[]) getPropertyNames.invoke(classMetadata,
emptyObjectArg);
+ return properties;
+ }
+
+ private Object getClassMetadata(String className)
+ throws NoSuchMethodException, IllegalAccessException,
+ InvocationTargetException {
+ Method getClassMetadata =
sessionFactory.getClass().getMethod("getClassMetadata", new Class[]
{String.class});
+ Object classMetadata = getClassMetadata.invoke(sessionFactory, new Object[]
{className});
+ return classMetadata;
+ }
+
+ private Object getClassMetadata(Class clazz) throws NoSuchMethodException,
+ IllegalAccessException, InvocationTargetException {
+ Method getClassMetadata = sessionFactory.getClass().getMethod(
+ "getClassMetadata", new Class[] { Class.class });
+ Object classMetadata = getClassMetadata.invoke(sessionFactory,
+ new Object[] { clazz });
+ return classMetadata;
+ }
+
+ private String getHibernatePropTypes(String className, String property) throws
NoSuchMethodException, IllegalAccessException, InvocationTargetException{
+ Object classMetadata = getClassMetadata(className);
+ Method getPropertyType =
classMetadata.getClass().getMethod("getPropertyType", new Class[]
{String.class});
+ Object type = getPropertyType.invoke(classMetadata, new Object[] {property});
+ return getReturnTypeName(type);
+ }
+
+ public void setMaxRows(int max) {
+ maxRows = max;
+ }
+
+ public void executeQuery(HibernateOdaQuery query) throws OdaException {
+ this.query = query;
+ try {
+ session = openSession();
+ Object q = createQuery(session,queryText);
+ Method list = q.getClass().getMethod("list", emptyClassArg);
+ result = (List) list.invoke(q, emptyObjectArg);
+ iterator = result.iterator();
+ Method getReturnTypes = q.getClass().getMethod("getReturnTypes",
emptyClassArg);
+ this.queryReturnTypes = (Object[]) getReturnTypes.invoke(q, emptyObjectArg);
+ } catch (Exception e) {
+ throw new OdaException(e.getLocalizedMessage());
+ }
+ }
+
+ public Iterator getIterator() {
+ return iterator;
+ }
+
+ public List getResult() {
+ return result;
+ }
+
+ public Object getResult(int rstcol) throws OdaException {
+ Object obj = this.currentRow;
+ Object value = null;
+ try {
+ if (checkEntityType(queryReturnTypes)) {
+ String checkClass = ((HibernateResultSetMetaData) getMetaData())
+ .getColumnClass(rstcol);
+ Object metadata = getClassMetadata(checkClass);
+ if (metadata == null) {
+ metadata = getClassMetadata(obj.getClass());
+ }
+ String className = "org.hibernate.EntityMode";
+ Class pojo = Activator.classForName(className, getClass());
+ Field pojoField = pojo.getField("POJO");
+ Object POJO = pojoField.get(null);
+ Class[] parameterTypes = new Class[] {Object.class,String.class, POJO.getClass()};
+ Method getPropertyValue = metadata.getClass().getMethod("getPropertyValue",
parameterTypes);
+ Object[] args = new Object[] {obj,getMetaData().getColumnName(rstcol),POJO};
+ value = getPropertyValue.invoke(metadata, args);
+ } else {
+ if (getMetaData().getColumnCount() == 1) {
+ value = obj;
+ } else {
+ Object[] values = (Object[]) obj;
+ value = values[rstcol - 1];
+ }
+ }
+ } catch (Exception e) {
+ throw new OdaException(e.getLocalizedMessage());
+ }
+ return (value);
+ }
+
+ public void next() {
+ currentRow = getIterator().next();
+ }
+
+ private IResultSetMetaData getMetaData() throws OdaException {
+ return query.getMetaData();
+ }
+}