SyncRule: make ready for basket

refs #1630
This commit is contained in:
Thomas Gelf 2018-10-06 17:14:53 +02:00
parent 063a46ea56
commit f32e568b8a
1 changed files with 39 additions and 13 deletions

View File

@ -6,16 +6,17 @@ use Icinga\Application\Benchmark;
use Icinga\Data\Filter\Filter; use Icinga\Data\Filter\Filter;
use Icinga\Module\Director\Data\Db\DbObject; use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Db; use Icinga\Module\Director\Db;
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
use Icinga\Module\Director\Exception\DuplicateKeyException; use Icinga\Module\Director\Exception\DuplicateKeyException;
use Icinga\Module\Director\Import\PurgeStrategy\PurgeStrategy; use Icinga\Module\Director\Import\PurgeStrategy\PurgeStrategy;
use Icinga\Module\Director\Import\Sync; use Icinga\Module\Director\Import\Sync;
use Exception; use Exception;
class SyncRule extends DbObject class SyncRule extends DbObject implements ExportInterface
{ {
protected $table = 'sync_rule'; protected $table = 'sync_rule';
protected $keyName = 'id'; protected $keyName = 'rule_name';
protected $autoincKeyName = 'id'; protected $autoincKeyName = 'id';
@ -58,6 +59,8 @@ class SyncRule extends DbObject
private $newSyncProperties; private $newSyncProperties;
private $originalId;
public function listInvolvedSourceIds() public function listInvolvedSourceIds()
{ {
if (! $this->hasBeenLoadedFromDb()) { if (! $this->hasBeenLoadedFromDb()) {
@ -81,7 +84,7 @@ class SyncRule extends DbObject
$sources = []; $sources = [];
foreach ($this->listInvolvedSourceIds() as $sourceId) { foreach ($this->listInvolvedSourceIds() as $sourceId) {
$sources[$sourceId] = ImportSource::load($sourceId, $this->getConnection()); $sources[$sourceId] = ImportSource::loadWithAutoIncId($sourceId, $this->getConnection());
} }
return $sources; return $sources;
@ -246,16 +249,17 @@ class SyncRule extends DbObject
public function export() public function export()
{ {
$plain = (object) $this->getProperties(); $plain = $this->getProperties();
$plain->originalId = $plain->id; $plain['originalId'] = $plain['id'];
unset($plain->id); unset($plain['id']);
foreach ($this->stateProperties as $key) { foreach ($this->stateProperties as $key) {
unset($plain->$key); unset($plain[$key]);
} }
$plain->properties = $this->exportSyncProperties(); $plain['properties'] = $this->exportSyncProperties();
ksort($plain);
return $plain; return (object) $plain;
} }
/** /**
@ -269,15 +273,21 @@ class SyncRule extends DbObject
public static function import($plain, Db $db, $replace = false) public static function import($plain, Db $db, $replace = false)
{ {
$properties = (array) $plain; $properties = (array) $plain;
$id = $properties['originalId']; if (isset($properties['originalId'])) {
unset($properties['originalId']); $id = $properties['originalId'];
unset($properties['originalId']);
} else {
$id = null;
}
$name = $properties['rule_name']; $name = $properties['rule_name'];
if ($replace && static::existsWithNameAndId($name, $id, $db)) { if ($replace && static::existsWithNameAndId($name, $id, $db)) {
$object = static::loadWithAutoIncId($id, $db); $object = static::loadWithAutoIncId($id, $db);
} elseif ($replace && static::exists($name, $db)) {
$object = static::load($name, $db);
} elseif (static::existsWithName($name, $db)) { } elseif (static::existsWithName($name, $db)) {
throw new DuplicateKeyException( throw new DuplicateKeyException(
'Import Source %s already exists', 'Sync Rule %s already exists',
$name $name
); );
} else { } else {
@ -287,10 +297,19 @@ class SyncRule extends DbObject
$object->newSyncProperties = $properties['properties']; $object->newSyncProperties = $properties['properties'];
unset($properties['properties']); unset($properties['properties']);
$object->setProperties($properties); $object->setProperties($properties);
if ($id !== null && (int) $id !== (int) $object->get('id')) {
$object->originalId = $object->get('id');
$object->reallySet('id', $id);
}
return $object; return $object;
} }
public function getUniqueIdentifier()
{
return $this->get('rule_name');
}
/** /**
* @throws DuplicateKeyException * @throws DuplicateKeyException
*/ */
@ -301,9 +320,15 @@ class SyncRule extends DbObject
$connection = $this->getConnection(); $connection = $this->getConnection();
$db = $connection->getDbAdapter(); $db = $connection->getDbAdapter();
$myId = $this->get('id'); $myId = $this->get('id');
if ($this->originalId === null) {
$originalId = $myId;
} else {
$originalId = $this->originalId;
$this->originalId = null;
}
if ($this->hasBeenLoadedFromDb()) { if ($this->hasBeenLoadedFromDb()) {
$db->delete( $db->delete(
'sync_rule_property', 'sync_property',
$db->quoteInto('rule_id = ?', $myId) $db->quoteInto('rule_id = ?', $myId)
); );
} }
@ -331,6 +356,7 @@ class SyncRule extends DbObject
unset($properties['id']); unset($properties['id']);
unset($properties['rule_id']); unset($properties['rule_id']);
unset($properties['source_id']); unset($properties['source_id']);
ksort($properties);
$all[] = (object) $properties; $all[] = (object) $properties;
} }