DaoMoveHelper.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Dmitry E. Demidov                               *
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 
00015     final class DaoMoveHelper extends StaticFactory
00016     {
00017         private static $nullValue   = 0;
00018         private static $property    = 'position';
00019         
00020         /* void */ public static function setNullValue($nullValue)
00021         {
00022             self::$nullValue = $nullValue;
00023         }
00024         
00025         /* void */ public static function setProperty($property)
00026         {
00027             self::$property = $property;
00028         }
00029         
00030         /* void */ public static function up(
00031             DAOConnected $object,
00032             LogicalObject $exp = null
00033         )
00034         {
00035             $getMethod = 'get'.ucfirst(self::$property);
00036             
00037             Assert::isTrue(
00038                 method_exists($object, $getMethod)
00039             );
00040             
00041             $criteria =
00042                 Criteria::create($object->dao())->
00043                 addOrder(
00044                     OrderBy::create(self::$property)->
00045                     desc()
00046                 )->
00047                 setLimit(1);
00048             
00049             if ($exp)
00050                 $criteria->add($exp);
00051             
00052             $oldPosition = $object->$getMethod();
00053             
00054             $criteria->add(
00055                 Expression::lt(
00056                     self::$property,
00057                     $oldPosition
00058                 )
00059             );
00060             
00061             if ($upperObject = $criteria->get()) {
00062                 DaoUtils::setNullValue(self::$nullValue);
00063                 DaoUtils::swap($upperObject, $object, self::$property);
00064             }
00065         }
00066         
00067         /* void */ public static function down(
00068             DAOConnected $object,
00069             LogicalObject $exp = null
00070         )
00071         {
00072             $getMethod = 'get'.ucfirst(self::$property);
00073             
00074             Assert::isTrue(
00075                 method_exists($object, $getMethod)
00076             );
00077             
00078             $oldPosition = $object->$getMethod();
00079             
00080             $criteria =
00081                 Criteria::create($object->dao())->
00082                 add(
00083                     Expression::gt(
00084                         self::$property,
00085                         $oldPosition
00086                     )
00087                 )->
00088                 addOrder(
00089                     OrderBy::create(self::$property)->asc()
00090                 )->
00091                 setLimit(1);
00092             
00093             if ($exp)
00094                 $criteria->add($exp);
00095             
00096             if ($lowerObject = $criteria->get()) {
00097                 DaoUtils::setNullValue(self::$nullValue);
00098                 DaoUtils::swap($lowerObject, $object, self::$property);
00099             }
00100         }
00101     }
00102 ?>