bulkchange.class.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. public function GetValue()
  36. {
  37. return $this->m_proposedValue;
  38. }
  39. abstract public function GetDescription();
  40. }
  41. class CellChangeSpec_Void extends CellChangeSpec
  42. {
  43. public function GetDescription()
  44. {
  45. return $this->GetValue();
  46. }
  47. }
  48. class CellChangeSpec_Unchanged extends CellChangeSpec
  49. {
  50. public function GetDescription()
  51. {
  52. return $this->GetValue()." (unchanged)";
  53. }
  54. }
  55. class CellChangeSpec_Init extends CellChangeSpec
  56. {
  57. public function GetDescription()
  58. {
  59. return $this->GetValue();
  60. }
  61. }
  62. class CellChangeSpec_Modify extends CellChangeSpec
  63. {
  64. protected $m_previousValue;
  65. public function __construct($proposedValue, $previousValue)
  66. {
  67. $this->m_previousValue = $previousValue;
  68. parent::__construct($proposedValue);
  69. }
  70. public function GetDescription()
  71. {
  72. return $this->GetValue()." (previous: ".$this->m_previousValue.")";
  73. }
  74. }
  75. class CellChangeSpec_Issue extends CellChangeSpec_Modify
  76. {
  77. protected $m_sReason;
  78. public function __construct($proposedValue, $previousValue, $sReason)
  79. {
  80. $this->m_sReason = $sReason;
  81. parent::__construct($proposedValue, $previousValue);
  82. }
  83. public function GetDescription()
  84. {
  85. if (is_null($this->m_proposedValue))
  86. {
  87. return 'Could not be changed - reason: '.$this->m_sReason;
  88. }
  89. return 'Could not be changed to "'.$this->GetValue().'" - reason: '.$this->m_sReason.' (previous: '.$this->m_previousValue.')';
  90. }
  91. }
  92. /**
  93. * RowStatus
  94. * A series of classes, keeping the information about a given row: could it be changed or not (and why)?
  95. *
  96. * @package iTopORM
  97. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  98. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  99. * @link www.itop.com
  100. * @since 1.0
  101. * @version $itopversion$
  102. */
  103. abstract class RowStatus
  104. {
  105. public function __construct()
  106. {
  107. }
  108. abstract public function GetDescription();
  109. }
  110. class RowStatus_NoChange extends RowStatus
  111. {
  112. public function GetDescription()
  113. {
  114. return "unchanged";
  115. }
  116. }
  117. class RowStatus_NewObj extends RowStatus
  118. {
  119. protected $m_iObjKey;
  120. public function __construct($iObjKey = null)
  121. {
  122. $this->m_iObjKey = $iObjKey;
  123. }
  124. public function GetDescription()
  125. {
  126. if (is_null($this->m_iObjKey))
  127. {
  128. return "Create";
  129. }
  130. else
  131. {
  132. return 'Created ('.$this->m_iObjKey.')';
  133. }
  134. }
  135. }
  136. class RowStatus_Modify extends RowStatus
  137. {
  138. protected $m_iChanged;
  139. public function __construct($iChanged)
  140. {
  141. $this->m_iChanged = $iChanged;
  142. }
  143. public function GetDescription()
  144. {
  145. return "update ".$this->m_iChanged." cols";
  146. }
  147. }
  148. class RowStatus_Issue extends RowStatus
  149. {
  150. protected $m_sReason;
  151. public function __construct($sReason)
  152. {
  153. $this->m_sReason = $sReason;
  154. }
  155. public function GetDescription()
  156. {
  157. return 'Skipped - reason:'.$this->m_sReason;
  158. }
  159. }
  160. /**
  161. * BulkChange
  162. *
  163. * @package iTopORM
  164. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  165. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  166. * @link www.itop.com
  167. * @since 1.0
  168. * @version $itopversion$
  169. */
  170. class BulkChange
  171. {
  172. protected $m_sClass;
  173. protected $m_aData;
  174. // Note: hereafter, iCol maybe actually be any acceptable key (string)
  175. // #@# todo: rename the variables to sColIndex
  176. protected $m_aAttList; // attcode => iCol
  177. protected $m_aReconcilKeys;// iCol => attcode
  178. protected $m_aExtKeys; // aExtKeys[sExtKeyAttCode][sExtReconcKeyAttCode] = iCol;
  179. public function __construct($sClass, $aData, $aAttList, $aReconcilKeys, $aExtKeys)
  180. {
  181. $this->m_sClass = $sClass;
  182. $this->m_aData = $aData;
  183. $this->m_aAttList = $aAttList;
  184. $this->m_aReconcilKeys = $aReconcilKeys;
  185. $this->m_aExtKeys = $aExtKeys;
  186. }
  187. protected function PrepareObject(&$oTargetObj, $aRowData, &$aErrors)
  188. {
  189. $aResults = array();
  190. $aErrors = array();
  191. // External keys reconciliation
  192. //
  193. foreach($this->m_aExtKeys as $sAttCode => $aKeyConfig)
  194. {
  195. $oExtKey = MetaModel::GetAttributeDef(get_class($oTargetObj), $sAttCode);
  196. $oReconFilter = new CMDBSearchFilter($oExtKey->GetTargetClass());
  197. foreach ($aKeyConfig as $sForeignAttCode => $iCol)
  198. {
  199. // The foreign attribute is one of our reconciliation key
  200. $sFieldId = MakeExtFieldSelectValue($sAttCode, $sForeignAttCode);
  201. $oReconFilter->AddCondition($sForeignAttCode, $aRowData[$iCol], '=');
  202. $aResults["col$iCol"] = new CellChangeSpec_Void($aRowData[$iCol]);
  203. }
  204. $oExtObjects = new CMDBObjectSet($oReconFilter);
  205. switch($oExtObjects->Count())
  206. {
  207. case 0:
  208. $aErrors[$sAttCode] = "Object not found";
  209. $aResults[$sAttCode]= new CellChangeSpec_Issue(null, $oTargetObj->Get($sAttCode), 'Object not found');
  210. break;
  211. case 1:
  212. // Do change the external key attribute
  213. $oForeignObj = $oExtObjects->Fetch();
  214. $oTargetObj->Set($sAttCode, $oForeignObj->GetKey());
  215. // Report it
  216. if (array_key_exists($sAttCode, $oTargetObj->ListChanges()))
  217. {
  218. if ($oTargetObj->IsNew())
  219. {
  220. $aResults[$sAttCode]= new CellChangeSpec_Init($oForeignObj->GetKey(), $oTargetObj->Get($sAttCode), $oTargetObj->GetOriginal($sAttCode));
  221. }
  222. else
  223. {
  224. $aResults[$sAttCode]= new CellChangeSpec_Modify($oForeignObj->GetKey(), $oTargetObj->Get($sAttCode), $oTargetObj->GetOriginal($sAttCode));
  225. }
  226. }
  227. else
  228. {
  229. $aResults[$sAttCode]= new CellChangeSpec_Unchanged($oTargetObj->Get($sAttCode));
  230. }
  231. break;
  232. default:
  233. $aErrors[$sAttCode] = "Found ".$oExtObjects->Count()." matches";
  234. $aResults[$sAttCode]= new CellChangeSpec_Issue(null, $oTargetObj->Get($sAttCode), "Found ".$oExtObjects->Count()." matches");
  235. }
  236. }
  237. // Set the object attributes
  238. //
  239. foreach ($this->m_aAttList as $sAttCode => $iCol)
  240. {
  241. if (!$oTargetObj->CheckValue($sAttCode, $aRowData[$iCol]))
  242. {
  243. $aErrors[$sAttCode] = "Unexpected value";
  244. }
  245. else
  246. {
  247. $oTargetObj->Set($sAttCode, $aRowData[$iCol]);
  248. }
  249. }
  250. // Reporting on fields
  251. //
  252. $aChangedFields = $oTargetObj->ListChanges();
  253. foreach ($this->m_aAttList as $sAttCode => $iCol)
  254. {
  255. if (isset($aErrors[$sAttCode]))
  256. {
  257. $aResults["col$iCol"]= new CellChangeSpec_Issue($aRowData[$iCol], $oTargetObj->Get($sAttCode), $aErrors[$sAttCode]);
  258. }
  259. elseif (array_key_exists($sAttCode, $aChangedFields))
  260. {
  261. $originalValue = $oTargetObj->GetOriginal($sAttCode);
  262. if ($oTargetObj->IsNew())
  263. {
  264. $aResults["col$iCol"]= new CellChangeSpec_Init($aRowData[$iCol], $oTargetObj->Get($sAttCode), $originalValue);
  265. }
  266. else
  267. {
  268. $aResults["col$iCol"]= new CellChangeSpec_Modify($aRowData[$iCol], $oTargetObj->Get($sAttCode), $originalValue);
  269. }
  270. }
  271. else
  272. {
  273. // By default... nothing happens
  274. $aResults["col$iCol"]= new CellChangeSpec_Void($aRowData[$iCol]);
  275. }
  276. }
  277. // Checks
  278. //
  279. if (!$oTargetObj->CheckConsistency())
  280. {
  281. $aErrors["GLOBAL"] = "Attributes not consistent with each others";
  282. }
  283. return $aResults;
  284. }
  285. protected function CreateObject(&$aResult, $iRow, $aRowData, CMDBChange $oChange = null)
  286. {
  287. $oTargetObj = MetaModel::NewObject($this->m_sClass);
  288. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  289. if (count($aErrors) > 0)
  290. {
  291. $sErrors = implode(', ', $aErrors);
  292. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  293. return;
  294. }
  295. // Check that any external key will have a value proposed
  296. // Could be said once for all rows !!!
  297. foreach(MetaModel::ListAttributeDefs($this->m_sClass) as $sAttCode=>$oAtt)
  298. {
  299. if (!$oAtt->IsExternalKey()) continue;
  300. }
  301. // Optionaly record the results
  302. //
  303. if ($oChange)
  304. {
  305. $newID = $oTargetObj->DBInsertTrackedNoReload($oChange);
  306. $aResult[$iRow]["__STATUS__"] = new RowStatus_NewObj($newID);
  307. }
  308. else
  309. {
  310. $aResult[$iRow]["__STATUS__"] = new RowStatus_NewObj();
  311. }
  312. }
  313. protected function UpdateObject(&$aResult, $iRow, $oTargetObj, $aRowData, CMDBChange $oChange = null)
  314. {
  315. $aResult[$iRow] = $this->PrepareObject($oTargetObj, $aRowData, $aErrors);
  316. // Reporting
  317. //
  318. if (count($aErrors) > 0)
  319. {
  320. $sErrors = implode(', ', $aErrors);
  321. $aResult[$iRow]["__STATUS__"] = new RowStatus_Issue("Unexpected attribute value(s)");
  322. return;
  323. }
  324. $aChangedFields = $oTargetObj->ListChanges();
  325. if (count($aChangedFields) > 0)
  326. {
  327. $aResult[$iRow]["__STATUS__"] = new RowStatus_Modify(count($aChangedFields));
  328. // Optionaly record the results
  329. //
  330. if ($oChange)
  331. {
  332. $oTargetObj->DBUpdateTracked($oChange);
  333. }
  334. }
  335. else
  336. {
  337. $aResult[$iRow]["__STATUS__"] = new RowStatus_NoChange();
  338. }
  339. }
  340. public function Process(CMDBChange $oChange = null)
  341. {
  342. // Note: $oChange can be null, in which case the aim is to check what would be done
  343. // Compute the results
  344. //
  345. $aResult = array();
  346. foreach($this->m_aData as $iRow => $aRowData)
  347. {
  348. $oReconciliationFilter = new CMDBSearchFilter($this->m_sClass);
  349. foreach($this->m_aReconcilKeys as $sAttCode)
  350. {
  351. $iCol = $this->m_aAttList[$sAttCode];
  352. $oReconciliationFilter->AddCondition($sAttCode, $aRowData[$iCol], '=');
  353. }
  354. $oReconciliationSet = new CMDBObjectSet($oReconciliationFilter);
  355. switch($oReconciliationSet->Count())
  356. {
  357. case 0:
  358. $this->CreateObject($aResult, $iRow, $aRowData, $oChange);
  359. // $aResult[$iRow]["__STATUS__"]=> set in CreateObject
  360. $aResult[$iRow]["__RECONCILIATION__"] = "Object not found";
  361. break;
  362. case 1:
  363. $oTargetObj = $oReconciliationSet->Fetch();
  364. $this->UpdateObject($aResult, $iRow, $oTargetObj, $aRowData, $oChange);
  365. $aResult[$iRow]["__RECONCILIATION__"] = "Found a match ".$oTargetObj->GetKey();
  366. // $aResult[$iRow]["__STATUS__"]=> set in UpdateObject
  367. break;
  368. default:
  369. foreach ($this->m_aAttList as $sAttCode => $iCol)
  370. {
  371. $aResult[$iRow]["col$iCol"]= $aRowData[$iCol];
  372. }
  373. $aResult[$iRow]["__RECONCILIATION__"] = "Found ".$oReconciliationSet->Count()." matches";
  374. $aResult[$iRow]["__STATUS__"]= new RowStatus_Issue("ambiguous reconciliation");
  375. }
  376. // Whatever happened, do report the reconciliation values
  377. foreach($this->m_aReconcilKeys as $sAttCode)
  378. {
  379. $iCol = $this->m_aAttList[$sAttCode];
  380. $aResult[$iRow]["col$iCol"] = new CellChangeSpec_Void($aRowData[$iCol]);
  381. }
  382. }
  383. return $aResult;
  384. }
  385. }
  386. ?>