FileDocCategorySizeDatePackage
NewsBean.javaAPI DocExample2872Thu Jun 28 16:14:16 BST 2001com.ora.jsp.beans.news

NewsBean

public class NewsBean extends Object implements Serializable
This class maintains a list of NewsItemBean objects. It's only intended as an example. A real implementation would use a database to keep track of the objects instead.
author
Hans Bergsten, Gefion software
version
1.0

Fields Summary
private Vector
newsItems
private int[]
idSequence
Constructors Summary
public NewsBean()
Creates a new instance and fills it with a few sample news items.

    
                      
      
        addDefaultItems();
    
Methods Summary
private voidaddDefaultItems()
Creates sample news items and adds them to the list.

        NewsItemBean item = new NewsItemBean();
        item.setCategory("JSP");
        item.setMsg("New O'Reilly JSP book released!");
        item.setPostedBy("Hans Bergsten");
        setNewsItem(item);
        
        item = new NewsItemBean();
        item.setCategory("Servlet");
        item.setMsg("Servets and JSP is a great combination.");
        item.setPostedBy("Hans Bergsten");
        setNewsItem(item);
        
        item = new NewsItemBean();
        item.setCategory("EJB");
        item.setMsg("JSP makes a great front-end for EJB.");
        item.setPostedBy("Hans Bergsten");
        setNewsItem(item);
    
public NewsItemBean[]getNewsItems(java.lang.String[] categories)
Returns an array of all items in the specified categories.

        Vector matches = new Vector();
        synchronized (newsItems) {
            for (int i = 0; i < newsItems.size(); i++) {
                NewsItemBean item = (NewsItemBean) newsItems.elementAt(i);
                if (ArraySupport.contains(categories, item.getCategory())) {
                    matches.addElement(item);
                }
            }
        }
        NewsItemBean[] matchingItems = new NewsItemBean[matches.size()];
        matches.copyInto(matchingItems);
        return matchingItems;
    
public voidremoveNewsItem(int id)
Removes the new item with the specified id.

        synchronized (newsItems) {
            for (int i = 0; i < newsItems.size(); i++) {
                NewsItemBean item = (NewsItemBean) newsItems.elementAt(i);
                if (id == item.getId()) {
                    newsItems.removeElementAt(i);
                    break;
                }
            }
        }
    
public voidsetNewsItem(NewsItemBean newsItem)
Adds a news item to the list.

        synchronized (idSequence) {
            newsItem.setId(idSequence[0]++);
        }
        newsItems.addElement(newsItem);