Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Event Bean Java code example - Click here to copy ->>>

   Can't find what you're looking for? Try our search:

Really working examples categorized by API, package, class. You can compile and run our examples right away! Not from source code for Java projects - only working examples! Copy, compile and run!
------------------

Code:

 
/*--

 Copyright (C) 2001 Brett McLaughlin.
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions, and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions, and the disclaimer that follows
    these conditions in the do*****entation and/or other materials
    provided with the distribution.

 3. The name "Building Java Enterprise Applications" must not be used
    to endorse or promote products derived from this software without
    prior written permission.  For written permission, please contact
    brett@newInstance.com.

 In addition, we request (but do not require) that you include in the
 end-user do*****entation provided with the redistribution and/or in the
 software itself an acknowledgement equivalent to the following:
     "This product includes software developed for the
      'Building Java Enterprise Applications' book, by Brett McLaughlin
      (O'Reilly & Associates)."

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED.  IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 */
package com.forethought.ejb.event;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.forethought.ejb.sequence.SequenceException;
import com.forethought.ejb.sequence.SequenceLocal;
import com.forethought.ejb.sequence.SequenceLocalHome;
import com.forethought.ejb.util.EntityAdapter;

// User bean
import com.forethought.ejb.user.User;
import com.forethought.ejb.user.UserHome;
import com.forethought.ejb.user.UserInfo;
import com.forethought.ejb.user.UserLocal;
import com.forethought.ejb.user.UserLocalHome;

public abstract class EventBean extends EntityAdapter {

    public Integer ejbCreate(String description, Date dateTime,
                             Collection attendees)
        throws CreateException {
        // Get the next primary key value
        try {
            Context context = new InitialContext();

            // Note that RMI-IIOP narrowing is not required
            SequenceLocalHome home = (SequenceLocalHome)
                context.lookup("java:comp/env/ejb/SequenceLocalHome");
            SequenceLocal sequence = home.create();
            String eventKey =
                (String)context.lookup(
                    "java:comp/env/constants/EventKey");
            Integer id = sequence.getNextValue(eventKey);

            // Set values
            setId(id);
            setDescription(description);
            setDateTime(dateTime);

            return null;
        } catch (NamingException e) {
            throw new CreateException("Could not obtain an " +
                "InitialContext.");
        } catch (SequenceException e) {
            throw new CreateException("Error getting primary key value: " +
                e.getMessage());
        }
    }

    public void ejbPostCreate(String description, Date dateTime,
                              Collection attendees) {
        // Handle CMP relationships
        setAttendees(attendees);
    }

    public EventInfo getInfo() throws RemoteException {
        EventInfo eventInfo =
            new EventInfo(getId().intValue(), getDescription(), getDateTime(),
                          getAttendees());
        return eventInfo;
    }

    public void setInfo(EventInfo eventInfo) {
        setDescription(eventInfo.getDescription());
        setDateTime(eventInfo.getDateTime());
        setAttendees(eventInfo.getAttendees());
    }

    public Collection getAttendees() {
        try {
            Collection attendeesLocal = getAttendeesLocal();
            Collection attendees = new LinkedList();

            // Get the UserHome interface
            Context context = new InitialContext();
            UserHome userHome =
                (UserHome)context.lookup(
                    "java:comp/env/ejb/UserHome");

            // Convert each local User into a remote User
            for (Iterator i = attendeesLocal.iterator(); i.hasNext(); ) {
                UserLocal userLocal = (UserLocal)i.next();

                // Construct primary key for this office
                Integer userID = userLocal.getId();

                User user = userHome.findByPrimaryKey(userID);
                attendees.add(user);
            }

            return attendees;
        } catch (NamingException e) {
            throw new EJBException("Error looking up User bean: " +
                e.getMessage());
        } catch (RemoteException e) {
            throw new EJBException("Error looking up User bean: " +
                e.getMessage());
        } catch (FinderException shouldNeverHappen) {
            // This should never happen; the ID from a user's remote
            //   interface should match a user's ID in a local interface
            throw new EJBException("Error matching remote User to " +
                "local User: " + shouldNeverHappen.getMessage());
        }
    }

    public void setAttendees(Collection attendees) {
        try {
            // Handle case where no attendees supplied
            if (attendees == null) {
                setAttendeesLocal(null);
                return;
            }

            // Get the local User home interface
            Context context = new InitialContext();
            UserLocalHome userLocalHome =
                (UserLocalHome)context.lookup(
                    "java:comp/env/ejb/UserLocalHome");

            Collection attendeesLocal = new LinkedList();
            // Convert each remote User to a local User
            for (Iterator i = attendees.iterator(); i.hasNext(); ) {
                // Construct primary key for this office
                Integer userID;
                Object obj = i.next();

                if (obj instanceof User) {
                    userID = ((User)obj).getId();
                } else if (obj instanceof UserInfo) {
                    userID = new Integer(((UserInfo)obj).getId());
                } else {
                    throw new EJBException("Invalid object type in attendee " +
                        "list.");
                }

                // Find the local interface for this user
                UserLocal userLocal =
                    userLocalHome.findByPrimaryKey(userID);
                attendeesLocal.add(userLocal);
            }

            setAttendeesLocal(attendeesLocal);

        } catch (NamingException e) {
            throw new EJBException("Error looking up User bean: " +
                e.getMessage());
        } catch (RemoteException e) {
            throw new EJBException("Error looking up User bean: " +
                e.getMessage());
        } catch (FinderException shouldNeverHappen) {
            // This should never happen; the ID from a user's remote
            //   interface should match a user's ID in a local interface
            throw new EJBException("Error matching remote User to " +
                "local User: " + shouldNeverHappen.getMessage());
        }
    }

    public abstract Integer getId();
    public abstract void setId(Integer id);

    public abstract String getDescription();
    public abstract void setDescription(String description);

    public abstract Date getDateTime();
    public abstract void setDateTime(Date dateTime);

    public abstract Collection getAttendeesLocal();
    public abstract void setAttendeesLocal(Collection attendeesLocal);
}


/*
 *   Building Java Enterprise Applications
 *   By Brett McLaughlin
 *   First Edition March 2002
 *   ISBN: 0-596-00123-1
 *   http://www.oreilly.com/catalog/javentappsv1/
 */



 
 



References.

The list of classes which were used on this page you can find below. The links to Java API contain official SUN documentation about all used classes.

  javax.naming.Context

  java.lang.Integer

  java.sql.Date

  java.util.Collection

  javax.naming.InitialContext

  java.util.Set

  javax.naming.NamingException

  java.lang.Error

  java.rmi.RemoteException

  java.util.LinkedList

  java.util.Iterator

  java.lang.Object

  org.omg.CORBA.DynAnyPackage.Invalid




[ Go Back ]



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy