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.commerce;
023:
024: import java.util.Collection;
025: import javax.ejb.CreateException;
026: import javax.ejb.EJBException;
027: import javax.ejb.EntityBean;
028: import javax.ejb.EntityContext;
029: import javax.ejb.FinderException;
030: import javax.naming.InitialContext;
031: import javax.rmi.PortableRemoteObject;
032:
033: import org.jboss.varia.autonumber.AutoNumberFactory;
034:
035: public abstract class CustomerBean implements EntityBean {
036: transient private EntityContext ctx;
037:
038: public Long ejbCreate() throws CreateException {
039: setId(new Long(AutoNumberFactory.getNextInteger("Customer")
040: .longValue()));
041: return null;
042: }
043:
044: public void ejbPostCreate() {
045: }
046:
047: public abstract Long getId();
048:
049: public abstract void setId(Long id);
050:
051: public abstract String getName();
052:
053: public abstract void setName(String name);
054:
055: public User getUser() {
056: return userLocalToUser(getUserLocal());
057: }
058:
059: public void setUser(User user) {
060: setUserLocal(userToUserLocal(user));
061: }
062:
063: public abstract UserLocal getUserLocal();
064:
065: public abstract void setUserLocal(UserLocal user);
066:
067: public abstract Collection getOrders();
068:
069: public abstract void setOrders(Collection c);
070:
071: public abstract Collection getAddresses();
072:
073: public abstract void setAddresses(Collection c);
074:
075: public abstract Collection ejbSelectAddressesInCAForCustomer(Long id)
076: throws FinderException;
077:
078: public void setEntityContext(EntityContext ctx) {
079: this .ctx = ctx;
080: }
081:
082: public void unsetEntityContext() {
083: this .ctx = null;
084: }
085:
086: public void ejbActivate() {
087: }
088:
089: public void ejbPassivate() {
090: }
091:
092: public void ejbLoad() {
093: }
094:
095: public void ejbStore() {
096: }
097:
098: public void ejbRemove() {
099: }
100:
101: protected User userLocalToUser(UserLocal userLocal) {
102: if (userLocal == null) {
103: return null;
104: }
105: UserHome userHome = getUserHome();
106: try {
107: return userHome.findByPrimaryKey((String) userLocal
108: .getPrimaryKey());
109: } catch (Exception e) {
110: throw new EJBException(
111: "Error converting user local into user", e);
112: }
113: }
114:
115: protected UserLocal userToUserLocal(User user) {
116: if (user == null) {
117: return null;
118: }
119: UserLocalHome userLocalHome = getUserLocalHome();
120: try {
121: return userLocalHome.findByPrimaryKey((String) user
122: .getPrimaryKey());
123: } catch (Exception e) {
124: throw new EJBException(
125: "Error converting user into user local", e);
126: }
127: }
128:
129: private UserHome getUserHome() {
130: try {
131: InitialContext jndiContext = new InitialContext();
132:
133: Object ref = jndiContext.lookup("java:comp/env/ejb/User");
134:
135: // remote interfaces have to be narrowed
136: return (UserHome) PortableRemoteObject.narrow(ref,
137: UserHome.class);
138: } catch (Exception e) {
139: throw new EJBException("Exception in getUserHome: ", e);
140: }
141: }
142:
143: private UserLocalHome getUserLocalHome() {
144: try {
145: InitialContext jndiContext = new InitialContext();
146:
147: return (UserLocalHome) jndiContext
148: .lookup("java:comp/env/ejb/UserLocal");
149: } catch (Exception e) {
150: throw new EJBException("Exception in getUserLocalHome: ", e);
151: }
152: }
153:
154: }
|