2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.wamblee.crawler.kiss.guide;
19 import java.util.Comparator;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.dom4j.DocumentFactory;
24 import org.dom4j.Element;
25 import org.wamblee.crawler.Action;
26 import org.wamblee.crawler.Page;
27 import org.wamblee.crawler.PageException;
28 import org.wamblee.crawler.kiss.main.SystemProperties;
31 * Represents a television program.
33 public class Program {
36 * Lexicographical comparison of programs based on (time, title, channel).
39 public static class TimeSorter implements Comparator<Program> {
42 * @see java.util.Comparator#compare(T, T)
44 public int compare(Program o1, Program o2) {
45 int value = o1.getInterval().getBegin().compareTo(o2.getInterval().getBegin());
49 value = o1.getName().compareTo(o2.getName());
53 return o1.getChannel().compareTo(o2.getChannel());
57 private static final Log LOG = LogFactory.getLog(Program.class);
60 * Name of the record action on the program details page.
62 private static final String RECORD_ACTION = "record";
64 private static final String RESULT_ELEM = "result";
66 public enum RecordingResult {
67 OK("Successfully recorded programs"), DUPLICATE(
68 "Already recorded programs"), CONFLICT(
69 "Programs in conflict with another recorded program"), OLDSHOW(
70 "Programs that occurred in the past"), ERROR(
71 "Programs that could not be recorded for technical reasons");
73 private String _description;
75 private RecordingResult(String aDescription) {
76 _description = aDescription;
79 public String getDescription() {
85 * Indent string to use for pretty printing.
87 private static final String INDENT = " ";
90 * Channel the program is on.
92 private String _channel;
100 * Program description.
102 private String _description;
105 * Keywords or classification of the program.
107 private String _keywords;
110 * Time interval for the program (from/to).
112 private TimeInterval _interval;
115 * Action to execute to obtain program information and/or record the
118 private Action _programInfo;
121 * Constructs the program.
127 * @param aDescription
130 * Keywords/classification.
133 * @param aProgramInfo
134 * Action to execute for detailed program information or for
135 * recording the page.
137 public Program(String aChannel, String aName, String aDescription,
138 String aKeywords, TimeInterval aInterval, Action aProgramInfo) {
141 _description = aDescription;
142 _keywords = aKeywords;
143 _interval = aInterval;
144 _programInfo = aProgramInfo;
152 public String getChannel() {
157 * Gets the program name.
161 public String getName() {
166 * Gets the description.
168 * @return Description.
170 public String getDescription() {
175 * Gets the keywords/classification.
177 * @return Keywords/classification
179 public String getKeywords() {
184 * Gets the time interval.
186 * @return Time interval.
188 public TimeInterval getInterval() {
193 * Checks if recording is possible.
194 * @return True iff recording is possible.
196 public boolean isRecordingPossible() {
198 Action record = _programInfo.execute().getAction(RECORD_ACTION);
199 if (record == null) {
203 } catch (PageException e) {
211 * @return Status describing the result of recording.
213 public RecordingResult record() {
214 LOG.info("Recording " + this);
215 if ( SystemProperties.isRecordDisabled() ) {
216 return RecordingResult.OK;
219 Action record = _programInfo.execute().getAction(RECORD_ACTION);
220 if (record == null) {
221 LOG.info(" result: " + RecordingResult.OLDSHOW);
222 return RecordingResult.OLDSHOW;
224 Page result = record.execute();
225 RecordingResult recordingResult = RecordingResult.valueOf(result.getContent().getText());
226 LOG.info(" result: " + recordingResult);
227 return recordingResult;
228 } catch (PageException e) {
229 LOG.warn("Technical problem recording program: '" + this + "'", e);
230 LOG.info(" result: " + RecordingResult.ERROR);
231 return RecordingResult.ERROR;
236 * Accepts the visitor.
241 public void accept(Visitor aVisitor) {
242 aVisitor.visitProgram(this);
248 * @see java.lang.Object#toString()
251 public String toString() {
252 return _interval + " - " + _name + " (" + _channel + "/" + _keywords
254 + (INDENT + _description).replaceAll("\n", "\n" + INDENT);
258 * @see java.lang.Object#equals(java.lang.Object)
261 public boolean equals(Object obj) {
262 if ( !(obj instanceof Program)) {
265 Program program = (Program)obj;
266 return getName().equals(program.getName()) &&
267 _programInfo.equals(program._programInfo);
271 * Converts program information to XML.
272 * @return XML representation of program information.
274 public Element asXml() {
275 DocumentFactory factory = DocumentFactory.getInstance();
276 Element program = factory.createElement("program");
277 program.addElement("name").setText(getName());
278 program.addElement("description").setText(getDescription());
279 program.addElement("keywords").setText(getKeywords());
280 program.addElement("channel").setText(getChannel());
281 Element interval = program.addElement("interval");
282 interval.addElement("begin").setText(getInterval().getBegin().toString());
283 interval.addElement("end").setText(getInterval().getEnd().toString());