(no commit message)
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / Program.java
index 7cb86cfdeecea92ccb66b2f2a3a4eae7b5e485a8..c80fea246badc9c8cfe19c2e9b0addb78e1ea392 100644 (file)
@@ -20,6 +20,8 @@ import java.util.Comparator;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.dom4j.DocumentFactory;
+import org.dom4j.Element;
 import org.wamblee.crawler.Action;
 import org.wamblee.crawler.Page;
 import org.wamblee.crawler.PageException;
@@ -29,13 +31,25 @@ 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) { 
-            return o1.getInterval().getBegin().compareTo(o2.getInterval().getBegin());
+            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());
         }
     }
     
@@ -196,15 +210,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;
         }
     }
@@ -243,4 +265,21 @@ public class Program {
         return getName().equals(program.getName()) && 
                _programInfo.equals(program._programInfo);
     }
+    
+    /**
+     * Converts program information to XML. 
+     * @return XML representation of program information.  
+     */
+    public Element asXml() { 
+        DocumentFactory factory = DocumentFactory.getInstance();
+        Element program = factory.createElement("program"); 
+        program.addElement("name").setText(getName());
+        program.addElement("description").setText(getDescription());
+        program.addElement("keywords").setText(getKeywords());
+        program.addElement("channel").setText(getChannel());
+        Element interval = program.addElement("interval");
+        interval.addElement("begin").setText(getInterval().getBegin().toString());
+        interval.addElement("end").setText(getInterval().getEnd().toString());
+        return program; 
+    }
 }