Author: dan.j.allen
Date: 2008-09-23 11:47:57 -0400 (Tue, 23 Sep 2008)
New Revision: 9122
Modified:
trunk/src/main/org/jboss/seam/Component.java
trunk/src/main/org/jboss/seam/framework/Home.java
trunk/src/main/org/jboss/seam/framework/Query.java
trunk/src/main/org/jboss/seam/util/Conversions.java
trunk/src/test/unit/org/jboss/seam/test/unit/HomeTest.java
trunk/src/test/unit/org/jboss/seam/test/unit/InitializationTest.java
trunk/src/test/unit/org/jboss/seam/test/unit/component/MyEntityHome.component.xml
trunk/src/test/unit/org/jboss/seam/test/unit/component/MyEntityHome.java
Log:
JBSEAM-2133
Modified: trunk/src/main/org/jboss/seam/Component.java
===================================================================
--- trunk/src/main/org/jboss/seam/Component.java 2008-09-23 15:21:06 UTC (rev 9121)
+++ trunk/src/main/org/jboss/seam/Component.java 2008-09-23 15:47:57 UTC (rev 9122)
@@ -496,7 +496,9 @@
private static InitialValue getInitialValue(Conversions.PropertyValue propertyValue,
Class parameterClass, Type parameterType)
{
- if ( propertyValue.isExpression() )
+ if ( parameterClass.equals(ValueExpression.class) ||
+ parameterClass.equals(MethodExpression.class) ||
+ propertyValue.isExpression() )
{
return new ELInitialValue(propertyValue, parameterClass, parameterType);
}
Modified: trunk/src/main/org/jboss/seam/framework/Home.java
===================================================================
--- trunk/src/main/org/jboss/seam/framework/Home.java 2008-09-23 15:21:06 UTC (rev 9121)
+++ trunk/src/main/org/jboss/seam/framework/Home.java 2008-09-23 15:47:57 UTC (rev 9122)
@@ -10,6 +10,7 @@
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;
+import org.jboss.seam.core.Expressions;
import org.jboss.seam.core.Expressions.ValueExpression;
/**
@@ -30,11 +31,11 @@
private Object id;
protected E instance;
private Class<E> entityClass;
- protected ValueExpression newInstance;
+ protected ValueExpression<T> newInstance;
- private String deletedMessage = "Successfully deleted";
- private String createdMessage = "Successfully created";
- private String updatedMessage = "Successfully updated";
+ private ValueExpression deletedMessage;
+ private ValueExpression createdMessage;
+ private ValueExpression updatedMessage;
/**
* Add a {@link javax.faces.application.FacesMessage} and log a message when
@@ -51,7 +52,7 @@
protected void updatedMessage()
{
debug("updated entity #0 #1", getEntityClass().getName(), getId());
- getStatusMessages().addFromResourceBundleOrDefault( INFO, getUpdatedMessageKey(),
getUpdatedMessage() );
+ getStatusMessages().addFromResourceBundleOrDefault( INFO, getUpdatedMessageKey(),
getUpdatedMessage().getExpressionString() );
}
/**
@@ -69,7 +70,7 @@
protected void deletedMessage()
{
debug("deleted entity #0 #1", getEntityClass().getName(), getId());
- getStatusMessages().addFromResourceBundleOrDefault( INFO, getDeletedMessageKey(),
getDeletedMessage() );
+ getStatusMessages().addFromResourceBundleOrDefault( INFO, getDeletedMessageKey(),
getDeletedMessage().getExpressionString() );
}
/**
@@ -87,7 +88,7 @@
protected void createdMessage()
{
debug("created entity #0 #1", getEntityClass().getName(), getId());
- getStatusMessages().addFromResourceBundleOrDefault( INFO, getCreatedMessageKey(),
getCreatedMessage() );
+ getStatusMessages().addFromResourceBundleOrDefault( INFO, getCreatedMessageKey(),
getCreatedMessage().getExpressionString() );
}
/**
@@ -103,7 +104,22 @@
{
throw new IllegalStateException("entityClass is null");
}
+ initDefaultMessages();
}
+
+ protected void initDefaultMessages()
+ {
+ Expressions expressions = new Expressions();
+ if (createdMessage == null) {
+ createdMessage = expressions.createValueExpression("Successfully
created");
+ }
+ if (updatedMessage == null) {
+ updatedMessage = expressions.createValueExpression("Successfully
updated");
+ }
+ if (deletedMessage == null) {
+ deletedMessage = expressions.createValueExpression("Successfully
deleted");
+ }
+ }
/**
* Get the managed entity, using the id from {@link #getId()} to load it from
@@ -340,7 +356,7 @@
/**
* Message displayed to user when the managed entity is created.
*/
- public String getCreatedMessage()
+ public ValueExpression getCreatedMessage()
{
return createdMessage;
}
@@ -348,7 +364,7 @@
/**
* Message displayed to user when the managed entity is created.
*/
- public void setCreatedMessage(String createdMessage)
+ public void setCreatedMessage(ValueExpression createdMessage)
{
this.createdMessage = createdMessage;
}
@@ -356,7 +372,7 @@
/**
* Message displayed to user when the managed entity is deleted.
*/
- public String getDeletedMessage()
+ public ValueExpression getDeletedMessage()
{
return deletedMessage;
}
@@ -364,7 +380,7 @@
/**
* Message displayed to user when the managed entity is deleted.
*/
- public void setDeletedMessage(String deletedMessage)
+ public void setDeletedMessage(ValueExpression deletedMessage)
{
this.deletedMessage = deletedMessage;
}
@@ -372,7 +388,7 @@
/**
* Message displayed to user when the managed entity is updated.
*/
- public String getUpdatedMessage()
+ public ValueExpression getUpdatedMessage()
{
return updatedMessage;
}
@@ -380,7 +396,7 @@
/**
* Message displayed to user when the managed entity is updated.
*/
- public void setUpdatedMessage(String updatedMessage)
+ public void setUpdatedMessage(ValueExpression updatedMessage)
{
this.updatedMessage = updatedMessage;
}
Modified: trunk/src/main/org/jboss/seam/framework/Query.java
===================================================================
--- trunk/src/main/org/jboss/seam/framework/Query.java 2008-09-23 15:21:06 UTC (rev 9121)
+++ trunk/src/main/org/jboss/seam/framework/Query.java 2008-09-23 15:47:57 UTC (rev 9122)
@@ -39,7 +39,7 @@
private String ejbql;
private Integer firstResult;
private Integer maxResults;
- private List<String> restrictions = new ArrayList<String>(0);
+ private List<ValueExpression> restrictions = new
ArrayList<ValueExpression>(0);
private String order;
private String orderColumn;
private String orderDirection;
@@ -213,12 +213,12 @@
queryParameters = qp.getParameterValueBindings();
parsedEjbql = qp.getEjbql();
- List<String> restrictionFragments = getRestrictions();
+ List<ValueExpression> restrictionFragments = getRestrictions();
parsedRestrictions = new ArrayList<String>( restrictionFragments.size()
);
restrictionParameters = new ArrayList<ValueExpression>(
restrictionFragments.size() );
- for ( String restriction: restrictionFragments )
+ for ( ValueExpression restriction: restrictionFragments )
{
- QueryParser rqp = new QueryParser( restriction, queryParameters.size() +
restrictionParameters.size() );
+ QueryParser rqp = new QueryParser( restriction.getExpressionString(),
queryParameters.size() + restrictionParameters.size() );
if ( rqp.getParameterValueBindings().size()!=1 )
{
throw new IllegalArgumentException("there should be exactly one value
binding in a restriction: " + restriction);
@@ -286,7 +286,8 @@
Matcher orderMatcher = ORDER_PATTERN.matcher(ejbql);
int orderLoc = orderMatcher.find() ? orderMatcher.start(1) : ejbql.length();
- return "select count(*) " + ejbql.substring(fromLoc, orderLoc);
+ String fromClause = ejbql.substring(fromLoc, orderLoc);
+ return "select count(" + fromClause.substring(5,
fromClause.indexOf(" ", 5)) + ") " + fromClause;
}
public String getEjbql()
@@ -355,7 +356,7 @@
* For a query such as 'from Foo f' a restriction could be
* 'f.bar = #{foo.bar}'
*/
- public List<String> getRestrictions()
+ public List<ValueExpression> getRestrictions()
{
return restrictions;
}
@@ -364,7 +365,7 @@
* Calling setRestrictions causes the restrictions to be reparsed and the
* query refreshed
*/
- public void setRestrictions(List<String> restrictions)
+ public void setRestrictions(List<ValueExpression> restrictions)
{
this.restrictions = restrictions;
parsedRestrictions = null;
Modified: trunk/src/main/org/jboss/seam/util/Conversions.java
===================================================================
--- trunk/src/main/org/jboss/seam/util/Conversions.java 2008-09-23 15:21:06 UTC (rev
9121)
+++ trunk/src/main/org/jboss/seam/util/Conversions.java 2008-09-23 15:47:57 UTC (rev
9122)
@@ -14,6 +14,8 @@
@SuppressWarnings("serial")
public class Conversions
{
+ private static final String EXPRESSION_MARKER = "#{";
+ private static final char EXPRESSION_ESCAPE_CHAR = '\\';
private static Map<Class, Converter> converters = new HashMap<Class,
Converter>() {{
put(String.class, new StringConverter());
@@ -300,7 +302,23 @@
public boolean isExpression()
{
- return string.startsWith("#{");
+ boolean containsExpr = false;
+ int idx = string.indexOf(EXPRESSION_MARKER);
+ if (idx == 0) {
+ containsExpr = true;
+ }
+ else {
+ while (idx != -1) {
+ if (string.charAt(idx - 1) == EXPRESSION_ESCAPE_CHAR) {
+ idx = string.indexOf(EXPRESSION_MARKER, idx + 2);
+ }
+ else {
+ containsExpr = true;
+ break;
+ }
+ }
+ }
+ return containsExpr;
}
public boolean isMultiValued()
Modified: trunk/src/test/unit/org/jboss/seam/test/unit/HomeTest.java
===================================================================
--- trunk/src/test/unit/org/jboss/seam/test/unit/HomeTest.java 2008-09-23 15:21:06 UTC
(rev 9121)
+++ trunk/src/test/unit/org/jboss/seam/test/unit/HomeTest.java 2008-09-23 15:47:57 UTC
(rev 9122)
@@ -1,13 +1,18 @@
package org.jboss.seam.test.unit;
+import java.lang.reflect.Field;
+import java.util.List;
+
import javax.persistence.EntityManager;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.framework.EntityHome;
import org.jboss.seam.framework.HibernateEntityHome;
import org.jboss.seam.framework.Home;
+import org.jboss.seam.international.StatusMessage;
import org.jboss.seam.international.StatusMessages;
import org.jboss.seam.test.unit.entity.SimpleEntity;
+import org.jboss.seam.util.Reflections;
import org.testng.annotations.Test;
public class HomeTest
@@ -93,6 +98,8 @@
// emulate @Create method
home.create();
home.triggerCreatedMessage();
+ List<StatusMessage> registeredMessages = home.getRegisteredMessages();
+ assert registeredMessages.size() == 1;
}
/**
@@ -140,6 +147,16 @@
{
return statusMessages;
}
+
+ protected List<StatusMessage> getRegisteredMessages()
+ {
+ Field field = Reflections.getField(statusMessages.getClass(),
"messages");
+ if (!field.isAccessible())
+ {
+ field.setAccessible(true);
+ }
+ return (List<StatusMessage>) Reflections.getAndWrap(field,
statusMessages);
+ }
@Override
public Class<SimpleEntity> getEntityClass()
@@ -160,5 +177,4 @@
}
}
-
}
Modified: trunk/src/test/unit/org/jboss/seam/test/unit/InitializationTest.java
===================================================================
--- trunk/src/test/unit/org/jboss/seam/test/unit/InitializationTest.java 2008-09-23
15:21:06 UTC (rev 9121)
+++ trunk/src/test/unit/org/jboss/seam/test/unit/InitializationTest.java 2008-09-23
15:47:57 UTC (rev 9122)
@@ -10,7 +10,11 @@
import org.jboss.seam.init.Initialization;
import org.jboss.seam.mock.MockServletContext;
import org.jboss.seam.test.unit.component.ConfigurableComponent;
+import org.jboss.seam.test.unit.component.MyEntityHome;
import org.jboss.seam.test.unit.component.PrimaryColor;
+import org.jboss.seam.transaction.NoTransaction;
+import org.jboss.seam.transaction.Transaction;
+import org.testng.Assert;
import org.testng.annotations.Test;
public class InitializationTest
@@ -47,6 +51,26 @@
ServletLifecycle.endApplication();
}
+
+ @Test
+ public void testEntityHomeConfiguration()
+ {
+ MockServletContext servletContext = new MockServletContext();
+ ServletLifecycle.beginApplication(servletContext);
+ new Initialization( servletContext ).create().init();
+ Lifecycle.beginCall();
+ Contexts.getEventContext().set(Seam.getComponentName(Transaction.class), new
NoTransaction());
+ MyEntityHome myEntityHome = (MyEntityHome)
Component.getInstance("myEntityHome");
+ assert myEntityHome != null;
+ // verify that the reference to new-instance remains unparsed
+ Assert.assertEquals(myEntityHome.getNewInstance().getExpressionString(),
"#{simpleEntity}");
+ // verify that the message string for the created/updated/deleted message remains
unparsed
+ Assert.assertEquals(myEntityHome.getCreatedMessage().getExpressionString(),
"You #{'created'} it! Yeah!");
+ // verify that the id is parsed prior to assignment
+ Assert.assertEquals(String.valueOf(myEntityHome.getId()), "11");
+
+ ServletLifecycle.endApplication();
+ }
//TODO: write a test for components.xml
}
Modified:
trunk/src/test/unit/org/jboss/seam/test/unit/component/MyEntityHome.component.xml
===================================================================
---
trunk/src/test/unit/org/jboss/seam/test/unit/component/MyEntityHome.component.xml 2008-09-23
15:21:06 UTC (rev 9121)
+++
trunk/src/test/unit/org/jboss/seam/test/unit/component/MyEntityHome.component.xml 2008-09-23
15:47:57 UTC (rev 9122)
@@ -10,9 +10,13 @@
The reason this attribute is significant in this case is because the namespace
references a class that does not have @Name.
To allow Seam to locate the @Name annotation, the subclass must be provided. -->
<framework:entity-home name="myEntityHome"
class="org.jboss.seam.test.unit.component.MyEntityHome">
- <framework:created-message>You created it!
Yeah!</framework:created-message>
+ <framework:id>1#{1}</framework:id>
+ <framework:new-instance>#{simpleEntity}</framework:new-instance>
+ <framework:created-message>You #{'created'} it!
Yeah!</framework:created-message>
<framework:updated-message>You updated it!
Yeah!</framework:updated-message>
<framework:deleted-message>You deleted it!
Yeah!</framework:deleted-message>
- </framework:entity-home>
+ </framework:entity-home>
+
+ <component name="simpleEntity"
class="org.jboss.seam.test.unit.entity.SimpleEntity"/>
</components>
Modified: trunk/src/test/unit/org/jboss/seam/test/unit/component/MyEntityHome.java
===================================================================
--- trunk/src/test/unit/org/jboss/seam/test/unit/component/MyEntityHome.java 2008-09-23
15:21:06 UTC (rev 9121)
+++ trunk/src/test/unit/org/jboss/seam/test/unit/component/MyEntityHome.java 2008-09-23
15:47:57 UTC (rev 9122)
@@ -2,8 +2,18 @@
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;
+import org.jboss.seam.test.unit.entity.SimpleEntity;
@Name("myEntityHome")
-public class MyEntityHome extends EntityHome
+public class MyEntityHome extends EntityHome<SimpleEntity>
{
+ @Override
+ public void create()
+ {
+ if ( getEntityClass()==null )
+ {
+ throw new IllegalStateException("entityClass is null");
+ }
+ initDefaultMessages();
+ }
}