hebdobot/src/main/java/fr/imirhil/april/hebdobot/Meeting.java
2011-09-03 16:29:09 +02:00

114 lines
3.3 KiB
Java

package fr.imirhil.april.hebdobot;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import fr.imirhil.april.hebdobot.xml.UserService;
public class Meeting {
private static final UserService USER_SERVICE = new UserService();
private static final int LENGTH = 74;
private final Set<String> participants = new HashSet<String>();
private final List<IndividualTopic> individualTopics =
new LinkedList<IndividualTopic>();
private final List<CollectiveTopic> collectiveTopics =
new LinkedList<CollectiveTopic>();
private final List<Message> messages = new LinkedList<Message>();
public void add(final Topic topic) {
if (topic instanceof IndividualTopic) {
this.individualTopics.add((IndividualTopic) topic);
} else if (topic instanceof CollectiveTopic) {
this.collectiveTopics.add((CollectiveTopic) topic);
}
}
public void add(final Message message) {
this.messages.add(message);
this.participants.add(message.getAuthor());
}
@Override
public String toString() {
final StringBuffer buffer = new StringBuffer();
addBox(buffer, "Revue de la semaine en cours", '#');
buffer.append("\n");
buffer.append("\n");
addBox(buffer, "Participants", '=');
buffer.append("\n");
for (final String participant : this.participants) {
buffer.append("* " + Meeting.USER_SERVICE.getRealName(participant)
+ "\n");
}
buffer.append("\n");
if (!this.individualTopics.isEmpty()) {
buffer.append("\n");
addBox(buffer, "Sujets individuels", '=');
for (final String participant : this.participants) {
buffer.append("\n");
buffer.append(getLine(
Meeting.USER_SERVICE.getRealName(participant), '-'));
for (final IndividualTopic topic : this.individualTopics) {
buffer.append("\n");
buffer.append(getTopic(topic));
buffer.append("\n");
for (final Message message : topic.getMessages(participant)) {
buffer.append("* " + message.getContent() + "\n");
}
}
}
buffer.append("\n");
}
if (!this.collectiveTopics.isEmpty()) {
buffer.append("\n");
addBox(buffer, "Sujets collectifs", '=');
for (final CollectiveTopic topic : this.collectiveTopics) {
buffer.append("\n");
addBox(buffer, topic.getTitle(), '-');
for (final Message message : topic.getMessages()) {
buffer.append("* " + message.getAuthor() + " : "
+ message.getContent() + "\n");
}
}
buffer.append("\n");
}
buffer.append("\n");
addBox(buffer, "Log complet", '=');
buffer.append("\n");
for (final Message message : this.messages) {
buffer.append("* " + message.getAuthor() + " : "
+ message.getContent() + "\n");
}
return buffer.toString();
}
private static String getLine(final Character c) {
return StringUtils.repeat(c.toString(), LENGTH) + "\n";
}
private static String getLine(final String content, final Character c) {
return StringUtils.center(" " + content + " ", LENGTH, c) + "\n";
}
private static String getTopic(final Topic topic) {
return "=== " + topic.getTitle() + " ===";
}
private static void addBox(final StringBuffer buffer, final String title,
final Character c) {
buffer.append(getLine(c));
buffer.append(getLine(title, ' '));
buffer.append(getLine(c));
}
}