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