a3d75fea0bc0b4d72177031825f4387516bb8e05
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / main / Report.java
1 /*
2  * 
3  * Copyright 2005 the original author or authors.
4  * 
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.wamblee.crawler.kiss.main;
19
20 import java.util.ArrayList;
21 import java.util.EnumMap;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.TreeMap;
27 import java.util.TreeSet;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.dom4j.DocumentFactory;
32 import org.dom4j.Element;
33 import org.wamblee.crawler.kiss.guide.Program;
34 import org.wamblee.crawler.kiss.guide.Program.RecordingResult;
35
36 /**
37  * Represents a report on the actions of the crawler.
38  */
39 public class Report {
40
41     private static final Log LOG = LogFactory
42             .getLog(Report.class);
43
44     /**
45      * A map of category name to a set of program. Useful for displaying the
46      * output of possibly interesting programs on a per category basis.
47      */
48     private Map<String, Set<Program>> _interestingShows;
49
50     /**
51      * Map or recording result to a set of programs.
52      */
53     private EnumMap<RecordingResult, Set<Program>> _recordings;
54
55     /**
56      * Messages generated while doing all the work.
57      */
58     private List<String> _messages;
59
60     /**
61      * Constructs the report.
62      * 
63      */
64     public Report() {
65         _interestingShows = new TreeMap<String, Set<Program>>();
66         _recordings = new EnumMap<RecordingResult, Set<Program>>(
67                 RecordingResult.class);
68         for (RecordingResult result : RecordingResult.values()) {
69             _recordings.put(result, new TreeSet<Program>(
70                     new Program.TimeComparator()));
71         }
72         _messages = new ArrayList<String>();
73     }
74
75     /**
76      * Adds a message.
77      * 
78      * @param aMessage
79      *            Message to add.
80      */
81     public void addMessage(String aMessage) {
82         _messages.add(aMessage);
83     }
84     
85     /**
86      * Adds a message.
87      * 
88      * @param aMessage
89      *            Message to add.
90      * @param aException Exception that caused the problem.
91      */
92     public void addMessage(String aMessage, Exception aException) {
93         String msg = aMessage; 
94         for (Throwable e = aException; e != null; e = e.getCause()) {
95             msg += ": " + e.getMessage(); 
96         }
97         addMessage(msg);
98     }
99
100     /**
101      * Called to indicate that a program is interesting. 
102      * 
103      * @param aCategory
104      *            Category of the program.
105      * @param aProgram
106      *            Program.
107      */
108     public void interestingProgram(String aCategory, Program aProgram) {
109         LOG.info("category = '" + aCategory + "', program: " + aProgram);
110         Set<Program> programs = _interestingShows.get(aCategory);
111         if (programs == null) {
112             programs = new TreeSet<Program>(new Program.TimeComparator());
113             _interestingShows.put(aCategory, programs);
114         }
115         programs.add(aProgram);
116     }
117     
118     /**
119      * Called to specify the result of recording a program. 
120      * @param aResult Result. 
121      * @param aProgram Program. 
122      */
123     public void setRecordingResult(RecordingResult aResult, Program aProgram) { 
124         _recordings.get(aResult).add(aProgram);   
125     }
126
127   
128     /**
129      * Get report as XML.
130      * 
131      * @return XML report
132      */
133     public Element asXml() {
134         DocumentFactory factory = DocumentFactory.getInstance();
135         Element report = factory.createElement("report");
136
137         if (_messages.size() > 0) {
138             Element messages = report.addElement("messages");
139             for (String message : _messages) {
140                 messages.addElement("message").setText(message);
141             }
142         }
143
144         Set<Program> reportedPrograms = new HashSet<Program>();
145
146         for (RecordingResult result : RecordingResult.values()) {
147             if (_recordings.get(result).size() > 0) {
148                 Element recordingResult = report.addElement("recorded")
149                         .addAttribute("result", result.toString());
150
151                 for (Program program : _recordings.get(result)) {
152                     recordingResult.add(program.asXml());
153                     reportedPrograms.add(program);
154                 }
155             }
156         }
157
158         if (_interestingShows.size() > 0) {
159             Element interesting = report.addElement("interesting");
160             for (String category : _interestingShows.keySet()) {
161                 Element categoryElem = interesting;
162                 if (category.length() > 0) {
163                     categoryElem = interesting.addElement("category");
164                     categoryElem.addAttribute("name", category);
165                 }
166                 for (Program program : _interestingShows.get(category)) {
167                     if (!reportedPrograms.contains(program)) {
168                         categoryElem.add(program.asXml());
169                     } else {
170                         LOG.info("Category '" + category + "', program "
171                                 + program + " already reported");
172                     }
173                 }
174                 if (categoryElem.elements().size() == 0) {
175                     // Remove empty category element.
176                     LOG
177                             .info("Removing element for category '" + category
178                                     + "'");
179                     interesting.remove(categoryElem);
180                 }
181             }
182
183         }
184
185         return report;
186     }
187 }