2 * Copyright 2005 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.wamblee.crawler.kiss.main;
19 import java.util.EnumMap;
22 import java.util.TreeMap;
23 import java.util.TreeSet;
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;
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.
37 public class ProgramActionExecutor {
39 private static final Log LOG = LogFactory.getLog(ProgramActionExecutor.class);
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.
45 private Map<String, Set<Program>> _interestingShows;
48 * Set of programs to record.
50 private Set<Program> _showsToRecord;
53 * Map or recording result to a set of programs.
55 private EnumMap<RecordingResult, Set<Program>> _recordings;
58 * Constructs the program action executor.
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()));
73 * Called by an action to indicate the desire to record a program.
76 * Priority of the program. Used to resolve conflicts.
80 public void recordProgram(int aPriority, Program aProgram) {
81 LOG.info("priority = " + aPriority + ", program: " + aProgram);
82 _showsToRecord.add(aProgram);
86 * Called by an action to indicate that a program is interesting.
89 * Category of the program.
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);
100 programs.add(aProgram);
104 * Makes sure that the actions are performed.
107 public void commit() {
108 for (Program program : _showsToRecord) {
109 RecordingResult result = program.record();
110 _recordings.get(result).add(program);
119 public Element getReport() {
120 DocumentFactory factory = DocumentFactory.getInstance();
121 Element report = factory.createElement("report");
123 for (RecordingResult result : RecordingResult.values()) {
124 if (_recordings.get(result).size() > 0) {
125 Element recordingResult = report.addElement("recorded")
126 .addAttribute("result", result.toString());
128 for (Program program : _recordings.get(result)) {
129 recordingResult.add(program.asXml());
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);
142 for (Program program : _interestingShows.get(category)) {
143 categoryElem.add(program.asXml());