From 69d8e3634c11bec3f67a45f694cddfb56a43c2f9 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sat, 31 Jul 2010 17:52:48 +0200 Subject: [PATCH] added first token implementations added first property implementations --- backend/lib/Model/Channel.php | 21 ++------ backend/lib/Model/Data.php | 13 +++-- backend/lib/Model/Entity.php | 42 ++++++++++++++- backend/lib/Model/Group.php | 9 +--- backend/lib/Model/Property.php | 68 ++++++++++++++++++++++++ backend/lib/Model/Token.php | 97 ++++++++++++++++++++++++++++++++++ share/sql/mysql.sql | 23 ++++---- 7 files changed, 235 insertions(+), 38 deletions(-) create mode 100644 backend/lib/Model/Property.php create mode 100644 backend/lib/Model/Token.php diff --git a/backend/lib/Model/Channel.php b/backend/lib/Model/Channel.php index 18e844b..47f8c59 100644 --- a/backend/lib/Model/Channel.php +++ b/backend/lib/Model/Channel.php @@ -32,34 +32,21 @@ use Doctrine\Common\Collections\ArrayCollection; * @package default * * @Entity - * @Table(name="channels") */ class Channel extends Entity { - /** @Column(type="string", nullable=false) */ - protected $name; - - /** @Column(type="string", nullable=true) */ - protected $description; - - /** @Column(type="string", nullable=false) */ - protected $indicator; - /** * @OneToMany(targetEntity="Data", mappedBy="channel", cascade={"remove"}) + * @OrderBy({"timestamp" = "ASC"}) */ protected $data = NULL; - /** @Column(type="integer", nullable=true) */ - protected $resolution; - - /** @Column(type="decimal", precision="5", scale="2", nullable=true) */ - protected $cost; - /** @ManyToMany(targetEntity="Group", mappedBy="channels") */ protected $groups; /** * indicator => interpreter, unit mapping + * + * @todo replace by properties? */ protected static $indicators = array( 'power' => array('meter', 'W'), @@ -119,6 +106,8 @@ class Channel extends Entity { /** * getter & setter + * + * @todo change to new property model */ public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } diff --git a/backend/lib/Model/Data.php b/backend/lib/Model/Data.php index a78ca50..4a606da 100644 --- a/backend/lib/Model/Data.php +++ b/backend/lib/Model/Data.php @@ -36,22 +36,29 @@ use Volkszaehler\Model; * @Table(name="data") */ class Data { + /** + * @Id + * @Column(type="smallint", nullable=false) + * @GeneratedValue(strategy="AUTO") + * + * @todo wait until DDC-117 is fixed (PKs on FKs) + */ + protected $id; + /** * ending timestamp of period in ms since 1970 * - * @Id * @Column(type="bigint") */ protected $timestamp; /** - * @Column(type="decimal", precision="10", scale="5") + * @Column(type="decimal", precision="5", scale="2") * @todo change to float after DCC-67 has been closed */ protected $value; /** - * @Id * @ManyToOne(targetEntity="Channel", inversedBy="data") * @JoinColumn(name="channel_id", referencedColumnName="id") */ diff --git a/backend/lib/Model/Entity.php b/backend/lib/Model/Entity.php index 6a39c85..d869c37 100644 --- a/backend/lib/Model/Entity.php +++ b/backend/lib/Model/Entity.php @@ -23,6 +23,7 @@ namespace Volkszaehler\Model; +use Doctrine\Common\Collections; use Volkszaehler\Util; /** @@ -31,12 +32,16 @@ use Volkszaehler\Util; * @author Steffen Vogel * @package default * - * @MappedSuperclass + * @Entity + * @Table(name="entities") + * @InheritanceType("SINGLE_TABLE") + * @DiscriminatorColumn(name="type", type="string") + * @DiscriminatorMap({"channel" = "Channel", "group" = "Group"}) */ abstract class Entity { /** * @Id - * @Column(type="integer", nullable=false) + * @Column(type="smallint", nullable=false) * @GeneratedValue(strategy="AUTO") */ protected $id; @@ -44,8 +49,41 @@ abstract class Entity { /** @Column(type="string", length=36, nullable=false) */ protected $uuid; + /** + * @OneToMany(targetEntity="Token", mappedBy="entity") + */ + protected $tokens = NULL; + + /** + * @OneToMany(targetEntity="Property", mappedBy="entity") + * @OrderBy({"key" = "ASC"}) + */ + protected $properties = NULL; + public function __construct() { $this->uuid = Util\UUID::mint(); + $this->tokens = new Collections\ArrayCollection(); + $this->properties = new Collections\ArrayCollection(); + } + + /** + * + * @param unknown_type $token + */ + public function validateToken($token) { + + } + + public function getToken() { + + } + + public function getProperty($name) { + + } + + public function setProperty($name) { + } /** diff --git a/backend/lib/Model/Group.php b/backend/lib/Model/Group.php index 17561d5..bcd6513 100644 --- a/backend/lib/Model/Group.php +++ b/backend/lib/Model/Group.php @@ -35,15 +35,8 @@ use Doctrine\Common\Collections\ArrayCollection; * @package default * * @Entity - * @Table(name="groups") */ class Group extends Entity { - /** @Column(type="string", nullable=false) */ - protected $name; - - /** @Column(type="string", nullable=true) */ - protected $description; - /** * @ManyToMany(targetEntity="Channel", inversedBy="groups") * @JoinTable(name="groups_channel", @@ -111,6 +104,8 @@ class Group extends Entity { /** * getter & setter + * + * @todo change to new property model */ public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } diff --git a/backend/lib/Model/Property.php b/backend/lib/Model/Property.php new file mode 100644 index 0000000..9c094ad --- /dev/null +++ b/backend/lib/Model/Property.php @@ -0,0 +1,68 @@ +. + */ + +namespace Volkszaehler\Model; + +use Volkszaehler\Model; + +/** + * Property entity + * + * @author Steffen Vogel + * @package default + * + * @Entity + * @Table(name="properties") + */ +class Property { + /** + * @Id + * @Column(type="smallint", nullable=false) + * @GeneratedValue(strategy="AUTO") + * + * @todo wait until DDC-117 is fixed (PKs on FKs) + */ + protected $id; + + /** @Column(type="string", nullable=false) */ + protected $name; + + /** @Column(type="string", nullable=false) */ + protected $value; + + /** @ManyToOne(targetEntity="Entity", inversedBy="properties") */ + protected $entity; + + /** + * Constructor + * + * @param string $key + * @param string $value + */ + public function __construct($name, $value) { + $this->name = (string) $name; + $this->value = (string) $value; + } +} + +?> diff --git a/backend/lib/Model/Token.php b/backend/lib/Model/Token.php new file mode 100644 index 0000000..e32cc5c --- /dev/null +++ b/backend/lib/Model/Token.php @@ -0,0 +1,97 @@ +. + */ + +namespace Volkszaehler\Model; + +use Volkszaehler\Util; + +use Volkszaehler\Model; + +/** + * Token entity + * + * @author Steffen Vogel + * @package default + * + * @Entity + * @Table(name="tokens") + */ +class Token { + /** + * @Id + * @Column(type="smallint", nullable=false) + * @GeneratedValue(strategy="AUTO") + * + * @todo wait until DDC-117 is fixed (PKs on FKs) + */ + protected $id; + + /** + * @Column(type="string", nullable=false, unique=true) + */ + protected $token; + + /** + * var integer timestamp until token is valid + * + * @Column(type="bigint") + * @todo implement + */ + protected $valid; + + /** + * @ManyToOne(targetEntity="Entity", inversedBy="tokens") + */ + protected $entity; + + const LENGTH = 10; + + protected static $chars = array( + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'v', 'u', 'w', 'x', 'y', 'z', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'V', 'U', 'W', 'X', 'Y', 'Z' + ); + + /** + * Constructor + * + * @param integer $length of the token + */ + public function __construct() { + $this->token = self::generate(); + } + + protected static function generate($length = Token::LENGTH) { + return Util\Random::getString(self::$chars, $length); + } + + public function __toString() { + return $this->getToken(); + } + + public function getToken() { + return $this->token; + } +} + +?> diff --git a/share/sql/mysql.sql b/share/sql/mysql.sql index d360144..327e6f1 100644 --- a/share/sql/mysql.sql +++ b/share/sql/mysql.sql @@ -1,10 +1,13 @@ -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 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; -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) +CREATE TABLE entities (id SMALLINT NOT NULL, uuid VARCHAR(36) NOT NULL, type VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; +CREATE TABLE groups_channel (group_id SMALLINT NOT NULL, channel_id SMALLINT NOT NULL, PRIMARY KEY(group_id, channel_id)) ENGINE = InnoDB; +CREATE TABLE groups_groups (parent_id SMALLINT NOT NULL, child_id SMALLINT NOT NULL, PRIMARY KEY(parent_id, child_id)) ENGINE = InnoDB; +CREATE TABLE data (id SMALLINT NOT NULL, channel_id SMALLINT DEFAULT NULL, timestamp BIGINT NOT NULL, value NUMERIC(5, 2) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; +CREATE TABLE tokens (id SMALLINT NOT NULL, entity_id SMALLINT DEFAULT NULL, token VARCHAR(255) NOT NULL, valid BIGINT NOT NULL, UNIQUE INDEX tokens_token_uniq (token), PRIMARY KEY(id)) ENGINE = InnoDB; +CREATE TABLE properties (id SMALLINT NOT NULL, entity_id SMALLINT DEFAULT NULL, name VARCHAR(255) NOT NULL, value VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; +ALTER TABLE groups_channel ADD FOREIGN KEY (group_id) REFERENCES entities(id); +ALTER TABLE groups_channel ADD FOREIGN KEY (channel_id) REFERENCES entities(id); +ALTER TABLE groups_groups ADD FOREIGN KEY (parent_id) REFERENCES entities(id); +ALTER TABLE groups_groups ADD FOREIGN KEY (child_id) REFERENCES entities(id); +ALTER TABLE data ADD FOREIGN KEY (channel_id) REFERENCES entities(id); +ALTER TABLE tokens ADD FOREIGN KEY (entity_id) REFERENCES entities(id); +ALTER TABLE properties ADD FOREIGN KEY (entity_id) REFERENCES entities(id)