bulkchange.class.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. protected function ResolveExternalKey($aRowData, $sAttCode, &$aResults)
  195. {
  196. $oExtKey = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  197. $oReconFilter = new CMDBSearchFilter($oExtKey->GetTargetClass());
  198. foreach ($this->m_aExtKeys[$sAttCode] as $sForeignAttCode => $iCol)
  199. {
  200. // The foreign attribute is one of our reconciliation key
  201. $oReconFilter->AddCondition($sForeignAttCode, $aRowData[$iCol], '=');
  202. $aResults[$iCol] = new CellStatus_Void($aRowData[$iCol]);
  203. }
  204. $oExtObjects = new CMDBObjectSet($oReconFilter);
  205. $aKeys = $oExtObjects->ToArray();
  206. return array($oReconFilter->ToOql(), $aKeys);
  207. }
  208. protected function PrepareObject(&$oTargetObj, $aRowData, &$aErrors)
  209. {
  210. $aResults = array();
  211. $aErrors = array();
  212. // External keys reconciliation
  213. //
  214. foreach($this->m_aExtKeys as $sAttCode => $aKeyConfig)
  215. {
  216. // Skip external keys used for the reconciliation process
  217. // if (!array_key_exists($sAttCode, $this->m_aAttList)) continue;
  218. $oExtKey = MetaModel::GetAttributeDef(get_class($oTargetObj), $sAttCode);
  219. $oReconFilter = new CMDBSearchFilter($oExtKey->GetTargetClass());
  220. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  221. {
  222. // The foreign attribute is one of our reconciliation key
  223. $oReconFilter->AddCondition($sForeignAttCode, $aRowData[$iCol], '=');
  224. $aResults[$iCol] = new CellStatus_Void($aRowData[$iCol]);
  225. }
  226. $oExtObjects = new CMDBObjectSet($oReconFilter);
  227. switch($oExtObjects->Count())
  228. {
  229. case 0:
  230. if ($oExtKey->IsNullAllowed())
  231. {
  232. $oTargetObj->Set($sAttCode, $oExtKey->GetNullValue());
  233. }
  234. else
  235. {
  236. $aErrors[$sAttCode] = "Object not found";
  237. $aResults[$sAttCode]= new CellStatus_Issue(null, $oTargetObj->Get($sAttCode), 'Object not found');
  238. }
  239. break;
  240. case 1:
  241. // Do change the external key attribute
  242. $oForeignObj = $oExtObjects->Fetch();
  243. $oTargetObj->Set($sAttCode, $oForeignObj->GetKey());
  244. break;
  245. default:
  246. $aErrors[$sAttCode] = "Found ".$oExtObjects->Count()." matches";
  247. $aResults[$sAttCode]= new CellStatus_Ambiguous($oTargetObj->Get($sAttCode), $oExtObjects->Count(), $oExtObjects->ToOql());
  248. }
  249. // Report
  250. if (!array_key_exists($sAttCode, $aResults))
  251. {
  252. $iForeignObj = $oTargetObj->Get($sAttCode);
  253. if (array_key_exists($sAttCode, $oTargetObj->ListChanges()))
  254. {
  255. if ($oTargetObj->IsNew())
  256. {
  257. $aResults[$sAttCode]= new CellStatus_Void($iForeignObj);
  258. }
  259. else
  260. {
  261. $aResults[$sAttCode]= new CellStatus_Modify($iForeignObj, $oTargetObj->GetOriginal($sAttCode));
  262. }
  263. }
  264. else
  265. {
  266. $aResults[$sAttCode]= new CellStatus_Void($iForeignObj);
  267. }
  268. }
  269. }
  270. // Set the object attributes
  271. //
  272. foreach ($this->m_aAttList as $sAttCode => $iCol)
  273. {
  274. // skip the private key, if any
  275. if ($sAttCode == 'id') continue;
  276. if (!$oTargetObj->CheckValue($sAttCode, $aRowData[$iCol]))
  277. {
  278. $aErrors[$sAttCode] = "Unexpected value";
  279. }
  280. else
  281. {
  282. $oTargetObj->Set($sAttCode, $aRowData[$iCol]);
  283. }
  284. }
  285. // Reporting on fields
  286. //
  287. $aChangedFields = $oTargetObj->ListChanges();
  288. foreach ($this->m_aAttList as $sAttCode => $iCol)
  289. {
  290. if ($sAttCode == 'id')
  291. {
  292. $aResults[$iCol]= new CellStatus_Void($aRowData[$iCol]);
  293. }
  294. if (isset($aErrors[$sAttCode]))
  295. {
  296. $aResults[$iCol]= new CellStatus_Issue($oTargetObj->Get($sAttCode), $oTargetObj->GetOriginal($sAttCode), $aErrors[$sAttCode]);
  297. }
  298. elseif (array_key_exists($sAttCode, $aChangedFields))
  299. {
  300. if ($oTargetObj->IsNew())
  301. {
  302. $aResults[$iCol]= new CellStatus_Void($oTargetObj->Get($sAttCode));
  303. }
  304. else
  305. {
  306. $aResults[$iCol]= new CellStatus_Modify($oTargetObj->Get($sAttCode), $oTargetObj->GetOriginal($sAttCode));
  307. }
  308. }
  309. else
  310. {
  311. // By default... nothing happens
  312. $aResults[$iCol]= new CellStatus_Void($aRowData[$iCol]);
  313. }
  314. }
  315. // Checks
  316. //
  317. if (!$oTargetObj->CheckConsistency())
  318. {
  319. $aErrors["GLOBAL"] = "Attributes not consistent with each others";
  320. }
  321. return $aResults;
  322. }
  323. protected function CreateObject(&$aResult, $iRow, $aRowData, CMDBChange $oChange = null)
  324. {
  325. $oTargetObj = MetaModel::NewObject($this->m_sClass);
  326. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  327. if (count($aErrors) > 0)
  328. {
  329. $sErrors = implode(', ', $aErrors);
  330. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  331. return;
  332. }
  333. // Check that any external key will have a value proposed
  334. $aMissingKeys = array();
  335. foreach (MetaModel::GetExternalKeys($this->m_sClass) as $sExtKeyAttCode => $oExtKey)
  336. {
  337. if (!$oExtKey->IsNullAllowed())
  338. {
  339. if (!array_key_exists($sExtKeyAttCode, $this->m_aExtKeys) && !array_key_exists($sExtKeyAttCode, $this->m_aAttList))
  340. {
  341. $aMissingKeys[] = $oExtKey->GetLabel();
  342. }
  343. }
  344. }
  345. if (count($aMissingKeys) > 0)
  346. {
  347. $sMissingKeys = implode(', ', $aMissingKeys);
  348. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Could not be created, due to missing external key(s): $sMissingKeys");
  349. return;
  350. }
  351. // Optionaly record the results
  352. //
  353. if ($oChange)
  354. {
  355. $newID = $oTargetObj->DBInsertTrackedNoReload($oChange);
  356. $aResult[$iRow]["__STATUS__"] = new RowStatus_NewObj($this->m_sClass, $newID);
  357. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  358. $aResult[$iRow]["id"] = new CellStatus_Void($newID);
  359. }
  360. else
  361. {
  362. $aResult[$iRow]["__STATUS__"] = new RowStatus_NewObj();
  363. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  364. $aResult[$iRow]["id"] = new CellStatus_Void(0);
  365. }
  366. }
  367. protected function UpdateObject(&$aResult, $iRow, $oTargetObj, $aRowData, CMDBChange $oChange = null)
  368. {
  369. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  370. // Reporting
  371. //
  372. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  373. $aResult[$iRow]["id"] = new CellStatus_Void($oTargetObj->GetKey());
  374. if (count($aErrors) > 0)
  375. {
  376. $sErrors = implode(', ', $aErrors);
  377. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  378. return;
  379. }
  380. $aChangedFields = $oTargetObj->ListChanges();
  381. if (count($aChangedFields) > 0)
  382. {
  383. $aResult[$iRow]["__STATUS__"] = new RowStatus_Modify(count($aChangedFields));
  384. // Optionaly record the results
  385. //
  386. if ($oChange)
  387. {
  388. $oTargetObj->DBUpdateTracked($oChange);
  389. }
  390. }
  391. else
  392. {
  393. $aResult[$iRow]["__STATUS__"] = new RowStatus_NoChange();
  394. }
  395. }
  396. public function Process(CMDBChange $oChange = null)
  397. {
  398. // Note: $oChange can be null, in which case the aim is to check what would be done
  399. // Compute the results
  400. //
  401. $aResult = array();
  402. foreach($this->m_aData as $iRow => $aRowData)
  403. {
  404. $oReconciliationFilter = new CMDBSearchFilter($this->m_sClass);
  405. $bSkipQuery = false;
  406. foreach($this->m_aReconcilKeys as $sAttCode)
  407. {
  408. $valuecondition = null;
  409. if (array_key_exists($sAttCode, $this->m_aExtKeys))
  410. {
  411. // The value has to be found or verified
  412. list($sQuery, $aMatches) = $this->ResolveExternalKey($aRowData, $sAttCode, $aResult[$iRow]);
  413. if (count($aMatches) == 1)
  414. {
  415. $oRemoteObj = reset($aMatches); // first item
  416. $valuecondition = $oRemoteObj->GetKey();
  417. $aResult[$iRow][$sAttCode] = new CellStatus_Void($oRemoteObj->GetKey());
  418. }
  419. elseif (count($aMatches) == 0)
  420. {
  421. $aResult[$iRow][$sAttCode] = new CellStatus_Issue(null, null, 'object not found');
  422. }
  423. else
  424. {
  425. $aResult[$iRow][$sAttCode] = new CellStatus_Ambiguous(null, count($aMatches), $sQuery);
  426. }
  427. }
  428. else
  429. {
  430. // The value is given in the data row
  431. $iCol = $this->m_aAttList[$sAttCode];
  432. $valuecondition = $aRowData[$iCol];
  433. }
  434. if (is_null($valuecondition))
  435. {
  436. $bSkipQuery = true;
  437. }
  438. else
  439. {
  440. $oReconciliationFilter->AddCondition($sAttCode, $valuecondition, '=');
  441. }
  442. }
  443. if ($bSkipQuery)
  444. {
  445. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue("failed to reconcile");
  446. }
  447. else
  448. {
  449. $oReconciliationSet = new CMDBObjectSet($oReconciliationFilter);
  450. switch($oReconciliationSet->Count())
  451. {
  452. case 0:
  453. $this->CreateObject($aResult, $iRow, $aRowData, $oChange);
  454. // $aResult[$iRow]["__STATUS__"]=> set in CreateObject
  455. break;
  456. case 1:
  457. $oTargetObj = $oReconciliationSet->Fetch();
  458. $this->UpdateObject($aResult, $iRow, $oTargetObj, $aRowData, $oChange);
  459. // $aResult[$iRow]["__STATUS__"]=> set in UpdateObject
  460. break;
  461. default:
  462. // Found several matches, ambiguous
  463. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue("ambiguous reconciliation");
  464. $aResult[$iRow]["id"]= new CellStatus_Ambiguous(0, $oReconciliationSet->Count(), $oReconciliationFilter->ToOql());
  465. $aResult[$iRow]["finalclass"]= 'n/a';
  466. }
  467. }
  468. // Whatever happened, do report the reconciliation values
  469. foreach($this->m_aAttList as $iCol)
  470. {
  471. if (!array_key_exists($iCol, $aResult[$iRow]))
  472. {
  473. $aResult[$iRow][$iCol] = new CellStatus_Void($aRowData[$iCol]);
  474. }
  475. }
  476. foreach($this->m_aExtKeys as $sAttCode => $aForeignAtts)
  477. {
  478. if (!array_key_exists($sAttCode, $aResult[$iRow]))
  479. {
  480. $aResult[$iRow][$sAttCode] = new CellStatus_Void('n/a');
  481. foreach ($aForeignAtts as $sForeignAttCode => $iCol)
  482. {
  483. // The foreign attribute is one of our reconciliation key
  484. $aResult[$iRow][$iCol] = new CellStatus_Void($aRowData[$iCol]);
  485. }
  486. }
  487. }
  488. }
  489. return $aResult;
  490. }
  491. }
  492. ?>