FileDocCategorySizeDatePackage
FakeDataAdapter.javaAPI DocExample4449Sun Sep 02 14:59:04 BST 2001com.oreilly.forum.fakeimpl

FakeDataAdapter

public class FakeDataAdapter extends DataAdapter

Fields Summary
private List
allBoards
private static long
nextMessageID
private Map
messageMap
Constructors Summary
public FakeDataAdapter()


        
        this.allBoards = new ArrayList();

        BoardSummary bs0 = new BoardSummaryImpl(0L,
                "Java Programming",
                "General programming questions about Java.");
        BoardSummary bs1 = new BoardSummaryImpl(1L,
                "XSLT Stylesheet Techniques",
                "Writing effective XSLT stylesheets.");
        this.allBoards.add(bs0);
        this.allBoards.add(bs1);

        this.postNewMessage(0L, "First subject in Java Prog",
                "burke_e@yahoo.com", "Sample message text");

    
Methods Summary
public java.util.IteratorgetAllBoards()

return
an iterator of BoardSummary objects.

        return this.allBoards.iterator();
    
public java.util.IteratorgetAllMessages(long boardID, MonthYear month)
If no messages exist for the specified board and month, return an empty iterator.

return
an iterator of MessageSummary objects.
throws
DataException if the boardID is illegal or a database error occurs.

        // this is slow, but works fine for a fake implementation
        List msgs = new ArrayList();
        Iterator iter = this.messageMap.values().iterator();
        while (iter.hasNext()) {
            MessageSummary curMsg = (MessageSummary) iter.next();
            if (curMsg.getBoard().getID() == boardID
                    && month.containsInMonth(curMsg.getCreateDate())) {
                msgs.add(curMsg);
            }
        }
        return msgs.iterator();
    
public BoardSummarygetBoardSummary(long boardID)

        Iterator iter = getAllBoards();
        while (iter.hasNext()) {
            BoardSummary curBoard = (BoardSummary) iter.next();
            if (curBoard.getID() == boardID) {
                return curBoard;
            }
        }
        throw new DataException("Illegal boardID: " + boardID);
    
public MessagegetMessage(long msgID)

param
msgID must be a valid message identifier.
return
the message with the specified id.
throws
DataException if msgID does not exist or a database error occurs.

        Message msg = (Message) this.messageMap.get(new Long(msgID));
        if (msg != null) {
            return msg;
        }
        throw new DataException("Invalid msgID");
    
private static synchronized longgetNextMessageID()

        nextMessageID++;
        return nextMessageID;
    
public MessagepostNewMessage(long boardID, java.lang.String msgSubject, java.lang.String authorEmail, java.lang.String msgText)
Post a new message.

return
the newly created message.
throws
DataException if a database error occurs, or if any parameter is illegal.

        BoardSummary boardSum = getBoardSummary(boardID);
        long msgID = getNextMessageID();

        Message msg = new MessageImpl(msgID, new DayMonthYear(), boardSum,
                msgSubject, authorEmail, msgText, -1);
        this.messageMap.put(new Long(msg.getID()), msg);

        ((BoardSummaryImpl) boardSum).messageAdded(msg);

        return msg;
    
public MessagereplyToMessage(long origMsgID, java.lang.String msgSubject, java.lang.String authorEmail, java.lang.String msgText)
Add a reply to an existing message.

throws
DataException if a database error occurs, or if any parameter is illegal.

        MessageSummary origMsg = getMessage(origMsgID);
        long msgID = getNextMessageID();

        Message msg = new MessageImpl(msgID, new DayMonthYear(), origMsg.getBoard(),
                msgSubject, authorEmail, msgText, origMsgID);

        this.messageMap.put(new Long(msg.getID()), msg);
        return msg;