more robustness, now a detailed report is always sent, also if crawling
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / main / ProgramActionExecutor.java
index 51aef60f1a2148f58300f1c085b642e4028a322f..c19b6473b04b16e829f0b30a6647d5938f6beb5c 100644 (file)
@@ -16,7 +16,6 @@
 
 package org.wamblee.crawler.kiss.main;
 
-import java.util.EnumMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
@@ -25,8 +24,6 @@ import java.util.TreeSet;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.dom4j.DocumentFactory;
-import org.dom4j.Element;
 import org.wamblee.crawler.kiss.guide.Program;
 import org.wamblee.crawler.kiss.guide.TimeInterval;
 import org.wamblee.crawler.kiss.guide.Program.RecordingResult;
@@ -41,35 +38,23 @@ public class ProgramActionExecutor {
     private static final Log LOG = LogFactory
             .getLog(ProgramActionExecutor.class);
 
-    /**
-     * A map of category name to a set of program. Useful for displaying the
-     * output of possibly interesting programs on a per category basis.
-     */
-    private Map<String, Set<Program>> _interestingShows;
-
     /**
      * Map of priority to set of programs.
      */
     private Map<Integer, Set<Program>> _showsToRecord;
-
+    
     /**
-     * Map or recording result to a set of programs.
+     * Report to use. 
      */
-    private EnumMap<RecordingResult, Set<Program>> _recordings;
+    private Report _report; 
 
     /**
      * Constructs the program action executor.
      * 
      */
-    public ProgramActionExecutor() {
-        _interestingShows = new TreeMap<String, Set<Program>>();
+    public ProgramActionExecutor(Report aReport) {
         _showsToRecord = new TreeMap<Integer, Set<Program>>();
-        _recordings = new EnumMap<RecordingResult, Set<Program>>(
-                RecordingResult.class);
-        for (RecordingResult result : RecordingResult.values()) {
-            _recordings.put(result, new TreeSet<Program>(
-                    new Program.TimeSorter()));
-        }
+        _report = aReport;
     }
 
     /**
@@ -82,12 +67,12 @@ public class ProgramActionExecutor {
      */
     public void recordProgram(int aPriority, Program aProgram) {
         LOG.info("priority = " + aPriority + ", program: " + aProgram);
-        // Putting -priority into the set makes sure that iteration order 
-        // over the priorities will go from higher priority to lower priority. 
+        // Putting -priority into the set makes sure that iteration order
+        // over the priorities will go from higher priority to lower priority.
         Set<Program> programs = _showsToRecord.get(-aPriority);
         if (programs == null) {
             programs = new TreeSet<Program>(new Program.TimeSorter());
-            _showsToRecord.put(-aPriority, programs); 
+            _showsToRecord.put(-aPriority, programs);
         }
         programs.add(aProgram);
     }
@@ -102,12 +87,7 @@ public class ProgramActionExecutor {
      */
     public void interestingProgram(String aCategory, Program aProgram) {
         LOG.info("category = '" + aCategory + "', program: " + aProgram);
-        Set<Program> programs = _interestingShows.get(aCategory);
-        if (programs == null) {
-            programs = new TreeSet<Program>(new Program.TimeSorter());
-            _interestingShows.put(aCategory, programs);
-        }
-        programs.add(aProgram);
+        _report.interestingProgram(aCategory, aProgram);
     }
 
     /**
@@ -117,80 +97,34 @@ public class ProgramActionExecutor {
         Set<TimeInterval> previouslyRecorded = new HashSet<TimeInterval>();
         for (Integer priority : _showsToRecord.keySet()) {
             for (Program program : _showsToRecord.get(priority)) {
-                TimeInterval interval = program.getInterval(); 
-                if ( recordingConflictExists(previouslyRecorded, interval)) { 
-                    _recordings.get(RecordingResult.CONFLICT).add(program);
+                TimeInterval interval = program.getInterval();
+                if (recordingConflictExists(previouslyRecorded, interval)) {
+                    _report.setRecordingResult(RecordingResult.CONFLICT, program);
                 } else {
                     RecordingResult result = program.record();
-                    _recordings.get(result).add(program);
+                    _report.setRecordingResult(result, program);
                     previouslyRecorded.add(interval);
                 }
             }
         }
     }
-    
-    /**
-     * Checks an interval for overlap with a previously recorded program. 
-     * @param aPreviouslyRecorded Previously recorded programs. 
-     * @param interval Interval. 
-     * @return True iff there is a recording conflict.
-     */
-    private boolean recordingConflictExists(Set<TimeInterval> aPreviouslyRecorded, TimeInterval interval) { 
-        for (TimeInterval recordedInterval: aPreviouslyRecorded ) { 
-            if ( interval.overlap(recordedInterval)) {
-                return true; 
-            }
-        }
-        return false; 
-    }
 
     /**
-     * Get report as XML.
+     * Checks an interval for overlap with a previously recorded program.
      * 
-     * @return XML report
+     * @param aPreviouslyRecorded
+     *            Previously recorded programs.
+     * @param aInterval
+     *            Interval.
+     * @return True iff there is a recording conflict.
      */
-    public Element getReport() {
-        DocumentFactory factory = DocumentFactory.getInstance();
-        Element report = factory.createElement("report");
-        
-        Set<Program> reportedPrograms = new HashSet<Program>();
-
-        for (RecordingResult result : RecordingResult.values()) {
-            if (_recordings.get(result).size() > 0) {
-                Element recordingResult = report.addElement("recorded")
-                        .addAttribute("result", result.toString());
-
-                for (Program program : _recordings.get(result)) {
-                    recordingResult.add(program.asXml());
-                    reportedPrograms.add(program);
-                }
-            }
-        }
-
-        if (_interestingShows.size() > 0) {
-            Element interesting = report.addElement("interesting");
-            for (String category : _interestingShows.keySet()) {
-                Element categoryElem = interesting;
-                if (category.length() > 0) {
-                    categoryElem = interesting.addElement("category");
-                    categoryElem.addAttribute("name", category);
-                }
-                for (Program program : _interestingShows.get(category)) {
-                    if ( !reportedPrograms.contains(program)) {
-                        categoryElem.add(program.asXml());
-                    } else { 
-                        LOG.info("Category '" + category + "', program " + program + " already reported");
-                    }
-                }
-                if ( categoryElem.elements().size() == 0 ) {
-                    // Remove empty category element. 
-                    LOG.info("Removing element for category '" + category + "'");
-                    interesting.remove(categoryElem);
-                }
+    private boolean recordingConflictExists(
+            Set<TimeInterval> aPreviouslyRecorded, TimeInterval aInterval) {
+        for (TimeInterval recordedInterval : aPreviouslyRecorded) {
+            if (aInterval.overlap(recordedInterval)) {
+                return true;
             }
-
         }
-
-        return report;
+        return false;
     }
 }