(no commit message)
[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      * Get report as XML. 
104      * @return XML report 
105      */
106     public Element getReport() { 
107         DocumentFactory factory = DocumentFactory.getInstance(); 
108         Element report = factory.createElement("report"); 
109         
110         for (RecordingResult result : RecordingResult.values()) {
111             if (_recordings.get(result).size() > 0) {
112                 Element recordingResult = report.addElement("recorded").addAttribute("result", result.toString());
113          
114                 for (Program program : _recordings.get(result)) {
115                     recordingResult.add(program.asXml()); 
116                 }
117             }
118         }
119         
120      
121         if ( _interestingShows.size() > 0 ) { 
122             Element interesting = report.addElement("interesting");
123             for (String category: _interestingShows.keySet()) { 
124                 Element categoryElem = interesting; 
125                 if ( category.length() > 0 ) { 
126                     categoryElem = interesting.addElement("category");
127                     categoryElem.addAttribute("name", category);
128                 }
129                 for (Program program: _interestingShows.get(category)) {
130                     categoryElem.add(program.asXml()); 
131                 }
132             }
133         
134         }
135  
136         return report; 
137     }
138 }