[jboss-cvs] jboss-seam/src/main/org/jboss/seam/core ...
Gavin King
gavin.king at jboss.com
Mon Jul 24 16:44:51 EDT 2006
User: gavin
Date: 06/07/24 16:44:51
Modified: src/main/org/jboss/seam/core ManagedEntity.java
ManagedHibernateEntity.java
Log:
handle nonstring id types more elegantly
Revision Changes Path
1.2 +65 -2 jboss-seam/src/main/org/jboss/seam/core/ManagedEntity.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ManagedEntity.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/core/ManagedEntity.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- ManagedEntity.java 19 Jul 2006 18:52:44 -0000 1.1
+++ ManagedEntity.java 24 Jul 2006 20:44:51 -0000 1.2
@@ -2,6 +2,8 @@
import static org.jboss.seam.InterceptionType.NEVER;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
import javax.persistence.EntityManager;
import org.jboss.seam.annotations.Intercept;
@@ -13,6 +15,9 @@
private EntityManager entityManager;
private Object id;
private String entityClass;
+ private Object newInstance;
+ private String converterId;
+ private Converter converter;
public EntityManager getEntityManager()
{
@@ -45,10 +50,68 @@
}
@Unwrap
- public Object getInstance() throws ClassNotFoundException
+ public Object getInstance() throws Exception
{
Class<?> clazz = Class.forName(entityClass);
- return entityManager.find(clazz, id);
+ if (id==null)
+ {
+ if (newInstance==null)
+ {
+ newInstance = clazz.newInstance();
+ }
+ return newInstance;
+ }
+ else
+ {
+ return entityManager.find( clazz, getConvertedId() );
+ }
+ }
+
+ //////////// TODO: copy/paste from ManagedHibernateEntity ///////////////////
+
+ private Object getConvertedId()
+ {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ if (converterId!=null)
+ {
+ converter = facesContext.getApplication().createConverter(converterId); //cache the lookup
+ }
+
+ if (converter==null)
+ {
+ //TODO: look for an @Id annotation and guess the id type!
+ //converter = facesContext.getApplication().createConverter(idClass)
+ return id;
}
+ else
+ {
+ return converter.getAsObject(
+ facesContext,
+ facesContext.getViewRoot(),
+ (String) id
+ );
+ }
+ }
+
+ public String getConverterId()
+ {
+ return converterId;
+ }
+
+ public void setConverterId(String converterId)
+ {
+ this.converterId = converterId;
+ }
+
+ public Converter getConverter()
+ {
+ return converter;
+ }
+
+ public void setConverter(Converter converter)
+ {
+ this.converter = converter;
+ }
+
}
1.3 +65 -2 jboss-seam/src/main/org/jboss/seam/core/ManagedHibernateEntity.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ManagedHibernateEntity.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/core/ManagedHibernateEntity.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- ManagedHibernateEntity.java 24 Jul 2006 00:05:28 -0000 1.2
+++ ManagedHibernateEntity.java 24 Jul 2006 20:44:51 -0000 1.3
@@ -2,6 +2,9 @@
import java.io.Serializable;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+
import org.hibernate.Session;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.annotations.Unwrap;
@@ -11,6 +14,9 @@
private Session session;
private Serializable id;
private String entityClass;
+ private Object newInstance;
+ private String converterId;
+ private Converter converter;
public Session getSession()
{
@@ -43,10 +49,67 @@
}
@Unwrap @Transactional
- public Object getInstance() throws ClassNotFoundException
+ public Object getInstance() throws Exception
{
Class clazz = Class.forName(entityClass);
- return session.get(clazz, id);
+ if (id==null)
+ {
+ if (newInstance==null)
+ {
+ newInstance = clazz.newInstance();
+ }
+ return newInstance;
+ }
+ else
+ {
+ return session.get( clazz, getConvertedId() );
+ }
+ }
+
+ ////////////TODO: copy/paste from ManagedEntity ///////////////////
+
+ private Serializable getConvertedId()
+ {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ if (converterId!=null)
+ {
+ converter = facesContext.getApplication().createConverter(converterId); //cache the lookup
+ }
+
+ if (converter==null)
+ {
+ //TODO: look for an @Id annotation and guess the id type!
+ //converter = facesContext.getApplication().createConverter(idClass)
+ return id;
+ }
+ else
+ {
+ return (Serializable) converter.getAsObject(
+ facesContext,
+ facesContext.getViewRoot(),
+ (String) id
+ );
+ }
+ }
+
+ public String getConverterId()
+ {
+ return converterId;
+ }
+
+ public void setConverterId(String converterId)
+ {
+ this.converterId = converterId;
+ }
+
+ public Converter getConverter()
+ {
+ return converter;
+ }
+
+ public void setConverter(Converter converter)
+ {
+ this.converter = converter;
}
}
More information about the jboss-cvs-commits
mailing list