added first token implementations

added first property implementations
This commit is contained in:
Steffen Vogel 2010-07-31 17:52:48 +02:00
parent 67a8b1b9ca
commit 69d8e3634c
7 changed files with 235 additions and 38 deletions

View file

@ -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; }

View file

@ -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")
*/

View file

@ -23,6 +23,7 @@
namespace Volkszaehler\Model;
use Doctrine\Common\Collections;
use Volkszaehler\Util;
/**
@ -31,12 +32,16 @@ use Volkszaehler\Util;
* @author Steffen Vogel <info@steffenvogel.de>
* @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) {
}
/**

View file

@ -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; }

View file

@ -0,0 +1,68 @@
<?php
/**
* @copyright Copyright (c) 2010, The volkszaehler.org project
* @package default
* @license http://www.opensource.org/licenses/gpl-license.php GNU Public License
*/
/*
* This file is part of volkzaehler.org
*
* volkzaehler.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* volkzaehler.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Volkszaehler\Model;
use Volkszaehler\Model;
/**
* Property entity
*
* @author Steffen Vogel <info@steffenvogel.de>
* @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;
}
}
?>

View file

@ -0,0 +1,97 @@
<?php
/**
* @copyright Copyright (c) 2010, The volkszaehler.org project
* @package default
* @license http://www.opensource.org/licenses/gpl-license.php GNU Public License
*/
/*
* This file is part of volkzaehler.org
*
* volkzaehler.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* volkzaehler.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Volkszaehler\Model;
use Volkszaehler\Util;
use Volkszaehler\Model;
/**
* Token entity
*
* @author Steffen Vogel <info@steffenvogel.de>
* @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;
}
}
?>

View file

@ -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)