Store the latest comic state.
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / triggers / NewComicTrigger.java
1 /*
2  * rhynodge - NewComicTrigger.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.rhynodge.triggers;
19
20 import static com.google.common.base.Preconditions.*;
21
22 import java.util.List;
23
24 import net.pterodactylus.rhynodge.Reaction;
25 import net.pterodactylus.rhynodge.State;
26 import net.pterodactylus.rhynodge.Trigger;
27 import net.pterodactylus.rhynodge.output.DefaultOutput;
28 import net.pterodactylus.rhynodge.output.Output;
29 import net.pterodactylus.rhynodge.states.ComicState;
30 import net.pterodactylus.rhynodge.states.ComicState.Comic;
31 import net.pterodactylus.rhynodge.states.ComicState.Strip;
32
33 import com.google.common.collect.Lists;
34 import org.apache.commons.lang3.StringEscapeUtils;
35 import org.apache.commons.lang3.StringUtils;
36
37 /**
38  * {@link Trigger} implementation that detects the presence of new {@link
39  * Comic}s in a {@link ComicState}.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class NewComicTrigger implements Trigger {
44
45         /** The new comics. */
46         private final List<Comic> newComics = Lists.newArrayList();
47
48         /** The latest comic state. */
49         private ComicState mergedComicState;
50
51         @Override
52         public State mergeStates(State previousState, State currentState) {
53                 checkArgument(previousState instanceof ComicState, "previous state must be a comic state");
54                 checkArgument(currentState instanceof ComicState, "current state must be a comic state");
55
56                 ComicState previousComicState = (ComicState) previousState;
57                 ComicState currentComicState = (ComicState) currentState;
58
59                 /* copy old state into new state. */
60                 mergedComicState = new ComicState();
61                 for (Comic comic : previousComicState) {
62                         mergedComicState.add(comic);
63                 }
64
65                 newComics.clear();
66                 for (Comic comic : currentComicState) {
67                         if (!mergedComicState.comics().contains(comic)) {
68                                 newComics.add(comic);
69                                 mergedComicState.add(comic);
70                         }
71                 }
72
73                 return mergedComicState;
74         }
75
76         @Override
77         public boolean triggers() {
78                 return !newComics.isEmpty();
79         }
80
81         @Override
82         public Output output(Reaction reaction) {
83                 DefaultOutput output = new DefaultOutput(String.format("New Comic found for “%s!”", reaction.name()));
84
85                 output.addText("text/plain", generatePlainText());
86                 output.addText("text/html", generateHtmlText());
87
88                 return output;
89         }
90
91         //
92         // PRIVATE METHODS
93         //
94
95         /**
96          * Generates a list of the new comics in plain text format.
97          *
98          * @return The list of new comics in plain text format
99          */
100         private String generatePlainText() {
101                 StringBuilder text = new StringBuilder();
102
103                 for (Comic newComic : newComics) {
104                         text.append("Comic Found: ").append(newComic.title()).append("\n\n");
105                         for (Strip strip : newComic) {
106                                 text.append("Image: ").append(strip.imageUrl()).append("\n");
107                                 if (!StringUtils.isBlank(strip.comment())) {
108                                         text.append("Comment: ").append(strip.comment()).append("\n");
109                                 }
110                         }
111                         text.append("\n\n");
112                 }
113
114                 return text.toString();
115         }
116
117         /**
118          * Generates a list of new comics in HTML format.
119          *
120          * @return The list of new comics in HTML format
121          */
122         private String generateHtmlText() {
123                 StringBuilder html = new StringBuilder();
124                 html.append("<body>");
125
126                 for (Comic newComic : newComics) {
127                         generateComicHtml(html, newComic);
128                 }
129
130                 return html.append("</body>").toString();
131         }
132
133         /**
134          * Generates the HTML for a single comic.
135          *
136          * @param html
137          *              The string builder to append the HTML to
138          * @param comic
139          *              The comic to render
140          */
141         private void generateComicHtml(StringBuilder html, Comic comic) {
142                 html.append("<h1>").append(StringEscapeUtils.escapeHtml4(comic.title())).append("</h1>\n");
143                 for (Strip strip : comic) {
144                         html.append("<div><img src=\"").append(StringEscapeUtils.escapeHtml4(strip.imageUrl()));
145                         html.append("\" alt=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
146                         html.append("\" title=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
147                         html.append("\"></div>\n");
148                 }
149         }
150
151 }