DaoUtils.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Nickolay G. Korolyov                            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: DaoUtils.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class DaoUtils extends StaticFactory
00017     {
00018         private static $nullValue   = 0;
00019         
00020         /* void */ public static function swap(
00021             DAOConnected $first,
00022             DAOConnected $second,
00023             $property = 'position'
00024         )
00025         {
00026             Assert::isTrue(
00027                 get_class($first) === get_class($second)
00028             );
00029             
00030             $setMethod = 'set'.ucfirst($property);
00031             $getMethod = 'get'.ucfirst($property);
00032             
00033             Assert::isTrue(
00034                 method_exists($first, $setMethod)
00035                 && method_exists($first, $getMethod)
00036             );
00037             
00038             $dao = $first->dao();
00039             $db = DBPool::me()->getByDao($dao);
00040 
00041             $oldPosition = $first->$getMethod();
00042             $newPosition = $second->$getMethod();
00043             
00044             $db->begin();
00045 
00046             $e = null;
00047             
00048             try {
00049                 $dao->save(
00050                     $first->$setMethod(self::$nullValue)
00051                 );
00052 
00053                 $dao->save(
00054                     $second->$setMethod($oldPosition)
00055                 );
00056                 
00057                 $dao->save(
00058                     $first->$setMethod($newPosition)
00059                 );
00060 
00061                 $db->commit();
00062             } catch (DatabaseException $e) {
00063                 $db->rollback();
00064             }
00065             
00066             $dao->
00067                 uncacheByIds(
00068                     array(
00069                         $first->getId(), $second->getId()
00070                     )
00071                 );
00072             
00073             if ($e)
00074                 throw $e;
00075         }
00076         
00077         /* void */ public static function setNullValue($nullValue)
00078         {
00079             self::$nullValue = $nullValue;
00080         }
00081     }
00082 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4