/**
  * Add metadata.
  *
  * Add a new matadata entry to the entity.
  *
  * @param string $key   The metadata key
  * @param string $value The metadata value
  *
  * @return sspmod_janus_Metadata The metadata.
  * @todo Make independent of type (make generic, support for more types than 
  * sp and idp)
  */
 public function addMetadata($key, $value)
 {
     if ($value === null || $value === '') {
         return false;
     }
     assert('is_string($key);');
     assert('$this->_entity instanceof Sspmod_Janus_Entity');
     $mb = new sspmod_janus_MetadataFieldBuilder($this->_config->getArray('metadatafields.' . $this->_entity->getType()));
     $fieldDefinitions = $mb->getMetadataFields();
     // Check if metadata is allowed
     if (!array_key_exists($key, $fieldDefinitions)) {
         SimpleSAML_Logger::info(__CLASS__ . ':addMetadata - Metadata key \'' . $key . ' not allowed');
         return false;
     }
     $fieldDefinition = $fieldDefinitions[$key];
     if (empty($this->_metadata)) {
         if (!$this->loadEntity()) {
             return false;
         }
     }
     $st = $this->execute('SELECT count(*) AS count 
         FROM ' . $this->getTablePrefix() . 'metadata
         WHERE `connectionRevisionId` = ? AND `key` = ?;', array($this->_entity->getId(), $key));
     if ($st === false) {
         SimpleSAML_Logger::error(__CLASS__ . ':addMetadata - Count check failed');
         return false;
     }
     $row = $st->fetchAll(PDO::FETCH_ASSOC);
     if ($row[0]['count'] > 0) {
         SimpleSAML_Logger::error(__CLASS__ . ':addMetadata - Metadata already exists');
         return false;
     }
     if ($fieldDefinition->getType() === 'select') {
         $allowedSelectValues = $fieldDefinition->getSelectValues();
         if (!in_array($value, $allowedSelectValues)) {
             SimpleSAML_Logger::error(__CLASS__ . ':addMetadata - Value: ' . $value . ' not allowed for field ' . $key);
             return false;
         }
     }
     $metadata = new sspmod_janus_Metadata($fieldDefinition, $key, $value);
     $metadata->setConnectionRevisionId($this->_entity->getId());
     $this->_metadata[] = $metadata;
     $this->_modified = true;
     // The metadata is not saved, since it is not part of the current
     // entity with current revision id
     return $metadata;
 }
 /**
  * Add metadata.
  *
  * Add a new matadata entry to the entity.
  *
  * @param string $key   The metadata key
  * @param string $value The metadata value
  *
  * @return sspmod_janus_Metadata The metadata.
  * @todo Make independent of type (make generic, support for more types than 
  * sp and idp)
  */
 public function addMetadata($key, $value)
 {
     assert('is_string($key);');
     //assert('is_string($value);');
     assert('$this->_entity instanceof Sspmod_Janus_Entity');
     $allowedfields = array();
     $mb = new sspmod_janus_MetadatafieldBuilder($this->_config->getArray('metadatafields.' . $this->_entity->getType()));
     $allowedfields = $mb->getMetadatafields();
     // Check if metadata is allowed
     if (!array_key_exists($key, $allowedfields)) {
         SimpleSAML_Logger::info('JANUS:EntityController:addMetadata - Metadata key \'' . $key . ' not allowed');
         return false;
     }
     if (empty($this->_metadata)) {
         if (!$this->loadEntity()) {
             return false;
         }
     }
     $st = $this->execute('SELECT count(*) AS count 
         FROM ' . self::$prefix . 'metadata 
         WHERE `eid` = ? AND `revisionid` = ? AND `key` = ?;', array($this->_entity->getEid(), $this->_entity->getRevisionid(), $key));
     if ($st === false) {
         SimpleSAML_Logger::error('JANUS:EntityController:addMetadata - Count check failed');
         return false;
     }
     $row = $st->fetchAll(PDO::FETCH_ASSOC);
     if ($row[0]['count'] > 0) {
         SimpleSAML_Logger::error('JANUS:EntityController:addMetadata - Metadata already exists');
         return false;
     }
     if ($allowedfields[$key]->type == 'select') {
         $allowedselectvalues = $allowedfields[$key]->select_values;
         if (!in_array($value, $allowedselectvalues)) {
             SimpleSAML_Logger::error('JANUS:EntityController:addMetadata - Value: ' . $value . ' not allowed for field ' . $key);
             return false;
         }
     }
     $metadata = new sspmod_janus_Metadata($this->_config->getValue('store'));
     $metadata->setEid($this->_entity->getEid());
     // Revision id is not set, since it is not save to the db and hence it
     // do not have a reversionid
     $metadata->setKey($key);
     $metadata->setValue($value);
     $this->_metadata[] = $metadata;
     $this->_modified = true;
     // The metadata is not saved, since it is not part of the current
     // entity with current revision id
     return $metadata;
 }