checkstyle and checkdoc are now ok.
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / main / ProgramActionExecutor.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16
17 package org.wamblee.crawler.kiss.main;
18
19 import java.util.EnumMap;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.TreeMap;
23 import java.util.TreeSet;
24
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;
29
30 /**
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.
34  */
35 public class ProgramActionExecutor {
36
37     /**
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.
40      */
41     private Map<String, Set<Program>> _interestingShows;
42
43     /**
44      * Set of programs to record.
45      */
46     private Set<Program> _showsToRecord;
47
48     /**
49      * Map or recording result to a set of programs.
50      */
51     private EnumMap<RecordingResult, Set<Program>> _recordings;
52
53     /**
54      * Constructs the program action executor.
55      * 
56      */
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()));
65         }
66     }
67
68     /**
69      * Called by an action to indicate the desire to record a program.
70      * 
71      * @param aPriority
72      *            Priority of the program. Used to resolve conflicts.
73      * @param aProgram
74      *            Program to record.
75      */
76     public void recordProgram(int aPriority, Program aProgram) {
77         _showsToRecord.add(aProgram);
78     }
79
80     /**
81      * Called by an action to indicate that a program is interesting.
82      * 
83      * @param aCategory
84      *            Category of the program.
85      * @param aProgram
86      *            Program.
87      */
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);
93         }
94         programs.add(aProgram);
95     }
96
97     /**
98      * Makes sure that the actions are performed.
99      * 
100      */
101     public void commit() {
102         for (Program program : _showsToRecord) {
103             RecordingResult result = program.record();
104             _recordings.get(result).add(program);
105         }
106     }
107
108     /**
109      * Get report as XML.
110      * 
111      * @return XML report
112      */
113     public Element getReport() {
114         DocumentFactory factory = DocumentFactory.getInstance();
115         Element report = factory.createElement("report");
116
117         for (RecordingResult result : RecordingResult.values()) {
118             if (_recordings.get(result).size() > 0) {
119                 Element recordingResult = report.addElement("recorded")
120                         .addAttribute("result", result.toString());
121
122                 for (Program program : _recordings.get(result)) {
123                     recordingResult.add(program.asXml());
124                 }
125             }
126         }
127
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);
135                 }
136                 for (Program program : _interestingShows.get(category)) {
137                     categoryElem.add(program.asXml());
138                 }
139             }
140
141         }
142
143         return report;
144     }
145 }