From fd2daa02ffd8bde80ccf0db7c55cd1c5812816dc Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sat, 4 Sep 2010 01:33:32 +0200 Subject: [PATCH] make data, token and properties cascading persist operations fixed several bugs in the new property system --- backend/lib/Model/Aggregator.php | 5 ++ backend/lib/Model/Channel.php | 2 +- backend/lib/Model/Data.php | 1 + backend/lib/Model/Entity.php | 49 +++--------------- backend/lib/Model/EntityDefinition.php | 63 ++++++++++++++++++++++++ backend/lib/Model/Property.php | 12 +++-- backend/lib/Model/PropertyDefinition.php | 8 +-- 7 files changed, 90 insertions(+), 50 deletions(-) create mode 100644 backend/lib/Model/EntityDefinition.php diff --git a/backend/lib/Model/Aggregator.php b/backend/lib/Model/Aggregator.php index eff1c0e..893ae0a 100644 --- a/backend/lib/Model/Aggregator.php +++ b/backend/lib/Model/Aggregator.php @@ -102,6 +102,11 @@ class Aggregator extends Entity { public function removeChannel(Channel $child) { $this->channels->removeElement($child); } + + /** + * @todo return array via ArrayCollection::toArray()? + */ + public function getChannels() { return $this->channels; } } ?> \ No newline at end of file diff --git a/backend/lib/Model/Channel.php b/backend/lib/Model/Channel.php index 5739114..0384fcd 100644 --- a/backend/lib/Model/Channel.php +++ b/backend/lib/Model/Channel.php @@ -35,7 +35,7 @@ use Doctrine\Common\Collections\ArrayCollection; */ class Channel extends Entity { /** - * @OneToMany(targetEntity="Data", mappedBy="channel", cascade={"remove"}) + * @OneToMany(targetEntity="Data", mappedBy="channel", cascade={"remove", "persist"}) * @OrderBy({"timestamp" = "ASC"}) */ protected $data = NULL; diff --git a/backend/lib/Model/Data.php b/backend/lib/Model/Data.php index f71199c..438493b 100644 --- a/backend/lib/Model/Data.php +++ b/backend/lib/Model/Data.php @@ -31,6 +31,7 @@ use Volkszaehler\Model; * * @author Steffen Vogel * @package default + * @todo rename? Bsp: DataSample, Sample, Reading * * @Entity * @Table(name="data") diff --git a/backend/lib/Model/Entity.php b/backend/lib/Model/Entity.php index 838e588..dc8a182 100644 --- a/backend/lib/Model/Entity.php +++ b/backend/lib/Model/Entity.php @@ -38,7 +38,7 @@ use Volkszaehler\Util; * @DiscriminatorColumn(name="class", type="string") * @DiscriminatorMap({ * "channel" = "Channel", - * "group" = "Aggregator" + * "aggregator" = "Aggregator" * }) * @HasLifecycleCallbacks */ @@ -57,12 +57,12 @@ abstract class Entity { protected $type; /** - * @OneToMany(targetEntity="Token", mappedBy="entity") + * @OneToMany(targetEntity="Token", mappedBy="entity", cascade={"remove", "persist"}) */ protected $tokens = NULL; /** - * @OneToMany(targetEntity="Property", mappedBy="entity") + * @OneToMany(targetEntity="Property", mappedBy="entity", cascade={"remove", "persist"}) * @OrderBy({"name" = "ASC"}) */ protected $properties = NULL; @@ -73,7 +73,7 @@ abstract class Entity { * @param string $type * @param array $properties of Model\Property */ - public function __construct($type, $properties = NULL) { + public function __construct($type) { if (!EntityDefinition::exists($type)) { throw new \Exception('unknown entity type'); } @@ -83,12 +83,6 @@ abstract class Entity { $this->tokens = new Collections\ArrayCollection(); $this->properties = new Collections\ArrayCollection(); - - if (isset($properties)) { - foreach($properties as $property) { - $this->properies->add($property); - } - } } /** @@ -136,10 +130,10 @@ abstract class Entity { /** * @param string $name of the property * @param string|integer|float $value of the property - * @todo to be implemented + * @todo check if already set for this entity */ - public function setProperty($name, $value) { - + public function setProperty(Property $property) { + $this->properties->add($property); } /** @@ -172,33 +166,4 @@ abstract class Entity { } } -class EntityDefinition extends Util\Definition { - /** @var string File containing the JSON definitons */ - const FILE = '/share/entities.json'; - - /** @var array list of required properties */ - protected $required = array(); - - /** @var array list of optional properties */ - protected $optional = array(); - - /** @var string classname of intepreter (see backend/lib/Interpreter/) */ - protected $interpreter; - - /** @var string optional for Aggregator class entities */ - protected $unit; - - /** - * @todo url relative or absolute? - * @var string - */ - protected $icon; - - /* - * Setter & Getter - */ - public function getInterpreter() { return $this->interpreter; } - public function getUnit() { return $this->unit; } -} - ?> diff --git a/backend/lib/Model/EntityDefinition.php b/backend/lib/Model/EntityDefinition.php new file mode 100644 index 0000000..3d01234 --- /dev/null +++ b/backend/lib/Model/EntityDefinition.php @@ -0,0 +1,63 @@ +. + */ + +namespace Volkszaehler; + +/** + * + * @author Steffen Vogel + * @todo extract to extra file + * + */ +class EntityDefinition extends Util\Definition { + /** @var string File containing the JSON definitons */ + const FILE = '/share/entities.json'; + + /** @var array list of required properties */ + protected $required = array(); + + /** @var array list of optional properties */ + protected $optional = array(); + + /** @var string classname of intepreter (see backend/lib/Interpreter/) */ + protected $interpreter; + + /** @var string optional for Aggregator class entities */ + protected $unit; + + static protected $definitions = NULL; + + /** + * @todo url relative or absolute? + * @var string + */ + protected $icon; + + /* + * Setter & Getter + */ + public function getInterpreter() { return $this->interpreter; } + public function getUnit() { return $this->unit; } +} + +?> \ No newline at end of file diff --git a/backend/lib/Model/Property.php b/backend/lib/Model/Property.php index f3842d0..a14f352 100644 --- a/backend/lib/Model/Property.php +++ b/backend/lib/Model/Property.php @@ -66,9 +66,11 @@ class Property { * @param string $key * @param string $value */ - public function __construct($name, $value) { + public function __construct(Model\Entity $entity, $name, $value) { $this->setName($name); $this->setValue($value); + + $this->entity = $entity; } /** @@ -90,12 +92,14 @@ class Property { */ public function getName() { return $this->name; } public function getValue() { return $this->value; } - public function getDefinition() { return PropertyDefinition::get($name); } + public function getDefinition() { return PropertyDefinition::get($this->name); } public function setValue($value) { - if (!$this->definition->validate($value)) { - throw new \Exception('invalid property value'); $this->value = $value; + if (!$this->getDefinition()->validateValue($value)) { + throw new \Exception('invalid property value'); } + + $this->value = $value; } /** diff --git a/backend/lib/Model/PropertyDefinition.php b/backend/lib/Model/PropertyDefinition.php index 5126253..855131c 100644 --- a/backend/lib/Model/PropertyDefinition.php +++ b/backend/lib/Model/PropertyDefinition.php @@ -56,6 +56,8 @@ class PropertyDefinition extends Util\Definition { */ protected $choices = array(); + protected static $definitions = NULL; + /** * File containing the JSON definitons @@ -80,11 +82,11 @@ class PropertyDefinition extends Util\Definition { break; case 'integer': - $invalid = !is_int($value); + $invalid = !is_int($value); // TODO check for numeric string break; case 'float': - $invalid = !is_float($value); + $invalid = !is_float($value); // TODO check for numeric string break; case 'multiple': @@ -95,7 +97,7 @@ class PropertyDefinition extends Util\Definition { throw new \Exception('unknown property type'); } - if ($type == 'integer' || $type == 'float') { + if ($this->type == 'integer' || $this->type == 'float') { $invalid |= isset($this->min) && $value < $this->min; $invalid |= isset($this->max) && $value > $this->max; }