KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > cmrtransaction > ejb > TreeEntity


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.cmrtransaction.ejb;
23
24 import java.util.Collection JavaDoc;
25
26 import javax.ejb.EJBException JavaDoc;
27 import javax.ejb.EntityBean JavaDoc;
28 import javax.ejb.EntityContext JavaDoc;
29 import javax.ejb.RemoveException JavaDoc;
30 import javax.ejb.EJBLocalObject JavaDoc;
31 import javax.ejb.CreateException JavaDoc;
32
33 import org.jboss.test.cmp2.cmrtransaction.interfaces.TreeLocal;
34
35
36 /**
37  * @author B Stansberry brian_stansberry@wanconcepts.com
38  */

39 public abstract class TreeEntity implements EntityBean JavaDoc
40 {
41     // ---------------------------------------------------- Abstract Accessors
42

43
44     // CMP Fields
45

46     public abstract String JavaDoc getId();
47     public abstract void setId(String JavaDoc id);
48     public abstract int getSortOrder();
49     public abstract void setSortOrder(int sortOrder);
50     public abstract Collection JavaDoc getMenuChildren();
51
52
53     // CMR Fields
54
public abstract void setMenuChildren(Collection JavaDoc menuChildren);
55     public abstract TreeLocal getMenuParent();
56
57     public abstract void setMenuParent(TreeLocal menuParent);
58
59     // ------------------------------------------------------- Instance Fields
60

61
62     private EntityContext JavaDoc entityContext = null;
63
64
65     // -------------------------------------------------------- Bean Lifecycle
66

67     public TreeEntity() {}
68
69
70     public String JavaDoc ejbCreate(String JavaDoc name, TreeLocal parent)
71         throws CreateException JavaDoc
72     {
73         setId(name);
74         setSortOrder(0);
75
76         return null;
77     }
78
79     public void ejbPostCreate(String JavaDoc name, TreeLocal parent)
80     {
81         setMenuParent(parent);
82     }
83
84
85     public void ejbRemove() throws RemoveException JavaDoc, EJBException JavaDoc {}
86     public void ejbActivate() throws EJBException JavaDoc {}
87     public void ejbPassivate() throws EJBException JavaDoc {}
88     public void ejbLoad() throws EJBException JavaDoc {}
89     public void ejbStore() throws EJBException JavaDoc {}
90
91     public void setEntityContext(EntityContext JavaDoc entityContext) throws EJBException JavaDoc
92     {
93         this.entityContext = entityContext;
94     }
95
96     public EntityContext JavaDoc getEntityContext()
97     {
98         return entityContext;
99     }
100
101     public void unsetEntityContext() throws EJBException JavaDoc
102     {
103         entityContext = null;
104     }
105
106
107     // -------------------------------------------------------- Public Methods
108

109     public void setPrecededBy(TreeLocal precedingNode)
110     {
111         EJBLocalObject JavaDoc self = getEntityContext().getEJBLocalObject();
112         if (precedingNode == null)
113         {
114             setSortOrder(1);
115         }
116         else if (self.isIdentical(precedingNode))
117         {
118             System.out.println("MenuResource " + getId() + ": "
119                                 + "attempt to set menu resource "
120                                 + precedingNode.getId()
121                                 + " as preceding node; cannot set a node as "
122                                 + "its own preceding node.");
123         }
124         else if (ejbEquals(getMenuParent(), precedingNode.getMenuParent()))
125         {
126             setSortOrder(precedingNode.getSortOrder() + 1);
127         }
128         else
129         {
130             System.out.println("MenuResource " + getId() + ": "
131                                 + "attempt to set menu resource "
132                                 + precedingNode.getId()
133                                 + " as preceding node; invalid as node has a "
134                                 + "different menu parent.");
135         }
136     }
137
138
139     // ------------------------------------------------------- Private Methods
140

141
142     /**
143      * Generic EJBObject equality algoritm that can handle null arguments.
144      *
145      * @param a The first object to compare
146      * @param b The second object to compare
147      *
148      * @return <code>true</code> if both arguments are null or neither is null
149      * and <code>a.isIdentical(b)</code>. Otherwise, returns
150      * <code>false</code>
151      *
152      * @pre $none
153      * @post $none
154      */

155     private boolean ejbEquals(EJBLocalObject JavaDoc a, EJBLocalObject JavaDoc b)
156     {
157         boolean result = false;
158
159         if (a == b)
160         {
161             result = true; // deals w/ null equality
162
}
163         else if ((a == null) || (b == null))
164         {
165             result = false;
166         }
167         else
168         {
169             result = a.isIdentical(b);
170         }
171
172         return result;
173     }
174
175
176 }
177
Popular Tags