bulkchange.class.inc.php 14 KB

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