001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.cmrtransaction.ejb;
023:
024: import java.util.Collection;
025: import java.util.Iterator;
026:
027: import javax.ejb.CreateException;
028: import javax.ejb.EJBException;
029: import javax.ejb.FinderException;
030: import javax.ejb.ObjectNotFoundException;
031: import javax.ejb.RemoveException;
032: import javax.ejb.SessionBean;
033: import javax.ejb.SessionContext;
034: import javax.naming.InitialContext;
035: import javax.naming.NamingException;
036: import javax.rmi.PortableRemoteObject;
037:
038: import org.jboss.test.cmp2.cmrtransaction.interfaces.TreeLocalHome;
039: import org.jboss.test.cmp2.cmrtransaction.interfaces.TreeLocal;
040:
041: /**
042: *
043: * @author B Stansberry brian_stansberry@wanconcepts.com
044: */
045: public class TreeFacadeSession implements SessionBean {
046: // ------------------------------------------------------- Instance Fields
047:
048: private transient TreeLocalHome treeHome = null;
049:
050: // -------------------------------------------------------- Bean Lifecycle
051:
052: public TreeFacadeSession() {
053: }
054:
055: public void ejbCreate() throws CreateException {
056: }
057:
058: public void setSessionContext(SessionContext sessionContext) {
059: }
060:
061: public void ejbRemove() throws EJBException {
062: }
063:
064: public void ejbActivate() throws EJBException {
065: }
066:
067: public void ejbPassivate() throws EJBException {
068: }
069:
070: // ------------------------------------------------------------ TreeFacade
071:
072: public void setup() {
073: try {
074: TreeLocalHome tlh = getTreeLocalHome();
075: TreeLocal tl = null;
076: try {
077: tl = tlh.findByPrimaryKey("Parent");
078: tl.remove();
079: } catch (ObjectNotFoundException f) {
080: }
081: try {
082: tl = tlh.findByPrimaryKey("Child 1");
083: tl.remove();
084: } catch (ObjectNotFoundException f) {
085: }
086: try {
087: tl = tlh.findByPrimaryKey("Child 2");
088: tl.remove();
089: } catch (ObjectNotFoundException f) {
090: }
091: } catch (NamingException n) {
092: throw new EJBException(n);
093: } catch (RemoveException r) {
094: throw new EJBException(r);
095: } catch (FinderException f) {
096: throw new EJBException(f);
097: }
098: }
099:
100: public void createNodes() {
101: try {
102: TreeLocalHome tlh = getTreeLocalHome();
103: TreeLocal parent = null;
104: parent = tlh.create("Parent", null);
105: tlh.create("Child 1", parent);
106: tlh.create("Child 2", null);
107: } catch (NamingException n) {
108: throw new EJBException(n);
109: } catch (CreateException c) {
110: throw new EJBException(c);
111: }
112: }
113:
114: public void rearrangeNodes() {
115: try {
116: TreeLocalHome tlh = getTreeLocalHome();
117: TreeLocal target = tlh.findByPrimaryKey("Child 2");
118: TreeLocal sibling = null;
119: sibling = tlh.findByPrimaryKey("Child 1");
120: /*
121: TreeLocal parent = tlh.findByPrimaryKey("Parent");
122: Collection coll = parent.getMenuChildren();
123: Iterator iter = coll.iterator();
124: sibling = (TreeLocal) iter.next();
125: */
126: target.setMenuParent(sibling.getMenuParent());
127: target.setPrecededBy(sibling);
128: } catch (NamingException n) {
129: throw new EJBException(n);
130: } catch (FinderException f) {
131: throw new EJBException(f);
132: }
133: }
134:
135: // ------------------------------------------------------- Private Methods
136:
137: private TreeLocalHome getTreeLocalHome() throws NamingException {
138: if (treeHome == null) {
139: InitialContext ctx = new InitialContext();
140: Object obj = ctx
141: .lookup("java:/comp/env/ejb/cmrTransactionTest/CMRTreeLocal");
142: treeHome = (TreeLocalHome) PortableRemoteObject.narrow(obj,
143: TreeLocalHome.class);
144: }
145: return treeHome;
146: }
147:
148: }
|