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.dom4j.DocumentFactory;
26 import org.dom4j.Element;
27 import org.wamblee.crawler.kiss.guide.Program;
28 import org.wamblee.crawler.kiss.guide.Program.RecordingResult;
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
36 public class ProgramActionExecutor {
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.
42 private Map<String, Set<Program>> _interestingShows;
45 * Set of programs to record.
47 private Set<Program> _showsToRecord;
50 * Map or recording result to a set of programs.
52 private EnumMap<RecordingResult, Set<Program>> _recordings;
55 * Constructs the program action executor.
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()));
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.
73 public void recordProgram(int aPriority, Program aProgram) {
74 _showsToRecord.add(aProgram);
78 * Called by an action to indicate that a program is interesting.
79 * @param aCategory Category of the program.
80 * @param aProgram Program.
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);
88 programs.add(aProgram);
92 * Makes sure that the actions are performed.
95 public void commit() {
96 for (Program program: _showsToRecord) {
97 RecordingResult result = program.record();
98 _recordings.get(result).add(program);
106 public Element getReport() {
107 DocumentFactory factory = DocumentFactory.getInstance();
108 Element report = factory.createElement("report");
110 for (RecordingResult result : RecordingResult.values()) {
111 if (_recordings.get(result).size() > 0) {
112 Element recordingResult = report.addElement("recorded").addAttribute("result", result.toString());
114 for (Program program : _recordings.get(result)) {
115 recordingResult.add(program.asXml());
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);
129 for (Program program: _interestingShows.get(category)) {
130 categoryElem.add(program.asXml());