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:
026: import javax.ejb.EJBException;
027: import javax.ejb.EntityBean;
028: import javax.ejb.EntityContext;
029: import javax.ejb.RemoveException;
030: import javax.ejb.EJBLocalObject;
031: import javax.ejb.CreateException;
032:
033: import org.jboss.test.cmp2.cmrtransaction.interfaces.TreeLocal;
034:
035: /**
036: * @author B Stansberry brian_stansberry@wanconcepts.com
037: */
038: public abstract class TreeEntity implements EntityBean {
039: // ---------------------------------------------------- Abstract Accessors
040:
041: // CMP Fields
042:
043: public abstract String getId();
044:
045: public abstract void setId(String id);
046:
047: public abstract int getSortOrder();
048:
049: public abstract void setSortOrder(int sortOrder);
050:
051: public abstract Collection getMenuChildren();
052:
053: // CMR Fields
054: public abstract void setMenuChildren(Collection menuChildren);
055:
056: public abstract TreeLocal getMenuParent();
057:
058: public abstract void setMenuParent(TreeLocal menuParent);
059:
060: // ------------------------------------------------------- Instance Fields
061:
062: private EntityContext entityContext = null;
063:
064: // -------------------------------------------------------- Bean Lifecycle
065:
066: public TreeEntity() {
067: }
068:
069: public String ejbCreate(String name, TreeLocal parent)
070: throws CreateException {
071: setId(name);
072: setSortOrder(0);
073:
074: return null;
075: }
076:
077: public void ejbPostCreate(String name, TreeLocal parent) {
078: setMenuParent(parent);
079: }
080:
081: public void ejbRemove() throws RemoveException, EJBException {
082: }
083:
084: public void ejbActivate() throws EJBException {
085: }
086:
087: public void ejbPassivate() throws EJBException {
088: }
089:
090: public void ejbLoad() throws EJBException {
091: }
092:
093: public void ejbStore() throws EJBException {
094: }
095:
096: public void setEntityContext(EntityContext entityContext)
097: throws EJBException {
098: this .entityContext = entityContext;
099: }
100:
101: public EntityContext getEntityContext() {
102: return entityContext;
103: }
104:
105: public void unsetEntityContext() throws EJBException {
106: entityContext = null;
107: }
108:
109: // -------------------------------------------------------- Public Methods
110:
111: public void setPrecededBy(TreeLocal precedingNode) {
112: EJBLocalObject self = getEntityContext().getEJBLocalObject();
113: if (precedingNode == null) {
114: setSortOrder(1);
115: } else if (self.isIdentical(precedingNode)) {
116: System.out.println("MenuResource " + getId() + ": "
117: + "attempt to set menu resource "
118: + precedingNode.getId()
119: + " as preceding node; cannot set a node as "
120: + "its own preceding node.");
121: } else if (ejbEquals(getMenuParent(), precedingNode
122: .getMenuParent())) {
123: setSortOrder(precedingNode.getSortOrder() + 1);
124: } else {
125: System.out.println("MenuResource " + getId() + ": "
126: + "attempt to set menu resource "
127: + precedingNode.getId()
128: + " as preceding node; invalid as node has a "
129: + "different menu parent.");
130: }
131: }
132:
133: // ------------------------------------------------------- Private Methods
134:
135: /**
136: * Generic EJBObject equality algoritm that can handle null arguments.
137: *
138: * @param a The first object to compare
139: * @param b The second object to compare
140: *
141: * @return <code>true</code> if both arguments are null or neither is null
142: * and <code>a.isIdentical(b)</code>. Otherwise, returns
143: * <code>false</code>
144: *
145: * @pre $none
146: * @post $none
147: */
148: private boolean ejbEquals(EJBLocalObject a, EJBLocalObject b) {
149: boolean result = false;
150:
151: if (a == b) {
152: result = true; // deals w/ null equality
153: } else if ((a == null) || (b == null)) {
154: result = false;
155: } else {
156: result = a.isIdentical(b);
157: }
158:
159: return result;
160: }
161:
162: }
|