X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=crawler%2Fkiss%2Fsrc%2Forg%2Fwamblee%2Fcrawler%2Fkiss%2Fmain%2FProgramActionExecutor.java;h=51aef60f1a2148f58300f1c085b642e4028a322f;hb=e1aafb0930f726a00368ce3468a48193d0fb6fac;hp=b9c103b7a7a46bcbef9e723e4a69f48b94515cf6;hpb=0119c69858055c84592338a202e9d1b18510a29b;p=utils diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramActionExecutor.java b/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramActionExecutor.java index b9c103b7..51aef60f 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramActionExecutor.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramActionExecutor.java @@ -17,14 +17,18 @@ package org.wamblee.crawler.kiss.main; import java.util.EnumMap; +import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.TreeMap; 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; /** @@ -34,6 +38,9 @@ import org.wamblee.crawler.kiss.guide.Program.RecordingResult; */ 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. @@ -41,9 +48,9 @@ public class ProgramActionExecutor { private Map> _interestingShows; /** - * Set of programs to record. + * Map of priority to set of programs. */ - private Set _showsToRecord; + private Map> _showsToRecord; /** * Map or recording result to a set of programs. @@ -56,7 +63,7 @@ public class ProgramActionExecutor { */ public ProgramActionExecutor() { _interestingShows = new TreeMap>(); - _showsToRecord = new TreeSet(new Program.TimeSorter()); + _showsToRecord = new TreeMap>(); _recordings = new EnumMap>( RecordingResult.class); for (RecordingResult result : RecordingResult.values()) { @@ -74,7 +81,15 @@ public class ProgramActionExecutor { * Program to record. */ public void recordProgram(int aPriority, Program aProgram) { - _showsToRecord.add(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. + Set programs = _showsToRecord.get(-aPriority); + if (programs == null) { + programs = new TreeSet(new Program.TimeSorter()); + _showsToRecord.put(-aPriority, programs); + } + programs.add(aProgram); } /** @@ -86,6 +101,7 @@ public class ProgramActionExecutor { * Program. */ public void interestingProgram(String aCategory, Program aProgram) { + LOG.info("category = '" + aCategory + "', program: " + aProgram); Set programs = _interestingShows.get(aCategory); if (programs == null) { programs = new TreeSet(new Program.TimeSorter()); @@ -96,14 +112,37 @@ public class ProgramActionExecutor { /** * Makes sure that the actions are performed. - * */ public void commit() { - for (Program program : _showsToRecord) { - RecordingResult result = program.record(); - _recordings.get(result).add(program); + Set previouslyRecorded = new HashSet(); + 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); + } else { + RecordingResult result = program.record(); + _recordings.get(result).add(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 aPreviouslyRecorded, TimeInterval interval) { + for (TimeInterval recordedInterval: aPreviouslyRecorded ) { + if ( interval.overlap(recordedInterval)) { + return true; + } + } + return false; + } /** * Get report as XML. @@ -113,6 +152,8 @@ public class ProgramActionExecutor { public Element getReport() { DocumentFactory factory = DocumentFactory.getInstance(); Element report = factory.createElement("report"); + + Set reportedPrograms = new HashSet(); for (RecordingResult result : RecordingResult.values()) { if (_recordings.get(result).size() > 0) { @@ -121,6 +162,7 @@ public class ProgramActionExecutor { for (Program program : _recordings.get(result)) { recordingResult.add(program.asXml()); + reportedPrograms.add(program); } } } @@ -134,7 +176,16 @@ public class ProgramActionExecutor { categoryElem.addAttribute("name", category); } for (Program program : _interestingShows.get(category)) { - categoryElem.add(program.asXml()); + 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); } }