Show new files in green and bold.
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / triggers / NewEpisodeTrigger.java
index a8e321c..075c6f6 100644 (file)
@@ -20,6 +20,8 @@ package net.pterodactylus.rhynodge.triggers;
 import static com.google.common.base.Preconditions.checkState;
 
 import java.util.Collection;
+import java.util.Map;
+import java.util.Map.Entry;
 
 import net.pterodactylus.rhynodge.Reaction;
 import net.pterodactylus.rhynodge.State;
@@ -32,8 +34,13 @@ import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
 
 import org.apache.commons.lang3.StringEscapeUtils;
 
-import com.google.common.base.Predicate;
-import com.google.common.collect.Collections2;
+import com.google.common.base.Function;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Ordering;
+import com.google.common.collect.Sets;
 
 /**
  * {@link Trigger} implementation that compares two {@link EpisodeState}s for
@@ -43,68 +50,67 @@ import com.google.common.collect.Collections2;
  */
 public class NewEpisodeTrigger implements Trigger {
 
+       /** All episodes. */
+       private final Collection<Episode> allEpisodes = Sets.newHashSet();
+
        /** All new episodes. */
-       private Collection<Episode> newEpisodes;
+       private final Collection<Episode> newEpisodes = Sets.newHashSet();
 
        /** All changed episodes. */
-       private Collection<Episode> changedEpisodes;
+       private final Collection<Episode> changedEpisodes = Sets.newHashSet();
+
+       /** All new torrent files. */
+       private final Collection<TorrentFile> newTorrentFiles = Sets.newHashSet();
 
        //
        // TRIGGER METHODS
        //
 
        /**
-        * {@inheritDoc}
+        * {@inheritDocs}
         */
        @Override
-       public boolean triggers(State currentState, State previousState) {
+       public State mergeStates(State previousState, State currentState) {
                checkState(currentState instanceof EpisodeState, "currentState is not a EpisodeState but a %s", currentState.getClass().getName());
                checkState(previousState instanceof EpisodeState, "previousState is not a EpisodeState but a %s", currentState.getClass().getName());
-               final EpisodeState currentEpisodeState = (EpisodeState) currentState;
-               final EpisodeState previousEpisodeState = (EpisodeState) previousState;
-
-               newEpisodes = Collections2.filter(currentEpisodeState.episodes(), new Predicate<Episode>() {
+               newEpisodes.clear();
+               changedEpisodes.clear();
+               this.allEpisodes.clear();
+               newTorrentFiles.clear();
+               Map<Episode, Episode> allEpisodes = Maps.newHashMap(FluentIterable.from(((EpisodeState) previousState).episodes()).toMap(new Function<Episode, Episode>() {
 
                        @Override
-                       public boolean apply(Episode episode) {
-                               return !previousEpisodeState.episodes().contains(episode);
+                       public Episode apply(Episode episode) {
+                               return episode;
                        }
-               });
-
-               changedEpisodes = Collections2.filter(currentEpisodeState.episodes(), new Predicate<Episode>() {
-
-                       @Override
-                       public boolean apply(Episode episode) {
-                               if (!previousEpisodeState.episodes().contains(episode)) {
-                                       return false;
-                               }
-
-                               /* find previous episode. */
-                               final Episode previousEpisode = findPreviousEpisode(episode);
-
-                               /* compare the list of torrent files. */
-                               Collection<TorrentFile> newTorrentFiles = Collections2.filter(episode.torrentFiles(), new Predicate<TorrentFile>() {
-
-                                       @Override
-                                       public boolean apply(TorrentFile torrentFile) {
-                                               return !previousEpisode.torrentFiles().contains(torrentFile);
-                                       }
-                               });
-
-                               return !newTorrentFiles.isEmpty();
+               }));
+               for (Episode episode : ((EpisodeState) currentState).episodes()) {
+                       if (!allEpisodes.containsKey(episode)) {
+                               allEpisodes.put(episode, episode);
+                               newEpisodes.add(episode);
                        }
-
-                       private Episode findPreviousEpisode(Episode episode) {
-                               for (Episode previousStateEpisode : previousEpisodeState) {
-                                       if (previousStateEpisode.equals(episode)) {
-                                               return previousStateEpisode;
-                                       }
+                       Episode existingEpisode = allEpisodes.get(episode);
+                       for (TorrentFile torrentFile : Lists.newArrayList(episode.torrentFiles())) {
+                               int oldSize = existingEpisode.torrentFiles().size();
+                               existingEpisode.addTorrentFile(torrentFile);
+                               int newSize = existingEpisode.torrentFiles().size();
+                               if (oldSize != newSize) {
+                                       newTorrentFiles.add(torrentFile);
+                               }
+                               if (!newEpisodes.contains(existingEpisode) && (oldSize != newSize)) {
+                                       changedEpisodes.add(existingEpisode);
                                }
-                               return null;
                        }
+               }
+               this.allEpisodes.addAll(allEpisodes.values());
+               return new EpisodeState(this.allEpisodes);
+       }
 
-               });
-
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public boolean triggers() {
                return !newEpisodes.isEmpty() || !changedEpisodes.isEmpty();
        }
 
@@ -124,8 +130,8 @@ public class NewEpisodeTrigger implements Trigger {
                        summary = String.format("%d changed Torrent(s) for “%s!”", changedEpisodes.size(), reaction.name());
                }
                DefaultOutput output = new DefaultOutput(summary);
-               output.addText("text/plain", generatePlainText(reaction, newEpisodes, changedEpisodes));
-               output.addText("text/html", generateHtmlText(reaction, newEpisodes, changedEpisodes));
+               output.addText("text/plain", generatePlainText(reaction));
+               output.addText("text/html", generateHtmlText(reaction));
                return output;
        }
 
@@ -138,13 +144,9 @@ public class NewEpisodeTrigger implements Trigger {
         *
         * @param reaction
         *            The reaction that was triggered
-        * @param newEpisodes
-        *            The new episodes
-        * @param changedEpisodes
-        *            The changed episodes
         * @return The plain text output
         */
-       private static String generatePlainText(Reaction reaction, Collection<Episode> newEpisodes, Collection<Episode> changedEpisodes) {
+       private String generatePlainText(Reaction reaction) {
                StringBuilder stringBuilder = new StringBuilder();
                if (!newEpisodes.isEmpty()) {
                        stringBuilder.append(reaction.name()).append(" - New Episodes\n\n");
@@ -176,6 +178,27 @@ public class NewEpisodeTrigger implements Trigger {
                                }
                        }
                }
+               /* list all known episodes. */
+               stringBuilder.append(reaction.name()).append(" - All Known Episodes\n\n");
+               ImmutableMap<Integer, Collection<Episode>> episodesBySeason = FluentIterable.from(allEpisodes).index(new Function<Episode, Integer>() {
+
+                       @Override
+                       public Integer apply(Episode episode) {
+                               return episode.season();
+                       }
+               }).asMap();
+               for (Entry<Integer, Collection<Episode>> seasonEntry : episodesBySeason.entrySet()) {
+                       stringBuilder.append("  Season ").append(seasonEntry.getKey()).append("\n\n");
+                       for (Episode episode : Ordering.natural().sortedCopy(seasonEntry.getValue())) {
+                               stringBuilder.append("    Episode ").append(episode.episode()).append("\n");
+                               for (TorrentFile torrentFile : episode) {
+                                       stringBuilder.append("      Size: ").append(torrentFile.size());
+                                       stringBuilder.append(" in ").append(torrentFile.fileCount()).append(" file(s): ");
+                                       stringBuilder.append(torrentFile.magnetUri());
+                               }
+                       }
+               }
+
                return stringBuilder.toString();
        }
 
@@ -184,68 +207,57 @@ public class NewEpisodeTrigger implements Trigger {
         *
         * @param reaction
         *            The reaction that was triggered
-        * @param newEpisodes
-        *            The new episodes
-        * @param changedEpisodes
-        *            The changed episodes
         * @return The HTML output
         */
-       private static String generateHtmlText(Reaction reaction, Collection<Episode> newEpisodes, Collection<Episode> changedEpisodes) {
+       private String generateHtmlText(Reaction reaction) {
                StringBuilder htmlBuilder = new StringBuilder();
                htmlBuilder.append("<html><body>\n");
-               htmlBuilder.append("<h1>").append(StringEscapeUtils.escapeHtml4(reaction.name())).append("</h1>\n");
-               if (!newEpisodes.isEmpty()) {
-                       htmlBuilder.append("<h2>New Episodes</h2>\n");
-                       htmlBuilder.append("<ul>\n");
-                       for (Episode episode : newEpisodes) {
-                               htmlBuilder.append("<li>Season ").append(episode.season()).append(", Episode ").append(episode.episode()).append("</li>\n");
-                               htmlBuilder.append("<ul>\n");
-                               for (TorrentFile torrentFile : episode) {
-                                       htmlBuilder.append("<li>").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("</li>\n");
-                                       htmlBuilder.append("<div>");
-                                       htmlBuilder.append("<strong>").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("</strong>, ");
-                                       htmlBuilder.append("<strong>").append(torrentFile.fileCount()).append("</strong> file(s), ");
-                                       htmlBuilder.append("<strong>").append(torrentFile.seedCount()).append("</strong> seed(s), ");
-                                       htmlBuilder.append("<strong>").append(torrentFile.leechCount()).append("</strong> leecher(s)</div>\n");
-                                       htmlBuilder.append("<div>");
-                                       if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) {
-                                               htmlBuilder.append("<a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.magnetUri())).append("\">Magnet</a> ");
-                                       }
-                                       if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) {
-                                               htmlBuilder.append("<a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.downloadUri())).append("\">Download</a>");
-                                       }
-                                       htmlBuilder.append("</div>\n");
-                               }
-                               htmlBuilder.append("</ul>\n");
-                       }
-                       htmlBuilder.append("</ul>\n");
-               }
-               if (!changedEpisodes.isEmpty()) {
-                       htmlBuilder.append("<h2>Changed Episodes</h2>\n");
-                       htmlBuilder.append("<ul>\n");
-                       for (Episode episode : changedEpisodes) {
-                               htmlBuilder.append("<li>Season ").append(episode.season()).append(", Episode ").append(episode.episode()).append("</li>\n");
-                               htmlBuilder.append("<ul>\n");
+               /* show all known episodes. */
+               htmlBuilder.append("<table>\n<caption>All Known Episodes</caption>\n");
+               htmlBuilder.append("<thead>\n");
+               htmlBuilder.append("<tr>");
+               htmlBuilder.append("<th>Season</th>");
+               htmlBuilder.append("<th>Episode</th>");
+               htmlBuilder.append("<th>Filename</th>");
+               htmlBuilder.append("<th>Size</th>");
+               htmlBuilder.append("<th>File(s)</th>");
+               htmlBuilder.append("<th>Seeds</th>");
+               htmlBuilder.append("<th>Leechers</th>");
+               htmlBuilder.append("<th>Magnet</th>");
+               htmlBuilder.append("<th>Download</th>");
+               htmlBuilder.append("</tr>\n");
+               htmlBuilder.append("</thead>\n");
+               htmlBuilder.append("<tbody>\n");
+               Episode lastEpisode = null;
+               for (Entry<Integer, Collection<Episode>> seasonEntry : FluentIterable.from(Ordering.natural().reverse().sortedCopy(allEpisodes)).index(Episode.BY_SEASON).asMap().entrySet()) {
+                       for (Episode episode : seasonEntry.getValue()) {
                                for (TorrentFile torrentFile : episode) {
-                                       htmlBuilder.append("<li>").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("</li>\n");
-                                       htmlBuilder.append("<div>");
-                                       htmlBuilder.append("<strong>").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("</strong>, ");
-                                       htmlBuilder.append("<strong>").append(torrentFile.fileCount()).append("</strong> file(s), ");
-                                       htmlBuilder.append("<strong>").append(torrentFile.seedCount()).append("</strong> seed(s), ");
-                                       htmlBuilder.append("<strong>").append(torrentFile.leechCount()).append("</strong> leecher(s)</div>\n");
-                                       htmlBuilder.append("<div>");
-                                       if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) {
-                                               htmlBuilder.append("<a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.magnetUri())).append("\">Magnet</a> ");
+                                       if (newEpisodes.contains(episode)) {
+                                               htmlBuilder.append("<tr style=\"color: #008000; font-weight: bold;\">");
+                                       } else if (newTorrentFiles.contains(torrentFile)) {
+                                               htmlBuilder.append("<tr style=\"color: #008000;\">");
+                                       } else {
+                                               htmlBuilder.append("<tr>");
                                        }
-                                       if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) {
-                                               htmlBuilder.append("<a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.downloadUri())).append("\">Download</a>");
+                                       if ((lastEpisode == null) || !lastEpisode.equals(episode)) {
+                                               htmlBuilder.append("<td>").append(episode.season()).append("</td><td>").append(episode.episode()).append("</td>");
+                                       } else {
+                                               htmlBuilder.append("<td colspan=\"2\"></td>");
                                        }
-                                       htmlBuilder.append("</div>\n");
+                                       htmlBuilder.append("<td>").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("</td>");
+                                       htmlBuilder.append("<td>").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("</td>");
+                                       htmlBuilder.append("<td>").append(torrentFile.fileCount()).append("</td>");
+                                       htmlBuilder.append("<td>").append(torrentFile.seedCount()).append("</td>");
+                                       htmlBuilder.append("<td>").append(torrentFile.leechCount()).append("</td>");
+                                       htmlBuilder.append("<td><a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.magnetUri())).append("\">Link</a></td>");
+                                       htmlBuilder.append("<td><a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.downloadUri())).append("\">Link</a></td>");
+                                       htmlBuilder.append("</tr>\n");
+                                       lastEpisode = episode;
                                }
-                               htmlBuilder.append("</ul>\n");
                        }
-                       htmlBuilder.append("</ul>\n");
                }
+               htmlBuilder.append("</tbody>\n");
+               htmlBuilder.append("</table>\n");
                htmlBuilder.append("</body></html>\n");
                return htmlBuilder.toString();
        }