package fr.imirhil.april.hebdobot.review; 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.UserAlias; public class Review { private static final int LENGTH = 80; private final Set participants = new HashSet(); private final List individualTopics = new LinkedList(); private final List collectiveTopics = new LinkedList(); private Topic currentTopic; private final List messages = new LinkedList(); private final String owner; 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; } public void add(final Message message) { if (this.currentTopic != null) { this.participants.add(message.getAuthor()); this.currentTopic.add(message); } } public void addRaw(final Message message) { this.messages.add(message); } public String toString(final UserAlias userService) { 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) { chunkAndAppend(buffer, "* " + userService.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(userService.getRealName(participant), '-')); for (final IndividualTopic topic : this.individualTopics) { if (topic.hasParticipant(participant)) { buffer.append("\n"); chunkAndAppend(buffer, getTopic(topic)); buffer.append("\n"); for (final Message message : topic .getMessages(participant)) { chunkAndAppend(buffer, "* " + 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()) { chunkAndAppend(buffer, "* " + message.getAuthor() + " : " + message.getContent() + "\n"); } } buffer.append("\n"); } buffer.append("\n"); addBox(buffer, "Log complet", '='); buffer.append("\n"); for (final Message message : this.messages) { chunkAndAppend(buffer, "* " + 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)); } 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(); } private static String chunk(final String content) { return chunk(content, LENGTH); } private static void chunkAndAppend(final StringBuffer buffer, final String content) { buffer.append(chunk(content)); } }