3ef511cffe4ce9c8b36c445d163e12c57dd7886e
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / main / ProgramActionExecutor.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.main;
18
19 import java.util.EnumMap;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.TreeMap;
23 import java.util.TreeSet;
24
25 import org.dom4j.DocumentFactory;
26 import org.dom4j.Element;
27 import org.wamblee.crawler.kiss.guide.Program;
28 import org.wamblee.crawler.kiss.guide.Program.RecordingResult;
29
30 /**
31  * Provides execution of actions for programs. Actions use
32  * this class to tell the executor what to do. The executor then decide
33  * on exactly what to do and in what order and makes decisions in case
34  * of conflicts.     
35  */
36 public class ProgramActionExecutor {
37     
38     /**
39      * A map of category name to a set of program. Useful for displaying the output of 
40      * possibly interesting programs on a per category basis. 
41      */
42     private Map<String, Set<Program>> _interestingShows;
43     
44     /**
45      * Set of programs to record. 
46      */
47     private Set<Program> _showsToRecord;
48    
49     /**
50      * Map or recording result to a set of programs. 
51      */
52     private EnumMap<RecordingResult, Set<Program>> _recordings;
53     
54     /**
55      * Constructs the program action executor. 
56      *
57      */
58     public ProgramActionExecutor() { 
59         _interestingShows = new TreeMap<String,Set<Program>>();
60         _showsToRecord = new TreeSet<Program>(new Program.TimeSorter());
61         _recordings = new EnumMap<RecordingResult, Set<Program>>(
62                 RecordingResult.class);
63         for (RecordingResult result : RecordingResult.values()) {
64             _recordings.put(result, new TreeSet<Program>(new Program.TimeSorter()));
65         }
66     }
67     
68     /**
69      * Called by an action to indicate the desire to record a program.
70      * @param aPriority Priority of the program. Used to resolve conflicts.  
71      * @param aProgram Program to record. 
72      */
73     public void recordProgram(int aPriority, Program aProgram) { 
74         _showsToRecord.add(aProgram);
75     }
76     
77     /**
78      * Called by an action to indicate that a program is interesting. 
79      * @param aCategory Category of the program. 
80      * @param aProgram Program. 
81      */
82     public void interestingProgram(String aCategory, Program aProgram) { 
83         Set<Program> programs = _interestingShows.get(aCategory);   
84         if ( programs == null ) { 
85             programs = new TreeSet<Program>(new Program.TimeSorter());
86             _interestingShows.put(aCategory, programs);
87         }
88         programs.add(aProgram);
89     }
90     
91     /**
92      * Makes sure that the actions are performed.
93      *
94      */
95     public void commit() { 
96         for (Program program: _showsToRecord) { 
97             RecordingResult result = program.record(); 
98             _recordings.get(result).add(program);
99         }
100     }
101     
102     /**
103      * Gets the report describing what was done. 
104      * @return Report. 
105      */
106     public String getReport() {
107         StringBuffer msg = new StringBuffer("Summary of KiSS crawler: \n\n\n");
108         
109         boolean printed = false; 
110
111         for (RecordingResult result : RecordingResult.values()) {
112             if (_recordings.get(result).size() > 0) {
113                 msg.append(result.getDescription() + "\n\n");
114                 for (Program program : _recordings.get(result)) {
115                     msg.append(program + "\n\n");
116                     printed = true; 
117                 }
118             }
119         }
120         
121         if ( _interestingShows.size() > 0 ) { 
122             msg.append("Possibly interesting shows:\n\n");
123             for (String category: _interestingShows.keySet()) { 
124                 if ( category.length() > 0 ) { 
125                     msg.append("Category: " + category + "\n\n");
126                 }
127                 for (Program program: _interestingShows.get(category)) { 
128                     msg.append(program + "\n\n");
129                     printed = true; 
130                 }
131             }
132         
133         }
134         if (!printed) {
135             msg.append("No suitable programs found");
136         }
137
138         return msg.toString(); 
139     }
140     
141     /**
142      * Get report as XML. 
143      * @return XML report 
144      */
145     public Element getXmlReport() { 
146         DocumentFactory factory = DocumentFactory.getInstance(); 
147         Element report = factory.createElement("report"); 
148         
149         for (RecordingResult result : RecordingResult.values()) {
150             if (_recordings.get(result).size() > 0) {
151                 Element recordingResult = report.addElement("recorded").addAttribute("result", result.toString());
152          
153                 for (Program program : _recordings.get(result)) {
154                     recordingResult.add(program.asXml()); 
155                 }
156             }
157         }
158         
159      
160         if ( _interestingShows.size() > 0 ) { 
161             Element interesting = report.addElement("interesting");
162             for (String category: _interestingShows.keySet()) { 
163                 Element categoryElem = interesting; 
164                 if ( category.length() > 0 ) { 
165                     categoryElem = interesting.addElement("category");
166                     categoryElem.addAttribute("name", category);
167                 }
168                 for (Program program: _interestingShows.get(category)) {
169                     categoryElem.add(program.asXml()); 
170                 }
171             }
172         
173         }
174  
175         return report; 
176     }
177 }