(no commit message)
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / Program.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16
17 package org.wamblee.crawler.kiss;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.wamblee.crawler.Action;
22 import org.wamblee.crawler.Page;
23 import org.wamblee.crawler.PageException;
24
25 /**
26  * Represents a television program.
27  */
28 public class Program {
29     
30     private static final Log LOG = LogFactory.getLog(Program.class);
31
32     /**
33      * Name of the record action on the program details page.
34      */
35     private static final String RECORD_ACTION = "record";
36
37     private static final String RESULT_ELEM = "result";
38
39     public enum RecordingResult {
40         OK("Successfully recorded programs"), DUPLICATE(
41                 "Already recorded programs"), CONFLICT(
42                 "Programs in conflict with another recorded program"), OLDSHOW(
43                 "Programs that occurred in the past"), ERROR(
44                 "Programs that could not be recorded for technical reasons");
45
46         private String _description;
47
48         private RecordingResult(String aDescription) {
49             _description = aDescription;
50         }
51
52         public String getDescription() {
53             return _description;
54         }
55     };
56
57     /**
58      * Indent string to use for pretty printing.
59      */
60     private static final String INDENT = "       ";
61
62     /**
63      * Channel the program is on.
64      */
65     private String _channel;
66
67     /**
68      * Program name.
69      */
70     private String _name;
71
72     /**
73      * Program description.
74      */
75     private String _description;
76
77     /**
78      * Keywords or classification of the program.
79      */
80     private String _keywords;
81
82     /**
83      * Time interval for the program (from/to).
84      */
85     private TimeInterval _interval;
86
87     /**
88      * Action to execute to obtain program information and/or record the
89      * program.
90      */
91     private Action _programInfo;
92
93     /**
94      * Constructs the program.
95      * 
96      * @param aChannel
97      *            Channel name.
98      * @param aName
99      *            Program name.
100      * @param aDescription
101      *            Description.
102      * @param aKeywords
103      *            Keywords/classification.
104      * @param aInterval
105      *            Time interval.
106      * @param aProgramInfo
107      *            Action to execute for detailed program information or for
108      *            recording the page.
109      */
110     public Program(String aChannel, String aName, String aDescription,
111             String aKeywords, TimeInterval aInterval, Action aProgramInfo) {
112         _channel = aChannel;
113         _name = aName;
114         _description = aDescription;
115         _keywords = aKeywords;
116         _interval = aInterval;
117         _programInfo = aProgramInfo;
118     }
119
120     /**
121      * Gets the channel.
122      * 
123      * @return Channel.
124      */
125     public String getChannel() {
126         return _channel;
127     }
128
129     /**
130      * Gets the program name.
131      * 
132      * @return Name.
133      */
134     public String getName() {
135         return _name;
136     }
137
138     /**
139      * Gets the description.
140      * 
141      * @return Description.
142      */
143     public String getDescription() {
144         return _description;
145     }
146
147     /**
148      * Gets the keywords/classification.
149      * 
150      * @return Keywords/classification
151      */
152     public String getKeywords() {
153         return _keywords;
154     }
155
156     /**
157      * Gets the time interval.
158      * 
159      * @return Time interval.
160      */
161     public TimeInterval getInterval() {
162         return _interval;
163     }
164
165     /**
166      * Records the show.
167      * 
168      * @return Status describing the result of recording.
169      */
170     public RecordingResult record() {
171         try {
172             Action record = _programInfo.execute().getAction(RECORD_ACTION);
173             if (record == null) {
174                 return RecordingResult.OLDSHOW;
175             }
176             Page result = record.execute();
177             return RecordingResult.valueOf(result.getContent().getText());
178         } catch (PageException e) {
179             LOG.warn("Technical problem recording program: '" + this + "'", e);
180             return RecordingResult.ERROR;
181         }
182     }
183
184     /**
185      * Accepts the visitor.
186      * 
187      * @param aVisitor
188      *            Visitor.
189      */
190     public void accept(Visitor aVisitor) {
191         aVisitor.visitProgram(this);
192     }
193
194     /*
195      * (non-Javadoc)
196      * 
197      * @see java.lang.Object#toString()
198      */
199     @Override
200     public String toString() {
201         return _interval + " - " + _name + " (" + _channel + "/" + _keywords
202                 + ")" + "\n"
203                 + (INDENT + _description).replaceAll("\n", "\n" + INDENT);
204     }
205 }