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;
public class CMRBugManagerBean
implements SessionBean
{
private CMRBugEJBLocalHome cmrBugHome;
private Category log = Category.getInstance(getClass());
public CMRBugManagerBean()
{
}
public void createCMRBugs(SortedMap cmrBugs)
{
try
{
if(!cmrBugs.isEmpty())
{
Iterator i = cmrBugs.entrySet().iterator();
Map.Entry entry = (Map.Entry)i.next();
String root = (String)entry.getKey();
String id = root;
String description = (String)entry.getValue();
CMRBugEJBLocal parent = cmrBugHome.create(id, description, null);
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)
{
String parentId = id.substring(0, index);
parent = (CMRBugEJBLocal)cmrBugs.get(parentId);
}
entry.setValue(cmrBugHome.create(id, description, parent));
}
}
}
catch(Exception e)
{
e.printStackTrace();
throw new EJBException(e.getMessage());
}
}
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());
}
}
public void setupLoadFKState()
throws Exception
{
CMRBugEJBLocal bug1 = cmrBugHome.create("first", null, null);
CMRBugEJBLocal bug2 = cmrBugHome.create("second", null, null);
CMRBugEJBLocal bug3 = cmrBugHome.create("third", null, null);
CMRBugEJBLocal bug4 = cmrBugHome.create("forth", null, null);
bug1.setNextNode(bug2);
bug2.setNextNode(bug3);
bug3.setNextNode(bug4);
bug4.setPrevNode(bug3);
bug3.setPrevNode(bug2);
bug2.setPrevNode(bug1);
}
public void moveLastNodeBack()
throws Exception
{
CMRBugEJBLocal bug = cmrBugHome.findByPrimaryKey("forth");
CMRBugEJBLocal prev = bug.getPrevNode();
CMRBugEJBLocal next = bug.getNextNode();
CMRBugEJBLocal prevPrev = prev.getPrevNode();
prevPrev.setNextNode(bug);
bug.setPrevNode(prevPrev);
bug.setNextNode(prev);
prev.setPrevNode(bug);
prev.setNextNode(next);
}
public boolean lastHasNextNode()
throws Exception
{
CMRBugEJBLocal bug = cmrBugHome.findByPrimaryKey("third");
return bug.getNextNode() != null;
}
public void tearDownLoadFKState()
throws Exception
{
cmrBugHome.remove("first");
cmrBugHome.remove("second");
cmrBugHome.remove("third");
cmrBugHome.remove("forth");
}
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");
}
}