PriorityColumn: add new object extension, use...

...it for SyncProperty
This commit is contained in:
Thomas Gelf 2017-08-20 15:04:06 +02:00
parent 3dce2a5d06
commit 4c7b940705
4 changed files with 59 additions and 26 deletions

View File

@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Forms;
use Exception;
use Icinga\Module\Director\Hook\ImportSourceHook;
use Icinga\Module\Director\Objects\SyncProperty;
use Icinga\Module\Director\Objects\SyncRule;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\ImportSource;
@ -355,6 +356,7 @@ class SyncPropertyForm extends DirectorObjectForm
public function onSuccess()
{
/** @var SyncProperty $object */
$object = $this->getObject();
$object->set('rule_id', $this->rule->get('id')); // ?!
@ -363,7 +365,6 @@ class SyncPropertyForm extends DirectorObjectForm
}
$sourceColumn = $this->getValue('source_column');
unset($this->source_column);
$this->removeElement('source_column');
if ($sourceColumn !== self::EXPRESSION) {
@ -376,13 +377,6 @@ class SyncPropertyForm extends DirectorObjectForm
$object->destination_field = 'vars.' . $destination;
}
if ($object->hasBeenModified()) {
if (! $object->hasBeenLoadedFromDb()) {
$object->priority = $this->rule->getPriorityForNextProperty();
}
}
return parent::onSuccess();
}

View File

@ -0,0 +1,40 @@
<?php
namespace Icinga\Module\Director\Objects\Extension;
use Zend_Db_Expr as Expr;
trait PriorityColumn
{
public function setNextPriority($prioSetColumn = null, $prioColumn = 'priority')
{
/** @var \Zend_Db_Adapter_Abstract $db */
$db = $this->getDb();
$prioValue = '(CASE WHEN MAX(priosub.priority) IS NULL THEN 1'
. ' ELSE MAX(priosub.priority) + 1 END)';
$query = $db->select()
->from(
['priosub' => $this->getTableName()],
"$prioValue"
);
if ($prioSetColumn !== null) {
$query->where("priosub.$prioSetColumn = ?", $this->get($prioSetColumn));
}
$this->set($prioColumn, new Expr('(' . $query . ')'));
return $this;
}
protected function refreshPriortyProperty($prioColumn = 'priority')
{
/** @var \Zend_Db_Adapter_Abstract $db */
$db = $this->getDb();
$idCol = $this->getAutoincKeyName();
$query = $db->select()
->from($this->getTableName(), $prioColumn)
->where("$idCol = ?", $this->get($idCol));
$this->reallySet($prioColumn, $db->fetchOne($query));
}
}

View File

@ -3,16 +3,19 @@
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Objects\Extension\PriorityColumn;
class SyncProperty extends DbObject
{
use PriorityColumn;
protected $table = 'sync_property';
protected $keyName = 'id';
protected $autoincKeyName = 'id';
protected $defaultProperties = array(
protected $defaultProperties = [
'id' => null,
'rule_id' => null,
'source_id' => null,
@ -21,5 +24,17 @@ class SyncProperty extends DbObject
'priority' => null,
'filter_expression' => null,
'merge_policy' => null
);
];
protected function beforeStore()
{
if (! $this->hasBeenLoadedFromDb()) {
$this->setNextPriority('rule_id');
}
}
protected function onInsert()
{
$this->refreshPriortyProperty();
}
}

View File

@ -111,22 +111,6 @@ class SyncRule extends DbObject
return $db->fetchOne($query);
}
public function getPriorityForNextProperty()
{
if (! $this->hasBeenLoadedFromDb()) {
return 1;
}
$db = $this->getDb();
return $db->fetchOne(
$db->select()
->from(
array('p' => 'sync_property'),
array('priority' => '(CASE WHEN MAX(p.priority) IS NULL THEN 1 ELSE MAX(p.priority) + 1 END)')
)->where('p.rule_id = ?', $this->get('id'))
);
}
public function matches($row)
{
if ($this->get('filter_expression') === null) {