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