bulkchange.class.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. $oObj = MetaModel::GetObject($sClass, $iId);
  214. if (is_null($oObj))
  215. {
  216. return $iId;
  217. }
  218. else
  219. {
  220. return $oObj;
  221. }
  222. }
  223. protected function PrepareObject(&$oTargetObj, $aRowData, &$aErrors)
  224. {
  225. $aResults = array();
  226. $aErrors = array();
  227. // External keys reconciliation
  228. //
  229. foreach($this->m_aExtKeys as $sAttCode => $aKeyConfig)
  230. {
  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["col$iCol"] = new CellChangeSpec_Void($aRowData[$iCol]);
  238. }
  239. $oExtObjects = new CMDBObjectSet($oReconFilter);
  240. switch($oExtObjects->Count())
  241. {
  242. case 0:
  243. $aErrors[$sAttCode] = "Object not found";
  244. $aResults[$sAttCode]= new CellChangeSpec_Issue(null, $oTargetObj->Get($sAttCode), 'Object not found - check the spelling (no space before/after)');
  245. break;
  246. case 1:
  247. // Do change the external key attribute
  248. $oForeignObj = $oExtObjects->Fetch();
  249. $oTargetObj->Set($sAttCode, $oForeignObj->GetKey());
  250. // Report it
  251. if (array_key_exists($sAttCode, $oTargetObj->ListChanges()))
  252. {
  253. if ($oTargetObj->IsNew())
  254. {
  255. $aResults[$sAttCode]= new CellChangeSpec_Init($oForeignObj);
  256. }
  257. else
  258. {
  259. $previousValue = self::MakeSpecObject($oExtKey->GetTargetClass(), $oTargetObj->GetOriginal($sAttCode));
  260. $aResults[$sAttCode]= new CellChangeSpec_Modify($oForeignObj, $previousValue);
  261. }
  262. }
  263. else
  264. {
  265. $aResults[$sAttCode]= new CellChangeSpec_Unchanged($oForeignObj);
  266. }
  267. break;
  268. default:
  269. $aErrors[$sAttCode] = "Found ".$oExtObjects->Count()." matches";
  270. $previousValue = self::MakeSpecObject($oExtKey->GetTargetClass(), $oTargetObj->Get($sAttCode));
  271. $aResults[$sAttCode]= new CellChangeSpec_Issue(null, $previousValue, "Found ".$oExtObjects->Count()." matches");
  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. if (!$oTargetObj->CheckValue($sAttCode, $aRowData[$iCol]))
  281. {
  282. $aErrors[$sAttCode] = "Unexpected value";
  283. }
  284. else
  285. {
  286. $oTargetObj->Set($sAttCode, $aRowData[$iCol]);
  287. }
  288. }
  289. // Reporting on fields
  290. //
  291. $aChangedFields = $oTargetObj->ListChanges();
  292. foreach ($this->m_aAttList as $sAttCode => $iCol)
  293. {
  294. if ($sAttCode == 'id')
  295. {
  296. if ($aRowData[$iCol] == $oTargetObj->GetKey())
  297. {
  298. $aResults["col$iCol"]= new CellChangeSpec_Void($aRowData[$iCol]);
  299. }
  300. else
  301. {
  302. $aResults["col$iCol"]= new CellChangeSpec_Init($aRowData[$iCol]);
  303. }
  304. }
  305. if (isset($aErrors[$sAttCode]))
  306. {
  307. $aResults["col$iCol"]= new CellChangeSpec_Issue($oTargetObj->Get($sAttCode), $oTargetObj->GetOriginal($sAttCode), $aErrors[$sAttCode]);
  308. }
  309. elseif (array_key_exists($sAttCode, $aChangedFields))
  310. {
  311. if ($oTargetObj->IsNew())
  312. {
  313. $aResults["col$iCol"]= new CellChangeSpec_Init($oTargetObj->Get($sAttCode));
  314. }
  315. else
  316. {
  317. $aResults["col$iCol"]= new CellChangeSpec_Modify($oTargetObj->Get($sAttCode), $oTargetObj->GetOriginal($sAttCode));
  318. }
  319. }
  320. else
  321. {
  322. // By default... nothing happens
  323. $aResults["col$iCol"]= new CellChangeSpec_Void($aRowData[$iCol]);
  324. }
  325. }
  326. // Checks
  327. //
  328. if (!$oTargetObj->CheckConsistency())
  329. {
  330. $aErrors["GLOBAL"] = "Attributes not consistent with each others";
  331. }
  332. return $aResults;
  333. }
  334. protected function CreateObject(&$aResult, $iRow, $aRowData, CMDBChange $oChange = null)
  335. {
  336. $oTargetObj = MetaModel::NewObject($this->m_sClass);
  337. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  338. if (count($aErrors) > 0)
  339. {
  340. $sErrors = implode(', ', $aErrors);
  341. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  342. return;
  343. }
  344. // Check that any external key will have a value proposed
  345. $aMissingKeys = array();
  346. foreach (MetaModel::GetExternalKeys($this->m_sClass) as $sExtKeyAttCode => $oExtKey)
  347. {
  348. if (!$oExtKey->IsNullAllowed())
  349. {
  350. if (!array_key_exists($sExtKeyAttCode, $this->m_aExtKeys) && !array_key_exists($sExtKeyAttCode, $this->m_aAttList))
  351. {
  352. $aMissingKeys[] = $oExtKey->GetLabel();
  353. }
  354. }
  355. }
  356. if (count($aMissingKeys) > 0)
  357. {
  358. $sMissingKeys = implode(', ', $aMissingKeys);
  359. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Could not be created, due to missing external key(s): $sMissingKeys");
  360. return;
  361. }
  362. // Optionaly record the results
  363. //
  364. if ($oChange)
  365. {
  366. $newID = $oTargetObj->DBInsertTrackedNoReload($oChange);
  367. $aResult[$iRow]["__STATUS__"] = new RowStatus_NewObj($this->m_sClass, $newID);
  368. }
  369. else
  370. {
  371. $aResult[$iRow]["__STATUS__"] = new RowStatus_NewObj();
  372. }
  373. }
  374. protected function UpdateObject(&$aResult, $iRow, $oTargetObj, $aRowData, CMDBChange $oChange = null)
  375. {
  376. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  377. // Reporting
  378. //
  379. if (count($aErrors) > 0)
  380. {
  381. $sErrors = implode(', ', $aErrors);
  382. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  383. return;
  384. }
  385. $aChangedFields = $oTargetObj->ListChanges();
  386. if (count($aChangedFields) > 0)
  387. {
  388. $aResult[$iRow]["__STATUS__"] = new RowStatus_Modify(count($aChangedFields));
  389. // Optionaly record the results
  390. //
  391. if ($oChange)
  392. {
  393. $oTargetObj->DBUpdateTracked($oChange);
  394. }
  395. }
  396. else
  397. {
  398. $aResult[$iRow]["__STATUS__"] = new RowStatus_NoChange();
  399. }
  400. }
  401. public function Process(CMDBChange $oChange = null)
  402. {
  403. // Note: $oChange can be null, in which case the aim is to check what would be done
  404. // Compute the results
  405. //
  406. $aResult = array();
  407. foreach($this->m_aData as $iRow => $aRowData)
  408. {
  409. $oReconciliationFilter = new CMDBSearchFilter($this->m_sClass);
  410. foreach($this->m_aReconcilKeys as $sAttCode)
  411. {
  412. $iCol = $this->m_aAttList[$sAttCode];
  413. $oReconciliationFilter->AddCondition($sAttCode, $aRowData[$iCol], '=');
  414. }
  415. $oReconciliationSet = new CMDBObjectSet($oReconciliationFilter);
  416. switch($oReconciliationSet->Count())
  417. {
  418. case 0:
  419. $this->CreateObject($aResult, $iRow, $aRowData, $oChange);
  420. // $aResult[$iRow]["__STATUS__"]=> set in CreateObject
  421. $aResult[$iRow]["__RECONCILIATION__"] = "Object not found";
  422. break;
  423. case 1:
  424. $oTargetObj = $oReconciliationSet->Fetch();
  425. $this->UpdateObject($aResult, $iRow, $oTargetObj, $aRowData, $oChange);
  426. $aResult[$iRow]["__RECONCILIATION__"] = "Found a match ".$oTargetObj->GetHyperLink();
  427. // $aResult[$iRow]["__STATUS__"]=> set in UpdateObject
  428. break;
  429. default:
  430. // Found several matches, ambiguous
  431. // Render "void" results on any column
  432. foreach($this->m_aExtKeys as $sAttCode => $aKeyConfig)
  433. {
  434. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  435. {
  436. $aResult[$iRow]["col$iCol"] = new CellChangeSpec_Void($aRowData[$iCol]);
  437. }
  438. $aResult[$iRow][$sAttCode] = new CellChangeSpec_Void('n/a');
  439. }
  440. foreach ($this->m_aAttList as $sAttCode => $iCol)
  441. {
  442. $aResult[$iRow]["col$iCol"]= new CellChangeSpec_Void($aRowData[$iCol]);
  443. }
  444. $aResult[$iRow]["__RECONCILIATION__"] = "Found ".$oReconciliationSet->Count()." matches";
  445. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue("ambiguous reconciliation");
  446. }
  447. // Whatever happened, do report the reconciliation values
  448. foreach($this->m_aReconcilKeys as $sAttCode)
  449. {
  450. $iCol = $this->m_aAttList[$sAttCode];
  451. $aResult[$iRow]["col$iCol"] = new CellChangeSpec_Void($aRowData[$iCol]);
  452. }
  453. }
  454. return $aResult;
  455. }
  456. }
  457. ?>