bulkchange.class.inc.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Bulk change facility (common to interactive and batch usages)
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. /**
  25. * BulkChange
  26. * Interpret a given data set and update the DB accordingly (fake mode avail.)
  27. *
  28. * @package iTopORM
  29. */
  30. class BulkChangeException extends CoreException
  31. {
  32. }
  33. /**
  34. * CellChangeSpec
  35. * A series of classes, keeping the information about a given cell: could it be changed or not (and why)?
  36. *
  37. * @package iTopORM
  38. */
  39. abstract class CellChangeSpec
  40. {
  41. protected $m_proposedValue;
  42. protected $m_sOql; // in case of ambiguity
  43. public function __construct($proposedValue, $sOql = '')
  44. {
  45. $this->m_proposedValue = $proposedValue;
  46. $this->m_sOql = $sOql;
  47. }
  48. static protected function ValueAsHtml($value)
  49. {
  50. if (MetaModel::IsValidObject($value))
  51. {
  52. return $value->GetHyperLink();
  53. }
  54. else
  55. {
  56. return htmlentities($value, ENT_QUOTES, 'UTF-8');
  57. }
  58. }
  59. public function GetValue()
  60. {
  61. return $this->m_proposedValue;
  62. }
  63. public function GetOql()
  64. {
  65. return $this->m_sOql;
  66. }
  67. abstract public function GetDescription();
  68. }
  69. class CellStatus_Void extends CellChangeSpec
  70. {
  71. public function GetDescription()
  72. {
  73. return '';
  74. }
  75. }
  76. class CellStatus_Modify extends CellChangeSpec
  77. {
  78. protected $m_previousValue;
  79. public function __construct($proposedValue, $previousValue)
  80. {
  81. $this->m_previousValue = $previousValue;
  82. parent::__construct($proposedValue);
  83. }
  84. public function GetDescription()
  85. {
  86. return 'Modified';
  87. }
  88. public function GetPreviousValue()
  89. {
  90. return $this->m_previousValue;
  91. }
  92. }
  93. class CellStatus_Issue extends CellStatus_Modify
  94. {
  95. protected $m_sReason;
  96. public function __construct($proposedValue, $previousValue, $sReason)
  97. {
  98. $this->m_sReason = $sReason;
  99. parent::__construct($proposedValue, $previousValue);
  100. }
  101. public function GetDescription()
  102. {
  103. if (is_null($this->m_proposedValue))
  104. {
  105. return 'Could not be changed - reason: '.$this->m_sReason;
  106. }
  107. return 'Could not be changed to '.$this->m_proposedValue.' - reason: '.$this->m_sReason;
  108. }
  109. }
  110. class CellStatus_SearchIssue extends CellStatus_Issue
  111. {
  112. public function __construct()
  113. {
  114. parent::__construct(null, null, null);
  115. }
  116. public function GetDescription()
  117. {
  118. return 'No match';
  119. }
  120. }
  121. class CellStatus_NullIssue extends CellStatus_Issue
  122. {
  123. public function __construct()
  124. {
  125. parent::__construct(null, null, null);
  126. }
  127. public function GetDescription()
  128. {
  129. return 'Missing mandatory value';
  130. }
  131. }
  132. class CellStatus_Ambiguous extends CellStatus_Issue
  133. {
  134. protected $m_iCount;
  135. public function __construct($previousValue, $iCount, $sOql)
  136. {
  137. $this->m_iCount = $iCount;
  138. $this->m_sQuery = $sOql;
  139. parent::__construct(null, $previousValue, '');
  140. }
  141. public function GetDescription()
  142. {
  143. $sCount = $this->m_iCount;
  144. return "Ambiguous: found $sCount objects";
  145. }
  146. }
  147. /**
  148. * RowStatus
  149. * A series of classes, keeping the information about a given row: could it be changed or not (and why)?
  150. *
  151. * @package iTopORM
  152. */
  153. abstract class RowStatus
  154. {
  155. public function __construct()
  156. {
  157. }
  158. abstract public function GetDescription();
  159. }
  160. class RowStatus_NoChange extends RowStatus
  161. {
  162. public function GetDescription()
  163. {
  164. return "unchanged";
  165. }
  166. }
  167. class RowStatus_NewObj extends RowStatus
  168. {
  169. public function GetDescription()
  170. {
  171. return "created";
  172. }
  173. }
  174. class RowStatus_Modify extends RowStatus
  175. {
  176. protected $m_iChanged;
  177. public function __construct($iChanged)
  178. {
  179. $this->m_iChanged = $iChanged;
  180. }
  181. public function GetDescription()
  182. {
  183. return "updated ".$this->m_iChanged." cols";
  184. }
  185. }
  186. class RowStatus_Disappeared extends RowStatus_Modify
  187. {
  188. public function GetDescription()
  189. {
  190. return "disappeared, changed ".$this->m_iChanged." cols";
  191. }
  192. }
  193. class RowStatus_Issue extends RowStatus
  194. {
  195. protected $m_sReason;
  196. public function __construct($sReason)
  197. {
  198. $this->m_sReason = $sReason;
  199. }
  200. public function GetDescription()
  201. {
  202. return 'Issue: '.$this->m_sReason;
  203. }
  204. }
  205. /**
  206. * BulkChange
  207. *
  208. * @package iTopORM
  209. */
  210. class BulkChange
  211. {
  212. protected $m_sClass;
  213. protected $m_aData; // Note: hereafter, iCol maybe actually be any acceptable key (string)
  214. // #@# todo: rename the variables to sColIndex
  215. protected $m_aAttList; // attcode => iCol
  216. protected $m_aExtKeys; // aExtKeys[sExtKeyAttCode][sExtReconcKeyAttCode] = iCol;
  217. protected $m_aReconcilKeys; // attcode (attcode = 'id' for the pkey)
  218. protected $m_sSynchroScope; // OQL - if specified, then the missing items will be reported
  219. protected $m_aOnDisappear; // array of attcode => value, values to be set when an object gets out of scope (ignored if no scope has been defined)
  220. public function __construct($sClass, $aData, $aAttList, $aExtKeys, $aReconcilKeys, $sSynchroScope = null, $aOnDisappear = null)
  221. {
  222. $this->m_sClass = $sClass;
  223. $this->m_aData = $aData;
  224. $this->m_aAttList = $aAttList;
  225. $this->m_aReconcilKeys = $aReconcilKeys;
  226. $this->m_aExtKeys = $aExtKeys;
  227. $this->m_sSynchroScope = $sSynchroScope;
  228. $this->m_aOnDisappear = $aOnDisappear;
  229. }
  230. protected function ResolveExternalKey($aRowData, $sAttCode, &$aResults)
  231. {
  232. $oExtKey = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  233. $oReconFilter = new CMDBSearchFilter($oExtKey->GetTargetClass());
  234. foreach ($this->m_aExtKeys[$sAttCode] as $sForeignAttCode => $iCol)
  235. {
  236. // The foreign attribute is one of our reconciliation key
  237. $oReconFilter->AddCondition($sForeignAttCode, $aRowData[$iCol], '=');
  238. $aResults[$iCol] = new CellStatus_Void($aRowData[$iCol]);
  239. }
  240. $oExtObjects = new CMDBObjectSet($oReconFilter);
  241. $aKeys = $oExtObjects->ToArray();
  242. return array($oReconFilter->ToOql(), $aKeys);
  243. }
  244. // Returns true if the CSV data specifies that the external key must be left undefined
  245. protected function IsNullExternalKeySpec($aRowData, $sAttCode)
  246. {
  247. $oExtKey = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  248. foreach ($this->m_aExtKeys[$sAttCode] as $sForeignAttCode => $iCol)
  249. {
  250. // The foreign attribute is one of our reconciliation key
  251. if (strlen($aRowData[$iCol]) > 0)
  252. {
  253. return false;
  254. }
  255. }
  256. return true;
  257. }
  258. protected function PrepareObject(&$oTargetObj, $aRowData, &$aErrors)
  259. {
  260. $aResults = array();
  261. $aErrors = array();
  262. // External keys reconciliation
  263. //
  264. foreach($this->m_aExtKeys as $sAttCode => $aKeyConfig)
  265. {
  266. // Skip external keys used for the reconciliation process
  267. // if (!array_key_exists($sAttCode, $this->m_aAttList)) continue;
  268. $oExtKey = MetaModel::GetAttributeDef(get_class($oTargetObj), $sAttCode);
  269. if ($this->IsNullExternalKeySpec($aRowData, $sAttCode))
  270. {
  271. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  272. {
  273. $aResults[$iCol] = new CellStatus_Void($aRowData[$iCol]);
  274. }
  275. if ($oExtKey->IsNullAllowed())
  276. {
  277. $oTargetObj->Set($sAttCode, $oExtKey->GetNullValue());
  278. $aResults[$sAttCode]= new CellStatus_Void($oExtKey->GetNullValue());
  279. }
  280. else
  281. {
  282. $aErrors[$sAttCode] = "Null not allowed";
  283. $aResults[$sAttCode]= new CellStatus_Issue(null, $oTargetObj->Get($sAttCode), 'Null not allowed');
  284. }
  285. }
  286. else
  287. {
  288. $oReconFilter = new CMDBSearchFilter($oExtKey->GetTargetClass());
  289. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  290. {
  291. // The foreign attribute is one of our reconciliation key
  292. $oReconFilter->AddCondition($sForeignAttCode, $aRowData[$iCol], '=');
  293. $aResults[$iCol] = new CellStatus_Void($aRowData[$iCol]);
  294. }
  295. $oExtObjects = new CMDBObjectSet($oReconFilter);
  296. switch($oExtObjects->Count())
  297. {
  298. case 0:
  299. $aErrors[$sAttCode] = "Object not found";
  300. $aResults[$sAttCode]= new CellStatus_SearchIssue();
  301. break;
  302. case 1:
  303. // Do change the external key attribute
  304. $oForeignObj = $oExtObjects->Fetch();
  305. $oTargetObj->Set($sAttCode, $oForeignObj->GetKey());
  306. break;
  307. default:
  308. $aErrors[$sAttCode] = "Found ".$oExtObjects->Count()." matches";
  309. $aResults[$sAttCode]= new CellStatus_Ambiguous($oTargetObj->Get($sAttCode), $oExtObjects->Count(), $oReconFilter->ToOql());
  310. }
  311. }
  312. // Report
  313. if (!array_key_exists($sAttCode, $aResults))
  314. {
  315. $iForeignObj = $oTargetObj->Get($sAttCode);
  316. if (array_key_exists($sAttCode, $oTargetObj->ListChanges()))
  317. {
  318. if ($oTargetObj->IsNew())
  319. {
  320. $aResults[$sAttCode]= new CellStatus_Void($iForeignObj);
  321. }
  322. else
  323. {
  324. $aResults[$sAttCode]= new CellStatus_Modify($iForeignObj, $oTargetObj->GetOriginal($sAttCode));
  325. }
  326. }
  327. else
  328. {
  329. $aResults[$sAttCode]= new CellStatus_Void($iForeignObj);
  330. }
  331. }
  332. }
  333. // Set the object attributes
  334. //
  335. foreach ($this->m_aAttList as $sAttCode => $iCol)
  336. {
  337. // skip the private key, if any
  338. if ($sAttCode == 'id') continue;
  339. $res = $oTargetObj->CheckValue($sAttCode, $aRowData[$iCol]);
  340. if ($res === true)
  341. {
  342. $oTargetObj->Set($sAttCode, $aRowData[$iCol]);
  343. }
  344. else
  345. {
  346. // $res is a string with the error description
  347. $aErrors[$sAttCode] = "Unexpected value for attribute '$sAttCode': $res";
  348. }
  349. }
  350. // Reporting on fields
  351. //
  352. $aChangedFields = $oTargetObj->ListChanges();
  353. foreach ($this->m_aAttList as $sAttCode => $iCol)
  354. {
  355. if ($sAttCode == 'id')
  356. {
  357. $aResults[$iCol]= new CellStatus_Void($aRowData[$iCol]);
  358. }
  359. if (isset($aErrors[$sAttCode]))
  360. {
  361. $aResults[$iCol]= new CellStatus_Issue($oTargetObj->Get($sAttCode), $oTargetObj->GetOriginal($sAttCode), $aErrors[$sAttCode]);
  362. }
  363. elseif (array_key_exists($sAttCode, $aChangedFields))
  364. {
  365. if ($oTargetObj->IsNew())
  366. {
  367. $aResults[$iCol]= new CellStatus_Void($oTargetObj->Get($sAttCode));
  368. }
  369. else
  370. {
  371. $aResults[$iCol]= new CellStatus_Modify($oTargetObj->Get($sAttCode), $oTargetObj->GetOriginal($sAttCode));
  372. }
  373. }
  374. else
  375. {
  376. // By default... nothing happens
  377. $aResults[$iCol]= new CellStatus_Void($aRowData[$iCol]);
  378. }
  379. }
  380. // Checks
  381. //
  382. $res = $oTargetObj->CheckConsistency();
  383. if ($res !== true)
  384. {
  385. // $res contains the error description
  386. $aErrors["GLOBAL"] = "Attributes not consistent with each others: $res";
  387. }
  388. return $aResults;
  389. }
  390. protected function PrepareMissingObject(&$oTargetObj, &$aErrors)
  391. {
  392. $aResults = array();
  393. $aErrors = array();
  394. // External keys
  395. //
  396. foreach($this->m_aExtKeys as $sAttCode => $aKeyConfig)
  397. {
  398. //$oExtKey = MetaModel::GetAttributeDef(get_class($oTargetObj), $sAttCode);
  399. $aResults[$sAttCode]= new CellStatus_Void($oTargetObj->Get($sAttCode));
  400. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  401. {
  402. $aResults[$iCol] = new CellStatus_Void('?');
  403. }
  404. }
  405. // Update attributes
  406. //
  407. foreach($this->m_aOnDisappear as $sAttCode => $value)
  408. {
  409. if (!MetaModel::IsValidAttCode(get_class($oTargetObj), $sAttCode))
  410. {
  411. throw new BulkChangeException('Invalid attribute code', array('class' => get_class($oTargetObj), 'attcode' => $sAttCode));
  412. }
  413. $oTargetObj->Set($sAttCode, $value);
  414. if (!array_key_exists($sAttCode, $this->m_aAttList))
  415. {
  416. // #@# will be out of the reporting... (counted anyway)
  417. }
  418. }
  419. // Reporting on fields
  420. //
  421. $aChangedFields = $oTargetObj->ListChanges();
  422. foreach ($this->m_aAttList as $sAttCode => $iCol)
  423. {
  424. if ($sAttCode == 'id')
  425. {
  426. $aResults[$iCol]= new CellStatus_Void($oTargetObj->GetKey());
  427. }
  428. if (array_key_exists($sAttCode, $aChangedFields))
  429. {
  430. $aResults[$iCol]= new CellStatus_Modify($oTargetObj->Get($sAttCode), $oTargetObj->GetOriginal($sAttCode));
  431. }
  432. else
  433. {
  434. // By default... nothing happens
  435. $aResults[$iCol]= new CellStatus_Void($oTargetObj->Get($sAttCode));
  436. }
  437. }
  438. // Checks
  439. //
  440. $res = $oTargetObj->CheckConsistency();
  441. if ($res !== true)
  442. {
  443. // $res contains the error description
  444. $aErrors["GLOBAL"] = "Attributes not consistent with each others: $res";
  445. }
  446. return $aResults;
  447. }
  448. protected function CreateObject(&$aResult, $iRow, $aRowData, CMDBChange $oChange = null)
  449. {
  450. $oTargetObj = MetaModel::NewObject($this->m_sClass);
  451. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  452. if (count($aErrors) > 0)
  453. {
  454. $sErrors = implode(', ', $aErrors);
  455. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  456. return;
  457. }
  458. // Check that any external key will have a value proposed
  459. $aMissingKeys = array();
  460. foreach (MetaModel::GetExternalKeys($this->m_sClass) as $sExtKeyAttCode => $oExtKey)
  461. {
  462. if (!$oExtKey->IsNullAllowed())
  463. {
  464. if (!array_key_exists($sExtKeyAttCode, $this->m_aExtKeys) && !array_key_exists($sExtKeyAttCode, $this->m_aAttList))
  465. {
  466. $aMissingKeys[] = $oExtKey->GetLabel();
  467. }
  468. }
  469. }
  470. if (count($aMissingKeys) > 0)
  471. {
  472. $sMissingKeys = implode(', ', $aMissingKeys);
  473. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Could not be created, due to missing external key(s): $sMissingKeys");
  474. return;
  475. }
  476. // Optionaly record the results
  477. //
  478. if ($oChange)
  479. {
  480. $newID = $oTargetObj->DBInsertTrackedNoReload($oChange);
  481. $aResult[$iRow]["__STATUS__"] = new RowStatus_NewObj($this->m_sClass, $newID);
  482. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  483. $aResult[$iRow]["id"] = new CellStatus_Void($newID);
  484. }
  485. else
  486. {
  487. $aResult[$iRow]["__STATUS__"] = new RowStatus_NewObj();
  488. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  489. $aResult[$iRow]["id"] = new CellStatus_Void(0);
  490. }
  491. }
  492. protected function UpdateObject(&$aResult, $iRow, $oTargetObj, $aRowData, CMDBChange $oChange = null)
  493. {
  494. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  495. // Reporting
  496. //
  497. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  498. $aResult[$iRow]["id"] = new CellStatus_Void($oTargetObj->GetKey());
  499. if (count($aErrors) > 0)
  500. {
  501. $sErrors = implode(', ', $aErrors);
  502. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  503. return;
  504. }
  505. $aChangedFields = $oTargetObj->ListChanges();
  506. if (count($aChangedFields) > 0)
  507. {
  508. $aResult[$iRow]["__STATUS__"] = new RowStatus_Modify(count($aChangedFields));
  509. // Optionaly record the results
  510. //
  511. if ($oChange)
  512. {
  513. $oTargetObj->DBUpdateTracked($oChange);
  514. }
  515. }
  516. else
  517. {
  518. $aResult[$iRow]["__STATUS__"] = new RowStatus_NoChange();
  519. }
  520. }
  521. protected function UpdateMissingObject(&$aResult, $iRow, $oTargetObj, CMDBChange $oChange = null)
  522. {
  523. $aResult[$iRow] = $this->PrepareMissingObject($oTargetObj, $aErrors);
  524. // Reporting
  525. //
  526. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  527. $aResult[$iRow]["id"] = new CellStatus_Void($oTargetObj->GetKey());
  528. if (count($aErrors) > 0)
  529. {
  530. $sErrors = implode(', ', $aErrors);
  531. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  532. return;
  533. }
  534. $aChangedFields = $oTargetObj->ListChanges();
  535. if (count($aChangedFields) > 0)
  536. {
  537. $aResult[$iRow]["__STATUS__"] = new RowStatus_Disappeared(count($aChangedFields));
  538. // Optionaly record the results
  539. //
  540. if ($oChange)
  541. {
  542. $oTargetObj->DBUpdateTracked($oChange);
  543. }
  544. }
  545. else
  546. {
  547. $aResult[$iRow]["__STATUS__"] = new RowStatus_Disappeared(0);
  548. }
  549. }
  550. public function Process(CMDBChange $oChange = null)
  551. {
  552. // Note: $oChange can be null, in which case the aim is to check what would be done
  553. // Debug...
  554. //
  555. if (false)
  556. {
  557. echo "<pre>\n";
  558. echo "Attributes:\n";
  559. print_r($this->m_aAttList);
  560. echo "ExtKeys:\n";
  561. print_r($this->m_aExtKeys);
  562. echo "Reconciliation:\n";
  563. print_r($this->m_aReconcilKeys);
  564. echo "Synchro scope:\n";
  565. print_r($this->m_sSynchroScope);
  566. echo "Synchro changes:\n";
  567. print_r($this->m_aOnDisappear);
  568. //echo "Data:\n";
  569. //print_r($this->m_aData);
  570. echo "</pre>\n";
  571. exit;
  572. }
  573. // Compute the results
  574. //
  575. if (!is_null($this->m_sSynchroScope))
  576. {
  577. $aVisited = array();
  578. }
  579. $aResult = array();
  580. foreach($this->m_aData as $iRow => $aRowData)
  581. {
  582. $oReconciliationFilter = new CMDBSearchFilter($this->m_sClass);
  583. $bSkipQuery = false;
  584. foreach($this->m_aReconcilKeys as $sAttCode)
  585. {
  586. $valuecondition = null;
  587. if (array_key_exists($sAttCode, $this->m_aExtKeys))
  588. {
  589. if ($this->IsNullExternalKeySpec($aRowData, $sAttCode))
  590. {
  591. $oExtKey = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  592. if ($oExtKey->IsNullAllowed())
  593. {
  594. $valuecondition = $oExtKey->GetNullValue();
  595. $aResult[$iRow][$sAttCode] = new CellStatus_Void($oExtKey->GetNullValue());
  596. }
  597. else
  598. {
  599. $aResult[$iRow][$sAttCode] = new CellStatus_NullIssue();
  600. }
  601. }
  602. else
  603. {
  604. // The value has to be found or verified
  605. list($sQuery, $aMatches) = $this->ResolveExternalKey($aRowData, $sAttCode, $aResult[$iRow]);
  606. if (count($aMatches) == 1)
  607. {
  608. $oRemoteObj = reset($aMatches); // first item
  609. $valuecondition = $oRemoteObj->GetKey();
  610. $aResult[$iRow][$sAttCode] = new CellStatus_Void($oRemoteObj->GetKey());
  611. }
  612. elseif (count($aMatches) == 0)
  613. {
  614. $aResult[$iRow][$sAttCode] = new CellStatus_SearchIssue();
  615. }
  616. else
  617. {
  618. $aResult[$iRow][$sAttCode] = new CellStatus_Ambiguous(null, count($aMatches), $sQuery);
  619. }
  620. }
  621. }
  622. else
  623. {
  624. // The value is given in the data row
  625. $iCol = $this->m_aAttList[$sAttCode];
  626. $valuecondition = $aRowData[$iCol];
  627. }
  628. if (is_null($valuecondition))
  629. {
  630. $bSkipQuery = true;
  631. }
  632. else
  633. {
  634. $oReconciliationFilter->AddCondition($sAttCode, $valuecondition, '=');
  635. }
  636. }
  637. if ($bSkipQuery)
  638. {
  639. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue("failed to reconcile");
  640. }
  641. else
  642. {
  643. $oReconciliationSet = new CMDBObjectSet($oReconciliationFilter);
  644. switch($oReconciliationSet->Count())
  645. {
  646. case 0:
  647. $this->CreateObject($aResult, $iRow, $aRowData, $oChange);
  648. // $aResult[$iRow]["__STATUS__"]=> set in CreateObject
  649. break;
  650. case 1:
  651. $oTargetObj = $oReconciliationSet->Fetch();
  652. $this->UpdateObject($aResult, $iRow, $oTargetObj, $aRowData, $oChange);
  653. // $aResult[$iRow]["__STATUS__"]=> set in UpdateObject
  654. if (!is_null($this->m_sSynchroScope))
  655. {
  656. $aVisited[] = $oTargetObj->GetKey();
  657. }
  658. break;
  659. default:
  660. // Found several matches, ambiguous
  661. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue("ambiguous reconciliation");
  662. $aResult[$iRow]["id"]= new CellStatus_Ambiguous(0, $oReconciliationSet->Count(), $oReconciliationFilter->ToOql());
  663. $aResult[$iRow]["finalclass"]= 'n/a';
  664. }
  665. }
  666. // Whatever happened, do report the reconciliation values
  667. foreach($this->m_aAttList as $iCol)
  668. {
  669. if (!array_key_exists($iCol, $aResult[$iRow]))
  670. {
  671. $aResult[$iRow][$iCol] = new CellStatus_Void($aRowData[$iCol]);
  672. }
  673. }
  674. foreach($this->m_aExtKeys as $sAttCode => $aForeignAtts)
  675. {
  676. if (!array_key_exists($sAttCode, $aResult[$iRow]))
  677. {
  678. $aResult[$iRow][$sAttCode] = new CellStatus_Void('n/a');
  679. }
  680. foreach ($aForeignAtts as $sForeignAttCode => $iCol)
  681. {
  682. if (!array_key_exists($iCol, $aResult[$iRow]))
  683. {
  684. // The foreign attribute is one of our reconciliation key
  685. $aResult[$iRow][$iCol] = new CellStatus_Void($aRowData[$iCol]);
  686. }
  687. }
  688. }
  689. }
  690. if (!is_null($this->m_sSynchroScope))
  691. {
  692. // Compute the delta between the scope and visited objects
  693. $oScopeSearch = DBObjectSearch::FromOQL($this->m_sSynchroScope);
  694. $oScopeSet = new DBObjectSet($oScopeSearch);
  695. while ($oObj = $oScopeSet->Fetch())
  696. {
  697. $iObj = $oObj->GetKey();
  698. if (!in_array($iObj, $aVisited))
  699. {
  700. $iRow++;
  701. $this->UpdateMissingObject($aResult, $iRow, $oObj, $oChange);
  702. }
  703. }
  704. }
  705. return $aResult;
  706. }
  707. }
  708. ?>