From b2d0565ec4c2fb506e4ff01f8141c804c3c1df2f Mon Sep 17 00:00:00 2001 From: Naomi Aro Date: Thu, 16 Feb 2012 19:46:14 +0100 Subject: [PATCH] CC-3174 : showbuilder checking to make sure UI is up to date with db --- airtime_mvc/application/models/Playlist.php | 2 +- airtime_mvc/application/models/Scheduler.php | 139 ++++++++++++------ airtime_mvc/application/models/StoredFile.php | 17 ++- .../library/events/library_showbuilder.js | 2 +- 4 files changed, 103 insertions(+), 57 deletions(-) diff --git a/airtime_mvc/application/models/Playlist.php b/airtime_mvc/application/models/Playlist.php index 9feca24ab..441ed9a43 100644 --- a/airtime_mvc/application/models/Playlist.php +++ b/airtime_mvc/application/models/Playlist.php @@ -768,4 +768,4 @@ class Application_Model_Playlist { } // class Playlist class PlaylistNotFoundException extends Exception {} -class OutDatedException extends Exception {} +class PlaylistOutDatedException extends Exception {} diff --git a/airtime_mvc/application/models/Scheduler.php b/airtime_mvc/application/models/Scheduler.php index 851064145..40f977122 100644 --- a/airtime_mvc/application/models/Scheduler.php +++ b/airtime_mvc/application/models/Scheduler.php @@ -31,11 +31,16 @@ class Application_Model_Scheduler { if ($type === "audioclip") { $file = CcFilesQuery::create()->findPK($id, $this->con); - $data = $this->fileInfo; - $data["id"] = $id; - $data["cliplength"] = $file->getDbLength(); + if (is_null($file) || !$file->getDbFileExists()) { + throw new Exception("A selected File does not exist!"); + } + else { + $data = $this->fileInfo; + $data["id"] = $id; + $data["cliplength"] = $file->getDbLength(); - $files[] = $data; + $files[] = $data; + } } else if ($type === "playlist") { @@ -44,17 +49,25 @@ class Application_Model_Scheduler { ->filterByDbPlaylistId($id) ->find($this->con); + if (is_null($contents)) { + throw new Exception("A selected Playlist does not exist!"); + } + foreach ($contents as $plItem) { - $data = $this->fileInfo; - $data["id"] = $plItem->getDbFileId(); - $data["cliplength"] = $plItem->getDbCliplength(); - $data["cuein"] = $plItem->getDbCuein(); - $data["cueout"] = $plItem->getDbCueout(); - $data["fadein"] = $plItem->getDbFadein(); - $data["fadeout"] = $plItem->getDbFadeout(); + $file = $plItem->getCcFiles($this->con); + if (isset($file) && $file->getDbFileExists()) { - $files[] = $data; + $data = $this->fileInfo; + $data["id"] = $plItem->getDbFileId(); + $data["cliplength"] = $plItem->getDbCliplength(); + $data["cuein"] = $plItem->getDbCuein(); + $data["cueout"] = $plItem->getDbCueout(); + $data["fadein"] = $plItem->getDbFadein(); + $data["fadeout"] = $plItem->getDbFadeout(); + + $files[] = $data; + } } } @@ -98,6 +111,8 @@ class Application_Model_Scheduler { try { + $affectedShowInstances = array(); + //dont want to recalculate times for moved items. $excludeIds = array(); foreach ($schedFiles as $file) { @@ -108,34 +123,42 @@ class Application_Model_Scheduler { foreach ($scheduleItems as $schedule) { $id = intval($schedule["id"]); + $ts = intval($schedule["timestamp"]); Logging::log("scheduling after scheduled item: ".$id); Logging::log("in show: ".intval($schedule["instance"])); if ($id !== 0) { $schedItem = CcScheduleQuery::create()->findPK($id, $this->con); - $instance = $schedItem->getDbInstanceId(); - - //user has an old copy of the time line opened. - if ($instance !== intval($schedule["instance"])) { - Logging::log("items have been since updated"); - return; + $instance = $schedItem->getCcShowInstances($this->con); + if (intval($schedule["instance"]) !== $instance->getDbId()) { + throw new OutDatedScheduleException("The schedule you're viewing is out of date!"); } - $nextStartDT = $schedItem->getDbEnds(null); } //selected empty row to add after else { - $showInstance = CcShowInstancesQuery::create()->findPK($schedule["instance"], $this->con); + $instance = CcShowInstancesQuery::create()->findPK($schedule["instance"], $this->con); $nextStartDT = $showInstance->getDbStarts(null); - $instance = intval($schedule["instance"]); + } + + $currTs = intval($instance->getDbLastScheduled("U")) ? : 0; + //user has an old copy of the time line opened. + if ($ts !== $currTs) { + Logging::log("currTs {$currTs}, ts {$ts}"); + $show = $instance->getCcShow($this->con); + throw new OutDatedScheduleException("The show {$show->getDbName()} has been previously updated!"); + } + + if (!in_array($instance->getDbId(), $affectedShowInstances)) { + $affectedShowInstances[] = $instance->getDbId(); } Logging::log("finding items >= {$nextStartDT->format("Y-m-d H:i:s.u")}"); if ($adjustSched === true) { $followingSchedItems = CcScheduleQuery::create() ->filterByDBStarts($nextStartDT->format("Y-m-d H:i:s.u"), Criteria::GREATER_EQUAL) - ->filterByDbInstanceId($instance) + ->filterByDbInstanceId($instance->getDbId()) ->filterByDbId($excludeIds, Criteria::NOT_IN) ->orderByDbStarts() ->find($this->con); @@ -171,7 +194,7 @@ class Application_Model_Scheduler { $sched->setDbFadeIn($file['fadein']); $sched->setDbFadeOut($file['fadeout']); $sched->setDbClipLength($file['cliplength']); - $sched->setDbInstanceId($instance); + $sched->setDbInstanceId($instance->getDbId()); $sched->save($this->con); $nextStartDT = $endTimeDT; @@ -195,6 +218,10 @@ class Application_Model_Scheduler { } } + //update the last scheduled timestamp. + CcShowInstancesQuery::create() + ->filterByPrimaryKeys($showInstances) + ->update(array('DbLastScheduled' => new DateTime("now", new DateTimeZone("UTC"))), $this->con); } catch (Exception $e) { throw $e; @@ -236,37 +263,46 @@ class Application_Model_Scheduler { * @param array $selectedItem * @param array $afterItem */ - public function moveItem($selectedItem, $afterItem, $adjustSched = true) { + public function moveItem($selectedItems, $afterItems, $adjustSched = true) { $this->con->beginTransaction(); try { - $origSelIns = intval($selectedItem[0]["instance"]); - $origAfterIns = intval($afterItem[0]["instance"]); + $origSelTs = intval($selectedItems[0]["timestamp"]); + $origAfterTs = intval($afterItems[0]["timestamp"]); - Logging::log("Moving item {$selectedItem[0]["id"]}"); - Logging::log("After {$afterItem[0]["id"]}"); + Logging::log("Moving item {$selectedItems[0]["id"]}"); + Logging::log("After {$afterItems[0]["id"]}"); - $selected = CcScheduleQuery::create()->findPk($selectedItem[0]["id"]); - $after = CcScheduleQuery::create()->findPk($afterItem[0]["id"]); + $selected = CcScheduleQuery::create()->findPk($selectedItems[0]["id"], $this->con); + $after = CcScheduleQuery::create()->findPk($afterItems[0]["id"], $this->con); - /* - if (isset($after) && $origSelIns !== $selected->getDBInstanceId() - || $origAfterIns !== $after->getDBInstanceId()) { - - Logging::log("items have been since updated"); - return; + if (is_null($selected) || is_null($after)) { + throw new OutDatedScheduleException("The schedule you're viewing is out of date!"); } - */ - $this->removeGaps($origSelIns, $selected->getDbId()); - - //moved to another show, remove gaps from original show. - if ($adjustSched === true && $origSelIns !== $origAfterIns) { + $selectedInstance = $selected->getCcShowInstances($this->con); + $afterInstance = $after->getCcShowInstances($this->con); + if (is_null($selectedInstance) || is_null($afterInstance)) { + throw new OutDatedScheduleException("The schedule you're viewing is out of date!"); } + $currTs = intval($selectedInstance->getDbLastScheduled("U")) ? : 0; + if ($origSelTs !== $currTs) { + $show = $selectedInstance->getCcShow($this->con); + throw new OutDatedScheduleException("The show {$show->getDbName()} has been previously updated!"); + } + + $currTs = intval($afterInstance->getDbLastScheduled("U")) ? : 0; + if ($origAfterTs !== $currTs) { + $show = $afterInstance->getCcShow($this->con); + throw new OutDatedScheduleException("The show {$show->getDbName()} has been previously updated!"); + } + + $this->removeGaps($selectedInstance->getDbId(), $selected->getDbId()); + $data = $this->fileInfo; $data["id"] = $selected->getDbFileId(); $data["cliplength"] = $selected->getDbClipLength(); @@ -276,7 +312,7 @@ class Application_Model_Scheduler { $data["fadeout"] = $selected->getDbFadeOut(); $data["sched_id"] = $selected->getDbId(); - $this->insertAfter($afterItem, array($data), $adjustSched); + $this->insertAfter($afterItems, array($data), $adjustSched); $this->con->commit(); @@ -297,7 +333,7 @@ class Application_Model_Scheduler { $scheduledIds = array(); foreach ($scheduledItems as $item) { - $scheduledIds[$item["id"]] = $item["timestamp"]; + $scheduledIds[$item["id"]] = intval($item["timestamp"]); } $removedItems = CcScheduleQuery::create()->findPks(array_keys($scheduledIds)); @@ -306,11 +342,14 @@ class Application_Model_Scheduler { foreach ($removedItems as $removedItem) { $ts = $scheduledIds[$removedItem->getDbId()]; $instance = $removedItem->getCcShowInstances($this->con); + if (is_null($instance)) { + throw new OutDatedScheduleException("The schedule you're viewing is out of date!"); + } $currTs = intval($instance->getDbLastScheduled("U")) ? : 0; if ($ts !== $currTs) { $show = $instance->getCcShow($this->con); - throw new OutDatedScheduleException("The show {$show->getDbName()} is outdated!"); + throw new OutDatedScheduleException("The show {$show->getDbName()} has been previously updated!"); } } @@ -331,6 +370,11 @@ class Application_Model_Scheduler { } } + //update the last scheduled timestamp. + CcShowInstancesQuery::create() + ->filterByPrimaryKeys($showInstances) + ->update(array('DbLastScheduled' => new DateTime("now", new DateTimeZone("UTC"))), $this->con); + $this->con->commit(); Application_Model_RabbitMq::PushSchedule(); @@ -346,13 +390,14 @@ class Application_Model_Scheduler { * @param array $exclude * ids of sched items to remove from the calulation. */ - public function removeGaps($showInstance, $exclude=null) { + private function removeGaps($showInstance, $exclude=null) { Logging::log("removing gaps from show instance #".$showInstance); $instance = CcShowInstancesQuery::create()->findPK($showInstance, $this->con); - $instance->setDbLastScheduled(new DateTime("now", new DateTimeZone("UTC"))); - $instance->save($this->con); + if (is_null($instance)) { + throw new OutDatedScheduleException("The schedule you're viewing is out of date!"); + } $itemStartDT = $instance->getDbStarts(null); diff --git a/airtime_mvc/application/models/StoredFile.php b/airtime_mvc/application/models/StoredFile.php index 05a25abef..7aca3decb 100644 --- a/airtime_mvc/application/models/StoredFile.php +++ b/airtime_mvc/application/models/StoredFile.php @@ -850,14 +850,14 @@ class Application_Model_StoredFile { //check to see if we have enough space in the /organize directory to copy the file $freeSpace = disk_free_space($destination_folder); $fileSize = filesize($audio_file); - + if ( $freeSpace < $fileSize ){ $freeSpace = floor($freeSpace/1024/1024); $fileSize = floor($fileSize/1024/1024); die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "The file was not uploaded, there was '.$freeSpace.'MB disk space left the file you are uploadings size is '.$fileSize.'MB."}}'); } } - + public static function copyFileToStor($p_targetDir, $fileName, $tempname){ $audio_file = $p_targetDir . DIRECTORY_SEPARATOR . $tempname; Logging::log('copyFileToStor: moving file '.$audio_file); @@ -875,13 +875,13 @@ class Application_Model_StoredFile { $storDir = Application_Model_MusicDir::getStorDir(); $stor = $storDir->getDirectory(); - + //check to see if there is enough space in $stor to continue. Application_Model_StoredFile::checkForEnoughDiskSpaceToCopy($stor, $audio_file); - - $stor .= "/organize"; - $audio_stor = $stor . DIRECTORY_SEPARATOR . $fileName; - + + $stor .= "/organize"; + $audio_stor = $stor . DIRECTORY_SEPARATOR . $fileName; + Logging::log("copyFileToStor: moving file $audio_file to $audio_stor"); //Martin K.: changed to rename: Much less load + quicker since this is an atomic operation $r = @rename($audio_file, $audio_stor); @@ -897,7 +897,7 @@ class Application_Model_StoredFile { //$r = @unlink($audio_file); } - + public static function getFileCount() { global $CC_CONFIG, $CC_DBC; @@ -1028,3 +1028,4 @@ class Application_Model_StoredFile { } class DeleteScheduledFileException extends Exception {} +class FileDoesNotExistException extends Exception {} diff --git a/airtime_mvc/public/js/airtime/library/events/library_showbuilder.js b/airtime_mvc/public/js/airtime/library/events/library_showbuilder.js index f5f3f0b8e..58dc3d549 100644 --- a/airtime_mvc/public/js/airtime/library/events/library_showbuilder.js +++ b/airtime_mvc/public/js/airtime/library/events/library_showbuilder.js @@ -59,7 +59,7 @@ var AIRTIME = (function(AIRTIME){ for (item in aData) { temp = aData[item]; if (temp !== null && temp.hasOwnProperty('id')) { - aSchedIds.push({"id": temp.id, "instance": temp.instance}); + aSchedIds.push({"id": temp.id, "instance": temp.instance, "timestamp": temp.timestamp}); } }