bulkchange.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. <?php
  2. /**
  3. * BulkChange
  4. * Interpret a given data set and update the DB accordingly (fake mode avail.)
  5. *
  6. * @package iTopORM
  7. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  8. * @author Denis Flaven <denisflave@free.fr>
  9. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  10. * @link www.itop.com
  11. * @since 1.0
  12. * @version 1.1.1.1 $
  13. */
  14. class BulkChangeException extends CoreException
  15. {
  16. }
  17. /**
  18. * CellChangeSpec
  19. * A series of classes, keeping the information about a given cell: could it be changed or not (and why)?
  20. *
  21. * @package iTopORM
  22. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  23. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  24. * @link www.itop.com
  25. * @since 1.0
  26. * @version $itopversion$
  27. */
  28. abstract class CellChangeSpec
  29. {
  30. protected $m_proposedValue;
  31. protected $m_sOql; // in case of ambiguity
  32. public function __construct($proposedValue, $sOql = '')
  33. {
  34. $this->m_proposedValue = $proposedValue;
  35. $this->m_sOql = $sOql;
  36. }
  37. static protected function ValueAsHtml($value)
  38. {
  39. if (MetaModel::IsValidObject($value))
  40. {
  41. return $value->GetHyperLink();
  42. }
  43. else
  44. {
  45. return htmlentities($value);
  46. }
  47. }
  48. public function GetValue()
  49. {
  50. return $this->m_proposedValue;
  51. }
  52. public function GetOql()
  53. {
  54. return $this->m_proposedValue;
  55. }
  56. abstract public function GetDescription();
  57. }
  58. class CellStatus_Void extends CellChangeSpec
  59. {
  60. public function GetDescription()
  61. {
  62. return '';
  63. }
  64. }
  65. class CellStatus_Modify extends CellChangeSpec
  66. {
  67. protected $m_previousValue;
  68. public function __construct($proposedValue, $previousValue)
  69. {
  70. $this->m_previousValue = $previousValue;
  71. parent::__construct($proposedValue);
  72. }
  73. public function GetDescription()
  74. {
  75. return 'Modified';
  76. }
  77. public function GetPreviousValue()
  78. {
  79. return $this->m_previousValue;
  80. }
  81. }
  82. class CellStatus_Issue extends CellStatus_Modify
  83. {
  84. protected $m_sReason;
  85. public function __construct($proposedValue, $previousValue, $sReason)
  86. {
  87. $this->m_sReason = $sReason;
  88. parent::__construct($proposedValue, $previousValue);
  89. }
  90. public function GetDescription()
  91. {
  92. if (is_null($this->m_proposedValue))
  93. {
  94. return 'Could not be changed - reason: '.$this->m_sReason;
  95. }
  96. return 'Could not be changed to '.$this->m_proposedValue.' - reason: '.$this->m_sReason;
  97. }
  98. }
  99. class CellStatus_Ambiguous extends CellStatus_Issue
  100. {
  101. protected $m_iCount;
  102. public function __construct($previousValue, $iCount, $sOql)
  103. {
  104. $this->m_iCount = $iCount;
  105. $this->m_sQuery = $sOql;
  106. parent::__construct(null, $previousValue, '');
  107. }
  108. public function GetDescription()
  109. {
  110. $sCount = $this->m_iCount;
  111. return "Ambiguous: found $sCount objects";
  112. }
  113. }
  114. /**
  115. * RowStatus
  116. * A series of classes, keeping the information about a given row: could it be changed or not (and why)?
  117. *
  118. * @package iTopORM
  119. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  120. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  121. * @link www.itop.com
  122. * @since 1.0
  123. * @version $itopversion$
  124. */
  125. abstract class RowStatus
  126. {
  127. public function __construct()
  128. {
  129. }
  130. abstract public function GetDescription();
  131. }
  132. class RowStatus_NoChange extends RowStatus
  133. {
  134. public function GetDescription()
  135. {
  136. return "unchanged";
  137. }
  138. }
  139. class RowStatus_NewObj extends RowStatus
  140. {
  141. public function GetDescription()
  142. {
  143. return "created";
  144. }
  145. }
  146. class RowStatus_Modify extends RowStatus
  147. {
  148. protected $m_iChanged;
  149. public function __construct($iChanged)
  150. {
  151. $this->m_iChanged = $iChanged;
  152. }
  153. public function GetDescription()
  154. {
  155. return "updated ".$this->m_iChanged." cols";
  156. }
  157. }
  158. class RowStatus_Issue extends RowStatus
  159. {
  160. protected $m_sReason;
  161. public function __construct($sReason)
  162. {
  163. $this->m_sReason = $sReason;
  164. }
  165. public function GetDescription()
  166. {
  167. return 'Issue: '.$this->m_sReason;
  168. }
  169. }
  170. /**
  171. ** BulkChange *
  172. ** @package iTopORM
  173. ** @author Romain Quetiez <romainquetiez@yahoo.fr>
  174. ** @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  175. ** @link www.itop.com
  176. ** @since 1.0
  177. ** @version $itopversion$ */
  178. class BulkChange
  179. {
  180. protected $m_sClass;
  181. protected $m_aData; // Note: hereafter, iCol maybe actually be any acceptable key (string)
  182. // #@# todo: rename the variables to sColIndex
  183. protected $m_aAttList; // attcode => iCol
  184. protected $m_aExtKeys; // aExtKeys[sExtKeyAttCode][sExtReconcKeyAttCode] = iCol;
  185. protected $m_aReconcilKeys;// attcode (attcode = 'id' for the pkey)
  186. public function __construct($sClass, $aData, $aAttList, $aExtKeys, $aReconcilKeys)
  187. {
  188. $this->m_sClass = $sClass;
  189. $this->m_aData = $aData;
  190. $this->m_aAttList = $aAttList;
  191. $this->m_aReconcilKeys = $aReconcilKeys;
  192. $this->m_aExtKeys = $aExtKeys;
  193. }
  194. static protected function MakeSpecObject($sClass, $iId)
  195. {
  196. try
  197. {
  198. $oObj = MetaModel::GetObject($sClass, $iId);
  199. }
  200. catch(CoreException $e)
  201. {
  202. // in case an ext key is 0 (which is currently acceptable)
  203. return $iId;
  204. }
  205. return $oObj;
  206. }
  207. protected function ResolveExternalKey($aRowData, $sAttCode, &$aResults)
  208. {
  209. $oExtKey = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  210. $oReconFilter = new CMDBSearchFilter($oExtKey->GetTargetClass());
  211. foreach ($this->m_aExtKeys[$sAttCode] as $sForeignAttCode => $iCol)
  212. {
  213. // The foreign attribute is one of our reconciliation key
  214. $oReconFilter->AddCondition($sForeignAttCode, $aRowData[$iCol], '=');
  215. $aResults[$iCol] = new CellStatus_Void($aRowData[$iCol]);
  216. }
  217. $oExtObjects = new CMDBObjectSet($oReconFilter);
  218. $aKeys = $oExtObjects->ToArray();
  219. return array($oReconFilter->ToOql(), $aKeys);
  220. }
  221. protected function PrepareObject(&$oTargetObj, $aRowData, &$aErrors)
  222. {
  223. $aResults = array();
  224. $aErrors = array();
  225. // External keys reconciliation
  226. //
  227. foreach($this->m_aExtKeys as $sAttCode => $aKeyConfig)
  228. {
  229. // Skip external keys used for the reconciliation process
  230. if (!array_key_exists($sAttCode, $this->m_aAttList)) continue;
  231. $oExtKey = MetaModel::GetAttributeDef(get_class($oTargetObj), $sAttCode);
  232. $oReconFilter = new CMDBSearchFilter($oExtKey->GetTargetClass());
  233. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  234. {
  235. // The foreign attribute is one of our reconciliation key
  236. $oReconFilter->AddCondition($sForeignAttCode, $aRowData[$iCol], '=');
  237. $aResults[$iCol] = new CellStatus_Void($aRowData[$iCol]);
  238. }
  239. $oExtObjects = new CMDBObjectSet($oReconFilter);
  240. switch($oExtObjects->Count())
  241. {
  242. case 0:
  243. if ($oExtKey->IsNullAllowed())
  244. {
  245. $oTargetObj->Set($sAttCode, $oExtKey->GetNullValue());
  246. }
  247. else
  248. {
  249. $aErrors[$sAttCode] = "Object not found";
  250. $aResults[$sAttCode]= new CellStatus_Issue(null, $oTargetObj->Get($sAttCode), 'Object not found - check the spelling (no space before/after)');
  251. }
  252. break;
  253. case 1:
  254. // Do change the external key attribute
  255. $oForeignObj = $oExtObjects->Fetch();
  256. $oTargetObj->Set($sAttCode, $oForeignObj->GetKey());
  257. break;
  258. default:
  259. $aErrors[$sAttCode] = "Found ".$oExtObjects->Count()." matches";
  260. $previousValue = self::MakeSpecObject($oExtKey->GetTargetClass(), $oTargetObj->Get($sAttCode));
  261. $aResults[$sAttCode]= new CellStatus_Ambiguous($previousValue, $oExtObjects->Count(), $oExtObjects->ToOql());
  262. }
  263. // Report
  264. if (!array_key_exists($sAttCode, $aResults))
  265. {
  266. $oForeignObj = $oTargetObj->Get($sAttCode);
  267. if (array_key_exists($sAttCode, $oTargetObj->ListChanges()))
  268. {
  269. if ($oTargetObj->IsNew())
  270. {
  271. $aResults[$sAttCode]= new CellStatus_Void($oForeignObj);
  272. }
  273. else
  274. {
  275. $previousValue = self::MakeSpecObject($oExtKey->GetTargetClass(), $oTargetObj->GetOriginal($sAttCode));
  276. $aResults[$sAttCode]= new CellStatus_Modify($oForeignObj, $previousValue);
  277. }
  278. }
  279. else
  280. {
  281. $aResults[$sAttCode]= new CellStatus_Void($oForeignObj);
  282. }
  283. }
  284. }
  285. // Set the object attributes
  286. //
  287. foreach ($this->m_aAttList as $sAttCode => $iCol)
  288. {
  289. // skip the private key, if any
  290. if ($sAttCode == 'id') continue;
  291. if (!$oTargetObj->CheckValue($sAttCode, $aRowData[$iCol]))
  292. {
  293. $aErrors[$sAttCode] = "Unexpected value";
  294. }
  295. else
  296. {
  297. $oTargetObj->Set($sAttCode, $aRowData[$iCol]);
  298. }
  299. }
  300. // Reporting on fields
  301. //
  302. $aChangedFields = $oTargetObj->ListChanges();
  303. foreach ($this->m_aAttList as $sAttCode => $iCol)
  304. {
  305. if ($sAttCode == 'id')
  306. {
  307. if ($aRowData[$iCol] == $oTargetObj->GetKey())
  308. {
  309. $aResults[$iCol]= new CellStatus_Void($aRowData[$iCol]);
  310. }
  311. else
  312. {
  313. $aResults[$iCol]= new CellStatus_Void($aRowData[$iCol]);
  314. }
  315. }
  316. if (isset($aErrors[$sAttCode]))
  317. {
  318. $aResults[$iCol]= new CellStatus_Issue($oTargetObj->Get($sAttCode), $oTargetObj->GetOriginal($sAttCode), $aErrors[$sAttCode]);
  319. }
  320. elseif (array_key_exists($sAttCode, $aChangedFields))
  321. {
  322. if ($oTargetObj->IsNew())
  323. {
  324. $aResults[$iCol]= new CellStatus_Void($oTargetObj->Get($sAttCode));
  325. }
  326. else
  327. {
  328. $aResults[$iCol]= new CellStatus_Modify($oTargetObj->Get($sAttCode), $oTargetObj->GetOriginal($sAttCode));
  329. }
  330. }
  331. else
  332. {
  333. // By default... nothing happens
  334. $aResults[$iCol]= new CellStatus_Void($aRowData[$iCol]);
  335. }
  336. }
  337. // Checks
  338. //
  339. if (!$oTargetObj->CheckConsistency())
  340. {
  341. $aErrors["GLOBAL"] = "Attributes not consistent with each others";
  342. }
  343. return $aResults;
  344. }
  345. protected function CreateObject(&$aResult, $iRow, $aRowData, CMDBChange $oChange = null)
  346. {
  347. $oTargetObj = MetaModel::NewObject($this->m_sClass);
  348. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  349. if (count($aErrors) > 0)
  350. {
  351. $sErrors = implode(', ', $aErrors);
  352. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  353. return;
  354. }
  355. // Check that any external key will have a value proposed
  356. $aMissingKeys = array();
  357. foreach (MetaModel::GetExternalKeys($this->m_sClass) as $sExtKeyAttCode => $oExtKey)
  358. {
  359. if (!$oExtKey->IsNullAllowed())
  360. {
  361. if (!array_key_exists($sExtKeyAttCode, $this->m_aExtKeys) && !array_key_exists($sExtKeyAttCode, $this->m_aAttList))
  362. {
  363. $aMissingKeys[] = $oExtKey->GetLabel();
  364. }
  365. }
  366. }
  367. if (count($aMissingKeys) > 0)
  368. {
  369. $sMissingKeys = implode(', ', $aMissingKeys);
  370. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Could not be created, due to missing external key(s): $sMissingKeys");
  371. return;
  372. }
  373. // Optionaly record the results
  374. //
  375. if ($oChange)
  376. {
  377. $newID = $oTargetObj->DBInsertTrackedNoReload($oChange);
  378. $aResult[$iRow]["__STATUS__"] = new RowStatus_NewObj($this->m_sClass, $newID);
  379. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  380. $aResult[$iRow]["id"] = CellStatus_Void($newID);
  381. }
  382. else
  383. {
  384. $aResult[$iRow]["__STATUS__"] = new RowStatus_NewObj();
  385. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  386. $aResult[$iRow]["id"] = CellStatus_Void(0);
  387. }
  388. }
  389. protected function UpdateObject(&$aResult, $iRow, $oTargetObj, $aRowData, CMDBChange $oChange = null)
  390. {
  391. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  392. // Reporting
  393. //
  394. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  395. $aResult[$iRow]["id"] = CellStatus_Void($aRowData[$iCol]);
  396. if (count($aErrors) > 0)
  397. {
  398. $sErrors = implode(', ', $aErrors);
  399. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  400. return;
  401. }
  402. $aChangedFields = $oTargetObj->ListChanges();
  403. if (count($aChangedFields) > 0)
  404. {
  405. $aResult[$iRow]["__STATUS__"] = new RowStatus_Modify(count($aChangedFields));
  406. // Optionaly record the results
  407. //
  408. if ($oChange)
  409. {
  410. $oTargetObj->DBUpdateTracked($oChange);
  411. }
  412. }
  413. else
  414. {
  415. $aResult[$iRow]["__STATUS__"] = new RowStatus_NoChange();
  416. }
  417. }
  418. public function Process(CMDBChange $oChange = null)
  419. {
  420. // Note: $oChange can be null, in which case the aim is to check what would be done
  421. // Compute the results
  422. //
  423. $aResult = array();
  424. foreach($this->m_aData as $iRow => $aRowData)
  425. {
  426. $oReconciliationFilter = new CMDBSearchFilter($this->m_sClass);
  427. $bSkipQuery = false;
  428. foreach($this->m_aReconcilKeys as $sAttCode)
  429. {
  430. $valuecondition = null;
  431. if (array_key_exists($sAttCode, $this->m_aExtKeys))
  432. {
  433. // The value has to be found or verified
  434. list($sQuery, $aMatches) = $this->ResolveExternalKey($aRowData, $sAttCode, $aResult[$iRow]);
  435. if (count($aMatches) == 1)
  436. {
  437. $oRemoteObj = reset($aMatches); // first item
  438. $valuecondition = $oRemoteObj->GetKey();
  439. $aResult[$iRow][$sAttCode] = new CellStatus_Void($oRemoteObj->GetKey());
  440. }
  441. elseif (count($aMatches) == 0)
  442. {
  443. $aResult[$iRow][$sAttCode] = new CellStatus_Issue(null, null, 'object not found');
  444. }
  445. else
  446. {
  447. $aResult[$iRow][$sAttCode] = new CellStatus_Ambiguous(null, count($aMatches), $sQuery);
  448. }
  449. }
  450. else
  451. {
  452. // The value is given in the data row
  453. $iCol = $this->m_aAttList[$sAttCode];
  454. $valuecondition = $aRowData[$iCol];
  455. }
  456. if (is_null($valuecondition))
  457. {
  458. $bSkipQuery = true;
  459. }
  460. else
  461. {
  462. $oReconciliationFilter->AddCondition($sAttCode, $valuecondition, '=');
  463. }
  464. }
  465. if ($bSkipQuery)
  466. {
  467. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue("failed to reconcile");
  468. }
  469. else
  470. {
  471. $oReconciliationSet = new CMDBObjectSet($oReconciliationFilter);
  472. switch($oReconciliationSet->Count())
  473. {
  474. case 0:
  475. $this->CreateObject($aResult, $iRow, $aRowData, $oChange);
  476. // $aResult[$iRow]["__STATUS__"]=> set in CreateObject
  477. break;
  478. case 1:
  479. $oTargetObj = $oReconciliationSet->Fetch();
  480. $this->UpdateObject($aResult, $iRow, $oTargetObj, $aRowData, $oChange);
  481. // $aResult[$iRow]["__STATUS__"]=> set in UpdateObject
  482. break;
  483. default:
  484. // Found several matches, ambiguous
  485. // Render "void" results on any column
  486. foreach($this->m_aExtKeys as $sAttCode => $aKeyConfig)
  487. {
  488. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  489. {
  490. $aResult[$iRow][$iCol] = new CellStatus_Void($aRowData[$iCol]);
  491. }
  492. $aResult[$iRow][$sAttCode] = new CellStatus_Void('n/a');
  493. }
  494. foreach ($this->m_aAttList as $sAttCode => $iCol)
  495. {
  496. $aResult[$iRow][$iCol]= new CellStatus_Void($aRowData[$iCol]);
  497. }
  498. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue("ambiguous reconciliation");
  499. $aResult[$iRow]["id"]= new CellStatus_Ambiguous(0, $oReconciliationSet->Count(), $oReconciliationFilter->ToOql());
  500. $aResult[$iRow]["finalclass"]= 'n/a';
  501. }
  502. }
  503. // Whatever happened, do report the reconciliation values
  504. foreach($this->m_aAttList as $iCol)
  505. {
  506. if (!array_key_exists($iCol, $aResult[$iRow]))
  507. {
  508. $aResult[$iRow][$iCol] = new CellStatus_Void($aRowData[$iCol]);
  509. }
  510. }
  511. foreach($this->m_aExtKeys as $sAttCode => $aForeignAtts)
  512. {
  513. if (!array_key_exists($sAttCode, $aResult[$iRow]))
  514. {
  515. $aResult[$iRow][$sAttCode] = new CellStatus_Void('n/a');
  516. foreach ($aForeignAtts as $sForeignAttCode => $iCol)
  517. {
  518. // The foreign attribute is one of our reconciliation key
  519. $aResult[$iRow][$iCol] = new CellStatus_Void($aRowData[$iCol]);
  520. }
  521. }
  522. }
  523. }
  524. return $aResult;
  525. }
  526. }
  527. ?>