2010-06-06 16:05:46 +02:00
< ? php
2010-07-21 12:44:01 +02:00
/**
* @ copyright Copyright ( c ) 2010 , The volkszaehler . org project
2010-07-22 10:32:51 +02:00
* @ package default
2010-07-21 12:44:01 +02:00
* @ license http :// www . opensource . org / licenses / gpl - license . php GNU Public License
2010-07-22 16:21:26 +02:00
*/
/*
2010-07-21 12:44:01 +02:00
* This file is part of volkzaehler . org
2010-06-06 16:05:46 +02:00
*
2010-07-21 12:44:01 +02:00
* 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 .
2010-06-06 16:05:46 +02:00
*
2010-07-21 12:44:01 +02:00
* volkzaehler . org is distributed in the hope that it will be useful ,
2010-06-06 16:05:46 +02:00
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
2010-07-21 12:44:01 +02:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
2010-06-06 16:05:46 +02:00
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2010-07-21 12:44:01 +02:00
* along with volkszaehler . org . If not , see < http :// www . gnu . org / licenses />.
2010-06-06 16:05:46 +02:00
*/
2010-07-18 17:12:00 +02:00
namespace Volkszaehler\Model ;
2010-09-19 20:48:19 +02:00
use Doctrine\ORM ;
2010-07-31 17:52:48 +02:00
use Doctrine\Common\Collections ;
2010-07-18 17:12:00 +02:00
use Volkszaehler\Util ;
2010-07-09 23:50:20 +02:00
/**
2010-08-28 03:27:14 +02:00
* Entity superclass for all objects referenced by a UUID
2010-07-22 10:32:51 +02:00
*
2010-07-21 12:44:01 +02:00
* @ author Steffen Vogel < info @ steffenvogel . de >
2010-07-22 10:32:51 +02:00
* @ package default
*
2010-07-31 17:52:48 +02:00
* @ Entity
* @ Table ( name = " entities " )
* @ InheritanceType ( " SINGLE_TABLE " )
2010-08-28 03:27:14 +02:00
* @ DiscriminatorColumn ( name = " class " , type = " string " )
* @ DiscriminatorMap ({
* " channel " = " Channel " ,
2010-09-04 01:33:32 +02:00
* " aggregator " = " Aggregator "
2010-08-28 03:27:14 +02:00
* })
* @ HasLifecycleCallbacks
2010-06-06 16:05:46 +02:00
*/
2010-07-09 23:50:20 +02:00
abstract class Entity {
/**
* @ Id
2010-07-31 17:52:48 +02:00
* @ Column ( type = " smallint " , nullable = false )
2010-07-18 17:12:00 +02:00
* @ GeneratedValue ( strategy = " AUTO " )
2010-07-09 23:50:20 +02:00
*/
protected $id ;
2010-07-22 10:32:51 +02:00
2010-08-24 15:33:55 +02:00
/** @Column(type="string", length=36, nullable=false, unique=true) */
2010-07-09 23:50:20 +02:00
protected $uuid ;
2010-07-22 10:32:51 +02:00
2010-08-28 03:27:14 +02:00
/** @Column(type="string", nullable=false) */
protected $type ;
2010-07-31 17:52:48 +02:00
/**
2010-09-04 01:33:32 +02:00
* @ OneToMany ( targetEntity = " Token " , mappedBy = " entity " , cascade = { " remove " , " persist " })
2010-07-31 17:52:48 +02:00
*/
protected $tokens = NULL ;
/**
2010-09-04 01:33:32 +02:00
* @ OneToMany ( targetEntity = " Property " , mappedBy = " entity " , cascade = { " remove " , " persist " })
2010-09-19 22:46:34 +02:00
* @ OrderBy ({ " key " = " ASC " })
2010-07-31 17:52:48 +02:00
*/
protected $properties = NULL ;
2010-09-22 20:54:51 +02:00
/**
* @ ManyToMany ( targetEntity = " Aggregator " , mappedBy = " children " )
*/
protected $parents = NULL ;
2010-08-24 15:33:55 +02:00
/**
* Constructor
*
2010-08-28 03:27:14 +02:00
* @ param string $type
2010-08-24 15:33:55 +02:00
*/
2010-09-04 01:33:32 +02:00
public function __construct ( $type ) {
2010-08-28 03:27:14 +02:00
if ( ! EntityDefinition :: exists ( $type )) {
2010-09-19 20:48:19 +02:00
throw new \Exception ( 'Unknown entity type' );
2010-08-28 03:27:14 +02:00
}
$this -> type = $type ;
2010-09-19 20:48:19 +02:00
$this -> uuid = ( string ) Util\UUID :: mint ();
2010-08-28 03:27:14 +02:00
2010-07-31 17:52:48 +02:00
$this -> tokens = new Collections\ArrayCollection ();
$this -> properties = new Collections\ArrayCollection ();
2010-09-22 20:54:51 +02:00
$this -> parents = new Collections\ArrayCollection ();
2010-07-31 17:52:48 +02:00
}
2010-09-19 20:48:19 +02:00
2010-08-24 15:33:55 +02:00
/**
2010-09-19 20:48:19 +02:00
* Checks for required and invalid properties
2010-08-28 03:27:14 +02:00
*
* @ PrePersist
2010-08-24 15:33:55 +02:00
*/
2010-09-19 20:48:19 +02:00
public function checkProperties () {
$missingProperties = array_diff ( $this -> getDefinition () -> getRequiredProperties (), array_keys ( $this -> getProperties ()));
$invalidProperties = array_diff ( array_keys ( $this -> getProperties ()), $this -> getDefinition () -> getValidProperties ());
if ( count ( $missingProperties ) > 0 ) {
throw new \Exception ( 'Entity "' . $this -> getType () . '" requires propert' . (( count ( $missingProperties ) == 1 ) ? 'y' : 'ies' ) . ': "' . implode ( ', ' , $missingProperties ) . '"' );
}
2010-08-28 03:27:14 +02:00
2010-09-19 20:48:19 +02:00
if ( count ( $invalidProperties ) > 0 ) {
throw new \Exception ( 'Propert' . (( count ( $invalidProperties ) == 1 ) ? 'y' : 'ies' ) . ' "' . implode ( ', ' , $unallowedProperties ) . '" ' . (( count ( $unallowedProperties ) == 1 ) ? 'is' : 'are' ) . ' not allowed for entity "' . $this -> getType () . '"' );
}
2010-08-28 03:27:14 +02:00
}
2010-08-24 15:33:55 +02:00
2010-07-31 17:52:48 +02:00
/**
2010-08-28 03:27:14 +02:00
* Get a property by name
2010-07-31 17:52:48 +02:00
*
2010-09-19 20:48:19 +02:00
* @ param string $key
* @ return mixed
2010-07-31 17:52:48 +02:00
*/
2010-09-19 20:48:19 +02:00
public function getProperty ( $key ) {
return $this -> findProperty ( $key ) -> getValue ();
2010-07-31 17:52:48 +02:00
}
2010-08-28 03:27:14 +02:00
/**
* Get all properties or properties by prefix
*
* @ param string $prefix
2010-09-19 20:48:19 +02:00
* @ return array
2010-08-28 03:27:14 +02:00
*/
public function getProperties ( $prefix = NULL ) {
2010-09-19 20:48:19 +02:00
$properties = array ();
foreach ( $this -> properties as $property ) {
if ( substr ( $property -> getKey (), 0 , strlen ( $prefix )) == $prefix ) {
$properties [ $property -> getKey ()] = $property -> getValue ();
}
2010-08-28 03:27:14 +02:00
}
2010-09-19 20:48:19 +02:00
return $properties ;
}
/**
*
* @ param string $key
* @ return Model\Property
*/
protected function findProperty ( $key ) {
foreach ( $this -> properties as $property ) {
if ( $property -> getKey () == $key ) {
return $property ;
}
2010-08-28 03:27:14 +02:00
}
2010-07-31 17:52:48 +02:00
}
2010-08-28 03:27:14 +02:00
/**
2010-09-19 20:48:19 +02:00
* Set property
*
* @ param string $key name of the property
* @ param mixed $value of the property
2010-08-28 03:27:14 +02:00
*/
2010-09-19 20:48:19 +02:00
public function setProperty ( $key , $value ) {
if ( $property = $this -> findProperty ( $key )) { // property already exists; just change value
$property -> setValue ( $value );
}
else { // create new property
$property = new Property ( $this , $key , $value );
$this -> properties -> add ( $property );
}
2010-07-31 17:52:48 +02:00
}
2010-08-28 03:27:14 +02:00
/**
2010-09-19 20:48:19 +02:00
* Unset property
*
2010-08-28 03:27:14 +02:00
* @ param string $name of the property
2010-09-19 20:48:19 +02:00
* @ param Doctrine\EntityManager $em
2010-08-28 03:27:14 +02:00
*/
2010-09-19 20:48:19 +02:00
public function unsetProperty ( $key , ORM\EntityManager $em ) {
$property = $this -> findProperty ( $key );
$em -> remove ( $property );
$this -> properties -> remove ( $index );
2010-07-18 17:12:00 +02:00
}
2010-07-22 10:32:51 +02:00
2010-08-28 03:27:14 +02:00
/*
* Setter & Getter
*/
2010-07-18 17:12:00 +02:00
public function getId () { return $this -> id ; } // read only
public function getUuid () { return $this -> uuid ; } // read only
2010-08-28 03:27:14 +02:00
public function getType () { return $this -> type ; } // read only
public function getDefinition () { return EntityDefinition :: get ( $this -> type ); }
2010-07-22 10:32:51 +02:00
2010-08-24 15:33:55 +02:00
/**
2010-08-28 03:27:14 +02:00
* Get interpreter to obtain data and statistical information for a given time interval
2010-08-24 15:33:55 +02:00
*
2010-08-28 03:27:14 +02:00
* @ param Doctrine\ORM\EntityManager $em
* @ param integer $from timestamp in ms since 1970
* @ param integer $to timestamp in ms since 1970
* @ return Interpreter
2010-08-24 15:33:55 +02:00
*/
2010-08-28 03:27:14 +02:00
public function getInterpreter ( \Doctrine\ORM\EntityManager $em , $from , $to ) {
2010-09-19 20:48:19 +02:00
$class = 'Volkszaehler\Interpreter\\' . $this -> getDefinition () -> getInterpreter ();
return new $class ( $this , $em , $from , $to );
2010-08-28 03:27:14 +02:00
}
}
2010-07-22 10:32:51 +02:00
?>