/*  PAT: Persistent Applications Toolkit (patsystem.sf.net)
 *  Copyright (C) 2004, 2005  Tomasz Nazar, nthx at irc dot pl
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Full version of the license is /docs/LICENSE.txt
 */
package org.nthx.pat.demo;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;



/** Represents root of object tree of demo application.
 *  <p>Your "root" class will look similar.
 *
 *  @@pat.root
 *
 *  @version $Id: Forum.java 3725 2005-06-09 23:57:03Z nthx $
 *  @author nthx@users.sourceforge.net
 */
public class Forum
{

    private Users users;
    private Topics topics;


    //--- Constructors ----------
    public Forum()
    {
        this.users = new Users();
        this.topics = new Topics();
    }

    public Forum(Users users, Topics topics)
    {
        this.users = users;
        this.topics = topics;
    }
    

    public Forum(Users users)
    {
        this.users = users;
        this.topics = new Topics();
    }
    

    //--- Getters and Setters ---
    public Users getUsers() { return users; }
    public Topics getTopics() { return topics; }


    //--- Implementation --------

    public void changeForTestsDDD()
    {
        this.users = new Users();
    }

    /** @@pat.transaction */
    public Topic newTopic(String subject, User author, String contents)
    {
        Topic topic = new Topic(subject, author, contents);
        topics.newTopic(topic);

        return topic;
    }

    /** @@pat.transaction */
    public void newMessage(User author, Topic topic, String contents)
    {
        Date date = GregorianCalendar.getInstance().getTime();
        topic.newMessage(author, contents, date);
        author.newMessage(topic, contents, date);
    }

    /** @@pat.transaction */
    public User newUser(String name, String surname, String login, String www)
    {
        User newUser = new User(name, surname, login, www);
        users.addUser(newUser);

        return newUser;
    }

    public User getUserByLogin(String login)
    {
        for (Iterator usersIter = users.iterator(); usersIter.hasNext();)
        {
            User user = (User) usersIter.next();
            if (login.equalsIgnoreCase(user.getLogin()))
                return user;
        }
        return null;
    }

    public Topic getTopic(String subject)
    {
        for (Iterator topicsIter = topics.iterator(); topicsIter.hasNext();)
        {
            Topic topic = (Topic) topicsIter.next();
            if (subject.equalsIgnoreCase(topic.getSubject()))
                return topic;
        }
        return null;
    }

    public int getAllMessagesNumber()
    {
        int number = 0;
        for (Iterator topicsIter = topics.iterator(); topicsIter.hasNext();)
        {
            Topic topic = (Topic) topicsIter.next();
            number += topic.getMessages().size();
        }
        return number;
    }

    protected final static long serialVersionUID = 1L;
}