bulkchange.class.inc.php 15 KB

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