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.cmrstress.ejb;
023:
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.Map;
027: import java.util.Set;
028:
029: import javax.ejb.CreateException;
030: import javax.ejb.EJBException;
031: import javax.ejb.EntityBean;
032: import javax.ejb.EntityContext;
033: import javax.ejb.RemoveException;
034: import javax.naming.NamingException;
035:
036: import org.apache.log4j.Logger;
037: import org.jboss.test.cmp2.cmrstress.interfaces.ChildLocal;
038: import org.jboss.test.cmp2.cmrstress.interfaces.ChildUtil;
039:
040: /**
041: * This class implements the "parent" side of 1..many unidirectional relationship.
042: * It doesn't do anything particularly interesting besides provide the CMR getter
043: * and a method (@see #getPropertyMap()) that provides a transactional context for
044: * iterating over the CMR collection.
045: *
046: * This code is based upon the original test case provided by Andrew May.
047: *
048: * @version <tt>$Revision: 57211 $</tt>
049: * @author <a href="mailto:steve@resolvesw.com">Steve Coy</a>.
050: *
051: * @ejb.bean name="Parent"
052: * type="CMP"
053: * cmp-version="2.x"
054: * view-type="both"
055: * jndi-name="cmrstress/Parent"
056: * primkey-field="id"
057: *
058: * @ejb.pk class="java.lang.String"
059: * generate="false"
060: *
061: * @ejb.persistence table-name="StressedParent"
062: *
063: * @@ejb.home generate="both"
064: * @@ejb.interface generate="both"
065: *
066: * @ejb.ejb-ref ejb-name="Child"
067: * view-type="local"
068: *
069: * @ejb.transaction type="Supports"
070: *
071: * @jboss.persistence
072: * create-table="true"
073: * remove-table="true"
074: * @jboss.tuned-updates tune="true"
075: */
076: public abstract class ParentBean implements EntityBean {
077: /**
078: * CMP get method for Id attribute.
079: * @ejb.interface-method view-type="remote"
080: * @ejb.persistent-field
081: * @jboss.column-name name="id"
082: * @jboss.method-attributes read-only="true"
083: */
084: public abstract String getId();
085:
086: /**
087: * CMP set method for Id attribute.
088: * @ejb.interface-method view-type="remote"
089: * @ejb.transaction type="Mandatory"
090: */
091: public abstract void setId(String id);
092:
093: /**
094: * Get Children that apply to this Parent.
095: *
096: * @ejb.interface-method view-type="remote"
097: * @ejb.relation name="Parent-Child"
098: * role-name="Parent-has-Children"
099: * cascade-delete="no"
100: * target-ejb="Child"
101: * target-role-name="Child-of-Parent"
102: * target-cascade-delete="yes"
103: * @jboss.target-relation related-pk-field="id"
104: * fk-column="parentid"
105: * jboss.method-attributes read-only="true"
106: */
107: public abstract Set getChildren();
108:
109: /**
110: * Set Children.
111: * @ejb.interface-method view-type="remote"
112: * @ejb.transaction type="Mandatory"
113: */
114: public abstract void setChildren(Set children);
115:
116: /**
117: * Get a map of Child values.
118: * This is the current axis of evil.
119: *
120: * @ejb.interface-method view-type="remote"
121: * @ejb.transaction type="Required"
122: * @jboss.method-attributes read-only="true"
123: */
124: public Map getPropertyMap() {
125: Map result = new HashMap();
126: Set children = getChildren();
127: for (Iterator i = children.iterator(); i.hasNext();) {
128: ChildLocal c = (ChildLocal) i.next();
129: result.put(c.getName(), c.getValue());
130: }
131:
132: return result;
133: }
134:
135: /**
136: * Adds a child bean with the given attributes to this bean.
137: * @ejb.interface-method view-type="remote"
138: * @ejb.transaction type="RequiresNew"
139: */
140: public void addChild(int k, String field1, String field2)
141: throws CreateException {
142: msLog.debug("Adding child with pk: " + k);
143: try {
144: getChildren().add(
145: ChildUtil.getLocalHome().create(
146: Integer.toString(k), field1, field2));
147: } catch (NamingException e) {
148: throw new EJBException(e);
149: }
150: }
151:
152: /**
153: * Create method for Entity.
154: * @ejb.create-method view-type="remote"
155: * @ejb.transaction type="RequiresNew"
156: */
157: public String ejbCreate(String id) throws javax.ejb.CreateException {
158: msLog.debug("Created '" + id + "'");
159: setId(id);
160: return null;
161: }
162:
163: public void ejbPostCreate(String id) {
164: }
165:
166: /**
167: * @see javax.ejb.EntityBean#ejbActivate()
168: */
169: public void ejbActivate() {
170: }
171:
172: /**
173: * @see javax.ejb.EntityBean#ejbLoad()
174: */
175: public void ejbLoad() {
176: }
177:
178: /**
179: * @see javax.ejb.EntityBean#ejbPassivate()
180: */
181: public void ejbPassivate() {
182: }
183:
184: /**
185: * @see javax.ejb.EntityBean#ejbRemove()
186: */
187: public void ejbRemove() throws RemoveException {
188: msLog.debug("Removed");
189: }
190:
191: /**
192: * @see javax.ejb.EntityBean#ejbStore()
193: */
194: public void ejbStore() {
195: }
196:
197: /**
198: * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
199: */
200: public void setEntityContext(EntityContext arg0) {
201: }
202:
203: /**
204: * @see javax.ejb.EntityBean#unsetEntityContext()
205: */
206: public void unsetEntityContext() {
207: }
208:
209: private static final Logger msLog = Logger
210: .getLogger(ParentBean.class);
211:
212: }
|