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 this class to tell
32 * the executor what to do. The executor then decide on exactly what to do and
33 * in what order and makes decisions in case of conflicts.
35 public class ProgramActionExecutor {
38 * A map of category name to a set of program. Useful for displaying the
39 * output of possibly interesting programs on a per category basis.
41 private Map<String, Set<Program>> _interestingShows;
44 * Set of programs to record.
46 private Set<Program> _showsToRecord;
49 * Map or recording result to a set of programs.
51 private EnumMap<RecordingResult, Set<Program>> _recordings;
54 * Constructs the program action executor.
57 public ProgramActionExecutor() {
58 _interestingShows = new TreeMap<String, Set<Program>>();
59 _showsToRecord = new TreeSet<Program>(new Program.TimeSorter());
60 _recordings = new EnumMap<RecordingResult, Set<Program>>(
61 RecordingResult.class);
62 for (RecordingResult result : RecordingResult.values()) {
63 _recordings.put(result, new TreeSet<Program>(
64 new Program.TimeSorter()));
69 * Called by an action to indicate the desire to record a program.
72 * Priority of the program. Used to resolve conflicts.
76 public void recordProgram(int aPriority, Program aProgram) {
77 _showsToRecord.add(aProgram);
81 * Called by an action to indicate that a program is interesting.
84 * Category of the program.
88 public void interestingProgram(String aCategory, Program aProgram) {
89 Set<Program> programs = _interestingShows.get(aCategory);
90 if (programs == null) {
91 programs = new TreeSet<Program>(new Program.TimeSorter());
92 _interestingShows.put(aCategory, programs);
94 programs.add(aProgram);
98 * Makes sure that the actions are performed.
101 public void commit() {
102 for (Program program : _showsToRecord) {
103 RecordingResult result = program.record();
104 _recordings.get(result).add(program);
113 public Element getReport() {
114 DocumentFactory factory = DocumentFactory.getInstance();
115 Element report = factory.createElement("report");
117 for (RecordingResult result : RecordingResult.values()) {
118 if (_recordings.get(result).size() > 0) {
119 Element recordingResult = report.addElement("recorded")
120 .addAttribute("result", result.toString());
122 for (Program program : _recordings.get(result)) {
123 recordingResult.add(program.asXml());
128 if (_interestingShows.size() > 0) {
129 Element interesting = report.addElement("interesting");
130 for (String category : _interestingShows.keySet()) {
131 Element categoryElem = interesting;
132 if (category.length() > 0) {
133 categoryElem = interesting.addElement("category");
134 categoryElem.addAttribute("name", category);
136 for (Program program : _interestingShows.get(category)) {
137 categoryElem.add(program.asXml());