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.wamblee.crawler.Action;
24 import org.wamblee.crawler.Page;
25 import org.wamblee.crawler.PageException;
28 * Represents a television program.
30 public class Program {
33 * Lexicographical comparison of programs based on (time, title, channel).
36 public static class TimeSorter implements Comparator<Program> {
39 * @see java.util.Comparator#compare(T, T)
41 public int compare(Program o1, Program o2) {
42 int value = o1.getInterval().getBegin().compareTo(o2.getInterval().getBegin());
46 value = o1.getName().compareTo(o2.getName());
50 return o1.getChannel().compareTo(o2.getChannel());
54 private static final Log LOG = LogFactory.getLog(Program.class);
57 * Name of the record action on the program details page.
59 private static final String RECORD_ACTION = "record";
61 private static final String RESULT_ELEM = "result";
63 public enum RecordingResult {
64 OK("Successfully recorded programs"), DUPLICATE(
65 "Already recorded programs"), CONFLICT(
66 "Programs in conflict with another recorded program"), OLDSHOW(
67 "Programs that occurred in the past"), ERROR(
68 "Programs that could not be recorded for technical reasons");
70 private String _description;
72 private RecordingResult(String aDescription) {
73 _description = aDescription;
76 public String getDescription() {
82 * Indent string to use for pretty printing.
84 private static final String INDENT = " ";
87 * Channel the program is on.
89 private String _channel;
97 * Program description.
99 private String _description;
102 * Keywords or classification of the program.
104 private String _keywords;
107 * Time interval for the program (from/to).
109 private TimeInterval _interval;
112 * Action to execute to obtain program information and/or record the
115 private Action _programInfo;
118 * Constructs the program.
124 * @param aDescription
127 * Keywords/classification.
130 * @param aProgramInfo
131 * Action to execute for detailed program information or for
132 * recording the page.
134 public Program(String aChannel, String aName, String aDescription,
135 String aKeywords, TimeInterval aInterval, Action aProgramInfo) {
138 _description = aDescription;
139 _keywords = aKeywords;
140 _interval = aInterval;
141 _programInfo = aProgramInfo;
149 public String getChannel() {
154 * Gets the program name.
158 public String getName() {
163 * Gets the description.
165 * @return Description.
167 public String getDescription() {
172 * Gets the keywords/classification.
174 * @return Keywords/classification
176 public String getKeywords() {
181 * Gets the time interval.
183 * @return Time interval.
185 public TimeInterval getInterval() {
190 * Checks if recording is possible.
191 * @return True iff recording is possible.
193 public boolean isRecordingPossible() {
195 Action record = _programInfo.execute().getAction(RECORD_ACTION);
196 if (record == null) {
200 } catch (PageException e) {
208 * @return Status describing the result of recording.
210 public RecordingResult record() {
211 LOG.info("Recording " + this);
212 if ( SystemProperties.isRecordDisabled() ) {
213 return RecordingResult.OK;
216 Action record = _programInfo.execute().getAction(RECORD_ACTION);
217 if (record == null) {
218 LOG.info(" result: " + RecordingResult.OLDSHOW);
219 return RecordingResult.OLDSHOW;
221 Page result = record.execute();
222 RecordingResult recordingResult = RecordingResult.valueOf(result.getContent().getText());
223 LOG.info(" result: " + recordingResult);
224 return recordingResult;
225 } catch (PageException e) {
226 LOG.warn("Technical problem recording program: '" + this + "'", e);
227 LOG.info(" result: " + RecordingResult.ERROR);
228 return RecordingResult.ERROR;
233 * Accepts the visitor.
238 public void accept(Visitor aVisitor) {
239 aVisitor.visitProgram(this);
245 * @see java.lang.Object#toString()
248 public String toString() {
249 return _interval + " - " + _name + " (" + _channel + "/" + _keywords
251 + (INDENT + _description).replaceAll("\n", "\n" + INDENT);
255 * @see java.lang.Object#equals(java.lang.Object)
258 public boolean equals(Object obj) {
259 if ( !(obj instanceof Program)) {
262 Program program = (Program)obj;
263 return getName().equals(program.getName()) &&
264 _programInfo.equals(program._programInfo);