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 {
32 public static class TimeSorter implements Comparator<Program> {
35 * @see java.util.Comparator#compare(T, T)
37 public int compare(Program o1, Program o2) {
38 return o1.getInterval().getBegin().compareTo(o2.getInterval().getBegin());
42 private static final Log LOG = LogFactory.getLog(Program.class);
45 * Name of the record action on the program details page.
47 private static final String RECORD_ACTION = "record";
49 private static final String RESULT_ELEM = "result";
51 public enum RecordingResult {
52 OK("Successfully recorded programs"), DUPLICATE(
53 "Already recorded programs"), CONFLICT(
54 "Programs in conflict with another recorded program"), OLDSHOW(
55 "Programs that occurred in the past"), ERROR(
56 "Programs that could not be recorded for technical reasons");
58 private String _description;
60 private RecordingResult(String aDescription) {
61 _description = aDescription;
64 public String getDescription() {
70 * Indent string to use for pretty printing.
72 private static final String INDENT = " ";
75 * Channel the program is on.
77 private String _channel;
85 * Program description.
87 private String _description;
90 * Keywords or classification of the program.
92 private String _keywords;
95 * Time interval for the program (from/to).
97 private TimeInterval _interval;
100 * Action to execute to obtain program information and/or record the
103 private Action _programInfo;
106 * Constructs the program.
112 * @param aDescription
115 * Keywords/classification.
118 * @param aProgramInfo
119 * Action to execute for detailed program information or for
120 * recording the page.
122 public Program(String aChannel, String aName, String aDescription,
123 String aKeywords, TimeInterval aInterval, Action aProgramInfo) {
126 _description = aDescription;
127 _keywords = aKeywords;
128 _interval = aInterval;
129 _programInfo = aProgramInfo;
137 public String getChannel() {
142 * Gets the program name.
146 public String getName() {
151 * Gets the description.
153 * @return Description.
155 public String getDescription() {
160 * Gets the keywords/classification.
162 * @return Keywords/classification
164 public String getKeywords() {
169 * Gets the time interval.
171 * @return Time interval.
173 public TimeInterval getInterval() {
178 * Checks if recording is possible.
179 * @return True iff recording is possible.
181 public boolean isRecordingPossible() {
183 Action record = _programInfo.execute().getAction(RECORD_ACTION);
184 if (record == null) {
188 } catch (PageException e) {
196 * @return Status describing the result of recording.
198 public RecordingResult record() {
200 Action record = _programInfo.execute().getAction(RECORD_ACTION);
201 if (record == null) {
202 return RecordingResult.OLDSHOW;
204 Page result = record.execute();
205 return RecordingResult.valueOf(result.getContent().getText());
206 } catch (PageException e) {
207 LOG.warn("Technical problem recording program: '" + this + "'", e);
208 return RecordingResult.ERROR;
213 * Accepts the visitor.
218 public void accept(Visitor aVisitor) {
219 aVisitor.visitProgram(this);
225 * @see java.lang.Object#toString()
228 public String toString() {
229 return _interval + " - " + _name + " (" + _channel + "/" + _keywords
231 + (INDENT + _description).replaceAll("\n", "\n" + INDENT);
235 * @see java.lang.Object#equals(java.lang.Object)
238 public boolean equals(Object obj) {
239 if ( !(obj instanceof Program)) {
242 Program program = (Program)obj;
243 return getName().equals(program.getName()) &&
244 _programInfo.equals(program._programInfo);