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.wamblee.crawler.kiss.Program.RecordingResult;
28 * Provides execution of actions for programs. Actions use
29 * this class to tell the executor what to do. The executor then decide
30 * on exactly what to do and in what order and makes decisions in case
33 public class ProgramActionExecutor {
36 * A map of category name to a set of program. Useful for displaying the output of
37 * possibly interesting programs on a per category basis.
39 private Map<String, Set<Program>> _interestingShows;
42 * Set of programs to record.
44 private Set<Program> _showsToRecord;
47 * Map or recording result to a set of programs.
49 private EnumMap<RecordingResult, Set<Program>> _recordings;
52 * Constructs the program action executor.
55 public ProgramActionExecutor() {
56 _interestingShows = new TreeMap<String,Set<Program>>();
57 _showsToRecord = new TreeSet<Program>(new Program.TimeSorter());
58 _recordings = new EnumMap<RecordingResult, Set<Program>>(
59 RecordingResult.class);
60 for (RecordingResult result : RecordingResult.values()) {
61 _recordings.put(result, new TreeSet<Program>(new Program.TimeSorter()));
66 * Called by an action to indicate the desire to record a program.
67 * @param aPriority Priority of the program. Used to resolve conflicts.
68 * @param aProgram Program to record.
70 public void recordProgram(int aPriority, Program aProgram) {
71 _showsToRecord.add(aProgram);
75 * Called by an action to indicate that a program is interesting.
76 * @param aCategory Category of the program.
77 * @param aProgram Program.
79 public void interestingProgram(String aCategory, Program aProgram) {
80 Set<Program> programs = _interestingShows.get(aCategory);
81 if ( programs == null ) {
82 programs = new TreeSet<Program>(new Program.TimeSorter());
83 _interestingShows.put(aCategory, programs);
85 programs.add(aProgram);
89 * Makes sure that the actions are performed.
92 public void commit() {
93 for (Program program: _showsToRecord) {
94 RecordingResult result = program.record();
95 _recordings.get(result).add(program);
100 * Gets the report describing what was done.
103 public String getReport() {
104 StringBuffer msg = new StringBuffer("Summary of KiSS crawler: \n\n\n");
106 boolean printed = false;
108 for (RecordingResult result : RecordingResult.values()) {
109 if (_recordings.get(result).size() > 0) {
110 msg.append(result.getDescription() + "\n\n");
111 for (Program program : _recordings.get(result)) {
112 msg.append(program + "\n\n");
118 if ( _interestingShows.size() > 0 ) {
119 msg.append("Possibly interesting shows:\n\n");
120 for (String category: _interestingShows.keySet()) {
121 if ( category.length() > 0 ) {
122 msg.append("Category: " + category + "\n\n");
124 for (Program program: _interestingShows.get(category)) {
125 msg.append(program + "\n\n");
132 msg.append("No suitable programs found");
135 return msg.toString();