1 22 package org.jboss.test.cmp2.cmr.ejb; 23 24 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import java.util.SortedMap ; 28 29 import javax.ejb.CreateException ; 30 import javax.ejb.EJBException ; 31 import javax.ejb.SessionBean ; 32 import javax.ejb.SessionContext ; 33 34 import javax.naming.InitialContext ; 35 import javax.naming.NamingException ; 36 37 import org.jboss.logging.Logger; 38 39 import org.jboss.test.cmp2.cmr.interfaces.CMRBugEJBLocalHome; 40 import org.jboss.test.cmp2.cmr.interfaces.CMRBugEJBLocal; 41 42 55 public class CMRBugManagerBean 56 implements SessionBean 57 { 58 private CMRBugEJBLocalHome cmrBugHome; 59 60 private Logger log = Logger.getLogger(getClass()); 61 62 public CMRBugManagerBean() 63 { 64 } 65 66 72 public void createCMRBugs(SortedMap cmrBugs) 73 { 74 try 75 { 76 if(!cmrBugs.isEmpty()) 77 { 78 Iterator i = cmrBugs.entrySet().iterator(); 79 Map.Entry entry = (Map.Entry )i.next(); 80 81 String root = (String )entry.getKey(); 85 86 String id = root; 87 String description = (String )entry.getValue(); 88 89 CMRBugEJBLocal parent = cmrBugHome.create(id, description, null); 90 entry.setValue(parent); 91 92 while(i.hasNext()) 93 { 94 entry = (Map.Entry )i.next(); 95 96 id = (String )entry.getKey(); 97 description = (String )entry.getValue(); 98 99 int index = id.lastIndexOf("."); 100 if(index != -1) 101 { 102 String parentId = id.substring(0, index); 106 parent = (CMRBugEJBLocal)cmrBugs.get(parentId); 107 } 108 entry.setValue(cmrBugHome.create(id, description, parent)); 109 } 110 } 111 } 112 catch(Exception e) 113 { 114 e.printStackTrace(); 115 throw new EJBException (e.getMessage()); 116 } 117 } 118 119 126 public String [] getParentFor(String id) 127 { 128 try 129 { 130 CMRBugEJBLocal cmrBug = cmrBugHome.findByPrimaryKey(id); 131 CMRBugEJBLocal parent = cmrBug.getParent(); 132 133 String [] parentIdAndDescription = null; 134 if(parent != null) 135 { 136 parentIdAndDescription = new String [2]; 137 parentIdAndDescription[0] = parent.getId(); 138 parentIdAndDescription[1] = parent.getDescription(); 139 } 140 141 return parentIdAndDescription; 142 } 143 catch(Exception e) 144 { 145 e.printStackTrace(); 146 throw new EJBException (e.getMessage()); 147 } 148 } 149 150 154 public void setupLoadFKState() 155 throws Exception 156 { 157 CMRBugEJBLocal bug1 = cmrBugHome.create("first", null, null); 158 CMRBugEJBLocal bug2 = cmrBugHome.create("second", null, null); 159 CMRBugEJBLocal bug3 = cmrBugHome.create("third", null, null); 160 CMRBugEJBLocal bug4 = cmrBugHome.create("forth", null, null); 161 162 bug1.setNextNode(bug2); 163 bug2.setNextNode(bug3); 164 bug3.setNextNode(bug4); 165 166 bug4.setPrevNode(bug3); 167 bug3.setPrevNode(bug2); 168 bug2.setPrevNode(bug1); 169 } 170 171 175 public void moveLastNodeBack() 176 throws Exception 177 { 178 CMRBugEJBLocal bug = cmrBugHome.findByPrimaryKey("forth"); 179 180 CMRBugEJBLocal prev = bug.getPrevNode(); 181 CMRBugEJBLocal next = bug.getNextNode(); 182 CMRBugEJBLocal prevPrev = prev.getPrevNode(); 183 184 prevPrev.setNextNode(bug); 185 bug.setPrevNode(prevPrev); 186 bug.setNextNode(prev); 187 prev.setPrevNode(bug); 188 prev.setNextNode(next); 189 } 190 191 195 public boolean lastHasNextNode() 196 throws Exception 197 { 198 CMRBugEJBLocal bug = cmrBugHome.findByPrimaryKey("third"); 199 return bug.getNextNode() != null; 200 } 201 202 206 public void tearDownLoadFKState() 207 throws Exception 208 { 209 cmrBugHome.remove("first"); 210 cmrBugHome.remove("second"); 211 cmrBugHome.remove("third"); 212 cmrBugHome.remove("forth"); 213 } 214 215 219 224 public void ejbCreate() 225 throws CreateException 226 { 227 try 228 { 229 cmrBugHome = lookupCMRBugHome(); 230 } 231 catch(Exception e) 232 { 233 throw new CreateException (e.getMessage()); 234 } 235 } 236 237 public void ejbActivate() 238 { 239 try 240 { 241 cmrBugHome = lookupCMRBugHome(); 242 } 243 catch(Exception e) 244 { 245 throw new EJBException (e.getMessage()); 246 } 247 } 248 249 public void ejbPassivate() 250 { 251 cmrBugHome = null; 252 } 253 254 public void ejbRemove() 255 { 256 } 257 258 public void setSessionContext(SessionContext sessionContext) 259 { 260 } 261 262 private CMRBugEJBLocalHome lookupCMRBugHome() 263 throws NamingException 264 { 265 InitialContext initialContext = new InitialContext (); 266 return (CMRBugEJBLocalHome)initialContext.lookup("java:comp/env/ejb/CMRBug"); 267 } 268 } 269 | Popular Tags |