synchrodatasource.class.inc.php 27 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. * Data Exchange - synchronization with external applications (incoming data)
  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. class SynchroDataSource extends cmdbAbstractObject
  25. {
  26. public static function Init()
  27. {
  28. $aParams = array
  29. (
  30. "category" => "core/cmdb,view_in_gui",
  31. "key_type" => "autoincrement",
  32. "name_attcode" => array('name'),
  33. "state_attcode" => "",
  34. "reconc_keys" => array(),
  35. "db_table" => "priv_sync_datasource",
  36. "db_key_field" => "id",
  37. "db_finalclass_field" => "realclass",
  38. "display_template" => "",
  39. );
  40. MetaModel::Init_Params($aParams);
  41. //MetaModel::Init_InheritAttributes();
  42. MetaModel::Init_AddAttribute(new AttributeString("name", array("allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  43. MetaModel::Init_AddAttribute(new AttributeString("description", array("allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  44. MetaModel::Init_AddAttribute(new AttributeEnum("status", array("allowed_values"=>new ValueSetEnum('implementation,production,obsolete'), "sql"=>"status", "default_value"=>"implementation", "is_null_allowed"=>false, "depends_on"=>array())));
  45. MetaModel::Init_AddAttribute(new AttributeExternalKey("user_id", array("targetclass"=>"User", "jointype"=>null, "allowed_values"=>null, "sql"=>"user_id", "is_null_allowed"=>true, "on_target_delete"=>DEL_MANUAL, "depends_on"=>array())));
  46. MetaModel::Init_AddAttribute(new AttributeString("scope", array("allowed_values"=>null, "sql"=>"scope", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  47. MetaModel::Init_AddAttribute(new AttributeDateTime("last_synchro_date", array("allowed_values"=>null, "sql"=>"last_synchro_date", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  48. MetaModel::Init_AddAttribute(new AttributeString("reconciliation_list", array("allowed_values"=>null, "sql"=>"reconciliation_list", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  49. MetaModel::Init_AddAttribute(new AttributeEnum("action_on_zero", array("allowed_values"=>new ValueSetEnum('create,error'), "sql"=>"action_on_zero", "default_value"=>"create", "is_null_allowed"=>false, "depends_on"=>array())));
  50. MetaModel::Init_AddAttribute(new AttributeEnum("action_on_one", array("allowed_values"=>new ValueSetEnum('update,error,delete'), "sql"=>"action_on_one", "default_value"=>"update", "is_null_allowed"=>false, "depends_on"=>array())));
  51. MetaModel::Init_AddAttribute(new AttributeEnum("action_on_multiple", array("allowed_values"=>new ValueSetEnum('take_first,create,error'), "sql"=>"action_on_multiple", "default_value"=>"error", "is_null_allowed"=>false, "depends_on"=>array())));
  52. MetaModel::Init_AddAttribute(new AttributeEnum("delete_policy", array("allowed_values"=>new ValueSetEnum('ignore,delete,update'), "sql"=>"delete_policy", "default_value"=>"ignore", "is_null_allowed"=>false, "depends_on"=>array())));
  53. MetaModel::Init_AddAttribute(new AttributeString("delete_policy_update", array("allowed_values"=>null, "sql"=>"delete_policy_update", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  54. MetaModel::Init_AddAttribute(new AttributeString("delete_policy_retention", array("allowed_values"=>null, "sql"=>"delete_policy_retention", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  55. // Display lists
  56. MetaModel::Init_SetZListItems('details', array('name', 'description', 'status', 'user_id', 'scope', 'last_synchro_date', 'reconciliation_list', 'action_on_zero', 'action_on_one', 'action_on_multiple', 'delete_policy', 'delete_policy_update', 'delete_policy_retention')); // Attributes to be displayed for the complete details
  57. MetaModel::Init_SetZListItems('list', array('name', 'status', 'scope', 'user_id')); // Attributes to be displayed for a list
  58. // Search criteria
  59. MetaModel::Init_SetZListItems('standard_search', array('name', 'status', 'scope', 'user_id')); // Criteria of the std search form
  60. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  61. }
  62. public function GetTargetClass()
  63. {
  64. // target_class could be the name of a class, or an OQL
  65. $sScope = trim($this->Get('scope'));
  66. if (substr($sScope, 0, 6) == 'SELECT')
  67. {
  68. $oFilter = DBObjectSearch::FromOQL($sScope);
  69. $sClass = $oFilter->GetClass();
  70. }
  71. else
  72. {
  73. $sClass = $sScope;
  74. }
  75. return $sClass;
  76. }
  77. public function GetDataTable()
  78. {
  79. $sName = trim(strtolower($this->Get('name')));
  80. $sName = str_replace('\'"&@|\\/ ', '_', $sName); // Remove forbidden characters from the table name
  81. $sTable = MetaModel::GetConfig()->GetDBSubName()."synchro_data_$sName"; // Add the prefix if any
  82. return $sTable;
  83. }
  84. protected function AfterInsert()
  85. {
  86. parent::AfterInsert();
  87. $sTable = $this->GetDataTable();
  88. $aColumns = $this->GetSQLColumns();
  89. $aFieldDefs = array();
  90. // Allow '0', otherwise mysql will render an error when the id is not given
  91. // (the trigger is expected to set the value, but it is not executed soon enough)
  92. $aFieldDefs[] = "id INTEGER(11) NOT NULL DEFAULT 0 ";
  93. $aFieldDefs[] = "`primary_key` VARCHAR(255) NULL DEFAULT NULL";
  94. foreach($aColumns as $sColumn => $ColSpec)
  95. {
  96. $aFieldDefs[] = "`$sColumn` $ColSpec NULL DEFAULT NULL";
  97. }
  98. $aFieldDefs[] = "INDEX (id)";
  99. $aFieldDefs[] = "INDEX (primary_key)";
  100. $sFieldDefs = implode(', ', $aFieldDefs);
  101. $sCreateTable = "CREATE TABLE `$sTable` ($sFieldDefs) ENGINE = innodb;";
  102. CMDBSource::Query($sCreateTable);
  103. $sTriggerInsert = "CREATE TRIGGER `{$sTable}_bi` BEFORE INSERT ON $sTable";
  104. $sTriggerInsert .= " FOR EACH ROW";
  105. $sTriggerInsert .= " BEGIN";
  106. $sTriggerInsert .= " INSERT INTO priv_sync_replica (sync_source_id, status_last_seen, `status`) VALUES ({$this->GetKey()}, NOW(), 'new');";
  107. $sTriggerInsert .= " SET NEW.id = LAST_INSERT_ID();";
  108. $sTriggerInsert .= " END;";
  109. CMDBSource::Query($sTriggerInsert);
  110. $aModified = array();
  111. foreach($aColumns as $sColumn => $ColSpec)
  112. {
  113. // <=> is a null-safe 'EQUALS' operator (there is no equivalent for "DIFFERS FROM")
  114. $aModified[] = "NOT(NEW.`$sColumn` <=> OLD.`$sColumn`)";
  115. }
  116. $sIsModified = '('.implode(') OR (', $aModified).')';
  117. // Update the replica
  118. //
  119. // status is forced to "new" if the replica was obsoleted directly from the state "new" (dest_id = null)
  120. // otherwise, if status was either 'obsolete' or 'synchronized' it is turned into 'modified' or 'synchronized' depending on the changes
  121. // otherwise, the status is left as is
  122. $sTriggerUpdate = "CREATE TRIGGER `{$sTable}_bu` BEFORE UPDATE ON $sTable";
  123. $sTriggerUpdate .= " FOR EACH ROW";
  124. $sTriggerUpdate .= " BEGIN";
  125. $sTriggerUpdate .= " IF @itopuser is null THEN";
  126. $sTriggerUpdate .= " UPDATE priv_sync_replica SET status_last_seen = NOW(), `status` = IF(`status` = 'obsolete', IF(`dest_id` IS NULL, 'new', 'modified'), IF(`status` IN ('synchronized') AND ($sIsModified), 'modified', `status`)) WHERE sync_source_id = {$this->GetKey()} AND id = OLD.id;";
  127. $sTriggerUpdate .= " SET NEW.id = OLD.id;"; // make sure this id won't change
  128. $sTriggerUpdate .= " END IF;";
  129. $sTriggerUpdate .= " END;";
  130. CMDBSource::Query($sTriggerUpdate);
  131. }
  132. public function Synchronize(&$aDataToReplica)
  133. {
  134. // Get all the replicas that were not seen in the last import
  135. // TO DO: mark them as obsolete... depending on the delete policy
  136. // Get all the replicas that are 'new' or modified
  137. // Get the list of SQL columns: TO DO: retrieve this list from the SynchroAttributes
  138. $sClass = $this->GetTargetClass();
  139. $aAttCodes = array();
  140. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  141. {
  142. if ($sAttCode == 'finalclass') continue;
  143. $aAttCodes[] = $sAttCode;
  144. }
  145. $aColumns = $this->GetSQLColumns($aAttCodes);
  146. $aExtDataSpec = array(
  147. 'table' => $this->GetDataTable(),
  148. 'join_key' => 'id',
  149. 'fields' => array_keys($aColumns));
  150. $sOQL = "SELECT SynchroReplica WHERE (status = 'new' OR status = 'modified') AND sync_source_id = :source_id";
  151. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, $aExtDataSpec, 0 /* limitCount */, 0 /* limitStart */);
  152. // Get the list of reconciliation keys, make sure they are valid
  153. $aReconciliationKeys = array();
  154. foreach( explode(',', $this->Get('reconciliation_list')) as $sKey)
  155. {
  156. $sFilterCode = trim($sKey);
  157. if (MetaModel::IsValidFilterCode($this->GetTargetClass(), $sFilterCode))
  158. {
  159. $aReconciliationKeys[] = $sFilterCode;
  160. }
  161. else
  162. {
  163. throw(new Exception('Invalid reconciliation criteria: '.$sFilterCode));
  164. }
  165. }
  166. // TO DO: Get the "real" list of enabled attributes ! Not all of them !
  167. // for now get all scalar & writable attributes
  168. $aAttributes = array();
  169. foreach($aAttCodes as $sAttCode)
  170. {
  171. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  172. if ($oAttDef->IsWritable() && $oAttDef->IsScalar())
  173. {
  174. $aAttributes[] = $sAttCode;
  175. }
  176. }
  177. // Create a change used for logging all the modifications/creations happening during the synchro
  178. $oMyChange = MetaModel::NewObject("CMDBChange");
  179. $oMyChange->Set("date", time());
  180. $sUserString = CMDBChange::GetCurrentUserName();
  181. $oMyChange->Set("userinfo", $sUserString);
  182. $iChangeId = $oMyChange->DBInsert();
  183. while($oReplica = $oSet->Fetch())
  184. {
  185. $oReplica->Synchro($this, $aReconciliationKeys, $aAttributes, $oMyChange);
  186. }
  187. // Get all the replicas that are obsolete / to be deleted
  188. // TO DO: update or delete them based on the delete_policy and retention period defined in the data source
  189. return;
  190. }
  191. /**
  192. * Get the list of SQL columns corresponding to a particular list of attribute codes
  193. * Defaults to the whole list of columns for the current task
  194. */
  195. public function GetSQLColumns($aAttributeCodes = null)
  196. {
  197. $aColumns = array();
  198. $sClass = $this->GetTargetClass();
  199. if (is_null($aAttributeCodes))
  200. {
  201. $aAttributeCodes = array();
  202. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  203. {
  204. if ($sAttCode == 'finalclass') continue;
  205. $aAttributeCodes[] = $sAttCode;
  206. }
  207. }
  208. foreach($aAttributeCodes as $sAttCode)
  209. {
  210. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  211. foreach($oAttDef->GetSQLColumns() as $sField => $sDBFieldType)
  212. {
  213. $aColumns[$sField] = $sDBFieldType;
  214. }
  215. }
  216. return $aColumns;
  217. }
  218. public function IsRunning()
  219. {
  220. return false;
  221. }
  222. public function GetLatestLog()
  223. {
  224. return null;
  225. }
  226. }
  227. class SynchroAttribute extends cmdbAbstractObject
  228. {
  229. public static function Init()
  230. {
  231. $aParams = array
  232. (
  233. "category" => "core/cmdb,view_in_gui",
  234. "key_type" => "autoincrement",
  235. "name_attcode" => "",
  236. "state_attcode" => "",
  237. "reconc_keys" => array(),
  238. "db_table" => "priv_sync_att",
  239. "db_key_field" => "id",
  240. "db_finalclass_field" => "",
  241. "display_template" => "",
  242. );
  243. MetaModel::Init_Params($aParams);
  244. MetaModel::Init_InheritAttributes();
  245. MetaModel::Init_AddAttribute(new AttributeExternalKey("sync_source_id", array("targetclass"=>"SynchroDataSource", "jointype"=> "", "allowed_values"=>null, "sql"=>"sync_source_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
  246. MetaModel::Init_AddAttribute(new AttributeString("attcode", array("allowed_values"=>null, "sql"=>"attcode", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  247. MetaModel::Init_AddAttribute(new AttributeBoolean("enabled", array("allowed_values"=>null, "sql"=>"enabled", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  248. MetaModel::Init_AddAttribute(new AttributeEnum("update_policy", array("allowed_values"=>new ValueSetEnum('master_locked,master_unlocked,write_once'), "sql"=>"update_policy", "default_value"=>"master_locked", "is_null_allowed"=>false, "depends_on"=>array())));
  249. // Display lists
  250. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'enabled', 'update_policy')); // Attributes to be displayed for the complete details
  251. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'enabled', 'update_policy')); // Attributes to be displayed for a list
  252. // Search criteria
  253. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  254. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  255. }
  256. }
  257. class SynchroAttExtKey extends SynchroAttribute
  258. {
  259. public static function Init()
  260. {
  261. $aParams = array
  262. (
  263. "category" => "core/cmdb,view_in_gui",
  264. "key_type" => "autoincrement",
  265. "name_attcode" => "",
  266. "state_attcode" => "",
  267. "reconc_keys" => array(),
  268. "db_table" => "priv_sync_att_extkey",
  269. "db_key_field" => "id",
  270. "db_finalclass_field" => "",
  271. "display_template" => "",
  272. );
  273. MetaModel::Init_Params($aParams);
  274. MetaModel::Init_InheritAttributes();
  275. MetaModel::Init_AddAttribute(new AttributeString("reconciliation_attcode", array("allowed_values"=>null, "sql"=>"reconciliation_attcode", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  276. // Display lists
  277. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'enabled', 'update_policy', 'reconciliation_attcode')); // Attributes to be displayed for the complete details
  278. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'enabled', 'update_policy')); // Attributes to be displayed for a list
  279. // Search criteria
  280. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  281. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  282. }
  283. }
  284. class SynchroAttLinkSet extends SynchroAttribute
  285. {
  286. public static function Init()
  287. {
  288. $aParams = array
  289. (
  290. "category" => "core/cmdb,view_in_gui",
  291. "key_type" => "autoincrement",
  292. "name_attcode" => "",
  293. "state_attcode" => "",
  294. "reconc_keys" => array(),
  295. "db_table" => "priv_sync_att_linkset",
  296. "db_key_field" => "id",
  297. "db_finalclass_field" => "",
  298. "display_template" => "",
  299. );
  300. MetaModel::Init_Params($aParams);
  301. MetaModel::Init_InheritAttributes();
  302. MetaModel::Init_AddAttribute(new AttributeString("row_separator", array("allowed_values"=>null, "sql"=>"row_separator", "default_value"=>'|', "is_null_allowed"=>true, "depends_on"=>array())));
  303. MetaModel::Init_AddAttribute(new AttributeString("attribute_separator", array("allowed_values"=>null, "sql"=>"attribute_separator", "default_value"=>';', "is_null_allowed"=>true, "depends_on"=>array())));
  304. // Display lists
  305. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'enabled', 'update_policy', 'row_separator', 'attribute_separator')); // Attributes to be displayed for the complete details
  306. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'enabled', 'update_policy')); // Attributes to be displayed for a list
  307. // Search criteria
  308. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  309. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  310. }
  311. }
  312. class SynchroLog extends cmdbAbstractObject
  313. {
  314. public static function Init()
  315. {
  316. $aParams = array
  317. (
  318. "category" => "core/cmdb,view_in_gui",
  319. "key_type" => "autoincrement",
  320. "name_attcode" => "",
  321. "state_attcode" => "",
  322. "reconc_keys" => array(),
  323. "db_table" => "priv_sync_log",
  324. "db_key_field" => "id",
  325. "db_finalclass_field" => "",
  326. "display_template" => "",
  327. );
  328. MetaModel::Init_Params($aParams);
  329. MetaModel::Init_InheritAttributes();
  330. MetaModel::Init_AddAttribute(new AttributeExternalKey("sync_source_id", array("targetclass"=>"SynchroDataSource", "jointype"=> "", "allowed_values"=>null, "sql"=>"sync_source_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
  331. MetaModel::Init_AddAttribute(new AttributeDateTime("start_date", array("allowed_values"=>null, "sql"=>"start_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  332. MetaModel::Init_AddAttribute(new AttributeDateTime("end_date", array("allowed_values"=>null, "sql"=>"end_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  333. MetaModel::Init_AddAttribute(new AttributeString("status", array("allowed_values"=>null, "sql"=>"status", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  334. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_seen", array("allowed_values"=>null, "sql"=>"stats_nb_seen", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  335. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_modified", array("allowed_values"=>null, "sql"=>"stats_nb_modified", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  336. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_errors", array("allowed_values"=>null, "sql"=>"stats_nb_errors", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  337. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_created", array("allowed_values"=>null, "sql"=>"stats_nb_created", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  338. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_deleted", array("allowed_values"=>null, "sql"=>"stats_nb_deleted", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  339. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_reconciled", array("allowed_values"=>null, "sql"=>"stats_nb_reconciled", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  340. // Display lists
  341. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'start_date', 'end_date', 'status', 'stats_nb_seen', 'stats_nb_modified', 'stats_nb_errors', 'stats_nb_created', 'stats_nb_deleted', 'stats_nb_reconciled')); // Attributes to be displayed for the complete details
  342. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'start_date', 'end_date', 'status', 'stats_nb_seen', 'stats_nb_modified', 'stats_nb_errors')); // Attributes to be displayed for a list
  343. // Search criteria
  344. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  345. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  346. }
  347. }
  348. class SynchroReplica extends cmdbAbstractObject
  349. {
  350. static $aSearches = array(); // Cache of OQL queries used for reconciliation (per data source)
  351. public static function Init()
  352. {
  353. $aParams = array
  354. (
  355. "category" => "core/cmdb,view_in_gui",
  356. "key_type" => "autoincrement",
  357. "name_attcode" => "",
  358. "state_attcode" => "",
  359. "reconc_keys" => array(),
  360. "db_table" => "priv_sync_replica",
  361. "db_key_field" => "id",
  362. "db_finalclass_field" => "",
  363. "display_template" => "",
  364. );
  365. MetaModel::Init_Params($aParams);
  366. MetaModel::Init_InheritAttributes();
  367. MetaModel::Init_AddAttribute(new AttributeExternalKey("sync_source_id", array("targetclass"=>"SynchroDataSource", "jointype"=> "", "allowed_values"=>null, "sql"=>"sync_source_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
  368. MetaModel::Init_AddAttribute(new AttributeInteger("dest_id", array("allowed_values"=>null, "sql"=>"dest_id", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  369. MetaModel::Init_AddAttribute(new AttributeDateTime("status_last_seen", array("allowed_values"=>null, "sql"=>"status_last_seen", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  370. MetaModel::Init_AddAttribute(new AttributeEnum("status", array("allowed_values"=>new ValueSetEnum('new,synchronized,modified,orphan,obsolete'), "sql"=>"status", "default_value"=>"new", "is_null_allowed"=>false, "depends_on"=>array())));
  371. MetaModel::Init_AddAttribute(new AttributeBoolean("status_dest_creator", array("allowed_values"=>null, "sql"=>"status_dest_creator", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  372. MetaModel::Init_AddAttribute(new AttributeString("status_last_error", array("allowed_values"=>null, "sql"=>"status_last_error", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  373. MetaModel::Init_AddAttribute(new AttributeDateTime("info_creation_date", array("allowed_values"=>null, "sql"=>"info_creation_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  374. MetaModel::Init_AddAttribute(new AttributeDateTime("info_last_modified", array("allowed_values"=>null, "sql"=>"info_last_modified", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  375. MetaModel::Init_AddAttribute(new AttributeDateTime("info_last_synchro", array("allowed_values"=>null, "sql"=>"info_last_synchro", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  376. // Display lists
  377. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'dest_id', 'status_last_seen', 'status', 'status_dest_creator', 'status_last_error', 'info_creation_date', 'info_last_modified', 'info_last_synchro')); // Attributes to be displayed for the complete details
  378. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'dest_id', 'status_last_seen', 'status', 'status_dest_creator', 'status_last_error')); // Attributes to be displayed for a list
  379. // Search criteria
  380. MetaModel::Init_SetZListItems('standard_search', array('sync_source_id', 'status_last_seen', 'status', 'status_dest_creator', 'dest_id', 'status_last_error')); // Criteria of the std search form
  381. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  382. }
  383. public function Synchro($oDataSource, $aReconciliationKeys, $aAttributes, $oChange)
  384. {
  385. switch($this->Get('status'))
  386. {
  387. case 'new':
  388. // If needed, construct the query used for the reconciliation
  389. if (!isset(self::$aSearches[$oDataSource->GetKey()]))
  390. {
  391. foreach($aReconciliationKeys as $sFilterCode)
  392. {
  393. $aCriterias[] = ($sFilterCode == 'primary_key' ? 'id' : $sFilterCode).' = :'.$sFilterCode;
  394. }
  395. $sOQL = "SELECT ".$oDataSource->GetTargetClass()." WHERE ".implode(' AND ', $aCriterias);
  396. self::$aSearches[$oDataSource->GetKey()] = DBObjectSearch::FromOQL($sOQL);
  397. }
  398. // Get the criterias for the search
  399. $aFilterValues = array();
  400. foreach($aReconciliationKeys as $sFilterCode)
  401. {
  402. $aFilterValues[$sFilterCode] = $this->GetValueFromExtData($sFilterCode);
  403. }
  404. $oDestSet = new DBObjectSet(self::$aSearches[$oDataSource->GetKey()], array(), $aFilterValues);
  405. $iCount = $oDestSet->Count();
  406. // How many objects match the reconciliation criterias
  407. switch($iCount)
  408. {
  409. case 0:
  410. $this->CreateObjectFromReplica($oDataSource->GetTargetClass(), $aAttributes, $oChange);
  411. break;
  412. case 1:
  413. $oDestObj = $oDestSet->Fetch();
  414. $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange);
  415. break;
  416. default:
  417. $aConditions = array();
  418. foreach($aFilterValues as $sCode => $sValue)
  419. {
  420. $aConditions[] = $sCode.'='.$sValue;
  421. }
  422. $sCondition = implode(' AND ', $aConditions);
  423. $this->Set('status_last_error', $iCount.' destination objects match the reconciliation criterias: '.$sCondition);
  424. }
  425. break;
  426. case 'modified':
  427. $oDestObj = MetaModel::GetObject($oDataSource->GetTargetClass(), $this->Get('dest_id'));
  428. if ($oDestObj == null)
  429. {
  430. $this->Set('status', 'orphan'); // The destination object has been deleted !
  431. $this->Set('status_last_error', 'Destination object deleted unexpectedly');
  432. }
  433. else
  434. {
  435. $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange);
  436. }
  437. break;
  438. default: // Do nothing in all other cases
  439. }
  440. $this->DBUpdateTracked($oChange);
  441. }
  442. /**
  443. * Updates the destination object with the Extended data found in the synchro_data_XXXX table
  444. */
  445. protected function UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange)
  446. {
  447. echo "<p>Update object ".$oDestObj->GetName()."</p>";
  448. foreach($aAttributes as $sAttCode)
  449. {
  450. $value = $this->GetValueFromExtData($sAttCode);
  451. $oDestObj->Set($sAttCode, $value);
  452. echo "<p>&nbsp;&nbsp;&nbsp;Setting $sAttCode to $value</p>";
  453. }
  454. try
  455. {
  456. $oDestObj->DBUpdateTracked($oChange);
  457. $this->Set('status_last_error', '');
  458. $this->Set('status', 'synchronized');
  459. }
  460. catch(Exception $e)
  461. {
  462. $this->Set('status_last_error', 'Unable to update destination object');
  463. }
  464. }
  465. /**
  466. * Creates the destination object populating it with the Extended data found in the synchro_data_XXXX table
  467. */
  468. protected function CreateObjectFromReplica($sClass, $aAttributes, $oChange)
  469. {
  470. echo "<p>Creating new $sClass</p>";
  471. $oDestObj = MetaModel::NewObject($sClass);
  472. foreach($aAttributes as $sAttCode)
  473. {
  474. $value = $this->GetValueFromExtData($sAttCode);
  475. $oDestObj->Set($sAttCode, $value);
  476. echo "<p>&nbsp;&nbsp;&nbsp;Setting $sAttCode to $value</p>";
  477. }
  478. try
  479. {
  480. $oDestObj->DBInsertTracked($oChange);
  481. $this->Set('dest_id', $oDestObj->GetKey());
  482. $this->Set('status_last_error', '');
  483. $this->Set('status', 'synchronized');
  484. }
  485. catch(Exception $e)
  486. {
  487. $this->Set('status_last_error', 'Unable to update destination object');
  488. }
  489. }
  490. /**
  491. * Get the value from the 'Extended Data' located in the synchro_data_xxx table for this replica
  492. */
  493. protected function GetValueFromExtData($sColumnName)
  494. {
  495. $aData = $this->GetExtendedData();
  496. return $aData[$sColumnName];
  497. }
  498. }
  499. ?>