package org.nthx.pat.demo;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
public class Forum
{
private Users users;
private Topics topics;
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();
}
public Users getUsers() { return users; }
public Topics getTopics() { return topics; }
public void changeForTestsDDD()
{
this.users = new Users();
}
public Topic newTopic(String subject, User author, String contents)
{
Topic topic = new Topic(subject, author, contents);
topics.newTopic(topic);
return topic;
}
public void newMessage(User author, Topic topic, String contents)
{
Date date = GregorianCalendar.getInstance().getTime();
topic.newMessage(author, contents, date);
author.newMessage(topic, contents, date);
}
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;
}