diff options
author | Bernhard Posselt <nukeawhale@gmail.com> | 2013-04-15 16:02:32 +0200 |
---|---|---|
committer | Bernhard Posselt <nukeawhale@gmail.com> | 2013-04-15 16:02:32 +0200 |
commit | 464ff6c4c1bda3edbd0f132c4d3d866539d3a117 (patch) | |
tree | 96b8fd57e24ebaab762a190a933cd98e1c7a4881 | |
parent | 89c31ab5fcb2f931fecc5ce82608ff7c8129510a (diff) |
renamed bl to businesslayer, handle exception in update routine, fix #69
48 files changed, 793 insertions, 670 deletions
diff --git a/backgroundjob/task.php b/backgroundjob/task.php index 2274f7b99..820d36cc7 100644 --- a/backgroundjob/task.php +++ b/backgroundjob/task.php @@ -34,8 +34,8 @@ class Task { static public function run() { $container = new DIContainer(); - $container['FeedBl']->updateAll(); - $container['ItemBl']->autoPurgeOld(); + $container['FeedBusinessLayer']->updateAll(); + $container['ItemBusinessLayer']->autoPurgeOld(); } diff --git a/bl/bl.php b/businesslayer/businesslayer.php index ee4ace588..4ce6f1625 100644 --- a/bl/bl.php +++ b/businesslayer/businesslayer.php @@ -23,7 +23,7 @@ * */ -namespace OCA\News\Bl; +namespace OCA\News\BusinessLayer; use \OCA\AppFramework\Db\DoesNotExistException; use \OCA\AppFramework\Db\MultipleObjectsReturnedException; @@ -31,7 +31,7 @@ use \OCA\AppFramework\Db\MultipleObjectsReturnedException; use \OCA\News\Db\IMapper; -abstract class Bl { +abstract class BusinessLayer { protected $mapper; @@ -50,9 +50,9 @@ abstract class Bl { try { return $this->mapper->find($id, $userId); } catch(DoesNotExistException $ex){ - throw new BLException($ex->getMessage()); + throw new BusinessLayerException($ex->getMessage()); } catch(MultipleObjectsReturnedException $ex){ - throw new BLException($ex->getMessage()); + throw new BusinessLayerException($ex->getMessage()); } } diff --git a/bl/blexception.php b/businesslayer/businesslayerexception.php index fca362ea7..03219a20f 100644 --- a/bl/blexception.php +++ b/businesslayer/businesslayerexception.php @@ -23,10 +23,10 @@ * */ -namespace OCA\News\Bl; +namespace OCA\News\BusinessLayer; -class BLException extends \Exception { +class BusinessLayerException extends \Exception { /** * Constructor diff --git a/bl/feedbl.php b/businesslayer/feedbusinesslayer.php index 97deaeff0..f7666fc1c 100644 --- a/bl/feedbl.php +++ b/businesslayer/feedbusinesslayer.php @@ -23,7 +23,7 @@ * */ -namespace OCA\News\Bl; +namespace OCA\News\BusinessLayer; use \OCA\AppFramework\Db\DoesNotExistException; use \OCA\AppFramework\Core\API; @@ -34,7 +34,7 @@ use \OCA\News\Db\ItemMapper; use \OCA\News\Utility\Fetcher; use \OCA\News\Utility\FetcherException; -class FeedBl extends Bl { +class FeedBusinessLayer extends BusinessLayer { private $feedFetcher; private $itemMapper; @@ -58,7 +58,7 @@ class FeedBl extends Bl { // first try if the feed exists already try { $this->mapper->findByUrlHash(md5($feedUrl), $userId); - throw new BLException( + throw new BusinessLayerException( $this->api->getTrans()->t('Can not add feed: Exists already')); } catch(DoesNotExistException $ex){} @@ -84,7 +84,7 @@ class FeedBl extends Bl { return $feed; } catch(FetcherException $ex){ $this->api->log($ex->getMessage()); - throw new BLException( + throw new BusinessLayerException( $this->api->getTrans()->t( 'Can not add feed: URL does not exist or has invalid xml')); } @@ -97,7 +97,7 @@ class FeedBl extends Bl { foreach($feeds as $feed){ try { $this->update($feed->getId(), $feed->getUserId()); - } catch(BLException $ex){ + } catch(BusinessLayerException $ex){ continue; } } @@ -105,50 +105,55 @@ class FeedBl extends Bl { public function update($feedId, $userId){ - $existingFeed = $this->mapper->find($feedId, $userId); try { - list($feed, $items) = $this->feedFetcher->fetch($existingFeed->getUrl()); - - // insert items in reverse order because the first one is usually the - // newest item - for($i=count($items)-1; $i>=0; $i--){ - $item = $items[$i]; - $item->setFeedId($existingFeed->getId()); - - // if a doesnotexist exception is being thrown the entry does not - // exist and the item needs to be created, otherwise - // update it - try { - $existing = $this->itemMapper->findByGuidHash( - $item->getGuidHash(), $feedId, $userId); - - // in case of an update the existing item has to be deleted - // if the pub_date changed because we sort by id on the - // client side since this is the only reliable way to do it - // to not get weird behaviour - if($existing->getPubDate() !== $item->getPubDate()){ - - // because the item is being replaced we need to keep - // status flags but we want the new entry to be unread - $item->setStatus($existing->getStatus()); - $item->setUnread(); - - $this->itemMapper->delete($existing); + $existingFeed = $this->mapper->find($feedId, $userId); + try { + list($feed, $items) = $this->feedFetcher->fetch($existingFeed->getUrl()); + + // insert items in reverse order because the first one is usually the + // newest item + for($i=count($items)-1; $i>=0; $i--){ + $item = $items[$i]; + $item->setFeedId($existingFeed->getId()); + + // if a doesnotexist exception is being thrown the entry does not + // exist and the item needs to be created, otherwise + // update it + try { + $existing = $this->itemMapper->findByGuidHash( + $item->getGuidHash(), $feedId, $userId); + + // in case of an update the existing item has to be deleted + // if the pub_date changed because we sort by id on the + // client side since this is the only reliable way to do it + // to not get weird behaviour + if($existing->getPubDate() !== $item->getPubDate()){ + + // because the item is being replaced we need to keep + // status flags but we want the new entry to be unread + $item->setStatus($existing->getStatus()); + $item->setUnread(); + + $this->itemMapper->delete($existing); + $this->itemMapper->insert($item); + } + + } catch(DoesNotExistException $ex){ $this->itemMapper->insert($item); } - - } catch(DoesNotExistException $ex){ - $this->itemMapper->insert($item); } - } - } catch(FetcherException $ex){ - // failed updating is not really a problem, so only log it - $this->api->log('Can not update feed with url' . $existingFeed->getUrl() . - ': Not found or bad source'); - } + } catch(FetcherException $ex){ + // failed updating is not really a problem, so only log it + $this->api->log('Can not update feed with url' . $existingFeed->getUrl() . + ': Not found or bad source'); + } + + return $this->mapper->find($feedId, $userId); - return $this->mapper->find($feedId, $userId); + } catch (DoesNotExistException $ex){ + throw new BusinessLayerException('Feed does not exist'); + } } diff --git a/bl/folderbl.php b/businesslayer/folderbusinesslayer.php index 59fdddf16..916476896 100644 --- a/bl/folderbl.php +++ b/businesslayer/folderbusinesslayer.php @@ -23,7 +23,7 @@ * */ -namespace OCA\News\Bl; +namespace OCA\News\BusinessLayer; use \OCA\AppFramework\Core\API; @@ -31,7 +31,7 @@ use \OCA\News\Db\Folder; use \OCA\News\Db\FolderMapper; -class FolderBl extends Bl { +class FolderBusinessLayer extends BusinessLayer { private $api; @@ -51,13 +51,13 @@ class FolderBl extends Bl { $existingFolders = $this->mapper->findByName($folderName, $userId); if(count($existingFolders) > 0){ - throw new BLException( + throw new BusinessLayerException( $this->api->getTrans()->t('Can not add folder: Exists already')); } } /** - * @throws BLException if name exists already + * @throws BusinessLayerException if name exists already */ public function create($folderName, $userId, $parentId=0) { $this->allowNoNameTwice($folderName, $userId); @@ -79,7 +79,7 @@ class FolderBl extends Bl { /** - * @throws BLException if name exists already + * @throws BusinessLayerException if name exists already */ public function rename($folderId, $folderName, $userId){ $this->allowNoNameTwice($folderName, $userId); diff --git a/bl/itembl.php b/businesslayer/itembusinesslayer.php index 504e48016..fd01506a6 100644 --- a/bl/itembl.php +++ b/businesslayer/itembusinesslayer.php @@ -23,7 +23,7 @@ * */ -namespace OCA\News\Bl; +namespace OCA\News\BusinessLayer; use \OCA\News\Db\Item; use \OCA\News\Db\ItemMapper; @@ -31,7 +31,7 @@ use \OCA\News\Db\StatusFlag; use \OCA\News\Db\FeedType; -class ItemBl extends Bl { +class ItemBusinessLayer extends BusinessLayer { private $statusFlag; private $autoPurgeCount; diff --git a/controller/exportcontroller.php b/controller/exportcontroller.php index 960748d3e..023a4d39d 100644 --- a/controller/exportcontroller.php +++ b/controller/exportcontroller.php @@ -30,21 +30,23 @@ use \OCA\AppFramework\Core\API; use \OCA\AppFramework\Http\Request; use \OCA\AppFramework\Http\TextDownloadResponse; -use \OCA\News\Bl\FeedBl; -use \OCA\News\Bl\FolderBl; +use \OCA\News\BusinessLayer\FeedBusinessLayer; +use \OCA\News\BusinessLayer\FolderBusinessLayer; use \OCA\News\Utility\OPMLExporter; class ExportController extends Controller { private $opmlExporter; - private $folderBl; - private $feedBl; + private $folderBusinessLayer; + private $feedBusinessLayer; - public function __construct(API $api, Request $request, FeedBl $feedBl, - FolderBl $folderBl, OPMLExporter $opmlExporter){ + public function __construct(API $api, Request $request, + FeedBusinessLayer $feedBusinessLayer, + FolderBusinessLayer $folderBusinessLayer, + OPMLExporter $opmlExporter){ parent::__construct($api, $request); - $this->feedBl = $feedBl; - $this->folderBl = $folderBl; + $this->feedBusinessLayer = $feedBusinessLayer; + $this->folderBusinessLayer = $folderBusinessLayer; $this->opmlExporter = $opmlExporter; } @@ -56,8 +58,8 @@ class ExportController extends Controller { */ public function opml(){ $user = $this->api->getUserId(); - $feeds = $this->feedBl->findAll($user); - $folders = $this->folderBl->findAll($user); + $feeds = $this->feedBusinessLayer->findAll($user); + $folders = $this->folderBusinessLayer->findAll($user); $opml = $this->opmlExporter->build($folders, $feeds)->saveXML(); return new TextDownloadResponse($opml, 'subscriptions.opml', 'text/xml'); } diff --git a/controller/feedcontroller.php b/controller/feedcontroller.php index 5e2a91f24..53257d73c 100644 --- a/controller/feedcontroller.php +++ b/controller/feedcontroller.php @@ -29,22 +29,23 @@ use \OCA\AppFramework\Controller\Controller; use \OCA\AppFramework\Core\API; use \OCA\AppFramework\Http\Request; -use \OCA\News\Bl\FeedBl; -use \OCA\News\Bl\FolderBl; -use \OCA\News\Bl\BLException; +use \OCA\News\BusinessLayer\FeedBusinessLayer; +use \OCA\News\BusinessLayer\FolderBusinessLayer; +use \OCA\News\BusinessLayer\BusinessLayerException; use \OCA\News\Db\FeedType; class FeedController extends Controller { - private $feedBl; - private $folderBl; + private $feedBusinessLayer; + private $folderBusinessLayer; - public function __construct(API $api, Request $request, FeedBl $feedBl, - FolderBl $folderBl){ + public function __construct(API $api, Request $request, + FeedBusinessLayer $feedBusinessLayer, + FolderBusinessLayer $folderBusinessLayer){ parent::__construct($api, $request); - $this->feedBl = $feedBl; - $this->folderBl = $folderBl; + $this->feedBusinessLayer = $feedBusinessLayer; + $this->folderBusinessLayer = $folderBusinessLayer; } @@ -55,7 +56,7 @@ class FeedController extends Controller { */ public function feeds(){ $userId = $this->api->getUserId(); - $result = $this->feedBl->findAll($userId); + $result = $this->feedBusinessLayer->findAll($userId); $params = array( 'feeds' => $result @@ -83,17 +84,17 @@ class FeedController extends Controller { // check if feed or folder exists try { if($feedType === FeedType::FOLDER){ - $this->folderBl->find($feedId, $userId); + $this->folderBusinessLayer->find($feedId, $userId); } elseif ($feedType === FeedType::FEED){ - $this->feedBl->find($feedId, $userId); + $this->feedBusinessLayer->find($feedId, $userId); // if its the first launch, those values will be null } elseif($feedType === null){ - throw new BLException(''); + throw new BusinessLayerException(''); } - } catch (BLException $ex){ + } catch (BusinessLayerException $ex){ $feedId = 0; $feedType = FeedType::SUBSCRIPTIONS; } @@ -120,13 +121,13 @@ class FeedController extends Controller { $userId = $this->api->getUserId(); try { - $feed = $this->feedBl->create($url, $parentFolderId, $userId); + $feed = $this->feedBusinessLayer->create($url, $parentFolderId, $userId); $params = array( 'feeds' => array($feed) ); return $this->renderJSON($params); - } catch(BLException $ex) { + } catch(BusinessLayerException $ex) { return $this->renderJSON(array(), $ex->getMessage()); } @@ -142,7 +143,7 @@ class FeedController extends Controller { $feedId = (int) $this->params('feedId'); $userId = $this->api->getUserId(); - $this->feedBl->delete($feedId, $userId); + $this->feedBusinessLayer->delete($feedId, $userId); return $this->renderJSON(); } @@ -154,16 +155,21 @@ class FeedController extends Controller { * @Ajax */ public function update(){ - $feedId = (int) $this->params('feedId'); - $userId = $this->api->getUserId(); + try { + $feedId = (int) $this->params('feedId'); + $userId = $this->api->getUserId(); - $feed = $this->feedBl->update($feedId, $userId); + $feed = $this->feedBusinessLayer->update($feedId, $userId); - $params = array( - 'feeds' => array($feed) - ); + $params = array( + 'feeds' => array($feed) + ); - return $this->renderJSON($params); + return $this->renderJSON($params); + + } catch(BusinessLayerException $ex) { + return $this->renderJSON(array(), $ex->getMessage()); + } } @@ -177,7 +183,7 @@ class FeedController extends Controller { $parentFolderId = (int) $this->params('parentFolderId'); $userId = $this->api->getUserId(); - $this->feedBl->move($feedId, $parentFolderId, $userId); + $this->feedBusinessLayer->move($feedId, $parentFolderId, $userId); return $this->renderJSON(); } diff --git a/controller/foldercontroller.php b/controller/foldercontroller.php index f905ddddf..f7934ae67 100644 --- a/controller/foldercontroller.php +++ b/controller/foldercontroller.php @@ -29,17 +29,18 @@ use \OCA\AppFramework\Controller\Controller; use \OCA\AppFramework\Core\API; use \OCA\AppFramework\Http\Request; -use \OCA\News\Bl\FolderBl; -use \OCA\News\Bl\BLException; +use \OCA\News\BusinessLayer\FolderBusinessLayer; +use \OCA\News\BusinessLayer\BusinessLayerException; class FolderController extends Controller { - private $folderBl; + private $folderBusinessLayer; - public function __construct(API $api, Request $request, FolderBl $folderBl){ + public function __construct(API $api, Request $request, + FolderBusinessLayer $folderBusinessLayer){ parent::__construct($api, $request); - $this->folderBl = $folderBl; + $this->folderBusinessLayer = $folderBusinessLayer; } @@ -49,7 +50,7 @@ class FolderController extends Controller { * @Ajax */ public function folders(){ - $folders = $this->folderBl->findAll($this->api->getUserId()); + $folders = $this->folderBusinessLayer->findAll($this->api->getUserId()); $result = array( 'folders' => $folders ); @@ -61,7 +62,7 @@ class FolderController extends Controller { $userId = $this->api->getUserId(); $folderId = (int) $this->params('folderId'); - $this->folderBl->open($folderId, $isOpened, $userId); + $this->folderBusinessLayer->open($folderId, $isOpened, $userId); } /** @@ -96,14 +97,14 @@ class FolderController extends Controller { $folderName = $this->params('folderName'); try { - $folder = $this->folderBl->create($folderName, $userId); + $folder = $this->folderBusinessLayer->create($folderName, $userId); $params = array( 'folders' => array($folder) ); return $this->renderJSON($params); - } catch (BLException $ex){ + } catch (BusinessLayerException $ex){ return $this->renderJSON(array(), $ex->getMessage()); } @@ -120,7 +121,7 @@ class FolderController extends Controller { $userId = $this->api->getUserId(); $folderId = (int) $this->params('folderId'); - $this->folderBl->delete($folderId, $userId); + $this->folderBusinessLayer->delete($folderId, $userId); return $this->renderJSON(); } @@ -137,14 +138,14 @@ class FolderController extends Controller { $folderId = (int) $this->params('folderId'); try { - $folder = $this->folderBl->rename($folderId, $folderName, $userId); + $folder = $this->folderBusinessLayer->rename($folderId, $folderName, $userId); $params |