Implement remaining getChanges() methods, simplify activesync driver
authorMichael J. Rubinsky <mrubinsk@horde.org>
Fri, 24 Dec 2010 20:25:39 +0000 (15:25 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Fri, 24 Dec 2010 20:25:39 +0000 (15:25 -0500)
framework/Core/lib/Horde/Core/ActiveSync/Connector.php
framework/Core/lib/Horde/Core/ActiveSync/Driver.php
kronolith/lib/Api.php
nag/lib/Api.php
turba/lib/Api.php

index 7464200..1c6cb8a 100644 (file)
@@ -75,26 +75,6 @@ class Horde_Core_ActiveSync_Connector
     }
 
     /**
-     * Obtain all calendar server chages that occured in the specified time
-     * interval.
-     *
-     * @param integer $from_ts  Starting timestamp
-     * @param integer $to_ts    Ending timestamp
-     *
-     * @return array  Hash of add, modify, and delete arrays
-     */
-    public function calendar_getChanges($from_ts, $to_ts)
-    {
-        try {
-            return $this->_registry->calendar->getChanges($from_ts, $to_ts);
-        } catch (Exception $e) {
-            return array('add' => array(),
-                         'modify' => array(),
-                         'delete' => array());
-        }
-    }
-
-    /**
      * Export the specified event as an ActiveSync message
      *
      * @param string $uid          The calendar id
@@ -392,4 +372,27 @@ class Horde_Core_ActiveSync_Connector
         return $this->_registry->hasInterface($api);
     }
 
+    /**
+     * Get all server changes for the specified collection
+     * @param string $collection  The collection type (calendar, contacts, tasks)
+     * @param integer $from_ts    Starting timestamp
+     * @param integer $to_ts      Ending timestamp
+     *
+     * @return array  A hash of add, modify, and delete uids
+     * @throws InvalidArgumentException
+     */
+    public function getChanges($collection, $from_ts, $to_ts)
+    {
+        if (!in_array($collection, array('calendar', 'contacts', 'tasks'))) {
+            throw new InvalidArgumentException('collection must be one of calendar, contacts, or tasks');
+        }
+        try {
+            return $this->_registry->{$collection}->getChanges($from_ts, $to_ts);
+        } catch (Exception $e) {
+            return array('add' => array(),
+                         'modify' => array(),
+                         'delete' => array());
+        }
+    }
+
 }
\ No newline at end of file
index ddfb3e9..77c62c3 100644 (file)
@@ -219,9 +219,9 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base
     public function getMessageList($folderid, $cutoffdate)
     {
         $this->_logger->debug('Horde::getMessageList(' . $folderid . ', ' . $cutoffdate . ')');
+
         ob_start();
         $messages = array();
-
         switch ($folderid) {
         case self::APPOINTMENTS_FOLDER:
             $startstamp = (int)$cutoffdate;
@@ -270,8 +270,8 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base
             $this->_endBuffer();
             return array();
         }
-
         $this->_endBuffer();
+
         return $messages;
     }
 
@@ -279,9 +279,10 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base
      * Get a list of server changes that occured during the specified time
      * period.
      *
-     * @param string $folderId  The server id of the collection to check.
-     * @param integer $from_ts  The starting timestamp
-     * @param integer $to_ts    The ending timestamp
+     * @param string $folderId     The server id of the collection to check.
+     * @param integer $from_ts     The starting timestamp
+     * @param integer $to_ts       The ending timestamp
+     * @param integer $cutoffdate  The earliest date to retrieve back to
      *
      * @return array A list of messge uids that have chnaged in the specified
      *               time period.
@@ -289,8 +290,8 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base
     public function getServerChanges($folderId, $from_ts, $to_ts, $cutoffdate)
     {
         $this->_logger->debug("Horde_ActiveSync_Driver_Horde::getServerChanges($folderId, $from_ts, $to_ts, $cutoffdate)");
-        $adds = array();
 
+        $changes = array();
         ob_start();
         switch ($folderId) {
         case self::APPOINTMENTS_FOLDER:
@@ -299,20 +300,15 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base
                 $startstamp = (int)$cutoffdate;
                 $endstamp = time() + 32140800; //60 * 60 * 24 * 31 * 12 == one year
                 try {
-                    $adds = $this->_connector->calendar_listUids($startstamp, $endstamp);
+                    $changes['add'] = $this->_connector->calendar_listUids($startstamp, $endstamp);
                 } catch (Horde_Exception $e) {
                     $this->_logger->err($e->getMessage());
                     $this->_endBuffer();
                     return array();
                 }
-                $edits = $deletes = array();
             } else {
                 try {
-                      $changes = $this->_connector->calendar_getChanges($from_ts, $to_ts);
-                      // @TODO: these assignments are just until all collections are refactored.
-                      $adds = $changes['add'];
-                      $edits = $changes['modify'];
-                      $deletes = $changes['delete'];
+                    $changes = $this->_connector->getChanges('calendar', $from_ts, $to_ts);
                 } catch (Horde_Exception $e) {
                     $this->_logger->err($e->getMessage());
                     $this->_endBuffer();
@@ -320,11 +316,12 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base
                 }
             }
             break;
+
         case self::CONTACTS_FOLDER:
             /* Can't use History for first sync */
             if ($from_ts == 0) {
                 try {
-                    $adds = $this->_connector->contacts_listUids();
+                    $changes['add'] = $this->_connector->contacts_listUids();
                 } catch (Horde_Exception $e) {
                     $this->_logger->err($e->getMessage());
                     $this->_endBuffer();
@@ -333,9 +330,7 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base
                 $edits = $deletes = array();
             } else {
                 try {
-                    $adds = $this->_connector->contacts_listBy('add', $from_ts, $to_ts);
-                    $edits = $this->_connector->contacts_listBy('modify', $from_ts, $to_ts);
-                    $deletes = $this->_connector->contacts_listBy('delete', $from_ts, $to_ts);
+                    $changes = $this->_connector->getChanges('contacts', $from_ts, $to_ts);
                 } catch (Horde_Exception $e) {
                     $this->_logger->err($e->getMessage());
                     $this->_endBuffer();
@@ -343,22 +338,20 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base
                 }
             }
             break;
+
         case self::TASKS_FOLDER:
             /* Can't use History for first sync */
             if ($from_ts == 0) {
                 try {
-                    $adds = $this->_connector->tasks_listUids();
+                    $changes['add'] = $this->_connector->tasks_listUids();
                 } catch (Horde_Exception $e) {
                     $this->_logger->err($e->getMessage());
                     $this->_endBuffer();
                     return array();
                 }
-                $edits = $deletes = array();
             } else {
                 try {
-                    $adds = $this->_connector->tasks_listBy('add', $from_ts, $to_ts);
-                    $edits = $this->_connector->tasks_listBy('modify', $from_ts, $to_ts);
-                    $deletes = $this->_connector->tasks_listBy('delete', $from_ts, $to_ts);
+                    $changes = $this->_connector->getChanges('tasks', $from_ts, $to_ts);
                 } catch (Horde_Exception $e) {
                     $this->_logger->err($e->getMessage());
                     $this->_endBuffer();
@@ -368,33 +361,32 @@ class Horde_Core_ActiveSync_Driver extends Horde_ActiveSync_Driver_Base
             break;
         }
 
-        /* Build the changes array */
-        $changes = array();
+        $results = array();
 
         /* Server additions */
-        foreach ($adds as $add) {
-            $changes[] = array(
+        foreach ($changes['add'] as $add) {
+            $results[] = array(
                 'id' => $add,
                 'type' => 'change',
                 'flags' => Horde_ActiveSync::FLAG_NEWMESSAGE);
         }
 
         /* Server changes */
-        foreach ($edits as $change) {
-            $changes[] = array(
+        foreach ($changes['modify'] as $change) {
+            $results[] = array(
                 'id' => $change,
                 'type' => 'change');
         }
 
         /* Server Deletions */
-        foreach ($deletes as $deleted) {
-            $changes[] = array(
+        foreach ($changes['delete'] as $deleted) {
+            $results[] = array(
                 'id' => $deleted,
                 'type' => 'delete');
         }
-
         $this->_endBuffer();
-        return $changes;
+        
+        return $results;
     }
 
     /**
index 7afd943..3959579 100644 (file)
@@ -537,7 +537,6 @@ class Kronolith_Api extends Horde_Registry_Api
             throw new Horde_Exception_PermissionDenied();
         }
 
-        /* Need to get changes, then ensure they are not from an exception */
         $changes = array('add' => array(),
                          'modify' => array(),
                          'delete' => array());
index 5c9ee86..03c112d 100644 (file)
@@ -710,6 +710,23 @@ class Nag_Api extends Horde_Registry_Api
     }
 
     /**
+     * Method for obtaining all server changes between two timestamps. Basically
+     * a wrapper around listBy(), but returns an array containing all adds,
+     * edits and deletions.
+     *
+     * @param integer $start             The starting timestamp
+     * @param integer $end               The ending timestamp.
+     *
+     * @return array  An hash with 'add', 'modify' and 'delete' arrays.
+     */
+    public function getChanges($start, $end)
+    {
+        return array('add' => $this->listBy('add', $start, null, $end),
+                     'modify' => $this->listBy('modify', $start, null, $end),
+                     'delete' => $this->listBy('delete', $start, null, $end));
+    }
+
+    /**
      * Returns the timestamp of an operation for a given uid an action.
      *
      * @param string $uid      The uid to look for.
index 9a669fb..de667c3 100644 (file)
@@ -535,6 +535,23 @@ class Turba_Api extends Horde_Registry_Api
     }
 
     /**
+     * Method for obtaining all server changes between two timestamps. Basically
+     * a wrapper around listBy(), but returns an array containing all adds,
+     * edits and deletions.
+     *
+     * @param integer $start             The starting timestamp
+     * @param integer $end               The ending timestamp.
+     *
+     * @return array  An hash with 'add', 'modify' and 'delete' arrays.
+     */
+    public function getChanges($start, $end)
+    {
+        return array('add' => $this->listBy('add', $start, null, $end),
+                     'modify' => $this->listBy('modify', $start, null, $end),
+                     'delete' => $this->listBy('delete', $start, null, $end));
+    }
+
+    /**
      * Returns the timestamp of an operation for a given uid an action.
      *
      * @param string $uid            The uid to look for.