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