2017-12-14 15:33:27 +01:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2011-2013 Nicolas Vinot <aeris@imirhil.fr>
|
|
|
|
* Copyright (C) 2017 Christian Pierre MOMON <cmomon@april.org>
|
|
|
|
*
|
|
|
|
* This file is part of (April) Hebdobot.
|
|
|
|
*
|
|
|
|
* Hebdobot is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Hebdobot is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with Hebdobot. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
2018-01-21 08:05:14 +01:00
|
|
|
package org.april.hebdobot.model.review;
|
2011-09-02 19:09:09 +02:00
|
|
|
|
2013-01-27 12:16:19 +01:00
|
|
|
import java.util.Collection;
|
2011-09-02 19:09:09 +02:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-12-15 02:42:52 +01:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2017-12-25 00:29:57 +01:00
|
|
|
import org.april.hebdobot.model.UserAliases;
|
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
|
|
|
|
2017-12-26 01:18:36 +01:00
|
|
|
import fr.devinsy.util.strings.StringSet;
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* The Class Review.
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
public class Review
|
|
|
|
{
|
|
|
|
private static final int LENGTH = 80;
|
|
|
|
|
2017-12-26 01:18:36 +01:00
|
|
|
private final StringSet participants;
|
2017-12-14 16:58:45 +01:00
|
|
|
private final List<IndividualTopic> individualTopics;
|
|
|
|
private final List<CollectiveTopic> collectiveTopics;
|
2017-12-14 15:37:29 +01:00
|
|
|
private Topic currentTopic;
|
2017-12-26 01:18:36 +01:00
|
|
|
private final MessageList messages;
|
2017-12-14 15:37:29 +01:00
|
|
|
private final String owner;
|
2017-12-25 00:29:57 +01:00
|
|
|
private final UserAliases aliases;
|
2017-12-14 15:37:29 +01:00
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Instantiates a new review.
|
|
|
|
*
|
|
|
|
* @param owner
|
|
|
|
* the owner
|
2018-01-04 23:28:30 +01:00
|
|
|
* @param aliases
|
|
|
|
* the aliases
|
2017-12-14 16:58:45 +01:00
|
|
|
*/
|
2017-12-25 00:29:57 +01:00
|
|
|
public Review(final String owner, final UserAliases aliases)
|
2017-12-14 15:37:29 +01:00
|
|
|
{
|
2017-12-26 01:18:36 +01:00
|
|
|
this.participants = new StringSet();
|
2017-12-14 16:58:45 +01:00
|
|
|
this.individualTopics = new LinkedList<IndividualTopic>();
|
|
|
|
this.collectiveTopics = new LinkedList<CollectiveTopic>();
|
2017-12-26 01:18:36 +01:00
|
|
|
this.messages = new MessageList();
|
2017-12-14 16:58:45 +01:00
|
|
|
|
2017-12-14 15:37:29 +01:00
|
|
|
this.owner = owner;
|
2017-12-25 00:29:57 +01:00
|
|
|
this.aliases = aliases;
|
2017-12-14 15:37:29 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Adds the.
|
|
|
|
*
|
|
|
|
* @param message
|
|
|
|
* the message
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
public void add(final Message message)
|
|
|
|
{
|
|
|
|
if (this.currentTopic != null)
|
|
|
|
{
|
|
|
|
this.participants.add(message.getAuthor());
|
|
|
|
this.currentTopic.add(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Adds the raw.
|
|
|
|
*
|
|
|
|
* @param message
|
|
|
|
* the message
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
public void addRaw(final Message message)
|
|
|
|
{
|
|
|
|
this.messages.add(message);
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Begin.
|
|
|
|
*
|
|
|
|
* @param topic
|
|
|
|
* the topic
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
public void begin(final CollectiveTopic topic)
|
|
|
|
{
|
|
|
|
this.collectiveTopics.add(topic);
|
|
|
|
this.currentTopic = topic;
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Begin.
|
|
|
|
*
|
|
|
|
* @param topic
|
|
|
|
* the topic
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
public void begin(final IndividualTopic topic)
|
|
|
|
{
|
|
|
|
this.individualTopics.add(topic);
|
|
|
|
this.currentTopic = topic;
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Gets the current topic.
|
|
|
|
*
|
|
|
|
* @return the current topic
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
public Topic getCurrentTopic()
|
|
|
|
{
|
|
|
|
return this.currentTopic;
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Gets the owner.
|
|
|
|
*
|
|
|
|
* @return the owner
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
public String getOwner()
|
|
|
|
{
|
|
|
|
return this.owner;
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Gets the participants.
|
|
|
|
*
|
|
|
|
* @return the participants
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
public Collection<String> getParticipants()
|
|
|
|
{
|
2017-12-14 16:58:45 +01:00
|
|
|
Collection<String> result;
|
|
|
|
|
|
|
|
result = new HashSet<String>();
|
|
|
|
|
2017-12-14 15:37:29 +01:00
|
|
|
for (final Topic topic : this.individualTopics)
|
|
|
|
{
|
2017-12-14 16:58:45 +01:00
|
|
|
result.addAll(topic.getParticipants());
|
2017-12-14 15:37:29 +01:00
|
|
|
}
|
2017-12-14 16:58:45 +01:00
|
|
|
|
2017-12-14 15:37:29 +01:00
|
|
|
for (final Topic topic : this.collectiveTopics)
|
|
|
|
{
|
2017-12-14 16:58:45 +01:00
|
|
|
result.addAll(topic.getParticipants());
|
2017-12-14 15:37:29 +01:00
|
|
|
}
|
2017-12-14 16:58:45 +01:00
|
|
|
|
2017-12-25 00:29:57 +01:00
|
|
|
//
|
2017-12-14 16:58:45 +01:00
|
|
|
return result;
|
2017-12-14 15:37:29 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 01:56:43 +01:00
|
|
|
/**
|
|
|
|
* Checks if is empty.
|
|
|
|
*
|
|
|
|
* @return true, if is empty
|
|
|
|
*/
|
|
|
|
public boolean isEmpty()
|
|
|
|
{
|
|
|
|
boolean result;
|
|
|
|
|
|
|
|
if ((this.individualTopics.isEmpty()) && (this.collectiveTopics.isEmpty()))
|
|
|
|
{
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Checks if is owner.
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* the name
|
|
|
|
* @return true, if is owner
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
public boolean isOwner(final String name)
|
|
|
|
{
|
2017-12-25 00:29:57 +01:00
|
|
|
boolean result;
|
|
|
|
|
|
|
|
result = this.owner.equalsIgnoreCase(name);
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
2017-12-14 15:37:29 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see java.lang.Object#toString()
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
@Override
|
|
|
|
public String toString()
|
|
|
|
{
|
2017-12-14 16:58:45 +01:00
|
|
|
String result;
|
|
|
|
|
2017-12-26 02:16:40 +01:00
|
|
|
StringBuffer buffer = new StringBuffer();
|
2017-12-14 15:37:29 +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", '-');
|
|
|
|
for (final String participant : this.participants)
|
|
|
|
{
|
2017-12-25 00:29:57 +01:00
|
|
|
addChunk(buffer, "* " + this.aliases.getRealName(participant) + "\n");
|
2017-12-14 15:37:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.individualTopics.isEmpty())
|
|
|
|
{
|
|
|
|
for (final String participant : this.participants)
|
|
|
|
{
|
|
|
|
addEmpty(buffer);
|
|
|
|
addLine(buffer, '=');
|
|
|
|
addEmpty(buffer);
|
2017-12-25 00:29:57 +01:00
|
|
|
addCenter(buffer, this.aliases.getRealName(participant), '-');
|
2017-12-14 15:37:29 +01:00
|
|
|
for (final IndividualTopic topic : this.individualTopics)
|
|
|
|
{
|
|
|
|
if (topic.hasParticipant(participant))
|
|
|
|
{
|
|
|
|
addEmpty(buffer);
|
|
|
|
add(buffer, getTopic(topic));
|
|
|
|
addEmpty(buffer);
|
|
|
|
for (final Message message : topic.getMessages(participant))
|
|
|
|
{
|
|
|
|
addChunk(buffer, "* " + message.getContent() + "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.collectiveTopics.isEmpty())
|
|
|
|
{
|
|
|
|
for (final CollectiveTopic topic : this.collectiveTopics)
|
|
|
|
{
|
|
|
|
addEmpty(buffer);
|
|
|
|
addLine(buffer, '=');
|
|
|
|
addCenter(buffer, topic.getTitle());
|
|
|
|
addLine(buffer, '=');
|
|
|
|
addEmpty(buffer);
|
|
|
|
for (final Message message : topic.getMessages())
|
|
|
|
{
|
|
|
|
addChunk(buffer, "* " + message.getAuthor() + " : " + message.getContent() + "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addEmpty(buffer);
|
|
|
|
addCenter(buffer, "Log IRC brut");
|
|
|
|
addEmpty(buffer);
|
|
|
|
for (final Message message : this.messages)
|
|
|
|
{
|
|
|
|
addChunk(buffer, "* " + message.getAuthor() + " : " + message.getContent() + "\n");
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
result = buffer.toString();
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
2017-12-14 15:37:29 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Adds the.
|
|
|
|
*
|
|
|
|
* @param buffer
|
|
|
|
* the buffer
|
|
|
|
* @param content
|
|
|
|
* the content
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
private static void add(final StringBuffer buffer, final String content)
|
|
|
|
{
|
|
|
|
buffer.append(content);
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Adds the center.
|
|
|
|
*
|
|
|
|
* @param buffer
|
|
|
|
* the buffer
|
|
|
|
* @param content
|
|
|
|
* the content
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
private static void addCenter(final StringBuffer buffer, final String content)
|
|
|
|
{
|
|
|
|
addCenter(buffer, content, ' ');
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Adds the center.
|
|
|
|
*
|
|
|
|
* @param buffer
|
|
|
|
* the buffer
|
|
|
|
* @param content
|
|
|
|
* the content
|
|
|
|
* @param c
|
|
|
|
* the c
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
private static void addCenter(final StringBuffer buffer, final String content, final char c)
|
|
|
|
{
|
|
|
|
buffer.append(getLine(content, c));
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Adds the chunk.
|
|
|
|
*
|
|
|
|
* @param buffer
|
|
|
|
* the buffer
|
|
|
|
* @param content
|
|
|
|
* the content
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
private static void addChunk(final StringBuffer buffer, final String content)
|
|
|
|
{
|
|
|
|
add(buffer, chunk(content));
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Adds the empty.
|
|
|
|
*
|
|
|
|
* @param buffer
|
|
|
|
* the buffer
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
private static void addEmpty(final StringBuffer buffer)
|
|
|
|
{
|
|
|
|
buffer.append("\n");
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Adds the line.
|
|
|
|
*
|
|
|
|
* @param buffer
|
|
|
|
* the buffer
|
|
|
|
* @param c
|
|
|
|
* the c
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
private static void addLine(final StringBuffer buffer, final char c)
|
|
|
|
{
|
|
|
|
buffer.append(getLine(c));
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Chunk.
|
|
|
|
*
|
|
|
|
* @param content
|
|
|
|
* the content
|
|
|
|
* @return the string
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
private static String chunk(final String content)
|
|
|
|
{
|
|
|
|
return chunk(content, LENGTH);
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Chunk.
|
|
|
|
*
|
|
|
|
* @param content
|
|
|
|
* the content
|
|
|
|
* @param length
|
|
|
|
* the length
|
|
|
|
* @return the string
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
private static String chunk(final String content, final int length)
|
|
|
|
{
|
2017-12-14 16:58:45 +01:00
|
|
|
String result;
|
|
|
|
|
2017-12-14 15:37:29 +01:00
|
|
|
final String[] words = content.split(" ");
|
2017-12-14 16:58:45 +01:00
|
|
|
final StringBuffer buffer = new StringBuffer();
|
2017-12-14 15:37:29 +01:00
|
|
|
StringBuffer current = new StringBuffer();
|
|
|
|
|
|
|
|
for (final String word : words)
|
|
|
|
{
|
|
|
|
if (current.length() + word.length() > length)
|
|
|
|
{
|
2017-12-14 16:58:45 +01:00
|
|
|
if (buffer.length() > 0)
|
2017-12-14 15:37:29 +01:00
|
|
|
{
|
2017-12-14 16:58:45 +01:00
|
|
|
buffer.append("\n");
|
2017-12-14 15:37:29 +01:00
|
|
|
}
|
2017-12-14 16:58:45 +01:00
|
|
|
buffer.append(current);
|
2017-12-14 15:37:29 +01:00
|
|
|
current = new StringBuffer(word);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (current.length() > 0)
|
|
|
|
{
|
|
|
|
current.append(" ");
|
|
|
|
}
|
|
|
|
current.append(word);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current.length() > 0)
|
|
|
|
{
|
2017-12-14 16:58:45 +01:00
|
|
|
if (buffer.length() > 0)
|
2017-12-14 15:37:29 +01:00
|
|
|
{
|
2017-12-14 16:58:45 +01:00
|
|
|
buffer.append("\n");
|
2017-12-14 15:37:29 +01:00
|
|
|
}
|
2017-12-14 16:58:45 +01:00
|
|
|
buffer.append(current);
|
2017-12-14 15:37:29 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
result = buffer.toString();
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
2017-12-14 15:37:29 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Gets the line.
|
|
|
|
*
|
|
|
|
* @param letter
|
|
|
|
* the letter
|
|
|
|
* @return the line
|
|
|
|
*/
|
|
|
|
private static String getLine(final Character letter)
|
2017-12-14 15:37:29 +01:00
|
|
|
{
|
2017-12-14 16:58:45 +01:00
|
|
|
String result;
|
|
|
|
|
|
|
|
result = StringUtils.repeat(letter.toString(), LENGTH) + "\n";
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
2017-12-14 15:37:29 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Gets the line.
|
|
|
|
*
|
|
|
|
* @param content
|
|
|
|
* the content
|
|
|
|
* @param letter
|
|
|
|
* the c
|
|
|
|
* @return the line
|
|
|
|
*/
|
|
|
|
private static String getLine(final String content, final char letter)
|
2017-12-14 15:37:29 +01:00
|
|
|
{
|
2017-12-14 16:58:45 +01:00
|
|
|
String result;
|
|
|
|
|
|
|
|
result = StringUtils.center(" " + content + " ", LENGTH, letter) + "\n";
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
2017-12-14 15:37:29 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 16:58:45 +01:00
|
|
|
/**
|
|
|
|
* Gets the topic.
|
|
|
|
*
|
|
|
|
* @param topic
|
|
|
|
* the topic
|
|
|
|
* @return the topic
|
|
|
|
*/
|
2017-12-14 15:37:29 +01:00
|
|
|
private static String getTopic(final Topic topic)
|
|
|
|
{
|
2017-12-14 16:58:45 +01:00
|
|
|
String result;
|
|
|
|
|
|
|
|
result = "=== " + topic.getTitle() + " ===\n";
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
2017-12-14 15:37:29 +01:00
|
|
|
}
|
2011-09-02 19:09:09 +02:00
|
|
|
}
|