synchrodatasource.class.inc.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. $sTriggerUpdate = "CREATE TRIGGER `{$sTable}_bu` BEFORE UPDATE ON $sTable";
  118. $sTriggerUpdate .= " FOR EACH ROW";
  119. $sTriggerUpdate .= " BEGIN";
  120. $sTriggerUpdate .= " IF @itopuser is null THEN";
  121. $sTriggerUpdate .= " UPDATE priv_sync_replica SET status_last_seen = NOW(), `status` = IF(($sIsModified) AND (`status` IN ('synchronized')), 'modified', `status`) WHERE sync_source_id = {$this->GetKey()} AND id = OLD.id;";
  122. $sTriggerUpdate .= " SET NEW.id = OLD.id;"; // make sure this id won't change
  123. $sTriggerUpdate .= " END IF;";
  124. $sTriggerUpdate .= " END;";
  125. CMDBSource::Query($sTriggerUpdate);
  126. }
  127. public function Synchronize(&$aDataToReplica)
  128. {
  129. // Get all the replicas that were not seen in the last import
  130. // TO DO: mark them as obsolete... depending on the delete policy
  131. // Get all the replicas that are 'new' or modified
  132. // Get the list of SQL columns: TO DO: retrieve this list from the SynchroAttributes
  133. $sClass = $this->GetTargetClass();
  134. $aAttCodes = array();
  135. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  136. {
  137. if ($sAttCode == 'finalclass') continue;
  138. $aAttCodes[] = $sAttCode;
  139. }
  140. $aColumns = $this->GetSQLColumns($aAttCodes);
  141. $aExtDataSpec = array(
  142. 'table' => $this->GetDataTable(),
  143. 'join_key' => 'id',
  144. 'fields' => array_keys($aColumns));
  145. $sOQL = "SELECT SynchroReplica WHERE (status = 'new' OR status = 'modified') AND sync_source_id = :source_id";
  146. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, $aExtDataSpec, 0 /* limitCount */, 0 /* limitStart */);
  147. // Get the list of reconciliation keys, make sure they are valid
  148. $aReconciliationKeys = array();
  149. foreach( explode(',', $this->Get('reconciliation_list')) as $sKey)
  150. {
  151. $sFilterCode = trim($sKey);
  152. if (MetaModel::IsValidFilterCode($this->GetTargetClass(), $sFilterCode))
  153. {
  154. $aReconciliationKeys[] = $sFilterCode;
  155. }
  156. else
  157. {
  158. throw(new Exception('Invalid reconciliation criteria: '.$sFilterCode));
  159. }
  160. }
  161. // TO DO: Get the "real" list of enabled attributes ! Not all of them !
  162. // for now get all scalar & writable attributes
  163. $aAttributes = array();
  164. foreach($aAttCodes as $sAttCode)
  165. {
  166. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  167. if ($oAttDef->IsWritable() && $oAttDef->IsScalar())
  168. {
  169. $aAttributes[] = $sAttCode;
  170. }
  171. }
  172. // Create a change used for logging all the modifications/creations happening during the synchro
  173. $oMyChange = MetaModel::NewObject("CMDBChange");
  174. $oMyChange->Set("date", time());
  175. $sUserString = CMDBChange::GetCurrentUserName();
  176. $oMyChange->Set("userinfo", $sUserString);
  177. $iChangeId = $oMyChange->DBInsert();
  178. while($oReplica = $oSet->Fetch())
  179. {
  180. $oReplica->Synchro($this, $aReconciliationKeys, $aAttributes, $oMyChange);
  181. }
  182. // Get all the replicas that are obsolete / to be deleted
  183. // TO DO: update or delete them based on the delete_policy and retention period defined in the data source
  184. return;
  185. }
  186. /**
  187. * Get the list of SQL columns corresponding to a particular list of attribute codes
  188. * Defaults to the whole list of columns for the current task
  189. */
  190. public function GetSQLColumns($aAttributeCodes = null)
  191. {
  192. $aColumns = array();
  193. $sClass = $this->GetTargetClass();
  194. if (is_null($aAttributeCodes))
  195. {
  196. $aAttributeCodes = array();
  197. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  198. {
  199. if ($sAttCode == 'finalclass') continue;
  200. $aAttributeCodes[] = $sAttCode;
  201. }
  202. }
  203. foreach($aAttributeCodes as $sAttCode)
  204. {
  205. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  206. foreach($oAttDef->GetSQLColumns() as $sField => $sDBFieldType)
  207. {
  208. $aColumns[$sField] = $sDBFieldType;
  209. }
  210. }
  211. return $aColumns;
  212. }
  213. public function IsRunning()
  214. {
  215. return false;
  216. }
  217. public function GetLatestLog()
  218. {
  219. return null;
  220. }
  221. }
  222. class SynchroAttribute extends cmdbAbstractObject
  223. {
  224. public static function Init()
  225. {
  226. $aParams = array
  227. (
  228. "category" => "core/cmdb,view_in_gui",
  229. "key_type" => "autoincrement",
  230. "name_attcode" => "",
  231. "state_attcode" => "",
  232. "reconc_keys" => array(),
  233. "db_table" => "priv_sync_att",
  234. "db_key_field" => "id",
  235. "db_finalclass_field" => "",
  236. "display_template" => "",
  237. );
  238. MetaModel::Init_Params($aParams);
  239. MetaModel::Init_InheritAttributes();
  240. 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())));
  241. MetaModel::Init_AddAttribute(new AttributeString("attcode", array("allowed_values"=>null, "sql"=>"attcode", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  242. MetaModel::Init_AddAttribute(new AttributeBoolean("enabled", array("allowed_values"=>null, "sql"=>"enabled", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  243. 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())));
  244. // Display lists
  245. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'enabled', 'update_policy')); // Attributes to be displayed for the complete details
  246. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'enabled', 'update_policy')); // Attributes to be displayed for a list
  247. // Search criteria
  248. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  249. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  250. }
  251. }
  252. class SynchroAttExtKey extends SynchroAttribute
  253. {
  254. public static function Init()
  255. {
  256. $aParams = array
  257. (
  258. "category" => "core/cmdb,view_in_gui",
  259. "key_type" => "autoincrement",
  260. "name_attcode" => "",
  261. "state_attcode" => "",
  262. "reconc_keys" => array(),
  263. "db_table" => "priv_sync_att_extkey",
  264. "db_key_field" => "id",
  265. "db_finalclass_field" => "",
  266. "display_template" => "",
  267. );
  268. MetaModel::Init_Params($aParams);
  269. MetaModel::Init_InheritAttributes();
  270. MetaModel::Init_AddAttribute(new AttributeString("reconciliation_attcode", array("allowed_values"=>null, "sql"=>"reconciliation_attcode", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  271. // Display lists
  272. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'enabled', 'update_policy', 'reconciliation_attcode')); // Attributes to be displayed for the complete details
  273. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'enabled', 'update_policy')); // Attributes to be displayed for a list
  274. // Search criteria
  275. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  276. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  277. }
  278. }
  279. class SynchroAttLinkSet extends SynchroAttribute
  280. {
  281. public static function Init()
  282. {
  283. $aParams = array
  284. (
  285. "category" => "core/cmdb,view_in_gui",
  286. "key_type" => "autoincrement",
  287. "name_attcode" => "",
  288. "state_attcode" => "",
  289. "reconc_keys" => array(),
  290. "db_table" => "priv_sync_att_linkset",
  291. "db_key_field" => "id",
  292. "db_finalclass_field" => "",
  293. "display_template" => "",
  294. );
  295. MetaModel::Init_Params($aParams);
  296. MetaModel::Init_InheritAttributes();
  297. MetaModel::Init_AddAttribute(new AttributeString("row_separator", array("allowed_values"=>null, "sql"=>"row_separator", "default_value"=>'|', "is_null_allowed"=>true, "depends_on"=>array())));
  298. MetaModel::Init_AddAttribute(new AttributeString("attribute_separator", array("allowed_values"=>null, "sql"=>"attribute_separator", "default_value"=>';', "is_null_allowed"=>true, "depends_on"=>array())));
  299. // Display lists
  300. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'enabled', 'update_policy', 'row_separator', 'attribute_separator')); // Attributes to be displayed for the complete details
  301. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'enabled', 'update_policy')); // Attributes to be displayed for a list
  302. // Search criteria
  303. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  304. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  305. }
  306. }
  307. class SynchroLog extends cmdbAbstractObject
  308. {
  309. public static function Init()
  310. {
  311. $aParams = array
  312. (
  313. "category" => "core/cmdb,view_in_gui",
  314. "key_type" => "autoincrement",
  315. "name_attcode" => "",
  316. "state_attcode" => "",
  317. "reconc_keys" => array(),
  318. "db_table" => "priv_sync_log",
  319. "db_key_field" => "id",
  320. "db_finalclass_field" => "",
  321. "display_template" => "",
  322. );
  323. MetaModel::Init_Params($aParams);
  324. MetaModel::Init_InheritAttributes();
  325. 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())));
  326. MetaModel::Init_AddAttribute(new AttributeDateTime("start_date", array("allowed_values"=>null, "sql"=>"start_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  327. MetaModel::Init_AddAttribute(new AttributeDateTime("end_date", array("allowed_values"=>null, "sql"=>"end_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  328. MetaModel::Init_AddAttribute(new AttributeString("status", array("allowed_values"=>null, "sql"=>"status", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  329. 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())));
  330. 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())));
  331. 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())));
  332. 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())));
  333. 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())));
  334. 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())));
  335. // Display lists
  336. 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
  337. 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
  338. // Search criteria
  339. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  340. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  341. }
  342. }
  343. class SynchroReplica extends cmdbAbstractObject
  344. {
  345. static $aSearches = array(); // Cache of OQL queries used for reconciliation (per data source)
  346. public static function Init()
  347. {
  348. $aParams = array
  349. (
  350. "category" => "core/cmdb,view_in_gui",
  351. "key_type" => "autoincrement",
  352. "name_attcode" => "",
  353. "state_attcode" => "",
  354. "reconc_keys" => array(),
  355. "db_table" => "priv_sync_replica",
  356. "db_key_field" => "id",
  357. "db_finalclass_field" => "",
  358. "display_template" => "",
  359. );
  360. MetaModel::Init_Params($aParams);
  361. MetaModel::Init_InheritAttributes();
  362. 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())));
  363. MetaModel::Init_AddAttribute(new AttributeInteger("dest_id", array("allowed_values"=>null, "sql"=>"dest_id", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  364. 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())));
  365. MetaModel::Init_AddAttribute(new AttributeEnum("status", array("allowed_values"=>new ValueSetEnum('new,synchronized,modified,orphan,deleted,obsolete'), "sql"=>"status", "default_value"=>"new", "is_null_allowed"=>false, "depends_on"=>array())));
  366. 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())));
  367. 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())));
  368. 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())));
  369. 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())));
  370. 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())));
  371. // Display lists
  372. 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
  373. 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
  374. // Search criteria
  375. 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
  376. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  377. }
  378. public function Synchro($oDataSource, $aReconciliationKeys, $aAttributes, $oChange)
  379. {
  380. switch($this->Get('status'))
  381. {
  382. case 'new':
  383. // If needed, construct the query used for the reconciliation
  384. if (!isset(self::$aSearches[$oDataSource->GetKey()]))
  385. {
  386. foreach($aReconciliationKeys as $sFilterCode)
  387. {
  388. $aCriterias[] = ($sFilterCode == 'primary_key' ? 'id' : $sFilterCode).' = :'.$sFilterCode;
  389. }
  390. $sOQL = "SELECT ".$oDataSource->GetTargetClass()." WHERE ".implode(' AND ', $aCriterias);
  391. self::$aSearches[$oDataSource->GetKey()] = DBObjectSearch::FromOQL($sOQL);
  392. }
  393. // Get the criterias for the search
  394. $aFilterValues = array();
  395. foreach($aReconciliationKeys as $sFilterCode)
  396. {
  397. $aFilterValues[$sFilterCode] = $this->GetValueFromExtData($sFilterCode);
  398. }
  399. $oDestSet = new DBObjectSet(self::$aSearches[$oDataSource->GetKey()], array(), $aFilterValues);
  400. $iCount = $oDestSet->Count();
  401. // How many objects match the reconciliation criterias
  402. switch($iCount)
  403. {
  404. case 0:
  405. $this->CreateObjectFromReplica($oDataSource->GetTargetClass(), $aAttributes, $oChange);
  406. break;
  407. case 1:
  408. $oDestObj = $oDestSet->Fetch();
  409. $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange);
  410. break;
  411. default:
  412. $aConditions = array();
  413. foreach($aFilterValues as $sCode => $sValue)
  414. {
  415. $aConditions[] = $sCode.'='.$sValue;
  416. }
  417. $sCondition = implode(' AND ', $aConditions);
  418. $this->Set('status_last_error', $iCount.' destination objects match the reconciliation criterias: '.$sCondition);
  419. }
  420. break;
  421. case 'modified':
  422. $oDestObj = MetaModel::GetObject($oDataSource->GetTargetClass(), $this->Get('dest_id'));
  423. if ($oDestObj == null)
  424. {
  425. $this->Set('status', 'orphan'); // The destination object has been deleted !
  426. $this->Set('status_last_error', 'Destination object deleted unexpectedly');
  427. }
  428. else
  429. {
  430. $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange);
  431. }
  432. break;
  433. default: // Do nothing in all other cases
  434. }
  435. $this->DBUpdateTracked($oChange);
  436. }
  437. /**
  438. * Updates the destination object with the Extended data found in the synchro_data_XXXX table
  439. */
  440. protected function UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange)
  441. {
  442. echo "<p>Update object ".$oDestObj->GetName()."</p>";
  443. foreach($aAttributes as $sAttCode)
  444. {
  445. $value = $this->GetValueFromExtData($sAttCode);
  446. $oDestObj->Set($sAttCode, $value);
  447. echo "<p>&nbsp;&nbsp;&nbsp;Setting $sAttCode to $value</p>";
  448. }
  449. try
  450. {
  451. $oDestObj->DBUpdateTracked($oChange);
  452. $this->Set('status_last_error', '');
  453. $this->Set('status', 'synchronized');
  454. }
  455. catch(Exception $e)
  456. {
  457. $this->Set('status_last_error', 'Unable to update destination object');
  458. }
  459. }
  460. /**
  461. * Creates the destination object populating it with the Extended data found in the synchro_data_XXXX table
  462. */
  463. protected function CreateObjectFromReplica($sClass, $aAttributes, $oChange)
  464. {
  465. echo "<p>Creating new $sClass</p>";
  466. $oDestObj = MetaModel::NewObject($sClass);
  467. foreach($aAttributes as $sAttCode)
  468. {
  469. $value = $this->GetValueFromExtData($sAttCode);
  470. $oDestObj->Set($sAttCode, $value);
  471. echo "<p>&nbsp;&nbsp;&nbsp;Setting $sAttCode to $value</p>";
  472. }
  473. try
  474. {
  475. $oDestObj->DBInsertTracked($oChange);
  476. $this->Set('dest_id', $oDestObj->GetKey());
  477. $this->Set('status_last_error', '');
  478. $this->Set('status', 'synchronized');
  479. }
  480. catch(Exception $e)
  481. {
  482. $this->Set('status_last_error', 'Unable to update destination object');
  483. }
  484. }
  485. /**
  486. * Get the value from the 'Extended Data' located in the synchro_data_xxx table for this replica
  487. */
  488. protected function GetValueFromExtData($sColumnName)
  489. {
  490. $aData = $this->GetExtendedData();
  491. return $aData[$sColumnName];
  492. }
  493. }
  494. ?>