diff --git a/backend/lib/Model/Channel.php b/backend/lib/Model/Channel.php index 1e3566a..ae81f38 100644 --- a/backend/lib/Model/Channel.php +++ b/backend/lib/Model/Channel.php @@ -35,13 +35,13 @@ use Doctrine\Common\Collections\ArrayCollection; * @Table(name="channels") */ class Channel extends Entity { - /** @Column(type="string") */ + /** @Column(type="string", nullable=false) */ protected $name; - /** @Column(type="string") */ + /** @Column(type="string", nullable=true) */ protected $description; - /** @Column(type="string") */ + /** @Column(type="string", nullable=false) */ protected $indicator; /** @@ -49,10 +49,10 @@ class Channel extends Entity { */ protected $data = NULL; - /** @Column(type="integer") */ + /** @Column(type="integer", nullable=true) */ protected $resolution; - /** @Column(type="decimal", precision="5", scale="2") */ + /** @Column(type="decimal", precision="5", scale="2", nullable=true) */ protected $cost; /** @@ -74,7 +74,7 @@ class Channel extends Entity { parent::__construct(); if (!in_array($indicator, self::$indicators)) { - throw new \InvalidArgumentException($indicator . ' is no known indicator'); + throw new \Exception($indicator . ' is no known indicator'); } $this->indicator = $indicator; @@ -95,7 +95,7 @@ class Channel extends Entity { public function getInterpreter(\Doctrine\ORM\EntityManager $em) { $interpreterClassName = 'Volkszaehler\Interpreter\\' . ucfirst(self::$indicators[$this->indicator][0]) . 'Interpreter'; if (!(\Volkszaehler\Util\ClassLoader::classExists($interpreterClassName)) || !is_subclass_of($interpreterClassName, '\Volkszaehler\Interpreter\Interpreter')) { - throw new \InvalidArgumentException('\'' . $interpreterClassName . '\' is not a valid Interpreter'); + throw new \Exception('\'' . $interpreterClassName . '\' is not a valid Interpreter'); } return new $interpreterClassName($this, $em); } diff --git a/backend/lib/Model/Entity.php b/backend/lib/Model/Entity.php index 525f26b..6b5fa2d 100644 --- a/backend/lib/Model/Entity.php +++ b/backend/lib/Model/Entity.php @@ -36,16 +36,16 @@ use Volkszaehler\Util; abstract class Entity { /** * @Id - * @Column(type="integer") + * @Column(type="integer", nullable=false) * @GeneratedValue(strategy="AUTO") */ protected $id; - /** @Column(type="string", length=36) */ + /** @Column(type="string", length=36, nullable=false) */ protected $uuid; public function __construct() { - $this->uuid = Util\Uuid::mint(); + $this->uuid = Util\UUID::mint(); } /** diff --git a/backend/lib/Model/Group.php b/backend/lib/Model/Group.php index c34c145..b241ba3 100644 --- a/backend/lib/Model/Group.php +++ b/backend/lib/Model/Group.php @@ -23,6 +23,8 @@ namespace Volkszaehler\Model; +use Doctrine\Common\Collections; + use Doctrine\Common\Collections\ArrayCollection; /** @@ -35,10 +37,10 @@ use Doctrine\Common\Collections\ArrayCollection; * @Table(name="groups") */ class Group extends Entity { - /** @Column(type="string") */ + /** @Column(type="string", nullable=false) */ protected $name; - /** @Column(type="string") */ + /** @Column(type="string", nullable=true) */ protected $description; /** @@ -69,6 +71,27 @@ class Group extends Entity { $this->children = new ArrayCollection(); } + /** + * adds group as new child + * + * @param Group $child + * @todo check against endless recursion + * @todo check if the group is already member of the group + */ + public function addGroup(Group $child) { + $this->children->add($child); + } + + /** + * adds channel as new child + * + * @param Channel $child + * @todo check if the channel is already member of the group + */ + public function addChannel(Channel $child) { + $this->channels->add($child); + } + /** * getter & setter */ @@ -76,6 +99,8 @@ class Group extends Entity { public function setName($name) { $this->name = $name; } public function getDescription() { return $this->description; } public function setDescription($description) { $this->description = $description; } + public function getSubGroups() { return $this->children; } + public function getChannels() { return $this->channels; } } ?> \ No newline at end of file diff --git a/share/sql/mysql.sql b/share/sql/mysql.sql index 87d43bc..d360144 100644 --- a/share/sql/mysql.sql +++ b/share/sql/mysql.sql @@ -1,14 +1,10 @@ -CREATE TABLE channels (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(255) NOT NULL, indicator VARCHAR(255) NOT NULL, resolution INT NOT NULL, cost NUMERIC(5, 2) NOT NULL, uuid VARCHAR(36) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; CREATE TABLE data (timestamp BIGINT NOT NULL, channel_id INT DEFAULT NULL, value NUMERIC(10, 5) NOT NULL, PRIMARY KEY(timestamp)) ENGINE = InnoDB; -CREATE TABLE groups (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(255) NOT NULL, uuid VARCHAR(36) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; +CREATE TABLE channels (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(255) DEFAULT NULL, indicator VARCHAR(255) NOT NULL, resolution INT DEFAULT NULL, cost NUMERIC(5, 2) DEFAULT NULL, uuid VARCHAR(36) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; +CREATE TABLE groups (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(255) DEFAULT NULL, uuid VARCHAR(36) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; CREATE TABLE groups_channel (group_id INT NOT NULL, channel_id INT NOT NULL, PRIMARY KEY(group_id, channel_id)) ENGINE = InnoDB; CREATE TABLE groups_groups (parent_id INT NOT NULL, child_id INT NOT NULL, PRIMARY KEY(parent_id, child_id)) ENGINE = InnoDB; -CREATE TABLE users (id INT AUTO_INCREMENT NOT NULL, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, uuid VARCHAR(36) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; -CREATE TABLE groups_users (user_id INT NOT NULL, group_id INT NOT NULL, PRIMARY KEY(user_id, group_id)) ENGINE = InnoDB; ALTER TABLE data ADD FOREIGN KEY (channel_id) REFERENCES channels(id); ALTER TABLE groups_channel ADD FOREIGN KEY (group_id) REFERENCES groups(id); ALTER TABLE groups_channel ADD FOREIGN KEY (channel_id) REFERENCES channels(id); ALTER TABLE groups_groups ADD FOREIGN KEY (parent_id) REFERENCES groups(id); -ALTER TABLE groups_groups ADD FOREIGN KEY (child_id) REFERENCES groups(id); -ALTER TABLE groups_users ADD FOREIGN KEY (user_id) REFERENCES users(id); -ALTER TABLE groups_users ADD FOREIGN KEY (group_id) REFERENCES groups(id) +ALTER TABLE groups_groups ADD FOREIGN KEY (child_id) REFERENCES groups(id)