bulkchange.class.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. *
  190. * @package iTopORM
  191. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  192. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  193. * @link www.itop.com
  194. * @since 1.0
  195. * @version $itopversion$
  196. */
  197. class BulkChange
  198. {
  199. protected $m_sClass;
  200. protected $m_aData;
  201. // Note: hereafter, iCol maybe actually be any acceptable key (string)
  202. // #@# todo: rename the variables to sColIndex
  203. protected $m_aAttList; // attcode => iCol
  204. protected $m_aReconcilKeys;// iCol => attcode
  205. protected $m_aExtKeys; // aExtKeys[sExtKeyAttCode][sExtReconcKeyAttCode] = iCol;
  206. public function __construct($sClass, $aData, $aAttList, $aReconcilKeys, $aExtKeys)
  207. {
  208. $this->m_sClass = $sClass;
  209. $this->m_aData = $aData;
  210. $this->m_aAttList = $aAttList;
  211. $this->m_aReconcilKeys = $aReconcilKeys;
  212. $this->m_aExtKeys = $aExtKeys;
  213. }
  214. static protected function MakeSpecObject($sClass, $iId)
  215. {
  216. $oObj = MetaModel::GetObject($sClass, $iId);
  217. if (is_null($oObj))
  218. {
  219. return $iId;
  220. }
  221. else
  222. {
  223. return $oObj;
  224. }
  225. }
  226. protected function PrepareObject(&$oTargetObj, $aRowData, &$aErrors)
  227. {
  228. $aResults = array();
  229. $aErrors = array();
  230. // External keys reconciliation
  231. //
  232. foreach($this->m_aExtKeys as $sAttCode => $aKeyConfig)
  233. {
  234. $oExtKey = MetaModel::GetAttributeDef(get_class($oTargetObj), $sAttCode);
  235. $oReconFilter = new CMDBSearchFilter($oExtKey->GetTargetClass());
  236. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  237. {
  238. // The foreign attribute is one of our reconciliation key
  239. $sFieldId = MakeExtFieldSelectValue($sAttCode, $sForeignAttCode);
  240. $oReconFilter->AddCondition($sForeignAttCode, $aRowData[$iCol], '=');
  241. $aResults["col$iCol"] = new CellChangeSpec_Void($aRowData[$iCol]);
  242. }
  243. $oExtObjects = new CMDBObjectSet($oReconFilter);
  244. switch($oExtObjects->Count())
  245. {
  246. case 0:
  247. $aErrors[$sAttCode] = "Object not found";
  248. $aResults[$sAttCode]= new CellChangeSpec_Issue(null, $oTargetObj->Get($sAttCode), 'Object not found - check the spelling (no space before/after)');
  249. break;
  250. case 1:
  251. // Do change the external key attribute
  252. $oForeignObj = $oExtObjects->Fetch();
  253. $oTargetObj->Set($sAttCode, $oForeignObj->GetKey());
  254. // Report it
  255. if (array_key_exists($sAttCode, $oTargetObj->ListChanges()))
  256. {
  257. if ($oTargetObj->IsNew())
  258. {
  259. $aResults[$sAttCode]= new CellChangeSpec_Init($oForeignObj);
  260. }
  261. else
  262. {
  263. $previousValue = self::MakeSpecObject($oExtKey->GetTargetClass(), $oTargetObj->GetOriginal($sAttCode));
  264. $aResults[$sAttCode]= new CellChangeSpec_Modify($oForeignObj, $previousValue);
  265. }
  266. }
  267. else
  268. {
  269. $aResults[$sAttCode]= new CellChangeSpec_Unchanged($oForeignObj);
  270. }
  271. break;
  272. default:
  273. $aErrors[$sAttCode] = "Found ".$oExtObjects->Count()." matches";
  274. $previousValue = self::MakeSpecObject($oExtKey->GetTargetClass(), $oTargetObj->Get($sAttCode));
  275. $aResults[$sAttCode]= new CellChangeSpec_Issue(null, $previousValue, "Found ".$oExtObjects->Count()." matches");
  276. }
  277. }
  278. // Set the object attributes
  279. //
  280. foreach ($this->m_aAttList as $sAttCode => $iCol)
  281. {
  282. if (!$oTargetObj->CheckValue($sAttCode, $aRowData[$iCol]))
  283. {
  284. $aErrors[$sAttCode] = "Unexpected value";
  285. }
  286. else
  287. {
  288. $oTargetObj->Set($sAttCode, $aRowData[$iCol]);
  289. }
  290. }
  291. // Reporting on fields
  292. //
  293. $aChangedFields = $oTargetObj->ListChanges();
  294. foreach ($this->m_aAttList as $sAttCode => $iCol)
  295. {
  296. if (isset($aErrors[$sAttCode]))
  297. {
  298. $aResults["col$iCol"]= new CellChangeSpec_Issue($oTargetObj->Get($sAttCode), $oTargetObj->GetOriginal($sAttCode), $aErrors[$sAttCode]);
  299. }
  300. elseif (array_key_exists($sAttCode, $aChangedFields))
  301. {
  302. if ($oTargetObj->IsNew())
  303. {
  304. $aResults["col$iCol"]= new CellChangeSpec_Init($oTargetObj->Get($sAttCode));
  305. }
  306. else
  307. {
  308. $aResults["col$iCol"]= new CellChangeSpec_Modify($oTargetObj->Get($sAttCode), $oTargetObj->GetOriginal($sAttCode));
  309. }
  310. }
  311. else
  312. {
  313. // By default... nothing happens
  314. $aResults["col$iCol"]= new CellChangeSpec_Void($aRowData[$iCol]);
  315. }
  316. }
  317. // Checks
  318. //
  319. if (!$oTargetObj->CheckConsistency())
  320. {
  321. $aErrors["GLOBAL"] = "Attributes not consistent with each others";
  322. }
  323. return $aResults;
  324. }
  325. protected function CreateObject(&$aResult, $iRow, $aRowData, CMDBChange $oChange = null)
  326. {
  327. $oTargetObj = MetaModel::NewObject($this->m_sClass);
  328. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  329. if (count($aErrors) > 0)
  330. {
  331. $sErrors = implode(', ', $aErrors);
  332. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  333. return;
  334. }
  335. // Check that any external key will have a value proposed
  336. $aMissingKeys = array();
  337. foreach (MetaModel::GetExternalKeys($this->m_sClass) as $sExtKeyAttCode => $oExtKey)
  338. {
  339. if (!$oExtKey->IsNullAllowed())
  340. {
  341. if (!array_key_exists($sExtKeyAttCode, $this->m_aExtKeys) && !array_key_exists($sExtKeyAttCode, $this->m_aAttList))
  342. {
  343. $aMissingKeys[] = $oExtKey->GetLabel();
  344. }
  345. }
  346. }
  347. if (count($aMissingKeys) > 0)
  348. {
  349. $sMissingKeys = implode(', ', $aMissingKeys);
  350. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Could not be created, due to missing external key(s): $sMissingKeys");
  351. return;
  352. }
  353. // Optionaly record the results
  354. //
  355. if ($oChange)
  356. {
  357. $newID = $oTargetObj->DBInsertTrackedNoReload($oChange);
  358. $aResult[$iRow]["__STATUS__"] = new RowStatus_NewObj($this->m_sClass, $newID);
  359. }
  360. else
  361. {
  362. $aResult[$iRow]["__STATUS__"] = new RowStatus_NewObj();
  363. }
  364. }
  365. protected function UpdateObject(&$aResult, $iRow, $oTargetObj, $aRowData, CMDBChange $oChange = null)
  366. {
  367. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  368. // Reporting
  369. //
  370. if (count($aErrors) > 0)
  371. {
  372. $sErrors = implode(', ', $aErrors);
  373. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  374. return;
  375. }
  376. $aChangedFields = $oTargetObj->ListChanges();
  377. if (count($aChangedFields) > 0)
  378. {
  379. $aResult[$iRow]["__STATUS__"] = new RowStatus_Modify(count($aChangedFields));
  380. // Optionaly record the results
  381. //
  382. if ($oChange)
  383. {
  384. $oTargetObj->DBUpdateTracked($oChange);
  385. }
  386. }
  387. else
  388. {
  389. $aResult[$iRow]["__STATUS__"] = new RowStatus_NoChange();
  390. }
  391. }
  392. public function Process(CMDBChange $oChange = null)
  393. {
  394. // Note: $oChange can be null, in which case the aim is to check what would be done
  395. // Compute the results
  396. //
  397. $aResult = array();
  398. foreach($this->m_aData as $iRow => $aRowData)
  399. {
  400. $oReconciliationFilter = new CMDBSearchFilter($this->m_sClass);
  401. foreach($this->m_aReconcilKeys as $sAttCode)
  402. {
  403. $iCol = $this->m_aAttList[$sAttCode];
  404. $oReconciliationFilter->AddCondition($sAttCode, $aRowData[$iCol], '=');
  405. }
  406. $oReconciliationSet = new CMDBObjectSet($oReconciliationFilter);
  407. switch($oReconciliationSet->Count())
  408. {
  409. case 0:
  410. $this->CreateObject($aResult, $iRow, $aRowData, $oChange);
  411. // $aResult[$iRow]["__STATUS__"]=> set in CreateObject
  412. $aResult[$iRow]["__RECONCILIATION__"] = "Object not found";
  413. break;
  414. case 1:
  415. $oTargetObj = $oReconciliationSet->Fetch();
  416. $this->UpdateObject($aResult, $iRow, $oTargetObj, $aRowData, $oChange);
  417. $aResult[$iRow]["__RECONCILIATION__"] = "Found a match ".$oTargetObj->GetHyperLink();
  418. // $aResult[$iRow]["__STATUS__"]=> set in UpdateObject
  419. break;
  420. default:
  421. foreach ($this->m_aAttList as $sAttCode => $iCol)
  422. {
  423. $aResult[$iRow]["col$iCol"]= $aRowData[$iCol];
  424. }
  425. $aResult[$iRow]["__RECONCILIATION__"] = "Found ".$oReconciliationSet->Count()." matches";
  426. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue("ambiguous reconciliation");
  427. }
  428. // Whatever happened, do report the reconciliation values
  429. foreach($this->m_aReconcilKeys as $sAttCode)
  430. {
  431. $iCol = $this->m_aAttList[$sAttCode];
  432. $aResult[$iRow]["col$iCol"] = new CellChangeSpec_Void($aRowData[$iCol]);
  433. }
  434. }
  435. return $aResult;
  436. }
  437. }
  438. ?>