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-11-05 15:19:12 +01:00
|
|
|
import org.joda.time.DateTime;
|
|
|
|
import org.joda.time.format.DateTimeFormat;
|
2011-09-02 19:09:09 +02:00
|
|
|
|
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-11-05 14:47:49 +01:00
|
|
|
private static final int LENGTH = 80;
|
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-11-05 15:19:12 +01:00
|
|
|
addLine(buffer, '=');
|
|
|
|
addCenter(buffer, "Revue de la semaine en cours");
|
|
|
|
addEmpty(buffer);
|
|
|
|
addCenter(buffer, DateTimeFormat.fullDate().print(new DateTime()));
|
|
|
|
addLine(buffer, '=');
|
|
|
|
addEmpty(buffer);
|
|
|
|
addEmpty(buffer);
|
|
|
|
|
|
|
|
addLine(buffer, '=');
|
|
|
|
addEmpty(buffer);
|
|
|
|
addCenter(buffer, "Participants", '-');
|
2011-09-02 19:09:09 +02:00
|
|
|
for (final String participant : this.participants) {
|
2011-11-05 15:19:12 +01:00
|
|
|
addChunk(buffer, "* " + userService.getRealName(participant) + "\n");
|
2011-09-02 19:09:09 +02:00
|
|
|
}
|
|
|
|
|
2011-09-03 16:29:09 +02:00
|
|
|
if (!this.individualTopics.isEmpty()) {
|
|
|
|
for (final String participant : this.participants) {
|
2011-11-05 15:19:12 +01:00
|
|
|
addEmpty(buffer);
|
|
|
|
addLine(buffer, '=');
|
|
|
|
addEmpty(buffer);
|
|
|
|
addCenter(buffer, userService.getRealName(participant), '-');
|
2011-09-03 16:29:09 +02:00
|
|
|
for (final IndividualTopic topic : this.individualTopics) {
|
2011-09-05 10:41:12 +02:00
|
|
|
if (topic.hasParticipant(participant)) {
|
2011-11-05 15:19:12 +01:00
|
|
|
addEmpty(buffer);
|
|
|
|
add(buffer, getTopic(topic));
|
|
|
|
addEmpty(buffer);
|
2011-09-05 10:41:12 +02:00
|
|
|
for (final Message message : topic
|
|
|
|
.getMessages(participant)) {
|
2011-11-05 15:19:12 +01:00
|
|
|
addChunk(buffer, "* " + message.getContent() + "\n");
|
2011-09-05 10:41:12 +02:00
|
|
|
}
|
2011-09-03 16:29:09 +02:00
|
|
|
}
|
2011-09-02 19:09:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-03 16:29:09 +02:00
|
|
|
if (!this.collectiveTopics.isEmpty()) {
|
|
|
|
for (final CollectiveTopic topic : this.collectiveTopics) {
|
2011-11-05 15:19:12 +01:00
|
|
|
addEmpty(buffer);
|
|
|
|
addLine(buffer, '=');
|
|
|
|
addCenter(buffer, topic.getTitle());
|
|
|
|
addLine(buffer, '=');
|
|
|
|
addEmpty(buffer);
|
2011-09-03 16:29:09 +02:00
|
|
|
for (final Message message : topic.getMessages()) {
|
2011-11-05 15:19:12 +01:00
|
|
|
addChunk(buffer, "* " + message.getAuthor() + " : "
|
2011-09-03 16:29:09 +02:00
|
|
|
+ message.getContent() + "\n");
|
|
|
|
}
|
2011-09-02 19:09:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-05 15:19:12 +01:00
|
|
|
addEmpty(buffer);
|
|
|
|
addCenter(buffer, "Log IRC brut");
|
|
|
|
addEmpty(buffer);
|
2011-09-02 19:09:09 +02:00
|
|
|
for (final Message message : this.messages) {
|
2011-11-05 15:19:12 +01:00
|
|
|
addChunk(buffer,
|
2011-11-05 14:47:49 +01:00
|
|
|
"* " + message.getAuthor() + " : " + message.getContent()
|
|
|
|
+ "\n");
|
2011-09-02 19:09:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-11-05 15:19:12 +01:00
|
|
|
private static String getLine(final String content, final char 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-11-05 15:19:12 +01:00
|
|
|
return "=== " + topic.getTitle() + " ===\n";
|
2011-09-03 16:29:09 +02:00
|
|
|
}
|
|
|
|
|
2011-11-05 15:19:12 +01:00
|
|
|
private static void addCenter(final StringBuffer buffer,
|
|
|
|
final String content, final char c) {
|
|
|
|
buffer.append(getLine(content, c));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void addCenter(final StringBuffer buffer,
|
|
|
|
final String content) {
|
|
|
|
addCenter(buffer, content, ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void addLine(final StringBuffer buffer, final char c) {
|
2011-09-03 16:29:09 +02:00
|
|
|
buffer.append(getLine(c));
|
2011-09-02 19:09:09 +02:00
|
|
|
}
|
2011-11-05 14:47:49 +01:00
|
|
|
|
2011-11-05 15:19:12 +01:00
|
|
|
private static void addEmpty(final StringBuffer buffer) {
|
|
|
|
buffer.append("\n");
|
|
|
|
}
|
|
|
|
|
2011-11-05 14:47:49 +01:00
|
|
|
private static String chunk(final String content, final int length) {
|
|
|
|
final String[] words = content.split(" ");
|
|
|
|
final StringBuffer result = new StringBuffer();
|
|
|
|
StringBuffer current = new StringBuffer();
|
|
|
|
|
|
|
|
for (final String word : words) {
|
|
|
|
if (current.length() + word.length() > length) {
|
|
|
|
if (result.length() > 0) {
|
|
|
|
result.append("\n");
|
|
|
|
}
|
|
|
|
result.append(current);
|
|
|
|
current = new StringBuffer(word);
|
|
|
|
} else {
|
|
|
|
if (current.length() > 0) {
|
|
|
|
current.append(" ");
|
|
|
|
}
|
|
|
|
current.append(word);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current.length() > 0) {
|
|
|
|
if (result.length() > 0) {
|
|
|
|
result.append("\n");
|
|
|
|
}
|
|
|
|
result.append(current);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.toString();
|
|
|
|
}
|
|
|
|
|
2011-11-05 15:19:12 +01:00
|
|
|
private static void add(final StringBuffer buffer, final String content) {
|
|
|
|
buffer.append(content);
|
|
|
|
}
|
|
|
|
|
2011-11-05 14:47:49 +01:00
|
|
|
private static String chunk(final String content) {
|
|
|
|
return chunk(content, LENGTH);
|
|
|
|
}
|
|
|
|
|
2011-11-05 15:19:12 +01:00
|
|
|
private static void
|
|
|
|
addChunk(final StringBuffer buffer, final String content) {
|
|
|
|
add(buffer, chunk(content));
|
2011-11-05 14:47:49 +01:00
|
|
|
}
|
2011-11-05 15:19:12 +01:00
|
|
|
|
2011-09-02 19:09:09 +02:00
|
|
|
}
|