Added priority mechanism.
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Thu, 23 Mar 2006 20:18:45 +0000 (20:18 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Thu, 23 Mar 2006 20:18:45 +0000 (20:18 +0000)
Corrected stylesheets to deal with recording conflicts as well, this
status was missing.

crawler/basic/src/org/wamblee/crawler/impl/App.java
crawler/kiss/conf/kiss/programs.xml
crawler/kiss/conf/kiss/reportToHtml.xsl
crawler/kiss/conf/kiss/reportToText.xsl
crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramActionExecutor.java
crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramConfigurationParser.java
crawler/kiss/src/org/wamblee/crawler/kiss/main/RecordProgramAction.java

index c1af31b227a9f07bf4f5cb5664a3b0af262f15d0..d4ca4709c78a19665736f93cdd4d543582d6942f 100644 (file)
@@ -5,8 +5,6 @@ import java.io.FileInputStream;
 import java.io.InputStream;
 
 import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.dom4j.Element;
 import org.wamblee.crawler.Action;
 import org.wamblee.crawler.Configuration;
index 82de82faaa9c20f29ed35da6ebe59dc283a2b8f7..57f14cf61fb56f7533de028ed3f94fa875aeaf10 100644 (file)
   </program>
   
   <program>
+    <priority>10</priority>
     <match>battlestar</match>
   </program>
 
   <program>
+    <priority>10</priority>
     <match>star trek</match>
   </program>
   
index f3901ea3c1ce3d6d465e657ae4f6d10acd891de9..9017431d23a8778d9d8392c878b71da8bf6cf6c7 100644 (file)
@@ -44,6 +44,9 @@
                 <xsl:when test="@result = 'DUPLICATE'">
                     <xsl:text>Already recorded programs</xsl:text>
                 </xsl:when>
+                <xsl:when test="@result = 'CONFLICT'">
+                    <xsl:text>Conflicts with other recorded program</xsl:text>
+                </xsl:when>
                 <xsl:when test="@result='ERROR'">
                     <xsl:text>Programs that could not be recorded for
                     technical reasons.</xsl:text>
index e46e4328bbe3e10b7575d51ee06373e9d7b4ce32..9d85cfaa57342269eba2366432dce46f2179caf7 100644 (file)
         <xsl:value-of select="$newline"/>
         <xsl:value-of select="$newline"/>
       </xsl:when>
+      <xsl:when test="@result = 'CONFLICT'">
+        <xsl:text>Conflicts with other recorded program</xsl:text>
+        <xsl:value-of select="$newline"/>
+        <xsl:value-of select="$newline"/>
+      </xsl:when>
       <xsl:when test="@result='ERROR'">
         <xsl:text>Programs that could not be recorded for technical reasons.</xsl:text>
         <xsl:value-of select="$newline"/>
index 44132b5f5e88d42f93ee2d82f9f64f4bf2331aa8..4b2476d06dd2653deb38dca28f36e9d64aba2b2e 100644 (file)
@@ -17,6 +17,7 @@
 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;
@@ -27,6 +28,7 @@ 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;
 
 /**
@@ -35,8 +37,9 @@ import org.wamblee.crawler.kiss.guide.Program.RecordingResult;
  * in what order and makes decisions in case of conflicts.
  */
 public class ProgramActionExecutor {
-    
-    private static final Log LOG = LogFactory.getLog(ProgramActionExecutor.class);
+
+    private static final Log LOG = LogFactory
+            .getLog(ProgramActionExecutor.class);
 
     /**
      * A map of category name to a set of program. Useful for displaying the
@@ -45,9 +48,9 @@ public class ProgramActionExecutor {
     private Map<String, Set<Program>> _interestingShows;
 
     /**
-     * Set of programs to record.
+     * Map of priority to set of programs.
      */
-    private Set<Program> _showsToRecord;
+    private Map<Integer, Set<Program>> _showsToRecord;
 
     /**
      * Map or recording result to a set of programs.
@@ -60,7 +63,7 @@ public class ProgramActionExecutor {
      */
     public ProgramActionExecutor() {
         _interestingShows = new TreeMap<String, Set<Program>>();
-        _showsToRecord = new TreeSet<Program>(new Program.TimeSorter());
+        _showsToRecord = new TreeMap<Integer, Set<Program>>();
         _recordings = new EnumMap<RecordingResult, Set<Program>>(
                 RecordingResult.class);
         for (RecordingResult result : RecordingResult.values()) {
@@ -79,7 +82,14 @@ public class ProgramActionExecutor {
      */
     public void recordProgram(int aPriority, Program aProgram) {
         LOG.info("priority = " + aPriority + ", program: " + aProgram);
-        _showsToRecord.add(aProgram);
+        // 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); 
+        }
+        programs.add(aProgram);
     }
 
     /**
@@ -105,10 +115,34 @@ public class ProgramActionExecutor {
      * 
      */
     public void commit() {
-        for (Program program : _showsToRecord) {
-            RecordingResult result = program.record();
-            _recordings.get(result).add(program);
+        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);
+                } 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<TimeInterval> aPreviouslyRecorded, TimeInterval interval) { 
+        for (TimeInterval recordedInterval: aPreviouslyRecorded ) { 
+            if ( interval.overlap(recordedInterval)) {
+                return true; 
+            }
         }
+        return false; 
     }
 
     /**
index 103ec0702c6caea299ca6583e265797c6e7cc8ee..c16f4f93df34043fe9fde22297cff6cc81559944 100644 (file)
@@ -38,11 +38,9 @@ import org.wamblee.crawler.kiss.notification.Notifier;
  * Parse the configuration of desired programs.
  */
 class ProgramConfigurationParser {
-
-    /**
-     * 
-     */
     private static final int DEFAULT_SMTP_PORT = 25;
+    
+    private static final int DEFAULT_PRIORITY = 1; 
 
     private static final String ELEM_PASSWORD = "password";
 
@@ -74,6 +72,8 @@ class ProgramConfigurationParser {
     // Configuration of interesting programs.
 
     private static final String ELEM_PROGRAM = "program";
+    
+    private static final String ELEM_PRIORITY = "priority";
 
     private static final String ELEM_PATTERN = "match";
 
@@ -117,7 +117,12 @@ class ProgramConfigurationParser {
                 }
 
                 Element actionElem = program.element(ELEM_ACTION);
-                ProgramAction action = new RecordProgramAction(1);
+                int priority = DEFAULT_PRIORITY; 
+                String priorityString = program.elementTextTrim(ELEM_PRIORITY);
+                if ( priorityString != null ) { 
+                    priority = Integer.valueOf(priorityString);
+                }
+                ProgramAction action = new RecordProgramAction(priority);
                 if (actionElem != null) {
                     if (actionElem.getText().equals(ACTION_NOTIFY)) {
                         action = new InterestingProgramAction(category);
index 8458a957495a2f79a4d62f71175b94edc44ecce7..4cbe12bc91aabbfb05ba103af050f2e319ca19ac 100644 (file)
@@ -27,8 +27,8 @@ public class RecordProgramAction implements ProgramAction {
 
     /**
      * Constructs the action.
-     * @param aPriority Priority of the recording action. Lower values mean
-     *   higher priority.  
+     * @param aPriority Priority of the recording action. Higher values have higher
+     *   priority.
      */
     public RecordProgramAction(int aPriority) {
         _priority = aPriority;