From 208f8ad12b573649db55a338031c65cac958d729 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Wed, 22 Sep 2010 02:28:09 +0200 Subject: [PATCH] adapted controllers to new mvc implementation added some operations --- .../lib/Controller/AggregatorController.php | 84 +++++++++++++++---- backend/lib/Controller/ChannelController.php | 20 ++--- backend/lib/Controller/Controller.php | 4 +- backend/lib/Controller/DataController.php | 32 +++++-- backend/lib/Controller/EntityController.php | 20 ++++- 5 files changed, 121 insertions(+), 39 deletions(-) diff --git a/backend/lib/Controller/AggregatorController.php b/backend/lib/Controller/AggregatorController.php index 6d0ea4b..1981bdd 100644 --- a/backend/lib/Controller/AggregatorController.php +++ b/backend/lib/Controller/AggregatorController.php @@ -32,41 +32,89 @@ namespace Volkszaehler\Controller; use Volkszaehler\Model; class AggregatorController extends EntityController { - /** * Get aggregator - * - * @param string $identifier */ public function get($identifier) { - $dql = 'SELECT a, p - FROM Volkszaehler\Model\Aggregator a - LEFT JOIN a.properties p - WHERE a.uuid = ?1'; + $aggregator = parent::get($identifier); - $q = $this->em->createQuery($dql); - $q->setParameter(1, $identifier); - - return $q->getSingleResult(); + if ($aggregator instanceof Model\Aggregator) { + return $aggregator; + } + else { + throw new \Exception($identifier . ' is not a group uuid'); + } } /** - * Add aggregator + * Create new aggregator or add entity to aggregator */ - public function add() { - $aggregator = new Model\Aggregator('group'); // TODO support for other aggregator types + public function add($identifier = NULL) { + if (isset($identifier)) { // add entity to aggregator + $aggregator = $this->get($identifier); - foreach ($this->view->request->getParameters() as $parameter => $value) { - if (Model\PropertyDefinition::exists($parameter)) { - $aggregator->setProperty($parameter, $value); + if ($uuid = $this->view->request->getParameter('uuid')) { + $ec = new EntityController($this->view, $this->em); + $entity = $ec->get($uuid); + + if ($entity instanceof Model\Channel) { + $aggregator->addChannel($entity); + } + elseif ($entity instanceof Model\Aggregator) { + $aggregator->addAggregator($entity); + } + } + else { // create new aggregator + throw new \Exception('You have to specifiy a uuid to add'); } } + else { + $aggregator = new Model\Aggregator('group'); // TODO support for other aggregator types + + foreach ($this->view->request->getParameters() as $parameter => $value) { + if (Model\PropertyDefinition::exists($parameter)) { + $aggregator->setProperty($parameter, $value); + } + } + + $this->em->persist($aggregator); + } - $this->em->persist($aggregator); $this->em->flush(); return $aggregator; } + + /** + * Delete Aggregator or remove entity from aggregator + */ + public function delete($identifier) { + if (isset($identifier) && $uuid = $this->view->request->getParameter('uuid')) { // remove entity from aggregator + $aggregator = $this->get($identifier); + + if ($uuid) { + $ec = new EntityController($this->view, $this->em); + $entity = $ec->get($uuid); + + if ($entity instanceof Model\Channel) { + $aggregator->removeChannel($entity); + } + elseif ($entity instanceof Model\Aggregator) { + $aggregator->removeAggregator($entity); + } + + $this->em->flush(); + } + else { // remove aggregator + throw new \Exception('You have to specifiy a uuid to remove'); + } + } + else { + parent::delete($identifier); + } + + return $aggregator; + } } ?> \ No newline at end of file diff --git a/backend/lib/Controller/ChannelController.php b/backend/lib/Controller/ChannelController.php index 7066642..69dfb33 100644 --- a/backend/lib/Controller/ChannelController.php +++ b/backend/lib/Controller/ChannelController.php @@ -23,6 +23,8 @@ namespace Volkszaehler\Controller; +use Volkszaehler\Model; + /** * Channel controller * @@ -30,22 +32,18 @@ namespace Volkszaehler\Controller; * @package default */ class ChannelController extends EntityController { - /** * Get channel - * - * @param string $identifier */ public function get($identifier) { - $dql = 'SELECT c, p - FROM Volkszaehler\Model\Channel c - LEFT JOIN c.properties p - WHERE c.uuid = ?1'; + $channel = parent::get($identifier); - $q = $this->em->createQuery($dql); - $q->setParameter(1, $identifier); - - return $q->getSingleResult(); + if ($channel instanceof Model\Channel) { + return $channel; + } + else { + throw new \Exception($identifier . ' is not a channel uuid'); + } } /** diff --git a/backend/lib/Controller/Controller.php b/backend/lib/Controller/Controller.php index 92bcda0..affdcb8 100644 --- a/backend/lib/Controller/Controller.php +++ b/backend/lib/Controller/Controller.php @@ -49,12 +49,12 @@ abstract class Controller { * * @param string $operation runs the operation if class method is available */ - public function run($operation, $params = NULL) { + public function run($operation, array $params = array()) { if (!is_callable(array($this, $operation))) { throw new \Exception('Invalid context operation: ' . $operation); } - return $this->$operation($params); + return call_user_func_array(array($this, $operation), $params); } } diff --git a/backend/lib/Controller/DataController.php b/backend/lib/Controller/DataController.php index 02e5f1b..a9e6949 100644 --- a/backend/lib/Controller/DataController.php +++ b/backend/lib/Controller/DataController.php @@ -30,7 +30,6 @@ use Volkszaehler\Util; * Data controller * * @author Steffen Vogel - * @todo call via redirect from Controller\Channel * @package default */ class DataController extends Controller { @@ -41,16 +40,39 @@ class DataController extends Controller { public function get(Model\Entity $entity) { $from = $this->view->request->getParameter('from'); $to = $this->view->request->getParameter('to'); - $groupBy = $this->view->request->getParameter('groupBy'); - return $entity->getInterpreter($this->em, $from, $to)->getValues($groupBy); + return $entity->getInterpreter($this->em, $from, $to); } /** - * Log new readings with logger interfaces + * Sporadic test/demo implemenation + * + * @todo replace by pluggable api parser */ - public function add() { + public function add(Model\Channel $channel) { + $timestamp = $this->view->request->getParameter('ts'); + $value = $this->view->request->getParameter('value'); + if (!$timestamp) { + $timestamp = round(microtime(TRUE) * 1000); + } + + if (!$value) { + $value = 1; + } + + $data = new Model\Data($channel, $timestamp, $value); + + $channel->addData($data); + + $this->em->flush(); + } + + public function run($operation, array $params = array()) { + $ec = new EntityController($this->view, $this->em); + $params[0] = $ec->get($params[0]); + + return parent::run($operation, $params); } } diff --git a/backend/lib/Controller/EntityController.php b/backend/lib/Controller/EntityController.php index af02839..6bef8fb 100644 --- a/backend/lib/Controller/EntityController.php +++ b/backend/lib/Controller/EntityController.php @@ -32,13 +32,27 @@ use Volkszaehler\Model; * @author Steffen Vogel * @package default */ -abstract class EntityController extends Controller { +class EntityController extends Controller { /** * Get entity * - * @param unknown_type $identifier + * @param string $identifier */ - abstract public function get($identifier); + public function get($identifier) { + $dql = 'SELECT a, p + FROM Volkszaehler\Model\Entity a + LEFT JOIN a.properties p + WHERE a.uuid = ?1'; + + $q = $this->em->createQuery($dql); + $q->setParameter(1, $identifier); + + try { + return $q->getSingleResult(); + } catch (\Doctrine\ORM\NoResultException $e) { + throw new \Exception('No entity found with uuid: ' . $identifier); + } + } /** * Delete entity by uuid