Java Reference
In-Depth Information
Post newPost = getPostHome().create(boardName, nextID, discussionID);
newPost.setAuthor(author);
newPost.setDate(new java.sql.Date(date.getTime()));
newPost.setSubject(subject);
newPost.setText(text);
posts.addElement(newPost);
return nextID;
}
First, in addPost , we need to play the same trick as earlier to find the maxi-
mum value. We don't have direct access to SQL aggregate functions, so we
iterate through the board, looking for the largest element. Here, our ID s are
relative to a post, so we pay only a modest penalty. A better implementation
might be to include a unique ID generator. Many possible implementations
exist. Next, let's look at how we load the EJB :
public void ejbLoad() throws java.rmi.RemoteException {
try {
java.util.Enumeration e =
getPostHome().findAllForDiscussion(boardName, discussionID);
posts = new java.util.Vector();
while (e.hasMoreElements()) {
posts.addElement(e.nextElement());
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
We simply request to load all of the posts in a discussion. Then, we put them
all into a vector called posts . This method is fired when the EJB is loaded into
the container. Next, we have a finder for a post within the discussion:
public Post getPost(int postID) throws java.rmi.RemoteException {
Post rcPost = null;
for (int i = 0; i < posts.size(); i++) {
Post thisPost = (Post)posts.elementAt(i);
int thisID = thisPost.getPostID();
if (thisID == postID) {
rcPost = thisPost;
break;
}
}
return rcPost;
}
In this method, we iterate through the posts vector (populated in EJBLoad ),
and then return the one that has the specified ID . Finally, let's look at remove :
Search WWH ::




Custom Search