bulkchange.class.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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_Ambiguous extends CellStatus_Issue
  111. {
  112. protected $m_iCount;
  113. public function __construct($previousValue, $iCount, $sOql)
  114. {
  115. $this->m_iCount = $iCount;
  116. $this->m_sQuery = $sOql;
  117. parent::__construct(null, $previousValue, '');
  118. }
  119. public function GetDescription()
  120. {
  121. $sCount = $this->m_iCount;
  122. return "Ambiguous: found $sCount objects";
  123. }
  124. }
  125. /**
  126. * RowStatus
  127. * A series of classes, keeping the information about a given row: could it be changed or not (and why)?
  128. *
  129. * @package iTopORM
  130. */
  131. abstract class RowStatus
  132. {
  133. public function __construct()
  134. {
  135. }
  136. abstract public function GetDescription();
  137. }
  138. class RowStatus_NoChange extends RowStatus
  139. {
  140. public function GetDescription()
  141. {
  142. return "unchanged";
  143. }
  144. }
  145. class RowStatus_NewObj extends RowStatus
  146. {
  147. public function GetDescription()
  148. {
  149. return "created";
  150. }
  151. }
  152. class RowStatus_Modify extends RowStatus
  153. {
  154. protected $m_iChanged;
  155. public function __construct($iChanged)
  156. {
  157. $this->m_iChanged = $iChanged;
  158. }
  159. public function GetDescription()
  160. {
  161. return "updated ".$this->m_iChanged." cols";
  162. }
  163. }
  164. class RowStatus_Issue extends RowStatus
  165. {
  166. protected $m_sReason;
  167. public function __construct($sReason)
  168. {
  169. $this->m_sReason = $sReason;
  170. }
  171. public function GetDescription()
  172. {
  173. return 'Issue: '.$this->m_sReason;
  174. }
  175. }
  176. /**
  177. * BulkChange
  178. *
  179. * @package iTopORM
  180. */
  181. class BulkChange
  182. {
  183. protected $m_sClass;
  184. protected $m_aData; // Note: hereafter, iCol maybe actually be any acceptable key (string)
  185. // #@# todo: rename the variables to sColIndex
  186. protected $m_aAttList; // attcode => iCol
  187. protected $m_aExtKeys; // aExtKeys[sExtKeyAttCode][sExtReconcKeyAttCode] = iCol;
  188. protected $m_aReconcilKeys;// attcode (attcode = 'id' for the pkey)
  189. public function __construct($sClass, $aData, $aAttList, $aExtKeys, $aReconcilKeys)
  190. {
  191. $this->m_sClass = $sClass;
  192. $this->m_aData = $aData;
  193. $this->m_aAttList = $aAttList;
  194. $this->m_aReconcilKeys = $aReconcilKeys;
  195. $this->m_aExtKeys = $aExtKeys;
  196. }
  197. protected function ResolveExternalKey($aRowData, $sAttCode, &$aResults)
  198. {
  199. $oExtKey = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  200. $oReconFilter = new CMDBSearchFilter($oExtKey->GetTargetClass());
  201. foreach ($this->m_aExtKeys[$sAttCode] as $sForeignAttCode => $iCol)
  202. {
  203. // The foreign attribute is one of our reconciliation key
  204. $oReconFilter->AddCondition($sForeignAttCode, $aRowData[$iCol], '=');
  205. $aResults[$iCol] = new CellStatus_Void($aRowData[$iCol]);
  206. }
  207. $oExtObjects = new CMDBObjectSet($oReconFilter);
  208. $aKeys = $oExtObjects->ToArray();
  209. return array($oReconFilter->ToOql(), $aKeys);
  210. }
  211. protected function PrepareObject(&$oTargetObj, $aRowData, &$aErrors)
  212. {
  213. $aResults = array();
  214. $aErrors = array();
  215. // External keys reconciliation
  216. //
  217. foreach($this->m_aExtKeys as $sAttCode => $aKeyConfig)
  218. {
  219. // Skip external keys used for the reconciliation process
  220. // if (!array_key_exists($sAttCode, $this->m_aAttList)) continue;
  221. $oExtKey = MetaModel::GetAttributeDef(get_class($oTargetObj), $sAttCode);
  222. $oReconFilter = new CMDBSearchFilter($oExtKey->GetTargetClass());
  223. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  224. {
  225. // The foreign attribute is one of our reconciliation key
  226. $oReconFilter->AddCondition($sForeignAttCode, $aRowData[$iCol], '=');
  227. $aResults[$iCol] = new CellStatus_Void($aRowData[$iCol]);
  228. }
  229. $oExtObjects = new CMDBObjectSet($oReconFilter);
  230. switch($oExtObjects->Count())
  231. {
  232. case 0:
  233. if ($oExtKey->IsNullAllowed())
  234. {
  235. $oTargetObj->Set($sAttCode, $oExtKey->GetNullValue());
  236. $aResults[$sAttCode]= new CellStatus_Issue(null, $oTargetObj->Get($sAttCode), 'Object not found');
  237. }
  238. else
  239. {
  240. $aErrors[$sAttCode] = "Object not found";
  241. $aResults[$sAttCode]= new CellStatus_Issue(null, $oTargetObj->Get($sAttCode), 'Object not found');
  242. }
  243. break;
  244. case 1:
  245. // Do change the external key attribute
  246. $oForeignObj = $oExtObjects->Fetch();
  247. $oTargetObj->Set($sAttCode, $oForeignObj->GetKey());
  248. break;
  249. default:
  250. $aErrors[$sAttCode] = "Found ".$oExtObjects->Count()." matches";
  251. $aResults[$sAttCode]= new CellStatus_Ambiguous($oTargetObj->Get($sAttCode), $oExtObjects->Count(), $oReconFilter->ToOql());
  252. }
  253. // Report
  254. if (!array_key_exists($sAttCode, $aResults))
  255. {
  256. $iForeignObj = $oTargetObj->Get($sAttCode);
  257. if (array_key_exists($sAttCode, $oTargetObj->ListChanges()))
  258. {
  259. if ($oTargetObj->IsNew())
  260. {
  261. $aResults[$sAttCode]= new CellStatus_Void($iForeignObj);
  262. }
  263. else
  264. {
  265. $aResults[$sAttCode]= new CellStatus_Modify($iForeignObj, $oTargetObj->GetOriginal($sAttCode));
  266. }
  267. }
  268. else
  269. {
  270. $aResults[$sAttCode]= new CellStatus_Void($iForeignObj);
  271. }
  272. }
  273. }
  274. // Set the object attributes
  275. //
  276. foreach ($this->m_aAttList as $sAttCode => $iCol)
  277. {
  278. // skip the private key, if any
  279. if ($sAttCode == 'id') continue;
  280. $res = $oTargetObj->CheckValue($sAttCode, $aRowData[$iCol]);
  281. if ($res === true)
  282. {
  283. $oTargetObj->Set($sAttCode, $aRowData[$iCol]);
  284. }
  285. else
  286. {
  287. // $res is a string with the error description
  288. $aErrors[$sAttCode] = "Unexpected value for attribute '$sAttCode': $res";
  289. }
  290. }
  291. // Reporting on fields
  292. //
  293. $aChangedFields = $oTargetObj->ListChanges();
  294. foreach ($this->m_aAttList as $sAttCode => $iCol)
  295. {
  296. if ($sAttCode == 'id')
  297. {
  298. $aResults[$iCol]= new CellStatus_Void($aRowData[$iCol]);
  299. }
  300. if (isset($aErrors[$sAttCode]))
  301. {
  302. $aResults[$iCol]= new CellStatus_Issue($oTargetObj->Get($sAttCode), $oTargetObj->GetOriginal($sAttCode), $aErrors[$sAttCode]);
  303. }
  304. elseif (array_key_exists($sAttCode, $aChangedFields))
  305. {
  306. if ($oTargetObj->IsNew())
  307. {
  308. $aResults[$iCol]= new CellStatus_Void($oTargetObj->Get($sAttCode));
  309. }
  310. else
  311. {
  312. $aResults[$iCol]= new CellStatus_Modify($oTargetObj->Get($sAttCode), $oTargetObj->GetOriginal($sAttCode));
  313. }
  314. }
  315. else
  316. {
  317. // By default... nothing happens
  318. $aResults[$iCol]= new CellStatus_Void($aRowData[$iCol]);
  319. }
  320. }
  321. // Checks
  322. //
  323. $res = $oTargetObj->CheckConsistency();
  324. if ($res !== true)
  325. {
  326. // $res contains the error description
  327. $aErrors["GLOBAL"] = "Attributes not consistent with each others: $res";
  328. }
  329. return $aResults;
  330. }
  331. protected function CreateObject(&$aResult, $iRow, $aRowData, CMDBChange $oChange = null)
  332. {
  333. $oTargetObj = MetaModel::NewObject($this->m_sClass);
  334. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  335. if (count($aErrors) > 0)
  336. {
  337. $sErrors = implode(', ', $aErrors);
  338. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  339. return;
  340. }
  341. // Check that any external key will have a value proposed
  342. $aMissingKeys = array();
  343. foreach (MetaModel::GetExternalKeys($this->m_sClass) as $sExtKeyAttCode => $oExtKey)
  344. {
  345. if (!$oExtKey->IsNullAllowed())
  346. {
  347. if (!array_key_exists($sExtKeyAttCode, $this->m_aExtKeys) && !array_key_exists($sExtKeyAttCode, $this->m_aAttList))
  348. {
  349. $aMissingKeys[] = $oExtKey->GetLabel();
  350. }
  351. }
  352. }
  353. if (count($aMissingKeys) > 0)
  354. {
  355. $sMissingKeys = implode(', ', $aMissingKeys);
  356. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Could not be created, due to missing external key(s): $sMissingKeys");
  357. return;
  358. }
  359. // Optionaly record the results
  360. //
  361. if ($oChange)
  362. {
  363. $newID = $oTargetObj->DBInsertTrackedNoReload($oChange);
  364. $aResult[$iRow]["__STATUS__"] = new RowStatus_NewObj($this->m_sClass, $newID);
  365. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  366. $aResult[$iRow]["id"] = new CellStatus_Void($newID);
  367. }
  368. else
  369. {
  370. $aResult[$iRow]["__STATUS__"] = new RowStatus_NewObj();
  371. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  372. $aResult[$iRow]["id"] = new CellStatus_Void(0);
  373. }
  374. }
  375. protected function UpdateObject(&$aResult, $iRow, $oTargetObj, $aRowData, CMDBChange $oChange = null)
  376. {
  377. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  378. // Reporting
  379. //
  380. $aResult[$iRow]["finalclass"] = get_class($oTargetObj);
  381. $aResult[$iRow]["id"] = new CellStatus_Void($oTargetObj->GetKey());
  382. if (count($aErrors) > 0)
  383. {
  384. $sErrors = implode(', ', $aErrors);
  385. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  386. return;
  387. }
  388. $aChangedFields = $oTargetObj->ListChanges();
  389. if (count($aChangedFields) > 0)
  390. {
  391. $aResult[$iRow]["__STATUS__"] = new RowStatus_Modify(count($aChangedFields));
  392. // Optionaly record the results
  393. //
  394. if ($oChange)
  395. {
  396. $oTargetObj->DBUpdateTracked($oChange);
  397. }
  398. }
  399. else
  400. {
  401. $aResult[$iRow]["__STATUS__"] = new RowStatus_NoChange();
  402. }
  403. }
  404. public function Process(CMDBChange $oChange = null)
  405. {
  406. // Note: $oChange can be null, in which case the aim is to check what would be done
  407. // Compute the results
  408. //
  409. $aResult = array();
  410. foreach($this->m_aData as $iRow => $aRowData)
  411. {
  412. $oReconciliationFilter = new CMDBSearchFilter($this->m_sClass);
  413. $bSkipQuery = false;
  414. foreach($this->m_aReconcilKeys as $sAttCode)
  415. {
  416. $valuecondition = null;
  417. if (array_key_exists($sAttCode, $this->m_aExtKeys))
  418. {
  419. // The value has to be found or verified
  420. list($sQuery, $aMatches) = $this->ResolveExternalKey($aRowData, $sAttCode, $aResult[$iRow]);
  421. if (count($aMatches) == 1)
  422. {
  423. $oRemoteObj = reset($aMatches); // first item
  424. $valuecondition = $oRemoteObj->GetKey();
  425. $aResult[$iRow][$sAttCode] = new CellStatus_Void($oRemoteObj->GetKey());
  426. }
  427. elseif (count($aMatches) == 0)
  428. {
  429. $aResult[$iRow][$sAttCode] = new CellStatus_Issue(null, null, 'object not found');
  430. }
  431. else
  432. {
  433. $aResult[$iRow][$sAttCode] = new CellStatus_Ambiguous(null, count($aMatches), $sQuery);
  434. }
  435. }
  436. else
  437. {
  438. // The value is given in the data row
  439. $iCol = $this->m_aAttList[$sAttCode];
  440. $valuecondition = $aRowData[$iCol];
  441. }
  442. if (is_null($valuecondition))
  443. {
  444. $bSkipQuery = true;
  445. }
  446. else
  447. {
  448. $oReconciliationFilter->AddCondition($sAttCode, $valuecondition, '=');
  449. }
  450. }
  451. if ($bSkipQuery)
  452. {
  453. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue("failed to reconcile");
  454. }
  455. else
  456. {
  457. $oReconciliationSet = new CMDBObjectSet($oReconciliationFilter);
  458. switch($oReconciliationSet->Count())
  459. {
  460. case 0:
  461. $this->CreateObject($aResult, $iRow, $aRowData, $oChange);
  462. // $aResult[$iRow]["__STATUS__"]=> set in CreateObject
  463. break;
  464. case 1:
  465. $oTargetObj = $oReconciliationSet->Fetch();
  466. $this->UpdateObject($aResult, $iRow, $oTargetObj, $aRowData, $oChange);
  467. // $aResult[$iRow]["__STATUS__"]=> set in UpdateObject
  468. break;
  469. default:
  470. // Found several matches, ambiguous
  471. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue("ambiguous reconciliation");
  472. $aResult[$iRow]["id"]= new CellStatus_Ambiguous(0, $oReconciliationSet->Count(), $oReconciliationFilter->ToOql());
  473. $aResult[$iRow]["finalclass"]= 'n/a';
  474. }
  475. }
  476. // Whatever happened, do report the reconciliation values
  477. foreach($this->m_aAttList as $iCol)
  478. {
  479. if (!array_key_exists($iCol, $aResult[$iRow]))
  480. {
  481. $aResult[$iRow][$iCol] = new CellStatus_Void($aRowData[$iCol]);
  482. }
  483. }
  484. foreach($this->m_aExtKeys as $sAttCode => $aForeignAtts)
  485. {
  486. if (!array_key_exists($sAttCode, $aResult[$iRow]))
  487. {
  488. $aResult[$iRow][$sAttCode] = new CellStatus_Void('n/a');
  489. foreach ($aForeignAtts as $sForeignAttCode => $iCol)
  490. {
  491. // The foreign attribute is one of our reconciliation key
  492. $aResult[$iRow][$iCol] = new CellStatus_Void($aRowData[$iCol]);
  493. }
  494. }
  495. }
  496. }
  497. return $aResult;
  498. }
  499. }
  500. ?>