some extra logging.
[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.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.dom4j.DocumentFactory;
28 import org.dom4j.Element;
29 import org.wamblee.crawler.kiss.guide.Program;
30 import org.wamblee.crawler.kiss.guide.Program.RecordingResult;
31
32 /**
33  * Provides execution of actions for programs. Actions use this class to tell
34  * the executor what to do. The executor then decides on exactly what to do and
35  * in what order and makes decisions in case of conflicts.
36  */
37 public class ProgramActionExecutor {
38     
39     private static final Log LOG = LogFactory.getLog(ProgramActionExecutor.class);
40
41     /**
42      * A map of category name to a set of program. Useful for displaying the
43      * output of possibly interesting programs on a per category basis.
44      */
45     private Map<String, Set<Program>> _interestingShows;
46
47     /**
48      * Set of programs to record.
49      */
50     private Set<Program> _showsToRecord;
51
52     /**
53      * Map or recording result to a set of programs.
54      */
55     private EnumMap<RecordingResult, Set<Program>> _recordings;
56
57     /**
58      * Constructs the program action executor.
59      * 
60      */
61     public ProgramActionExecutor() {
62         _interestingShows = new TreeMap<String, Set<Program>>();
63         _showsToRecord = new TreeSet<Program>(new Program.TimeSorter());
64         _recordings = new EnumMap<RecordingResult, Set<Program>>(
65                 RecordingResult.class);
66         for (RecordingResult result : RecordingResult.values()) {
67             _recordings.put(result, new TreeSet<Program>(
68                     new Program.TimeSorter()));
69         }
70     }
71
72     /**
73      * Called by an action to indicate the desire to record a program.
74      * 
75      * @param aPriority
76      *            Priority of the program. Used to resolve conflicts.
77      * @param aProgram
78      *            Program to record.
79      */
80     public void recordProgram(int aPriority, Program aProgram) {
81         LOG.info("priority = " + aPriority + ", program: " + aProgram);
82         _showsToRecord.add(aProgram);
83     }
84
85     /**
86      * Called by an action to indicate that a program is interesting.
87      * 
88      * @param aCategory
89      *            Category of the program.
90      * @param aProgram
91      *            Program.
92      */
93     public void interestingProgram(String aCategory, Program aProgram) {
94         LOG.info("category = '" + aCategory + "', program: " + aProgram);
95         Set<Program> programs = _interestingShows.get(aCategory);
96         if (programs == null) {
97             programs = new TreeSet<Program>(new Program.TimeSorter());
98             _interestingShows.put(aCategory, programs);
99         }
100         programs.add(aProgram);
101     }
102
103     /**
104      * Makes sure that the actions are performed.
105      * 
106      */
107     public void commit() {
108         for (Program program : _showsToRecord) {
109             RecordingResult result = program.record();
110             _recordings.get(result).add(program);
111         }
112     }
113
114     /**
115      * Get report as XML.
116      * 
117      * @return XML report
118      */
119     public Element getReport() {
120         DocumentFactory factory = DocumentFactory.getInstance();
121         Element report = factory.createElement("report");
122
123         for (RecordingResult result : RecordingResult.values()) {
124             if (_recordings.get(result).size() > 0) {
125                 Element recordingResult = report.addElement("recorded")
126                         .addAttribute("result", result.toString());
127
128                 for (Program program : _recordings.get(result)) {
129                     recordingResult.add(program.asXml());
130                 }
131             }
132         }
133
134         if (_interestingShows.size() > 0) {
135             Element interesting = report.addElement("interesting");
136             for (String category : _interestingShows.keySet()) {
137                 Element categoryElem = interesting;
138                 if (category.length() > 0) {
139                     categoryElem = interesting.addElement("category");
140                     categoryElem.addAttribute("name", category);
141                 }
142                 for (Program program : _interestingShows.get(category)) {
143                     categoryElem.add(program.asXml());
144                 }
145             }
146
147         }
148
149         return report;
150     }
151 }