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;
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;
30 * Represents a television program.
32 public class Program {
35 * Lexicographical comparison of programs based on (time, title, channel).
38 public static class TimeSorter implements Comparator<Program> {
41 * @see java.util.Comparator#compare(T, T)
43 public int compare(Program o1, Program o2) {
44 int value = o1.getInterval().getBegin().compareTo(o2.getInterval().getBegin());
48 value = o1.getName().compareTo(o2.getName());
52 return o1.getChannel().compareTo(o2.getChannel());
56 private static final Log LOG = LogFactory.getLog(Program.class);
59 * Name of the record action on the program details page.
61 private static final String RECORD_ACTION = "record";
63 private static final String RESULT_ELEM = "result";
65 public enum RecordingResult {
66 OK("Successfully recorded programs"), DUPLICATE(
67 "Already recorded programs"), CONFLICT(
68 "Programs in conflict with another recorded program"), OLDSHOW(
69 "Programs that occurred in the past"), ERROR(
70 "Programs that could not be recorded for technical reasons");
72 private String _description;
74 private RecordingResult(String aDescription) {
75 _description = aDescription;
78 public String getDescription() {
84 * Indent string to use for pretty printing.
86 private static final String INDENT = " ";
89 * Channel the program is on.
91 private String _channel;
99 * Program description.
101 private String _description;
104 * Keywords or classification of the program.
106 private String _keywords;
109 * Time interval for the program (from/to).
111 private TimeInterval _interval;
114 * Action to execute to obtain program information and/or record the
117 private Action _programInfo;
120 * Constructs the program.
126 * @param aDescription
129 * Keywords/classification.
132 * @param aProgramInfo
133 * Action to execute for detailed program information or for
134 * recording the page.
136 public Program(String aChannel, String aName, String aDescription,
137 String aKeywords, TimeInterval aInterval, Action aProgramInfo) {
140 _description = aDescription;
141 _keywords = aKeywords;
142 _interval = aInterval;
143 _programInfo = aProgramInfo;
151 public String getChannel() {
156 * Gets the program name.
160 public String getName() {
165 * Gets the description.
167 * @return Description.
169 public String getDescription() {
174 * Gets the keywords/classification.
176 * @return Keywords/classification
178 public String getKeywords() {
183 * Gets the time interval.
185 * @return Time interval.
187 public TimeInterval getInterval() {
192 * Checks if recording is possible.
193 * @return True iff recording is possible.
195 public boolean isRecordingPossible() {
197 Action record = _programInfo.execute().getAction(RECORD_ACTION);
198 if (record == null) {
202 } catch (PageException e) {
210 * @return Status describing the result of recording.
212 public RecordingResult record() {
213 LOG.info("Recording " + this);
214 if ( SystemProperties.isRecordDisabled() ) {
215 return RecordingResult.OK;
218 Action record = _programInfo.execute().getAction(RECORD_ACTION);
219 if (record == null) {
220 LOG.info(" result: " + RecordingResult.OLDSHOW);
221 return RecordingResult.OLDSHOW;
223 Page result = record.execute();
224 RecordingResult recordingResult = RecordingResult.valueOf(result.getContent().getText());
225 LOG.info(" result: " + recordingResult);
226 return recordingResult;
227 } catch (PageException e) {
228 LOG.warn("Technical problem recording program: '" + this + "'", e);
229 LOG.info(" result: " + RecordingResult.ERROR);
230 return RecordingResult.ERROR;
235 * Accepts the visitor.
240 public void accept(Visitor aVisitor) {
241 aVisitor.visitProgram(this);
247 * @see java.lang.Object#toString()
250 public String toString() {
251 return _interval + " - " + _name + " (" + _channel + "/" + _keywords
253 + (INDENT + _description).replaceAll("\n", "\n" + INDENT);
257 * @see java.lang.Object#equals(java.lang.Object)
260 public boolean equals(Object obj) {
261 if ( !(obj instanceof Program)) {
264 Program program = (Program)obj;
265 return getName().equals(program.getName()) &&
266 _programInfo.equals(program._programInfo);
270 * Converts program information to XML.
271 * @return XML representation of program information.
273 public Element asXml() {
274 DocumentFactory factory = DocumentFactory.getInstance();
275 Element program = factory.createElement("program");
276 program.addElement("name").setText(getName());
277 program.addElement("description").setText(getDescription());
278 program.addElement("keywords").setText(getKeywords());
279 program.addElement("channel").setText(getChannel());
280 Element interval = program.addElement("interval");
281 interval.addElement("begin").setText(getInterval().getBegin().toString());
282 interval.addElement("end").setText(getInterval().getEnd().toString());