(no commit message)
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / Program.java
index 83531ff75100d4040f2c1572d9f44cb28ee84247..89c3c88ce1704612eda0dc5044abac95474f6349 100644 (file)
@@ -16,6 +16,8 @@
 
 package org.wamblee.crawler.kiss;
 
+import java.util.Comparator;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.wamblee.crawler.Action;
@@ -27,6 +29,28 @@ import org.wamblee.crawler.PageException;
  */
 public class Program {
     
+    /**
+     * Lexicographical comparison of programs based on (time, title, channel). 
+     *
+     */
+    public static class TimeSorter implements Comparator<Program> { 
+     
+        /* (non-Javadoc)
+         * @see java.util.Comparator#compare(T, T)
+         */
+        public int compare(Program o1, Program o2) { 
+            int value = o1.getInterval().getBegin().compareTo(o2.getInterval().getBegin());
+            if ( value != 0 ) { 
+                return value; 
+            }
+            value = o1.getName().compareTo(o2.getName()); 
+            if (value != 0 ) { 
+                return value; 
+            }
+            return o1.getChannel().compareTo(o2.getChannel());
+        }
+    }
+    
     private static final Log LOG = LogFactory.getLog(Program.class);
 
     /**
@@ -161,6 +185,22 @@ public class Program {
     public TimeInterval getInterval() {
         return _interval;
     }
+    
+    /**
+     * Checks if recording is possible. 
+     * @return True iff recording is possible. 
+     */
+    public boolean isRecordingPossible() { 
+        try {
+            Action record = _programInfo.execute().getAction(RECORD_ACTION);
+            if (record == null) {
+                return false; 
+            }
+            return true; 
+        } catch (PageException e) {
+            return false; 
+        }
+    }
 
     /**
      * Records the show.
@@ -168,15 +208,23 @@ public class Program {
      * @return Status describing the result of recording.
      */
     public RecordingResult record() {
+        LOG.info("Recording " + this);
+        if ( SystemProperties.isRecordDisabled() ) { 
+            return RecordingResult.OK;
+        }
         try {
             Action record = _programInfo.execute().getAction(RECORD_ACTION);
             if (record == null) {
+                LOG.info("  result: " + RecordingResult.OLDSHOW);
                 return RecordingResult.OLDSHOW;
             }
             Page result = record.execute();
-            return RecordingResult.valueOf(result.getContent().getText());
+            RecordingResult recordingResult = RecordingResult.valueOf(result.getContent().getText());
+            LOG.info("  result: " + recordingResult);
+            return recordingResult;
         } catch (PageException e) {
             LOG.warn("Technical problem recording program: '" + this + "'", e);
+            LOG.info("  result: " + RecordingResult.ERROR);
             return RecordingResult.ERROR;
         }
     }
@@ -202,4 +250,17 @@ public class Program {
                 + ")" + "\n"
                 + (INDENT + _description).replaceAll("\n", "\n" + INDENT);
     }
+    
+    /* (non-Javadoc)
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    @Override
+    public boolean equals(Object obj) {
+        if ( !(obj instanceof Program)) { 
+            return false; 
+        }
+        Program program = (Program)obj; 
+        return getName().equals(program.getName()) && 
+               _programInfo.equals(program._programInfo);
+    }
 }