agirbot/src/main/java/fr/imirhil/april/hebdobot/review/Review.java

139 lines
3.7 KiB
Java
Raw Normal View History

2011-09-30 00:35:49 +02:00
package fr.imirhil.april.hebdobot.review;
2011-09-02 19:09:09 +02:00
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
2011-09-30 00:35:49 +02:00
import fr.imirhil.april.hebdobot.xml.UserAlias;
2011-09-03 16:29:09 +02:00
2011-09-30 00:35:49 +02:00
public class Review {
2011-09-03 16:29:09 +02:00
private static final int LENGTH = 74;
2011-09-02 19:09:09 +02:00
private final Set<String> participants = new HashSet<String>();
private final List<IndividualTopic> individualTopics =
new LinkedList<IndividualTopic>();
private final List<CollectiveTopic> collectiveTopics =
new LinkedList<CollectiveTopic>();
2011-09-30 00:35:49 +02:00
private Topic currentTopic;
2011-09-02 19:09:09 +02:00
private final List<Message> messages = new LinkedList<Message>();
2011-09-30 00:35:49 +02:00
private final String owner;
2011-09-02 19:09:09 +02:00
2011-09-30 00:35:49 +02:00
public String getOwner() {
return this.owner;
}
public boolean isOwner(final String name) {
return this.owner.equalsIgnoreCase(name);
}
public Review(final String owner) {
this.owner = owner;
}
public Topic getCurrentTopic() {
return this.currentTopic;
}
public void begin(final IndividualTopic topic) {
this.individualTopics.add(topic);
this.currentTopic = topic;
}
public void begin(final CollectiveTopic topic) {
this.collectiveTopics.add(topic);
this.currentTopic = topic;
2011-09-02 19:09:09 +02:00
}
public void add(final Message message) {
2011-09-30 00:35:49 +02:00
if (this.currentTopic != null) {
this.participants.add(message.getAuthor());
this.currentTopic.add(message);
}
}
public void addRaw(final Message message) {
2011-09-02 19:09:09 +02:00
this.messages.add(message);
}
2011-09-30 00:35:49 +02:00
public String toString(final UserAlias userService) {
2011-09-02 19:09:09 +02:00
final StringBuffer buffer = new StringBuffer();
2011-09-03 16:29:09 +02:00
addBox(buffer, "Revue de la semaine en cours", '#');
2011-09-02 19:09:09 +02:00
buffer.append("\n");
2011-09-03 16:29:09 +02:00
buffer.append("\n");
addBox(buffer, "Participants", '=');
buffer.append("\n");
2011-09-02 19:09:09 +02:00
for (final String participant : this.participants) {
2011-09-30 00:35:49 +02:00
buffer.append("* " + userService.getRealName(participant) + "\n");
2011-09-02 19:09:09 +02:00
}
buffer.append("\n");
2011-09-03 16:29:09 +02:00
if (!this.individualTopics.isEmpty()) {
2011-09-02 19:09:09 +02:00
buffer.append("\n");
2011-09-03 16:29:09 +02:00
addBox(buffer, "Sujets individuels", '=');
for (final String participant : this.participants) {
2011-09-02 19:09:09 +02:00
buffer.append("\n");
2011-09-30 00:35:49 +02:00
buffer.append(getLine(userService.getRealName(participant), '-'));
2011-09-03 16:29:09 +02:00
for (final IndividualTopic topic : this.individualTopics) {
if (topic.hasParticipant(participant)) {
buffer.append("\n");
buffer.append(getTopic(topic));
buffer.append("\n");
for (final Message message : topic
.getMessages(participant)) {
buffer.append("* " + message.getContent() + "\n");
}
2011-09-03 16:29:09 +02:00
}
2011-09-02 19:09:09 +02:00
}
}
buffer.append("\n");
}
2011-09-03 16:29:09 +02:00
if (!this.collectiveTopics.isEmpty()) {
2011-09-02 19:09:09 +02:00
buffer.append("\n");
2011-09-03 16:29:09 +02:00
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");
}
2011-09-02 19:09:09 +02:00
}
2011-09-03 16:29:09 +02:00
buffer.append("\n");
2011-09-02 19:09:09 +02:00
}
buffer.append("\n");
2011-09-03 16:29:09 +02:00
addBox(buffer, "Log complet", '=');
buffer.append("\n");
2011-09-02 19:09:09 +02:00
for (final Message message : this.messages) {
buffer.append("* " + message.getAuthor() + " : "
+ message.getContent() + "\n");
}
return buffer.toString();
}
2011-09-03 16:29:09 +02:00
private static String getLine(final Character c) {
return StringUtils.repeat(c.toString(), LENGTH) + "\n";
}
2011-09-02 19:09:09 +02:00
private static String getLine(final String content, final Character c) {
2011-09-03 16:29:09 +02:00
return StringUtils.center(" " + content + " ", LENGTH, c) + "\n";
2011-09-02 19:09:09 +02:00
}
private static String getTopic(final Topic topic) {
2011-09-03 16:29:09 +02:00
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));
2011-09-02 19:09:09 +02:00
}
}