(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.wamblee.crawler.Action;
20 import org.wamblee.crawler.Page;
21 import org.wamblee.crawler.PageException;
22
23 /**
24  * Represents a television program. 
25  */
26 public class Program {
27
28     /**
29      * Name of the record action on the program details page. 
30      */
31     private static final String RECORD_ACTION = "record";
32     
33     private static final String RESULT_ELEM = "result";
34     
35     public enum RecordingResult {
36             OK("Successfully recorded programs"), 
37             DUPLICATE("Already recorded programs"), 
38             CONFLICT("Programs in conflict with another recorded program"), 
39             OLDSHOW("Programs that occured in the past"), 
40             ERROR("Programs that could not be recorded for technical reasons");
41             
42             private String _description; 
43             
44             private RecordingResult(String aDescription) { 
45                 _description = aDescription;     
46             }
47             
48             public String getDescription() { 
49                 return _description; 
50             }
51     };
52
53     /**
54      * Indent string to use for pretty printing. 
55      */
56     private static final String INDENT = "       ";
57
58     /**
59      * Channel the program is on.  
60      */
61     private String _channel;
62
63     /**
64      * Program name. 
65      */
66     private String _name;
67
68     /**
69      * Program description. 
70      */
71     private String _description;
72
73     /**
74      * Keywords or classification of the program. 
75      */
76     private String _keywords;
77
78     /**
79      * Time interval for the program (from/to).
80      */
81     private TimeInterval _interval;
82
83     /**
84      * Action to execute to obtain program information and/or record the program.
85      */
86     private Action _programInfo;
87
88     /**
89      * Constructs the program. 
90      * @param aChannel Channel name. 
91      * @param aName Program name. 
92      * @param aDescription Description. 
93      * @param aKeywords Keywords/classification.
94      * @param aInterval Time interval. 
95      * @param aProgramInfo Action to execute for detailed program information or
96      *    for recording the page. 
97      */
98     public Program(String aChannel, String aName, String aDescription,
99             String aKeywords, TimeInterval aInterval, Action aProgramInfo) {
100         _channel = aChannel;
101         _name = aName;
102         _description = aDescription;
103         _keywords = aKeywords;
104         _interval = aInterval;
105         _programInfo = aProgramInfo;
106     }
107
108     /**
109      * Gets the channel.
110      * @return Channel.
111      */
112     public String getChannel() {
113         return _channel;
114     }
115
116     /**
117      * Gets the program name.
118      * @return Name. 
119      */
120     public String getName() {
121         return _name;
122     }
123
124     /**
125      * Gets the description.
126      * @return Description. 
127      */
128     public String getDescription() {
129         return _description;
130     }
131
132     /**
133      * Gets the keywords/classification. 
134      * @return Keywords/classification
135      */
136     public String getKeywords() {
137         return _keywords;
138     }
139
140     /**
141      * Gets the time interval. 
142      * @return Time interval. 
143      */
144     public TimeInterval getInterval() {
145         return _interval;
146     }
147
148     /**
149      * Records the show.
150      * @return True iff an attempt could be made to record the page. 
151      * @throws PageException In case of problems recording the page.
152      */
153     public RecordingResult record() throws PageException {
154         Action record = _programInfo.execute().getAction(RECORD_ACTION);
155         if (record == null) {
156             return RecordingResult.OLDSHOW;
157         }
158         Page result = record.execute();
159         return RecordingResult.valueOf(result.getContent().getText());    
160     }
161
162     /**
163      * Accepts the visitor. 
164      * @param aVisitor Visitor. 
165      */
166     public void accept(Visitor aVisitor) {
167         aVisitor.visitProgram(this);
168     }
169
170     /*
171      * (non-Javadoc)
172      * 
173      * @see java.lang.Object#toString()
174      */
175     @Override
176     public String toString() {
177         return _interval + " - " + _name + " (" + _channel + "/" + _keywords
178                 + ")" + "\n"
179                 + (INDENT + _description).replaceAll("\n", "\n" + INDENT);
180     }
181 }