User: d_jencks
Date: 02/03/01 17:02:58
Added: src/main/org/jboss/test/cmp2/cmr/ejb CMRBugBean.java
CMRBugManagerBean.java
Log:
Testcase for bugs 523627, making sure changes in PostCreate are written to db, and
bug 523239, a problem with cmr relationships (at least) when using xdoclet generated
jbosscmp-jdbc.xml. The post create is fixed, the cmr relationship problem is not.
Revision Changes Path
1.1 jbosstest/src/main/org/jboss/test/cmp2/cmr/ejb/CMRBugBean.java
Index: CMRBugBean.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*
*/
package org.jboss.test.cmp2.cmr.ejb;
import java.util.Collection;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import org.jboss.test.cmp2.cmr.interfaces.CMRBugEJBLocal;
/**
* class <code>CMRBugBean</code> demonstrates bug 523627. CMR fields may get
changed
* in ejbPostCreate, so newly created entities must be marked as needing ejbSave.
* Currently this is done by putting them in GlobalTxEntityMap.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Michael Newcomb</a>
* @author <a href="mailto:[EMAIL PROTECTED]">David Jencks</a>
* @version 1.0
*
* @ejb:bean name="CMRBugEJB"
* local-jndi-name="LocalReadOnly"
* view-type="local"
* type="CMP"
* cmp-version="2.x"
* primkey-field="id"
* reentrant="true"
* schema="CMRBug"
* @ejb:pk class="java.lang.String"
* @ejb:finder signature="java.util.Collection findAll()"
* query="select object(cmr_bug) from CMRBug as cmr_bug"
* @jboss:create-table create="true"
* @jboss:remove-table remove="true"
*/
public abstract class CMRBugBean
implements EntityBean
{
private EntityContext context;
/**
* Describe <code>getId</code> method here.
*
* @return a <code>String</code> value
* @ejb:persistent-field
* @ejb:interface-method
*/
public abstract String getId();
/**
* Describe <code>setId</code> method here.
*
* @param id a <code>String</code> value
* @ejb:persistent-field
* @ejb:interface-method
*/
public abstract void setId(String id);
/**
* Describe <code>getDescription</code> method here.
*
* @return a <code>String</code> value
* @ejb:persistent-field
* @ejb:interface-method
*/
public abstract String getDescription();
/**
* Describe <code>setDescription</code> method here.
*
* @param description a <code>String</code> value
* @ejb:persistent-field
* @ejb:interface-method
*/
public abstract void setDescription(String description);
/**
* Describe <code>getParent</code> method here.
*
* @return a <code>CMRBugEJBLocal</code> value
* @ejb:interface-method
* @ejb:relation name="CMRBug-CMRBug"
* role-name="parent"
* cascade-delete="yes"
*/
public abstract CMRBugEJBLocal getParent();
/**
* Describe <code>setParent</code> method here.
*
* @param parent a <code>CMRBugEJBLocal</code> value
* @ejb:interface-method
*/
public abstract void setParent(CMRBugEJBLocal parent);
/**
* Describe <code>getChildren</code> method here.
*
* @return a <code>Collection</code> value
* @ejb:interface-method
* @ejb:relation name="CMRBug-CMRBug"
* role-name="children"
* multiple="yes"
*/
public abstract Collection getChildren();
/**
* Describe <code>setChildren</code> method here.
*
* @param children a <code>Collection</code> value
* @ejb:interface-method
*/
public abstract void setChildren(Collection children);
/**
* Describe <code>addChild</code> method here.
*
* @param child a <code>CMRBugEJBLocal</code> value
* @return a <code>boolean</code> value
* @ejb:interface-method
*/
public boolean addChild(CMRBugEJBLocal child)
{
try
{
Collection children = getChildren();
return children.add(child);
}
catch (Exception e)
{
throw new EJBException(e);
}
}
/**
* Describe <code>removeChild</code> method here.
*
* @param child a <code>CMRBugEJBLocal</code> value
* @return a <code>boolean</code> value
* @ejb:interface-method
*/
public boolean removeChild(CMRBugEJBLocal child)
{
try
{
Collection children = getChildren();
return children.remove(child);
}
catch (Exception e)
{
throw new EJBException(e);
}
}
// --------------------------------------------------------------------------
// EntityBean methods
//
/**
* Describe <code>ejbCreate</code> method here.
*
* @param id a <code>String</code> value
* @param description a <code>String</code> value
* @param parent a <code>CMRBugEJBLocal</code> value
* @return an <code>Integer</code> value
* @exception CreateException if an error occurs
* @ejb:create-method
*/
public Integer ejbCreate(String id, String description, CMRBugEJBLocal parent)
throws CreateException
{
setId(id);
setDescription(description);
// CMP beans return null for this method
//
return null;
}
/**
* Describe <code>ejbPostCreate</code> method here.
*
* @param id a <code>String</code> value
* @param description a <code>String</code> value
* @param parent a <code>CMRBugEJBLocal</code> value
* @exception CreateException if an error occurs
*/
public void ejbPostCreate(String id, String description, CMRBugEJBLocal parent)
throws CreateException
{
// must set the CMR fields in the post create
//
setParent(parent);
}
public void setEntityContext(EntityContext context)
{
this.context = context;
}
public void unsetEntityContext()
{
context = null;
}
public void ejbRemove() {}
public void ejbLoad() {}
public void ejbStore() {}
public void ejbActivate() {}
public void ejbPassivate() {}
}
1.1
jbosstest/src/main/org/jboss/test/cmp2/cmr/ejb/CMRBugManagerBean.java
Index: CMRBugManagerBean.java
===================================================================
package org.jboss.test.cmp2.cmr.ejb;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.log4j.Category;
import org.jboss.test.cmp2.cmr.interfaces.CMRBugEJBLocalHome;
import org.jboss.test.cmp2.cmr.interfaces.CMRBugEJBLocal;
/**
* Describe class <code>CMRBugManagerBean</code> here.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Michael Newcomb</a>
* @author <a href="mailto:[EMAIL PROTECTED]">David Jencks</a>
* @version 1.0
* @ejb:bean type="Stateless" name="CMRBugManagerEJB" jndi-name="CMRBugManager"
* @ejb:ejb-ref ejb-name="CMRBugEJB"
* view-type="local"
* ref-name="ejb/CMRBug"
* @ejb:transaction type="Required"
* @ejb:transaction-type type="Container"
*/
public class CMRBugManagerBean
implements SessionBean
{
private CMRBugEJBLocalHome cmrBugHome;
private Category log = Category.getInstance(getClass());
public CMRBugManagerBean() {}
/**
* Describe <code>createCMRBugs</code> method here.
*
* @param cmrBugs a <code>SortedMap</code> value
* @ejb:interface-method view-type="remote"
*/
public void createCMRBugs(SortedMap cmrBugs)
{
try
{
if (!cmrBugs.isEmpty())
{
Iterator i = cmrBugs.entrySet().iterator();
Map.Entry entry = (Map.Entry) i.next();
// the root id (of which all others are based) is the first key in
// the SortedMap
//
String root = (String) entry.getKey();
String id = root;
String description = (String) entry.getValue();
CMRBugEJBLocal parent = cmrBugHome.create(id, description, null);
/*
if (succeed)
{
// replace the description with the id
//
entry.setValue(parent.getId());
}
else // fail
{*/
// replace the description in the map with the actual CMRBugEJBLocal
//
entry.setValue(parent);
//}
while (i.hasNext())
{
entry = (Map.Entry) i.next();
id = (String) entry.getKey();
description = (String) entry.getValue();
int index = id.lastIndexOf(".");
if (index != -1)
{
// determine the parent id and then try to find the parent's
// CMRBugEJBLocal in the map
//
String parentId = id.substring(0, index);
/*
if (succeed)
{
parent =
cmrBugHome.findByPrimaryKey((String) cmrBugs.get(parentId));
}
else // fail (sometimes)
{*/
parent = (CMRBugEJBLocal) cmrBugs.get(parentId);
//}
}
/*
if (succeed)
{
// replace the description with the id
//
entry.setValue(cmrBugHome.create(id, description, parent).getId());
}
else // fail (sometimes)
{*/
// replace the description in the map with the actual CMRBugEJBLocal
//
entry.setValue(cmrBugHome.create(id, description, parent));
//}
}
}
}
catch (Exception e)
{
e.printStackTrace();
throw new EJBException(e.getMessage());
}
}
/**
* Describe <code>getParentFor</code> method here.
*
* @param id a <code>String</code> value
* @return a <code>String[]</code> value
* @ejb:interface-method view-type="remote"
*/
public String[] getParentFor(String id)
{
try
{
CMRBugEJBLocal cmrBug = cmrBugHome.findByPrimaryKey(id);
CMRBugEJBLocal parent = cmrBug.getParent();
String[] parentIdAndDescription = null;
if (parent != null)
{
parentIdAndDescription = new String[2];
parentIdAndDescription[0] = parent.getId();
parentIdAndDescription[1] = parent.getDescription();
}
return parentIdAndDescription;
}
catch (Exception e)
{
e.printStackTrace();
throw new EJBException(e.getMessage());
}
}
// --------------------------------------------------------------------------
// SessionBean methods
//
/**
* Describe <code>ejbCreate</code> method here.
*
* @exception CreateException if an error occurs
*/
public void ejbCreate()
throws CreateException
{
try
{
cmrBugHome = lookupCMRBugHome();
}
catch (Exception e)
{
throw new CreateException(e.getMessage());
}
}
public void ejbActivate()
{
try
{
cmrBugHome = lookupCMRBugHome();
}
catch (Exception e)
{
throw new EJBException(e.getMessage());
}
}
public void ejbPassivate()
{
cmrBugHome = null;
}
public void ejbRemove() {}
public void setSessionContext(SessionContext sessionContext) {}
private CMRBugEJBLocalHome lookupCMRBugHome()
throws NamingException
{
InitialContext initialContext = new InitialContext();
return (CMRBugEJBLocalHome) initialContext.lookup("java:comp/env/ejb/CMRBug");
}
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development