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;
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.Program.RecordingResult;
30 * Provides execution of actions for programs. Actions use
31 * this class to tell the executor what to do. The executor then decide
32 * on exactly what to do and in what order and makes decisions in case
35 public class ProgramActionExecutor {
38 * A map of category name to a set of program. Useful for displaying the output of
39 * 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>(new Program.TimeSorter()));
68 * Called by an action to indicate the desire to record a program.
69 * @param aPriority Priority of the program. Used to resolve conflicts.
70 * @param aProgram Program to record.
72 public void recordProgram(int aPriority, Program aProgram) {
73 _showsToRecord.add(aProgram);
77 * Called by an action to indicate that a program is interesting.
78 * @param aCategory Category of the program.
79 * @param aProgram Program.
81 public void interestingProgram(String aCategory, Program aProgram) {
82 Set<Program> programs = _interestingShows.get(aCategory);
83 if ( programs == null ) {
84 programs = new TreeSet<Program>(new Program.TimeSorter());
85 _interestingShows.put(aCategory, programs);
87 programs.add(aProgram);
91 * Makes sure that the actions are performed.
94 public void commit() {
95 for (Program program: _showsToRecord) {
96 RecordingResult result = program.record();
97 _recordings.get(result).add(program);
102 * Gets the report describing what was done.
105 public String getReport() {
106 StringBuffer msg = new StringBuffer("Summary of KiSS crawler: \n\n\n");
108 boolean printed = false;
110 for (RecordingResult result : RecordingResult.values()) {
111 if (_recordings.get(result).size() > 0) {
112 msg.append(result.getDescription() + "\n\n");
113 for (Program program : _recordings.get(result)) {
114 msg.append(program + "\n\n");
120 if ( _interestingShows.size() > 0 ) {
121 msg.append("Possibly interesting shows:\n\n");
122 for (String category: _interestingShows.keySet()) {
123 if ( category.length() > 0 ) {
124 msg.append("Category: " + category + "\n\n");
126 for (Program program: _interestingShows.get(category)) {
127 msg.append(program + "\n\n");
134 msg.append("No suitable programs found");
137 return msg.toString();
144 public Element getXmlReport() {
145 DocumentFactory factory = DocumentFactory.getInstance();
146 Element report = factory.createElement("report");
148 for (RecordingResult result : RecordingResult.values()) {
149 if (_recordings.get(result).size() > 0) {
150 Element recordingResult = report.addElement("recorded").addAttribute("result", result.toString());
152 for (Program program : _recordings.get(result)) {
153 recordingResult.add(program.asXml());
159 if ( _interestingShows.size() > 0 ) {
160 Element interesting = report.addElement("interesting");
161 for (String category: _interestingShows.keySet()) {
162 Element categoryElem = interesting;
163 if ( category.length() > 0 ) {
164 categoryElem = interesting.addElement("category");
165 categoryElem.addAttribute("name", category);
167 for (Program program: _interestingShows.get(category)) {
168 categoryElem.add(program.asXml());