diff --git a/datamodels/2.x/itop-config-mgmt/datamodel.itop-config-mgmt.xml b/datamodels/2.x/itop-config-mgmt/datamodel.itop-config-mgmt.xml
index acea8c11d..0403cfc46 100755
--- a/datamodels/2.x/itop-config-mgmt/datamodel.itop-config-mgmt.xml
+++ b/datamodels/2.x/itop-config-mgmt/datamodel.itop-config-mgmt.xml
@@ -5828,30 +5828,69 @@
Overload-cmdbAbstractObject
Get('connectableci_id'));
- if (is_object($oDevice) && (get_class($oDevice) == 'NetworkDevice'))
- {
- // Note: in case a port has been changed, search with the original values
- $sOQL = "SELECT lnkConnectableCIToNetworkDevice WHERE connectableci_id = :device AND networkdevice_id = :network AND network_port = :nwport AND device_port = :devport";
+ $aFields = array('networkdevice_id','connectableci_id','network_port','device_port','connection_type');
+ $aChanges = $this->ListPreviousValuesForUpdatedAttributes();
+ $aPrev = array(); // Previous values of the current link object before it was modified
+ foreach ($aFields as $sFieldCode) {
+ $aPrev[$sFieldCode] = array_key_exists($sFieldCode, $aChanges) ? $aChanges[$sFieldCode] : $this->Get($sFieldCode);
+ }
+ $sPrevLink = ($aPrev['connection_type'] == 'uplink') ? 'downlink' : 'uplink';
+ $sConnLink = ($this->Get('connection_type') == 'uplink') ? 'downlink' : 'uplink';
+
+ $oNewDevice = MetaModel::GetObject('ConnectableCI', $this->Get('connectableci_id'), false);
+ $oPrevDevice = MetaModel::GetObject('ConnectableCI', $aPrev['connectableci_id'], false);
+ $bNew = (is_object($oNewDevice) && (get_class($oNewDevice) == 'NetworkDevice'));
+ $bPrev = (is_object($oPrevDevice) && (get_class($oPrevDevice) == 'NetworkDevice'));
+ $sOQL = "SELECT lnkConnectableCIToNetworkDevice WHERE connectableci_id = :device AND networkdevice_id = :network AND network_port = :nwport AND device_port = :devport AND connection_type = :link";
+
+ if ($bPrev) { // There was a twin
+ // Retrieve twin link using previous values of the current link
+ $oConnectionSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL),
+ array(),
+ array(
+ 'network' => $aPrev['connectableci_id'],
+ 'device' => $aPrev['networkdevice_id'],
+ 'devport' => $aPrev['network_port'],
+ 'nwport' => $aPrev['device_port'],
+ 'link' => $sPrevLink,
+ )
+ );
+ if ($bNew) { // and a twin must still exist, so update the existing
+ while ($oConnection = $oConnectionSet->Fetch()) {
+ $oConnection->Set('networkdevice_id', $this->Get('connectableci_id'));
+ $oConnection->Set('connectableci_id', $this->Get('networkdevice_id'));
+ $oConnection->Set('network_port', $this->Get('device_port'));
+ $oConnection->Set('device_port', $this->Get('network_port'));
+ $oConnection->Set('connection_type',$sConnLink);
+ $oConnection->DBUpdate();
+ }
+ }
+ else { // and no twin is needed anymore, so delete the existing
+ while ($oConnection = $oConnectionSet->Fetch()) {
+ $oConnection->DBDelete();
+ }
+ }
+ }
+ elseif ($bNew) { // There was no twin but a twin must exist now
+ // Search for a twin link using current values inverted
$oConnectionSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL),
array(),
array(
'network' => $this->Get('connectableci_id'),
'device' => $this->Get('networkdevice_id'),
- 'devport' => $this->GetOriginal('network_port'),
- 'nwport' => $this->GetOriginal('device_port'),
+ 'devport' => $this->Get('device_port'),
+ 'nwport' => $this->Get('network_port'),
+ 'link' => $sConnLink,
)
);
- $sLink = $this->Get('connection_type');
- $sConnLink = ($sLink == 'uplink') ? 'downlink' : 'uplink';
-
- // There should be one link - do it in a safe manner anyway
- while ($oConnection = $oConnectionSet->Fetch())
- {
- $oConnection->Set('connection_type', $sConnLink);
- $oConnection->Set('network_port', $this->Get('device_port'));
- $oConnection->Set('device_port', $this->Get('network_port'));
- $oConnection->DBUpdate();
+ if ($oConnectionSet->Count() == 0) {
+ $oNewLink = new lnkConnectableCIToNetworkDevice();
+ $oNewLink->Set('networkdevice_id', $this->Get('connectableci_id'));
+ $oNewLink->Set('connectableci_id', $this->Get('networkdevice_id'));
+ $oNewLink->Set('network_port', $this->Get('device_port'));
+ $oNewLink->Set('device_port', $this->Get('network_port'));
+ $oNewLink->Set('connection_type', $sConnLink);
+ $oNewLink->DBInsert();
}
}
}]]>