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:
023: package org.jboss.ejb3.test.stateful.nested.base.xpc;
024:
025: import java.util.HashSet;
026: import java.util.Set;
027:
028: import javax.ejb.Remote;
029: import javax.ejb.Stateful;
030: import javax.ejb.TransactionAttribute;
031: import javax.ejb.TransactionAttributeType;
032: import javax.persistence.EntityManager;
033: import javax.persistence.PersistenceContext;
034:
035: import org.jboss.ejb3.test.stateful.nested.base.BeanMonitorBean;
036:
037: /**
038: * A NestedXPCMonitorBean.
039: *
040: * @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a>
041: * @version $Revision: 1.1 $
042: */
043: @Stateful
044: @Remote(NestedXPCMonitor.class)
045: @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
046: public class NestedXPCMonitorBean extends BeanMonitorBean implements
047: NestedXPCMonitor {
048: @PersistenceContext(unitName="tempdb")
049: private EntityManager em;
050:
051: private Set<Long> ids = new HashSet<Long>();
052:
053: @TransactionAttribute(TransactionAttributeType.REQUIRED)
054: public Customer find(long id) {
055: ids.add(new Long(id));
056: return em.find(Customer.class, id);
057: }
058:
059: public boolean compareLocalNestedToLocalDeepNested(long id) {
060: try {
061: Contained midLevel = (Contained) localNested;
062: midLevel.setCustomer(id);
063: midLevel.setContainedCustomer();
064: return midLevel.checkContainedCustomer();
065: } catch (Exception e) {
066: log.debug("compareLocalNestedToDeepNested(): "
067: + e.getLocalizedMessage());
068: }
069: return false;
070: }
071:
072: public boolean compareNestedToDeepNested(long id) {
073: try {
074: Contained midLevel = (Contained) nested;
075: midLevel.setCustomer(id);
076: midLevel.setContainedCustomer();
077: return midLevel.checkContainedCustomer();
078: } catch (Exception e) {
079: log.debug("compareNestedToDeepNested(): "
080: + e.getLocalizedMessage());
081: }
082: return false;
083: }
084:
085: public boolean compareTopToLocalNested(long id) {
086: try {
087: ShoppingCart cart = (ShoppingCart) parent;
088: cart.setCustomer(id);
089: cart.setContainedCustomer();
090: return cart.checkContainedCustomer();
091: } catch (Exception e) {
092: log.debug("compareTopToLocalNested(): "
093: + e.getLocalizedMessage());
094: }
095: return false;
096: }
097:
098: public boolean compareTopToNested(long id) {
099: try {
100: Customer mid = findNested(id);
101: Customer top = findParent(id);
102:
103: return (mid != null && mid != top && mid.equals(top));
104: } catch (Exception e) {
105: log.debug("compareTopToNested(): "
106: + e.getLocalizedMessage());
107: }
108: return false;
109: }
110:
111: public boolean compareNestedToLocalNested(long id) {
112: try {
113: Customer remote = findNested(id);
114: Customer local = findLocalNested(id);
115:
116: return (remote != null && remote.equals(local));
117: } catch (Exception e) {
118: log.debug("compareTopToLocalNested(): "
119: + e.getLocalizedMessage());
120: }
121: return false;
122: }
123:
124: @TransactionAttribute(TransactionAttributeType.SUPPORTS)
125: public Customer findDeepNested(long id) {
126: try {
127: return ((DeepNestedContained) deepNested).find(id);
128: } catch (Exception e) {
129: log.debug("findDeepNested(): " + e.getLocalizedMessage());
130: }
131: return null;
132: }
133:
134: @TransactionAttribute(TransactionAttributeType.SUPPORTS)
135: public Customer findLocalDeepNested(long id) {
136: try {
137: return ((DeepNestedContained) localDeepNested).find(id);
138: } catch (Exception e) {
139: log.debug("findLocalDeepNested(): "
140: + e.getLocalizedMessage());
141: }
142: return null;
143: }
144:
145: @TransactionAttribute(TransactionAttributeType.SUPPORTS)
146: public Customer findLocalNested(long id) {
147: try {
148: return ((Contained) localNested).find(id);
149: } catch (Exception e) {
150: log.debug("findLocalNested(): " + e.getLocalizedMessage());
151: }
152: return null;
153: }
154:
155: @TransactionAttribute(TransactionAttributeType.SUPPORTS)
156: public Customer findNested(long id) {
157: try {
158: return ((Contained) nested).find(id);
159: } catch (Exception e) {
160: log.debug("findNested(): " + e.getLocalizedMessage());
161: }
162: return null;
163: }
164:
165: @TransactionAttribute(TransactionAttributeType.SUPPORTS)
166: public Customer findParent(long id) {
167: try {
168: return ((ShoppingCart) parent).find(id);
169: } catch (Exception e) {
170: log.debug("findParent(): " + e.getLocalizedMessage());
171: }
172: return null;
173: }
174:
175: @Override
176: @TransactionAttribute(TransactionAttributeType.REQUIRED)
177: public void remove() {
178: super .remove();
179:
180: for (Long id : ids) {
181: try {
182: Customer c = em.find(Customer.class, id.longValue());
183: if (c != null)
184: em.remove(c);
185: } catch (Exception e) {
186: log.debug("remove(): Problem removing " + id, e);
187: }
188: }
189: }
190:
191: }
|