synchrodatasource.class.inc.php 26 KB

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