Author: swd847
Date: 2010-07-28 05:21:42 -0400 (Wed, 28 Jul 2010)
New Revision: 13522
Added:
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/QueryParser.java
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/HotelNameProducer.java
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/ManagedPersistenceContextELTest.java
Modified:
modules/persistence/trunk/impl/pom.xml
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextExtension.java
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProxyHandler.java
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/ManagedPersistenceContextTest.java
Log:
add the ability to use el in jpa queries
Modified: modules/persistence/trunk/impl/pom.xml
===================================================================
--- modules/persistence/trunk/impl/pom.xml 2010-07-28 09:05:52 UTC (rev 13521)
+++ modules/persistence/trunk/impl/pom.xml 2010-07-28 09:21:42 UTC (rev 13522)
@@ -54,6 +54,11 @@
<artifactId>jboss-interceptors-api_1.1_spec</artifactId>
<scope>provided</scope>
</dependency>
+
+ <dependency>
+ <groupId>javax.el</groupId>
+ <artifactId>el-api</artifactId>
+ </dependency>
<dependency>
<groupId>javax.transaction</groupId>
Modified:
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextExtension.java
===================================================================
---
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextExtension.java 2010-07-28
09:05:52 UTC (rev 13521)
+++
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextExtension.java 2010-07-28
09:21:42 UTC (rev 13522)
@@ -110,13 +110,6 @@
}
}
- /**
- */
- public void processProducer()
- {
-
- }
-
public void registerManagedPersistenceContext(Set<Annotation> qualifiers,
Class<? extends Annotation> scope, BeanManager manager)
{
// create the new bean to be registerd later
Modified:
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProxyHandler.java
===================================================================
---
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProxyHandler.java 2010-07-28
09:05:52 UTC (rev 13521)
+++
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProxyHandler.java 2010-07-28
09:21:42 UTC (rev 13522)
@@ -29,12 +29,15 @@
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.persistence.EntityManager;
+import javax.persistence.Query;
import javax.transaction.Synchronization;
import javax.transaction.SystemException;
import org.jboss.seam.persistence.transaction.DefaultTransaction;
import org.jboss.seam.persistence.transaction.SeamTransaction;
import org.jboss.seam.persistence.transaction.literal.DefaultTransactionLiteral;
+import org.jboss.weld.extensions.el.Expressions;
+import org.jboss.weld.extensions.literal.DefaultLiteral;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -61,10 +64,13 @@
static final Logger log =
LoggerFactory.getLogger(ManagedPersistenceContextProxyHandler.class);
+ private final Bean<Expressions> expressionsBean;
+
public ManagedPersistenceContextProxyHandler(EntityManager delegate, BeanManager
beanManager)
{
this.delegate = delegate;
this.beanManager = beanManager;
+ this.expressionsBean = (Bean)
beanManager.resolve(beanManager.getBeans(Expressions.class, DefaultLiteral.INSTANCE));
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
@@ -73,6 +79,10 @@
{
joinTransaction();
}
+ if ("createQuery".equals(method.getName()) &&
method.getParameterTypes().length > 0 &&
method.getParameterTypes()[0].equals(String.class))
+ {
+ return handleCreateQueryWithString(method, args);
+ }
return method.invoke(delegate, args);
}
@@ -122,4 +132,30 @@
}
+ protected Object handleCreateQueryWithString(Method method, Object[] args) throws
Throwable
+ {
+ if (args[0] == null)
+ {
+ return method.invoke(delegate, args);
+ }
+ String ejbql = (String) args[0];
+ if (ejbql.indexOf('#') > 0)
+ {
+ CreationalContext<Expressions> ctx =
beanManager.createCreationalContext(expressionsBean);
+ Expressions expressions = (Expressions)
beanManager.getReference(expressionsBean, Expressions.class, ctx);
+ QueryParser qp = new QueryParser(expressions, ejbql);
+ Object[] newArgs = args.clone();
+ newArgs[0] = qp.getEjbql();
+ Query query = (Query) method.invoke(delegate, newArgs);
+ for (int i = 0; i < qp.getParameterValues().size(); i++)
+ {
+ query.setParameter(QueryParser.getParameterName(i),
qp.getParameterValues().get(i));
+ }
+ return query;
+ }
+ else
+ {
+ return method.invoke(delegate, args);
+ }
+ }
}
Added:
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/QueryParser.java
===================================================================
---
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/QueryParser.java
(rev 0)
+++
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/QueryParser.java 2010-07-28
09:21:42 UTC (rev 13522)
@@ -0,0 +1,84 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.seam.persistence;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.StringTokenizer;
+
+import org.jboss.weld.extensions.el.Expressions;
+
+public class QueryParser
+{
+ private final List<Object> parameterValues = new ArrayList<Object>();
+ private final StringBuilder ejbqlBuilder;
+
+ public static String getParameterName(int loc)
+ {
+ return "el" + (loc + 1);
+ }
+
+ public String getEjbql()
+ {
+ return ejbqlBuilder.toString();
+ }
+
+ public List<Object> getParameterValues()
+ {
+ return parameterValues;
+ }
+
+ public QueryParser(Expressions expressions, String ejbql)
+ {
+ this(expressions, ejbql, 0);
+ }
+
+ public QueryParser(Expressions expressions, String ejbql, int
startingParameterNumber)
+ {
+ StringTokenizer tokens = new StringTokenizer(ejbql, "#}", true);
+ ejbqlBuilder = new StringBuilder(ejbql.length());
+ while (tokens.hasMoreTokens())
+ {
+ String token = tokens.nextToken();
+ if ("#".equals(token) && tokens.hasMoreTokens())
+ {
+ String expressionToken = tokens.nextToken();
+
+ if (!expressionToken.startsWith("{") || !tokens.hasMoreTokens())
+ {
+ ejbqlBuilder.append(token).append(expressionToken);
+ }
+ else
+ {
+ String expression = token + expressionToken + tokens.nextToken();
+
ejbqlBuilder.append(':').append(getParameterName(startingParameterNumber +
parameterValues.size()));
+ parameterValues.add(expressions.evaluateValueExpression(expression));
+ }
+ }
+ else
+ {
+ ejbqlBuilder.append(token);
+ }
+ }
+ }
+
+}
Added:
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/HotelNameProducer.java
===================================================================
---
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/HotelNameProducer.java
(rev 0)
+++
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/HotelNameProducer.java 2010-07-28
09:21:42 UTC (rev 13522)
@@ -0,0 +1,14 @@
+package org.jboss.seam.persistence.test;
+
+import javax.enterprise.inject.Produces;
+import javax.inject.Named;
+
+public class HotelNameProducer
+{
+ @Produces
+ @Named("hotelName")
+ public String getHotelName()
+ {
+ return "Hilton";
+ }
+}
Added:
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/ManagedPersistenceContextELTest.java
===================================================================
---
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/ManagedPersistenceContextELTest.java
(rev 0)
+++
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/ManagedPersistenceContextELTest.java 2010-07-28
09:21:42 UTC (rev 13522)
@@ -0,0 +1,100 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.seam.persistence.test;
+
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.SystemException;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.seam.persistence.PersistenceContextExtension;
+import org.jboss.seam.persistence.transaction.DefaultTransaction;
+import org.jboss.seam.persistence.transaction.SeamTransaction;
+import org.jboss.seam.persistence.transaction.TransactionExtension;
+import org.jboss.seam.persistence.util.NamingUtils;
+import org.jboss.seam.transactions.test.util.ArtifactNames;
+import org.jboss.seam.transactions.test.util.Hotel;
+import org.jboss.seam.transactions.test.util.ManagedPersistenceContextProvider;
+import org.jboss.seam.transactions.test.util.MavenArtifactResolver;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+(a)RunWith(Arquillian.class)
+public class ManagedPersistenceContextELTest
+{
+ @Deployment
+ public static Archive<?> createTestArchive()
+ {
+ WebArchive war =
ShrinkWrap.createDomain().getArchiveFactory().create(WebArchive.class,
"test.war");
+ war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.WELD_EXTENSIONS));
+
war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.SEAM_PERSISTENCE_API));
+ war.addPackage(TransactionExtension.class.getPackage());
+ war.addPackage(PersistenceContextExtension.class.getPackage());
+ war.addPackage(NamingUtils.class.getPackage());
+ war.addClasses(ManagedPersistenceContextELTest.class, Hotel.class,
ManagedPersistenceContextProvider.class, HotelNameProducer.class);
+ war.addWebResource("META-INF/persistence.xml",
"classes/META-INF/persistence.xml");
+ war.addWebResource(new ByteArrayAsset(new byte[0]), "beans.xml");
+
war.addWebResource("META-INF/services/javax.enterprise.inject.spi.Extension",
"classes/META-INF/services/javax.enterprise.inject.spi.Extension");
+ return war;
+ }
+
+ @Inject
+ @DefaultTransaction
+ SeamTransaction transaction;
+
+ @Inject
+ EntityManager em;
+
+ @Test
+ public void testELInInquery() throws NotSupportedException, SystemException,
SecurityException, IllegalStateException, RollbackException, HeuristicMixedException,
HeuristicRollbackException
+ {
+ transaction.begin();
+ Hotel h = new Hotel("Hilton", "Fake St",
"Wollongong", "NSW", "2518", "Australia");
+ em.persist(h);
+ em.flush();
+ transaction.commit();
+
+ transaction.begin();
+ h = new Hotel("Other Hotel", "Real St ",
"Wollongong", "NSW", "2518", "Australia");
+ em.persist(h);
+ em.flush();
+ transaction.commit();
+
+ transaction.begin();
+ Hotel hilton = (Hotel) em.createQuery("select h from Hotel h where
h.name=#{hotelName}").getSingleResult();
+ Assert.assertTrue(hilton.getName().equals("Hilton"));
+ Assert.assertTrue(hilton.getAddress().equals("Fake St"));
+ transaction.commit();
+
+ }
+
+}
Modified:
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/ManagedPersistenceContextTest.java
===================================================================
---
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/ManagedPersistenceContextTest.java 2010-07-28
09:05:52 UTC (rev 13521)
+++
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/ManagedPersistenceContextTest.java 2010-07-28
09:21:42 UTC (rev 13522)
@@ -1,3 +1,24 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
package org.jboss.seam.persistence.test;
import java.util.List;