hebdobot/src/org/april/hebdobot/model/review/Review.java

594 lines
13 KiB
Java

/**
* 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/>
*/
package org.april.hebdobot.model.review;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import org.apache.commons.lang3.StringUtils;
import org.april.hebdobot.model.UserAliases;
import fr.devinsy.strings.StringSet;
/**
* The Class Review.
*/
public class Review
{
private static final int LENGTH = 80;
private final StringSet participants;
private final List<IndividualTopic> individualTopics;
private final List<CollectiveTopic> collectiveTopics;
private Topic currentTopic;
private final MessageList messages;
private final String owner;
private final UserAliases aliases;
private LocalDateTime startTime;
private LocalDateTime endTime;
/**
* Instantiates a new review.
*
* @param owner
* the owner
* @param aliases
* the aliases
*/
public Review(final String owner, final UserAliases aliases)
{
this.participants = new StringSet();
this.individualTopics = new LinkedList<>();
this.collectiveTopics = new LinkedList<>();
this.messages = new MessageList();
this.owner = owner;
this.aliases = aliases;
this.startTime = LocalDateTime.now();
this.endTime = null;
}
/**
* Adds the.
*
* @param message
* the message
*/
public void add(final Message message)
{
if (this.currentTopic != null)
{
this.participants.add(message.getAuthor());
this.currentTopic.add(message);
}
}
/**
* Adds the raw.
*
* @param message
* the message
*/
public void addRaw(final Message message)
{
this.messages.add(message);
}
/**
* Begin.
*
* @param topic
* the topic
*/
public void begin(final CollectiveTopic topic)
{
this.collectiveTopics.add(topic);
this.currentTopic = topic;
}
/**
* Begin.
*
* @param topic
* the topic
*/
public void begin(final IndividualTopic topic)
{
this.individualTopics.add(topic);
this.currentTopic = topic;
}
/**
* Gets the current topic.
*
* @return the current topic
*/
public Topic getCurrentTopic()
{
return this.currentTopic;
}
public long getDuration()
{
long result;
if (this.endTime == null)
{
result = Duration.between(this.startTime, LocalDateTime.now()).getSeconds();
}
else
{
result = Duration.between(this.startTime, this.endTime).getSeconds();
}
//
return result;
}
/**
* Gets the duration.
*
* @return the duration
*/
public long getDurationInMinutes()
{
long result;
result = (long) Math.ceil(getDuration() / 60.0);
//
return result;
}
/**
* Gets the end time.
*
* @return the end time
*/
public LocalDateTime getEndTime()
{
return this.endTime;
}
/**
* Gets the formatted end time.
*
* @return the formatted end time
*/
public String getFormattedEndTime()
{
String result;
if (this.endTime == null)
{
result = "??h??";
}
else
{
result = this.endTime.format(DateTimeFormatter.ofPattern("kk'h'mm"));
}
//
return result;
}
/**
* Gets the formatted start time.
*
* @return the formatted start time
*/
public String getFormattedStartTime()
{
String result;
result = this.startTime.format(DateTimeFormatter.ofPattern("kk'h'mm"));
//
return result;
}
/**
* Gets the owner.
*
* @return the owner
*/
public String getOwner()
{
return this.owner;
}
/**
* Gets the participants.
*
* @return the participants
*/
public Collection<String> getParticipants()
{
Collection<String> result;
result = new HashSet<>();
for (final Topic topic : this.individualTopics)
{
result.addAll(topic.getParticipants());
}
for (final Topic topic : this.collectiveTopics)
{
result.addAll(topic.getParticipants());
}
//
return result;
}
/**
* Gets the start time.
*
* @return the start time
*/
public LocalDateTime getStartTime()
{
return this.startTime;
}
/**
* 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;
}
/**
* Checks if is owner.
*
* @param name
* the name
* @return true, if is owner
*/
public boolean isOwner(final String name)
{
boolean result;
result = this.owner.equalsIgnoreCase(name);
//
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
String result;
//
StringBuffer buffer = new StringBuffer();
addLine(buffer, '=');
addCenter(buffer, "Revue de la semaine en cours");
addEmpty(buffer);
addCenter(buffer, StringUtils.capitalize(LocalDateTime.now().format(DateTimeFormatter.ofPattern("EEEE dd MMMM yyyy", Locale.FRENCH))));
addLine(buffer, '=');
addEmpty(buffer);
addEmpty(buffer);
//
addLine(buffer, '=');
addEmpty(buffer);
addCenter(buffer, "Participants", '-');
for (final String participant : this.participants)
{
addChunk(buffer, "* " + this.aliases.getRealName(participant) + "\n");
}
//
if (!this.individualTopics.isEmpty())
{
for (final String participant : this.participants)
{
addEmpty(buffer);
addLine(buffer, '=');
addEmpty(buffer);
addCenter(buffer, this.aliases.getRealName(participant), '-');
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");
}
//
this.endTime = LocalDateTime.now();
addEmpty(buffer);
addCenter(buffer, "Statistiques");
addEmpty(buffer);
addChunk(buffer, "Horaire de début de la revue : " + getFormattedStartTime() + "\n");
addChunk(buffer, "Horaire de fin de la revue : " + getFormattedEndTime() + "\n");
addChunk(buffer, "Durée de la revue : " + getDurationInMinutes() + " minutes\n");
addChunk(buffer, "Nombre de participants : " + this.participants.size() + "\n");
//
result = buffer.toString();
//
return result;
}
/**
* Adds the.
*
* @param buffer
* the buffer
* @param content
* the content
*/
private static void add(final StringBuffer buffer, final String content)
{
buffer.append(content);
}
/**
* Adds the center.
*
* @param buffer
* the buffer
* @param content
* the content
*/
private static void addCenter(final StringBuffer buffer, final String content)
{
addCenter(buffer, content, ' ');
}
/**
* Adds the center.
*
* @param buffer
* the buffer
* @param content
* the content
* @param c
* the c
*/
private static void addCenter(final StringBuffer buffer, final String content, final char c)
{
buffer.append(getLine(content, c));
}
/**
* Adds the chunk.
*
* @param buffer
* the buffer
* @param content
* the content
*/
private static void addChunk(final StringBuffer buffer, final String content)
{
add(buffer, chunk(content));
}
/**
* Adds the empty.
*
* @param buffer
* the buffer
*/
private static void addEmpty(final StringBuffer buffer)
{
buffer.append("\n");
}
/**
* Adds the line.
*
* @param buffer
* the buffer
* @param c
* the c
*/
private static void addLine(final StringBuffer buffer, final char c)
{
buffer.append(getLine(c));
}
/**
* Chunk.
*
* @param content
* the content
* @return the string
*/
private static String chunk(final String content)
{
String result;
result = chunk(content, LENGTH);
//
return result;
}
/**
* Chunk.
*
* @param content
* the content
* @param length
* the length
* @return the string
*/
private static String chunk(final String content, final int length)
{
String result;
final String[] words = content.split(" ");
final StringBuffer buffer = new StringBuffer();
StringBuffer current = new StringBuffer();
for (final String word : words)
{
if (current.length() + word.length() > length)
{
if (buffer.length() > 0)
{
buffer.append("\n");
}
buffer.append(current);
current = new StringBuffer(word);
}
else
{
if (current.length() > 0)
{
current.append(" ");
}
current.append(word);
}
}
if (current.length() > 0)
{
if (buffer.length() > 0)
{
buffer.append("\n");
}
buffer.append(current);
}
result = buffer.toString();
//
return result;
}
/**
* Gets the line.
*
* @param letter
* the letter
* @return the line
*/
private static String getLine(final Character letter)
{
String result;
result = StringUtils.repeat(letter.toString(), LENGTH) + "\n";
//
return result;
}
/**
* Gets the line.
*
* @param content
* the content
* @param letter
* the c
* @return the line
*/
private static String getLine(final String content, final char letter)
{
String result;
result = StringUtils.center(" " + content + " ", LENGTH, letter) + "\n";
//
return result;
}
/**
* Gets the topic.
*
* @param topic
* the topic
* @return the topic
*/
private static String getTopic(final Topic topic)
{
String result;
result = "=== " + topic.getTitle() + " ===\n";
//
return result;
}
}