(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 java.util.Comparator;
20
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;
26
27 /**
28  * Represents a television program.
29  */
30 public class Program {
31     
32     public static class TimeSorter implements Comparator<Program> { 
33      
34         /* (non-Javadoc)
35          * @see java.util.Comparator#compare(T, T)
36          */
37         public int compare(Program o1, Program o2) { 
38             return o1.getInterval().getBegin().compareTo(o2.getInterval().getBegin());
39         }
40     }
41     
42     private static final Log LOG = LogFactory.getLog(Program.class);
43
44     /**
45      * Name of the record action on the program details page.
46      */
47     private static final String RECORD_ACTION = "record";
48
49     private static final String RESULT_ELEM = "result";
50
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");
57
58         private String _description;
59
60         private RecordingResult(String aDescription) {
61             _description = aDescription;
62         }
63
64         public String getDescription() {
65             return _description;
66         }
67     };
68
69     /**
70      * Indent string to use for pretty printing.
71      */
72     private static final String INDENT = "       ";
73
74     /**
75      * Channel the program is on.
76      */
77     private String _channel;
78
79     /**
80      * Program name.
81      */
82     private String _name;
83
84     /**
85      * Program description.
86      */
87     private String _description;
88
89     /**
90      * Keywords or classification of the program.
91      */
92     private String _keywords;
93
94     /**
95      * Time interval for the program (from/to).
96      */
97     private TimeInterval _interval;
98
99     /**
100      * Action to execute to obtain program information and/or record the
101      * program.
102      */
103     private Action _programInfo;
104
105     /**
106      * Constructs the program.
107      * 
108      * @param aChannel
109      *            Channel name.
110      * @param aName
111      *            Program name.
112      * @param aDescription
113      *            Description.
114      * @param aKeywords
115      *            Keywords/classification.
116      * @param aInterval
117      *            Time interval.
118      * @param aProgramInfo
119      *            Action to execute for detailed program information or for
120      *            recording the page.
121      */
122     public Program(String aChannel, String aName, String aDescription,
123             String aKeywords, TimeInterval aInterval, Action aProgramInfo) {
124         _channel = aChannel;
125         _name = aName;
126         _description = aDescription;
127         _keywords = aKeywords;
128         _interval = aInterval;
129         _programInfo = aProgramInfo;
130     }
131
132     /**
133      * Gets the channel.
134      * 
135      * @return Channel.
136      */
137     public String getChannel() {
138         return _channel;
139     }
140
141     /**
142      * Gets the program name.
143      * 
144      * @return Name.
145      */
146     public String getName() {
147         return _name;
148     }
149
150     /**
151      * Gets the description.
152      * 
153      * @return Description.
154      */
155     public String getDescription() {
156         return _description;
157     }
158
159     /**
160      * Gets the keywords/classification.
161      * 
162      * @return Keywords/classification
163      */
164     public String getKeywords() {
165         return _keywords;
166     }
167
168     /**
169      * Gets the time interval.
170      * 
171      * @return Time interval.
172      */
173     public TimeInterval getInterval() {
174         return _interval;
175     }
176     
177     /**
178      * Checks if recording is possible. 
179      * @return True iff recording is possible. 
180      */
181     public boolean isRecordingPossible() { 
182         try {
183             Action record = _programInfo.execute().getAction(RECORD_ACTION);
184             if (record == null) {
185                 return false; 
186             }
187             return true; 
188         } catch (PageException e) {
189             return false; 
190         }
191     }
192
193     /**
194      * Records the show.
195      * 
196      * @return Status describing the result of recording.
197      */
198     public RecordingResult record() {
199         try {
200             Action record = _programInfo.execute().getAction(RECORD_ACTION);
201             if (record == null) {
202                 return RecordingResult.OLDSHOW;
203             }
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;
209         }
210     }
211
212     /**
213      * Accepts the visitor.
214      * 
215      * @param aVisitor
216      *            Visitor.
217      */
218     public void accept(Visitor aVisitor) {
219         aVisitor.visitProgram(this);
220     }
221
222     /*
223      * (non-Javadoc)
224      * 
225      * @see java.lang.Object#toString()
226      */
227     @Override
228     public String toString() {
229         return _interval + " - " + _name + " (" + _channel + "/" + _keywords
230                 + ")" + "\n"
231                 + (INDENT + _description).replaceAll("\n", "\n" + INDENT);
232     }
233     
234     /* (non-Javadoc)
235      * @see java.lang.Object#equals(java.lang.Object)
236      */
237     @Override
238     public boolean equals(Object obj) {
239         if ( !(obj instanceof Program)) { 
240             return false; 
241         }
242         Program program = (Program)obj; 
243         return getName().equals(program.getName()) && 
244                _programInfo.equals(program._programInfo);
245     }
246 }