bulkchange.class.inc.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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 $oTargetObj;
  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 $oTargetObj;
  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. return $oTargetObj;
  492. }
  493. protected function UpdateObject(&$aResult, $iRow, $oTargetObj, $aRowData, CMDBChange $oChange = null)
  494. {
  495. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  496. // Reporting
  497. //
  498. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  499. $aResult[$iRow]["id"] = new CellStatus_Void($oTargetObj->GetKey());
  500. if (count($aErrors) > 0)
  501. {
  502. $sErrors = implode(', ', $aErrors);
  503. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  504. return;
  505. }
  506. $aChangedFields = $oTargetObj->ListChanges();
  507. if (count($aChangedFields) > 0)
  508. {
  509. $aResult[$iRow]["__STATUS__"] = new RowStatus_Modify(count($aChangedFields));
  510. // Optionaly record the results
  511. //
  512. if ($oChange)
  513. {
  514. $oTargetObj->DBUpdateTracked($oChange);
  515. }
  516. }
  517. else
  518. {
  519. $aResult[$iRow]["__STATUS__"] = new RowStatus_NoChange();
  520. }
  521. }
  522. protected function UpdateMissingObject(&$aResult, $iRow, $oTargetObj, CMDBChange $oChange = null)
  523. {
  524. $aResult[$iRow] = $this->PrepareMissingObject($oTargetObj, $aErrors);
  525. // Reporting
  526. //
  527. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  528. $aResult[$iRow]["id"] = new CellStatus_Void($oTargetObj->GetKey());
  529. if (count($aErrors) > 0)
  530. {
  531. $sErrors = implode(', ', $aErrors);
  532. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  533. return;
  534. }
  535. $aChangedFields = $oTargetObj->ListChanges();
  536. if (count($aChangedFields) > 0)
  537. {
  538. $aResult[$iRow]["__STATUS__"] = new RowStatus_Disappeared(count($aChangedFields));
  539. // Optionaly record the results
  540. //
  541. if ($oChange)
  542. {
  543. $oTargetObj->DBUpdateTracked($oChange);
  544. }
  545. }
  546. else
  547. {
  548. $aResult[$iRow]["__STATUS__"] = new RowStatus_Disappeared(0);
  549. }
  550. }
  551. public function Process(CMDBChange $oChange = null)
  552. {
  553. // Note: $oChange can be null, in which case the aim is to check what would be done
  554. // Debug...
  555. //
  556. if (false)
  557. {
  558. echo "<pre>\n";
  559. echo "Attributes:\n";
  560. print_r($this->m_aAttList);
  561. echo "ExtKeys:\n";
  562. print_r($this->m_aExtKeys);
  563. echo "Reconciliation:\n";
  564. print_r($this->m_aReconcilKeys);
  565. echo "Synchro scope:\n";
  566. print_r($this->m_sSynchroScope);
  567. echo "Synchro changes:\n";
  568. print_r($this->m_aOnDisappear);
  569. //echo "Data:\n";
  570. //print_r($this->m_aData);
  571. echo "</pre>\n";
  572. exit;
  573. }
  574. // Compute the results
  575. //
  576. if (!is_null($this->m_sSynchroScope))
  577. {
  578. $aVisited = array();
  579. }
  580. $aResult = array();
  581. foreach($this->m_aData as $iRow => $aRowData)
  582. {
  583. $oReconciliationFilter = new CMDBSearchFilter($this->m_sClass);
  584. $bSkipQuery = false;
  585. foreach($this->m_aReconcilKeys as $sAttCode)
  586. {
  587. $valuecondition = null;
  588. if (array_key_exists($sAttCode, $this->m_aExtKeys))
  589. {
  590. if ($this->IsNullExternalKeySpec($aRowData, $sAttCode))
  591. {
  592. $oExtKey = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  593. if ($oExtKey->IsNullAllowed())
  594. {
  595. $valuecondition = $oExtKey->GetNullValue();
  596. $aResult[$iRow][$sAttCode] = new CellStatus_Void($oExtKey->GetNullValue());
  597. }
  598. else
  599. {
  600. $aResult[$iRow][$sAttCode] = new CellStatus_NullIssue();
  601. }
  602. }
  603. else
  604. {
  605. // The value has to be found or verified
  606. list($sQuery, $aMatches) = $this->ResolveExternalKey($aRowData, $sAttCode, $aResult[$iRow]);
  607. if (count($aMatches) == 1)
  608. {
  609. $oRemoteObj = reset($aMatches); // first item
  610. $valuecondition = $oRemoteObj->GetKey();
  611. $aResult[$iRow][$sAttCode] = new CellStatus_Void($oRemoteObj->GetKey());
  612. }
  613. elseif (count($aMatches) == 0)
  614. {
  615. $aResult[$iRow][$sAttCode] = new CellStatus_SearchIssue();
  616. }
  617. else
  618. {
  619. $aResult[$iRow][$sAttCode] = new CellStatus_Ambiguous(null, count($aMatches), $sQuery);
  620. }
  621. }
  622. }
  623. else
  624. {
  625. // The value is given in the data row
  626. $iCol = $this->m_aAttList[$sAttCode];
  627. $valuecondition = $aRowData[$iCol];
  628. }
  629. if (is_null($valuecondition))
  630. {
  631. $bSkipQuery = true;
  632. }
  633. else
  634. {
  635. $oReconciliationFilter->AddCondition($sAttCode, $valuecondition, '=');
  636. }
  637. }
  638. if ($bSkipQuery)
  639. {
  640. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue("failed to reconcile");
  641. }
  642. else
  643. {
  644. $oReconciliationSet = new CMDBObjectSet($oReconciliationFilter);
  645. switch($oReconciliationSet->Count())
  646. {
  647. case 0:
  648. $oTargetObj = $this->CreateObject($aResult, $iRow, $aRowData, $oChange);
  649. // $aResult[$iRow]["__STATUS__"]=> set in CreateObject
  650. $aVisited[] = $oTargetObj->GetKey();
  651. break;
  652. case 1:
  653. $oTargetObj = $oReconciliationSet->Fetch();
  654. $this->UpdateObject($aResult, $iRow, $oTargetObj, $aRowData, $oChange);
  655. // $aResult[$iRow]["__STATUS__"]=> set in UpdateObject
  656. if (!is_null($this->m_sSynchroScope))
  657. {
  658. $aVisited[] = $oTargetObj->GetKey();
  659. }
  660. break;
  661. default:
  662. // Found several matches, ambiguous
  663. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue("ambiguous reconciliation");
  664. $aResult[$iRow]["id"]= new CellStatus_Ambiguous(0, $oReconciliationSet->Count(), $oReconciliationFilter->ToOql());
  665. $aResult[$iRow]["finalclass"]= 'n/a';
  666. }
  667. }
  668. // Whatever happened, do report the reconciliation values
  669. foreach($this->m_aAttList as $iCol)
  670. {
  671. if (!array_key_exists($iCol, $aResult[$iRow]))
  672. {
  673. $aResult[$iRow][$iCol] = new CellStatus_Void($aRowData[$iCol]);
  674. }
  675. }
  676. foreach($this->m_aExtKeys as $sAttCode => $aForeignAtts)
  677. {
  678. if (!array_key_exists($sAttCode, $aResult[$iRow]))
  679. {
  680. $aResult[$iRow][$sAttCode] = new CellStatus_Void('n/a');
  681. }
  682. foreach ($aForeignAtts as $sForeignAttCode => $iCol)
  683. {
  684. if (!array_key_exists($iCol, $aResult[$iRow]))
  685. {
  686. // The foreign attribute is one of our reconciliation key
  687. $aResult[$iRow][$iCol] = new CellStatus_Void($aRowData[$iCol]);
  688. }
  689. }
  690. }
  691. }
  692. if (!is_null($this->m_sSynchroScope))
  693. {
  694. // Compute the delta between the scope and visited objects
  695. $oScopeSearch = DBObjectSearch::FromOQL($this->m_sSynchroScope);
  696. $oScopeSet = new DBObjectSet($oScopeSearch);
  697. while ($oObj = $oScopeSet->Fetch())
  698. {
  699. $iObj = $oObj->GetKey();
  700. if (!in_array($iObj, $aVisited))
  701. {
  702. $iRow++;
  703. $this->UpdateMissingObject($aResult, $iRow, $oObj, $oChange);
  704. }
  705. }
  706. }
  707. return $aResult;
  708. }
  709. /**
  710. * Display the history of bulk imports
  711. */
  712. static function DisplayImportHistory(WebPage $oPage, $bFromAjax = false, $bShowAll = false)
  713. {
  714. $sAjaxDivId = "CSVImportHistory";
  715. if (!$bFromAjax)
  716. {
  717. $oPage->add('<div id="'.$sAjaxDivId.'">');
  718. }
  719. $oPage->p(Dict::S('UI:History:BulkImports+'));
  720. $oBulkChangeSearch = DBObjectSearch::FromOQL("SELECT CMDBChange WHERE userinfo LIKE '%(CSV)'");
  721. $iQueryLimit = $bShowAll ? 0 : MetaModel::GetConfig()->GetMaxDisplayLimit() + 1;
  722. $oBulkChanges = new DBObjectSet($oBulkChangeSearch, array('date' => false), array(), null, $iQueryLimit);
  723. $oAppContext = new ApplicationContext();
  724. $bLimitExceeded = false;
  725. if ($oBulkChanges->Count() > MetaModel::GetConfig()->GetMaxDisplayLimit())
  726. {
  727. $bLimitExceeded = true;
  728. if (!$bShowAll)
  729. {
  730. $iMaxObjects = MetaModel::GetConfig()->GetMinDisplayLimit();
  731. $oBulkChanges->SetLimit($iMaxObjects);
  732. }
  733. }
  734. $oBulkChanges->Seek(0);
  735. $aDetails = array();
  736. while ($oChange = $oBulkChanges->Fetch())
  737. {
  738. $sDate = '<a href="?step=10&changeid='.$oChange->GetKey().'&'.$oAppContext->GetForLink().'">'.$oChange->Get('date').'</a>';
  739. $sUser = $oChange->GetUserName();
  740. if (preg_match('/^(.*)\\(CSV\\)$/i', $oChange->Get('userinfo'), $aMatches))
  741. {
  742. $sUser = $aMatches[1];
  743. }
  744. else
  745. {
  746. $sUser = $oChange->Get('userinfo');
  747. }
  748. $oOpSearch = DBObjectSearch::FromOQL("SELECT CMDBChangeOpCreate WHERE change = :change_id");
  749. $oOpSet = new DBObjectSet($oOpSearch, array(), array('change_id' => $oChange->GetKey()));
  750. $iCreated = $oOpSet->Count();
  751. // Get the class from the first item found (assumption: a CSV load is done for a single class)
  752. if ($oCreateOp = $oOpSet->Fetch())
  753. {
  754. $sClass = $oCreateOp->Get('objclass');
  755. }
  756. $oOpSearch = DBObjectSearch::FromOQL("SELECT CMDBChangeOpSetAttribute WHERE change = :change_id");
  757. $oOpSet = new DBObjectSet($oOpSearch, array(), array('change_id' => $oChange->GetKey()));
  758. $aModified = array();
  759. $aAttList = array();
  760. while ($oModified = $oOpSet->Fetch())
  761. {
  762. // Get the class (if not done earlier on object creation)
  763. $sClass = $oModified->Get('objclass');
  764. $iKey = $oModified->Get('objkey');
  765. $sAttCode = $oModified->Get('attcode');
  766. $aAttList[$sClass][$sAttCode] = true;
  767. $aModified["$sClass::$iKey"] = true;
  768. }
  769. $iModified = count($aModified);
  770. // Assumption: there is only one class of objects being loaded
  771. // Then the last class found gives us the class for every object
  772. if ( ($iModified > 0) || ($iCreated > 0))
  773. {
  774. $aDetails[] = array('date' => $sDate, 'user' => $sUser, 'class' => $sClass, 'created' => $iCreated, 'modified' => $iModified);
  775. }
  776. }
  777. $aConfig = array( 'date' => array('label' => Dict::S('UI:History:Date'), 'description' => Dict::S('UI:History:Date+')),
  778. 'user' => array('label' => Dict::S('UI:History:User'), 'description' => Dict::S('UI:History:User+')),
  779. 'class' => array('label' => Dict::S('Core:AttributeClass'), 'description' => Dict::S('Core:AttributeClass+')),
  780. 'created' => array('label' => Dict::S('UI:History:StatsCreations'), 'description' => Dict::S('UI:History:StatsCreations+')),
  781. 'modified' => array('label' => Dict::S('UI:History:StatsModifs'), 'description' => Dict::S('UI:History:StatsModifs+')),
  782. );
  783. if ($bLimitExceeded)
  784. {
  785. if ($bShowAll)
  786. {
  787. // Collapsible list
  788. $oPage->add('<p>'.Dict::Format('UI:CountOfResults', $oBulkChanges->Count()).'&nbsp;&nbsp;<a class="truncated" onclick="OnTruncatedHistoryToggle(false);">'.Dict::S('UI:CollapseList').'</a></p>');
  789. }
  790. else
  791. {
  792. // Truncated list
  793. $iMinDisplayLimit = MetaModel::GetConfig()->GetMinDisplayLimit();
  794. $sCollapsedLabel = Dict::Format('UI:TruncatedResults', $iMinDisplayLimit, $oBulkChanges->Count());
  795. $sLinkLabel = Dict::S('UI:DisplayAll');
  796. $oPage->add('<p>'.$sCollapsedLabel.'&nbsp;&nbsp;<a class="truncated" onclick="OnTruncatedHistoryToggle(true);">'.$sLinkLabel.'</p>');
  797. $oPage->add_ready_script(
  798. <<<EOF
  799. $('#$sAjaxDivId table.listResults').addClass('truncated');
  800. $('#$sAjaxDivId table.listResults tr:last td').addClass('truncated');
  801. EOF
  802. );
  803. $sAppContext = $oAppContext->GetForLink();
  804. $oPage->add_script(
  805. <<<EOF
  806. function OnTruncatedHistoryToggle(bShowAll)
  807. {
  808. $.get('../pages/ajax.render.php?{$sAppContext}', {operation: 'displayCSVHistory', showall: bShowAll}, function(data)
  809. {
  810. $('#$sAjaxDivId').html(data);
  811. var table = $('#$sAjaxDivId .listResults');
  812. table.tableHover(); // hover tables
  813. table.tablesorter( { widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  814. }
  815. );
  816. }
  817. EOF
  818. );
  819. }
  820. }
  821. else
  822. {
  823. // Normal display - full list without any decoration
  824. }
  825. $oPage->table($aConfig, $aDetails);
  826. if (!$bFromAjax)
  827. {
  828. $oPage->add('</div>');
  829. }
  830. }
  831. /**
  832. * Display the details of an import
  833. */
  834. static function DisplayImportHistoryDetails(iTopWebPage $oPage, $iChange)
  835. {
  836. if ($iChange == 0)
  837. {
  838. throw new Exception("Missing parameter changeid");
  839. }
  840. $oChange = MetaModel::GetObject('CMDBChange', $iChange, false);
  841. if (is_null($oChange))
  842. {
  843. throw new Exception("Unknown change: $iChange");
  844. }
  845. $oPage->add("<div><p><h1>".Dict::Format('UI:History:BulkImportDetails', $oChange->Get('date'), $oChange->GetUserName())."</h1></p></div>\n");
  846. // Assumption : change made one single class of objects
  847. $aObjects = array();
  848. $aAttributes = array(); // array of attcode => occurences
  849. $oOpSearch = DBObjectSearch::FromOQL("SELECT CMDBChangeOp WHERE change = :change_id");
  850. $oOpSet = new DBObjectSet($oOpSearch, array(), array('change_id' => $iChange));
  851. while ($oOperation = $oOpSet->Fetch())
  852. {
  853. $sClass = $oOperation->Get('objclass');
  854. $iKey = $oOperation->Get('objkey');
  855. $iObjId = "$sClass::$iKey";
  856. if (!isset($aObjects[$iObjId]))
  857. {
  858. $aObjects[$iObjId] = array();
  859. $aObjects[$iObjId]['__class__'] = $sClass;
  860. $aObjects[$iObjId]['__id__'] = $iKey;
  861. }
  862. if (get_class($oOperation) == 'CMDBChangeOpCreate')
  863. {
  864. $aObjects[$iObjId]['__created__'] = true;
  865. }
  866. elseif (is_subclass_of($oOperation, 'CMDBChangeOpSetAttribute'))
  867. {
  868. $sAttCode = $oOperation->Get('attcode');
  869. if (get_class($oOperation) == 'CMDBChangeOpSetAttributeScalar')
  870. {
  871. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  872. if ($oAttDef->IsExternalKey())
  873. {
  874. $oOldTarget = MetaModel::GetObject($oAttDef->GetTargetClass(), $oOperation->Get('oldvalue'));
  875. $oNewTarget = MetaModel::GetObject($oAttDef->GetTargetClass(), $oOperation->Get('newvalue'));
  876. $sOldValue = $oOldTarget->GetHyperlink();
  877. $sNewValue = $oNewTarget->GetHyperlink();
  878. }
  879. else
  880. {
  881. $sOldValue = $oOperation->GetAsHTML('oldvalue');
  882. $sNewValue = $oOperation->GetAsHTML('newvalue');
  883. }
  884. $aObjects[$iObjId][$sAttCode] = $sOldValue.' -&gt; '.$sNewValue;
  885. }
  886. else
  887. {
  888. $aObjects[$iObjId][$sAttCode] = 'n/a';
  889. }
  890. if (isset($aAttributes[$sAttCode]))
  891. {
  892. $aAttributes[$sAttCode]++;
  893. }
  894. else
  895. {
  896. $aAttributes[$sAttCode] = 1;
  897. }
  898. }
  899. }
  900. $aDetails = array();
  901. foreach($aObjects as $iUId => $aObjData)
  902. {
  903. $aRow = array();
  904. $oObject = MetaModel::GetObject($aObjData['__class__'], $aObjData['__id__'], false);
  905. if (is_null($oObject))
  906. {
  907. $aRow['object'] = $aObjData['__class__'].'::'.$aObjData['__id__'].' (deleted)';
  908. }
  909. else
  910. {
  911. $aRow['object'] = $oObject->GetHyperlink();
  912. }
  913. if (isset($aObjData['__created__']))
  914. {
  915. $aRow['operation'] = Dict::S('Change:ObjectCreated');
  916. }
  917. else
  918. {
  919. $aRow['operation'] = Dict::S('Change:ObjectModified');
  920. }
  921. foreach ($aAttributes as $sAttCode => $iOccurences)
  922. {
  923. if (isset($aObjData[$sAttCode]))
  924. {
  925. $aRow[$sAttCode] = $aObjData[$sAttCode];
  926. }
  927. elseif (!is_null($oObject))
  928. {
  929. // This is the current vaslue: $oObject->GetAsHtml($sAttCode)
  930. // whereas we are displaying the value that was set at the time
  931. // the object was created
  932. // This requires addtional coding...let's do that later
  933. $aRow[$sAttCode] = '';
  934. }
  935. else
  936. {
  937. $aRow[$sAttCode] = '';
  938. }
  939. }
  940. $aDetails[] = $aRow;
  941. }
  942. $aConfig = array();
  943. $aConfig['object'] = array('label' => MetaModel::GetName($sClass), 'description' => MetaModel::GetClassDescription($sClass));
  944. $aConfig['operation'] = array('label' => Dict::S('UI:History:Changes'), 'description' => Dict::S('UI:History:Changes+'));
  945. foreach ($aAttributes as $sAttCode => $iOccurences)
  946. {
  947. $aConfig[$sAttCode] = array('label' => MetaModel::GetLabel($sClass, $sAttCode), 'description' => MetaModel::GetDescription($sClass, $sAttCode));
  948. }
  949. $oPage->table($aConfig, $aDetails);
  950. }
  951. }
  952. ?>