synchrodatasource.class.inc.php 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  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 SynchroExceptionNotStarted extends CoreException
  25. {
  26. }
  27. class SynchroDataSource extends cmdbAbstractObject
  28. {
  29. public static function Init()
  30. {
  31. $aParams = array
  32. (
  33. "category" => "core/cmdb,view_in_gui",
  34. "key_type" => "autoincrement",
  35. "name_attcode" => array('name'),
  36. "state_attcode" => "",
  37. "reconc_keys" => array(),
  38. "db_table" => "priv_sync_datasource",
  39. "db_key_field" => "id",
  40. "db_finalclass_field" => "realclass",
  41. "display_template" => "",
  42. "icon" => "../images/synchro.png",
  43. );
  44. MetaModel::Init_Params($aParams);
  45. //MetaModel::Init_InheritAttributes();
  46. MetaModel::Init_AddAttribute(new AttributeString("name", array("allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  47. MetaModel::Init_AddAttribute(new AttributeString("description", array("allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  48. 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())));
  49. 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())));
  50. MetaModel::Init_AddAttribute(new AttributeClass("scope_class", array("class_category"=>"bizmodel", "more_values"=>"", "sql"=>"scope_class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  51. // Declared here for a future usage, but ignored so far
  52. MetaModel::Init_AddAttribute(new AttributeString("scope_restriction", array("allowed_values"=>null, "sql"=>"scope_restriction", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  53. //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())));
  54. // Format: seconds (int)
  55. MetaModel::Init_AddAttribute(new AttributeDuration("full_load_periodicity", array("allowed_values"=>null, "sql"=>"full_load_periodicity", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  56. // MetaModel::Init_AddAttribute(new AttributeString("reconciliation_list", array("allowed_values"=>null, "sql"=>"reconciliation_list", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  57. MetaModel::Init_AddAttribute(new AttributeEnum("reconciliation_policy", array("allowed_values"=>new ValueSetEnum('use_primary_key,use_attributes'), "sql"=>"reconciliation_policy", "default_value"=>"use_attributes", "is_null_allowed"=>false, "depends_on"=>array())));
  58. 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())));
  59. MetaModel::Init_AddAttribute(new AttributeEnum("action_on_one", array("allowed_values"=>new ValueSetEnum('update,error'), "sql"=>"action_on_one", "default_value"=>"update", "is_null_allowed"=>false, "depends_on"=>array())));
  60. 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())));
  61. MetaModel::Init_AddAttribute(new AttributeEnum("delete_policy", array("allowed_values"=>new ValueSetEnum('ignore,delete,update,update_then_delete'), "sql"=>"delete_policy", "default_value"=>"ignore", "is_null_allowed"=>false, "depends_on"=>array())));
  62. 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())));
  63. // Format: seconds (unsigned int)
  64. MetaModel::Init_AddAttribute(new AttributeDuration("delete_policy_retention", array("allowed_values"=>null, "sql"=>"delete_policy_retention", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  65. MetaModel::Init_AddAttribute(new AttributeLinkedSet("attribute_list", array("linked_class"=>"SynchroAttribute", "ext_key_to_me"=>"sync_source_id", "allowed_values"=>null, "count_min"=>0, "count_max"=>0, "depends_on"=>array())));
  66. // Display lists
  67. MetaModel::Init_SetZListItems('details', array('name', 'description', 'scope_class', /*'scope_restriction', */'status', 'user_id', 'full_load_periodicity', 'reconciliation_policy', 'action_on_zero', 'action_on_one', 'action_on_multiple', 'delete_policy', 'delete_policy_update', 'delete_policy_retention' /*'attribute_list'*/)); // Attributes to be displayed for the complete details
  68. MetaModel::Init_SetZListItems('list', array('scope_class', 'status', 'user_id', 'full_load_periodicity')); // Attributes to be displayed for a list
  69. // Search criteria
  70. MetaModel::Init_SetZListItems('standard_search', array('name', 'status', 'scope_class', 'user_id')); // Criteria of the std search form
  71. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  72. }
  73. public function DisplayBareRelations(WebPage $oPage, $bEditMode = false)
  74. {
  75. if (!$this->IsNew())
  76. {
  77. $oPage->SetCurrentTab(Dict::S('Core:SynchroAttributes'));
  78. $oAttributeSet = $this->Get('attribute_list');
  79. $aAttributes = array();
  80. while($oAttribute = $oAttributeSet->Fetch())
  81. {
  82. $aAttributes[$oAttribute->Get('attcode')] = $oAttribute;
  83. }
  84. $aAttribs = array(
  85. 'attcode' => array('label'=>'Attribute', 'description' => 'Field of the object'),
  86. 'reconciliation' => array('label'=>'Reconciliation ?', 'description' => 'Used for searching'),
  87. 'update' => array('label'=>'Update ?', 'description' => 'Used to update the object'),
  88. 'update_policy' => array('label'=>'Update Policy', 'description' => 'Behavior of the updated field'),
  89. );
  90. $aValues = array();
  91. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  92. {
  93. if ($oAttDef->IsScalar() && $oAttDef->IsWritable())
  94. {
  95. if (isset($aAttributes[$sAttCode]))
  96. {
  97. $oAttribute = $aAttributes[$sAttCode];
  98. }
  99. else
  100. {
  101. if ($oAttDef->IsExternalKey())
  102. {
  103. $oAttribute = new SynchroAttExtKey();
  104. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  105. }
  106. else
  107. {
  108. $oAttribute = new SynchroAttribute();
  109. }
  110. $oAttribute->Set('sync_source_id', $this->GetKey());
  111. $oAttribute->Set('attcode', $sAttCode);
  112. $oAttribute->Set('reconcile', MetaModel::IsReconcKey($this->GetTargetClass(), $sAttCode) ? 1 : 0);
  113. $oAttribute->Set('update', 1);
  114. $oAttribute->Set('update_policy', 'master_locked');
  115. }
  116. if (!$bEditMode)
  117. {
  118. // Read-only mode
  119. $aRow['reconciliation'] = $oAttribute->Get('reconcile') == 1 ? Dict::S('Core:SynchroReconcile:Yes') : Dict::S('Core:SynchroReconcile:No');
  120. $aRow['update'] = $oAttribute->Get('update') == 1 ? Dict::S('Core:SynchroUpdate:Yes') : Dict::S('Core:SynchroUpdate:No');
  121. $aRow['attcode'] = MetaModel::GetLabel($this->GetTargetClass(), $oAttribute->Get('attcode'));
  122. $aRow['update_policy'] = $oAttribute->GetAsHTML('update_policy');
  123. }
  124. else
  125. {
  126. // Read-only mode
  127. $sAttCode = $oAttribute->Get('attcode');
  128. $sChecked = $oAttribute->Get('reconcile') == 1 ? 'checked' : '';
  129. $aRow['reconciliation'] = "<input type=\"checkbox\" name=\"reconciliation[$sAttCode]\" $sChecked/>";
  130. $sChecked = $oAttribute->Get('update') == 1 ? 'checked' : '';
  131. $aRow['update'] = "<input type=\"checkbox\" name=\"update[$sAttCode]\" $sChecked/>";
  132. $aRow['attcode'] = MetaModel::GetLabel($this->GetTargetClass(), $oAttribute->Get('attcode'));
  133. $oAttDef = MetaModel::GetAttributeDef(get_class($oAttribute), 'update_policy');
  134. $aRow['update_policy'] = cmdbAbstractObject::GetFormElementForField($oPage, get_class($oAttribute), 'update_policy', $oAttDef, $oAttribute->Get('update_policy'), '', 'update_policy_'.$sAttCode, "[$sAttCode]");
  135. }
  136. $aValues[] = $aRow;
  137. }
  138. }
  139. $oPage->Table($aAttribs, $aValues);
  140. $this->DisplayStatusTab($oPage);
  141. }
  142. parent::DisplayBareRelations($oPage, $bEditMode);
  143. }
  144. /**
  145. * Displays the status (SynchroLog) of the datasource in a graphical manner
  146. * @param $oPage WebPage
  147. * @return void
  148. */
  149. protected function DisplayStatusTab(WebPage $oPage)
  150. {
  151. $oPage->SetCurrentTab(Dict::S('Core:SynchroStatus'));
  152. $sSelectSynchroLog = 'SELECT SynchroLog WHERE sync_source_id = :source_id';
  153. $oSetSynchroLog = new CMDBObjectSet(DBObjectSearch::FromOQL($sSelectSynchroLog), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()));
  154. if ($oSetSynchroLog->Count() > 0)
  155. {
  156. $oLastLog = $oSetSynchroLog->Fetch();
  157. $sStartDate = $oLastLog->Get('start_date');
  158. $oLastLog->Get('stats_nb_replica_seen');
  159. $iLastLog = 0;
  160. $iDSid = $this->GetKey();
  161. if ($oLastLog->Get('status') == 'running')
  162. {
  163. // Still running !
  164. $oPage->p('<h2>'.Dict::Format('Core:Synchro:SynchroRunningStartedOn_Date', $sStartDate).'</h2>');
  165. }
  166. else
  167. {
  168. $sEndDate = $oLastLog->Get('end_date');
  169. $iLastLog = $oLastLog->GetKey();
  170. $oPage->p('<h2>'.Dict::Format('Core:Synchro:SynchroEndedOn_Date', $sEndDate).'</h2>');
  171. }
  172. $oPage->add('<table class="synoptics"><tr><td style="color:#333;vertical-align:top">');
  173. // List all the log entries for the user to select
  174. $oPage->add('<h2>'.Dict::S('Core:Synchro:History').'</h2>');
  175. $oSetSynchroLog->Rewind();
  176. $oPage->add('<select size="25" onChange="UpdateSynoptics(this.value);">');
  177. $sSelected = ' selected'; // First log is selected by default
  178. $sScript = "var aSynchroLog = {\n";
  179. while($oLog = $oSetSynchroLog->Fetch())
  180. {
  181. $sLogTitle = Dict::Format('Core:SynchroLogTitle', $oLog->Get('status'), $oLog->Get('start_date'));
  182. $oPage->add('<option value="'.$oLog->GetKey().'"'.$sSelected.'>'.$sLogTitle.'</option>');
  183. $sSelected = ''; // only the first log is selected by default
  184. $aData = $this->ProcessLog($oLog);
  185. $sScript .= '"'.$oLog->GetKey().'": '.json_encode($aData).",\n";
  186. }
  187. $sScript .= "end: 'Done'";
  188. $sScript .= "};\n";
  189. $sScript .= <<<EOF
  190. var sLastLog = '$iLastLog';
  191. function UpdateSynoptics(id)
  192. {
  193. var aValues = aSynchroLog[id];
  194. for (var sKey in aValues)
  195. {
  196. $('#c_'+sKey).html(aValues[sKey]);
  197. var fOpacity = (aValues[sKey] == 0) ? 0.3 : 1;
  198. $('#'+sKey).fadeTo("slow", fOpacity);
  199. }
  200. //alert('id = '+id+', lastLog='+sLastLog+', id==sLastLog: '+(id==sLastLog)+' obj_updated_errors: '+aValues['obj_updated_errors']);
  201. if ( (id == sLastLog) && (aValues['obj_new_errors'] > 0) )
  202. {
  203. $('#new_errors_link').show();
  204. }
  205. else
  206. {
  207. $('#new_errors_link').hide();
  208. }
  209. if ( (id == sLastLog) && (aValues['obj_updated_errors'] > 0) )
  210. {
  211. $('#updated_errors_link').show();
  212. }
  213. else
  214. {
  215. $('#updated_errors_link').hide();
  216. }
  217. if ( (id == sLastLog) && (aValues['obj_disappeared_errors'] > 0) )
  218. {
  219. $('#disappeared_errors_link').show();
  220. }
  221. else
  222. {
  223. $('#disappeared_errors_link').hide();
  224. }
  225. }
  226. EOF
  227. ;
  228. $oPage->add_script($sScript);
  229. $oPage->add('</select>');
  230. $oPage->add('</td><td style="vertical-align:top;">');
  231. // Now build the big "synoptics" view
  232. $aData = $this->ProcessLog($oLastLog);
  233. $sNbReplica = $this->GetIcon()."&nbsp;".Dict::Format('Core:Synchro:Nb_Replica', "<span id=\"c_nb_replica_total\">{$aData['nb_replica_total']}</span>");
  234. $sNbObjects = MetaModel::GetClassIcon($this->GetTargetClass())."&nbsp;".Dict::Format('Core:Synchro:Nb_Class:Objects', $this->GetTargetClass(), "<span id=\"c_nb_obj_total\">{$aData['nb_obj_total']}</span>");
  235. $oPage->add(
  236. <<<EOF
  237. <table class="synoptics">
  238. <tr class="synoptics_header">
  239. <td>$sNbReplica</td><td>&nbsp;</td><td>$sNbObjects</td>
  240. </tr>
  241. <tr>
  242. EOF
  243. );
  244. $sBaseOQL = "SELECT SynchroReplica WHERE sync_source_id=".$this->GetKey()." AND status_last_error!=''";
  245. $oPage->add($this->HtmlBox('repl_ignored', $aData, '#999').'<td colspan="2">&nbsp;</td>');
  246. $oPage->add("</tr>\n<tr>");
  247. $oPage->add($this->HtmlBox('repl_disappeared', $aData, '#630', 'rowspan="4"').'<td rowspan="4" class="arrow">=&gt;</td>'.$this->HtmlBox('obj_disappeared_no_action', $aData, '#333'));
  248. $oPage->add("</tr>\n<tr>");
  249. $oPage->add($this->HtmlBox('obj_deleted', $aData, '#000'));
  250. $oPage->add("</tr>\n<tr>");
  251. $oPage->add($this->HtmlBox('obj_obsoleted', $aData, '#630'));
  252. $oPage->add("</tr>\n<tr>");
  253. $sOQL = urlencode($sBaseOQL." AND status='obsolete'");
  254. $oPage->add($this->HtmlBox('obj_disappeared_errors', $aData, '#C00', '', " <a style=\"color:#fff\" href=\"../synchro/replica?operation=oql&datasource=$iDSid&oql=$sOQL\" id=\"disappeared_errors_link\">Show</a>"));
  255. $oPage->add("</tr>\n<tr>");
  256. $oPage->add($this->HtmlBox('repl_existing', $aData, '#093', 'rowspan="3"').'<td rowspan="3" class="arrow">=&gt;</td>'.$this->HtmlBox('obj_unchanged', $aData, '#393'));
  257. $oPage->add("</tr>\n<tr>");
  258. $oPage->add($this->HtmlBox('obj_updated', $aData, '#3C3'));
  259. $oPage->add("</tr>\n<tr>");
  260. $sOQL = urlencode($sBaseOQL." AND status='modified'");
  261. $oPage->add($this->HtmlBox('obj_updated_errors', $aData, '#C00', '', " <a style=\"color:#fff\" href=\"../synchro/replica?operation=oql&datasource=$iDSid&oql=$sOQL\" id=\"updated_errors_link\">Show</a>"));
  262. $oPage->add("</tr>\n<tr>");
  263. $oPage->add($this->HtmlBox('repl_new', $aData, '#339', 'rowspan="4"').'<td rowspan="4" class="arrow">=&gt;</td>'.$this->HtmlBox('obj_new_unchanged', $aData, '#393'));
  264. $oPage->add("</tr>\n<tr>");
  265. $oPage->add($this->HtmlBox('obj_new_updated', $aData, '#3C3'));
  266. $oPage->add("</tr>\n<tr>");
  267. $oPage->add($this->HtmlBox('obj_created', $aData, '#339'));
  268. $oPage->add("</tr>\n<tr>");
  269. $sOQL = urlencode($sBaseOQL." AND status='new'");
  270. $oPage->add($this->HtmlBox('obj_new_errors', $aData, '#C00', '', " <a style=\"color:#fff\" href=\"../synchro/replica?operation=oql&datasource=$iDSid&oql=$sOQL\" id=\"new_errors_link\">Show</a>"));
  271. $oPage->add("</tr>\n</table>\n");
  272. $oPage->add('</td></tr></table>');
  273. $oPage->add_ready_script("UpdateSynoptics('$iLastLog')");
  274. }
  275. else
  276. {
  277. $oPage->p('<h2>'.Dict::S('Core:Synchro:NeverRun').'</h2>');
  278. }
  279. }
  280. protected function HtmlBox($sId, $aData, $sColor, $sHTMLAttribs = '', $sErrorLink = '')
  281. {
  282. $iCount = $aData[$sId];
  283. $sCount = "<span id=\"c_{$sId}\">$iCount</span>";
  284. $sLabel = Dict::Format('Core:Synchro:label_'.$sId, $sCount);
  285. $sOpacity = ($iCount==0) ? "opacity:0.3;" : "";
  286. return "<td id=\"$sId\" style=\"background-color:$sColor;$sOpacity;\" {$sHTMLAttribs}>{$sLabel}{$sErrorLink}</td>";
  287. }
  288. protected function ProcessLog($oLastLog)
  289. {
  290. $aData = array(
  291. 'obj_deleted' => $oLastLog->Get('stats_nb_obj_deleted'),
  292. 'obj_obsoleted' => $oLastLog->Get('stats_nb_obj_obsoleted'),
  293. 'obj_disappeared_errors' => $oLastLog->Get('stats_nb_obj_obsoleted_errors') + $oLastLog->Get('stats_nb_obj_deleted_errors'),
  294. 'obj_disappeared_no_action' => $oLastLog->Get('stats_nb_replica_disappeared_no_action'),
  295. 'obj_updated' => $oLastLog->Get('stats_nb_obj_updated'),
  296. 'obj_updated_errors' => $oLastLog->Get('stats_nb_obj_updated_errors'),
  297. 'obj_new_updated' => $oLastLog->Get('stats_nb_obj_new_updated'),
  298. 'obj_new_unchanged' => $oLastLog->Get('stats_nb_obj_new_unchanged'),
  299. 'obj_created' => $oLastLog->Get('stats_nb_obj_created'),
  300. 'obj_created_errors' => $oLastLog->Get('stats_nb_obj_created_errors'),
  301. );
  302. $iReconciledErrors = $oLastLog->Get('stats_nb_replica_reconciled_errors');
  303. $iDisappeared = $aData['obj_disappeared_errors'] + $aData['obj_obsoleted'] + $aData['obj_deleted'] + $aData['obj_disappeared_no_action'];
  304. $aData['repl_disappeared'] = $iDisappeared;
  305. $iNewErrors = $aData['obj_created_errors'] + $oLastLog->Get('stats_nb_replica_reconciled_errors');
  306. $aData['obj_new_errors'] = $iNewErrors;
  307. $iNew = $aData['obj_created'] + $iNewErrors + $aData['obj_new_updated'] + $aData['obj_new_unchanged'];
  308. $aData['repl_new'] = $iNew;
  309. $iExisting = $oLastLog->Get('stats_nb_replica_seen') - $iNew;
  310. $aData['repl_existing'] = $iExisting;
  311. $aData['obj_unchanged'] = $iExisting - $aData['obj_updated'] - $aData['obj_updated_errors'];
  312. $iIgnored = $oLastLog->Get('stats_nb_replica_total') - $iNew - $iExisting - $iDisappeared;
  313. $aData['repl_ignored'] = $iIgnored;
  314. $aData['nb_obj_total'] = $iNew + $iExisting + $iDisappeared;
  315. $aData['nb_replica_total'] = $aData['nb_obj_total'] + $iIgnored;
  316. return $aData;
  317. }
  318. public function GetAttributeFlags($sAttCode)
  319. {
  320. if (($sAttCode == 'scope_class') && (!$this->IsNew()))
  321. {
  322. return OPT_ATT_READONLY;
  323. }
  324. return parent::GetAttributeFlags($sAttCode);
  325. }
  326. public function UpdateObject($sFormPrefix = '')
  327. {
  328. parent::UpdateObject($sFormPrefix);
  329. // And now read the other post parameters...
  330. $oAttributeSet = $this->Get('attribute_list');
  331. $aAttributes = array();
  332. while($oAttribute = $oAttributeSet->Fetch())
  333. {
  334. $aAttributes[$oAttribute->Get('attcode')] = $oAttribute;
  335. }
  336. $aReconcile = utils::ReadPostedParam('reconciliation', array());
  337. $aUpdate = utils::ReadPostedParam('update', array());
  338. $aUpdatePolicy = utils::ReadPostedParam('attr_update_policy', array());
  339. // update_policy cannot be empty, so there is one entry per attribute, use this to iterate
  340. // through all the writable attributes
  341. foreach($aUpdatePolicy as $sAttCode => $sValue)
  342. {
  343. if(!isset($aAttributes[$sAttCode]))
  344. {
  345. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  346. if ($oAttDef->IsExternalKey())
  347. {
  348. $oAttribute = new SynchroAttExtKey();
  349. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  350. }
  351. else
  352. {
  353. $oAttribute = new SynchroAttribute();
  354. }
  355. $oAttribute->Set('sync_source_id', $this->GetKey());
  356. $oAttribute->Set('attcode', $sAttCode);
  357. }
  358. else
  359. {
  360. $oAttribute = $aAttributes[$sAttCode];
  361. }
  362. $bReconcile = 0;
  363. if (isset($aReconcile[$sAttCode]))
  364. {
  365. $bReconcile = $aReconcile[$sAttCode] == 'on' ? 1 : 0;
  366. }
  367. $bUpdate = 0 ; // Default / initial value
  368. if (isset($aUpdate[$sAttCode]))
  369. {
  370. $bUpdate = $aUpdate[$sAttCode] == 'on' ? 1 : 0;
  371. }
  372. $oAttribute->Set('reconcile', $bReconcile);
  373. $oAttribute->Set('update', $bUpdate);
  374. $oAttribute->Set('update_policy', $sValue);
  375. $oAttributeSet->AddObject($oAttribute);
  376. }
  377. $this->Set('attribute_list', $oAttributeSet);
  378. }
  379. /*
  380. * Overload the standard behavior
  381. */
  382. public function DoCheckToWrite()
  383. {
  384. parent::DoCheckToWrite();
  385. if ($this->IsNew())
  386. {
  387. // When inserting a new datasource object, also create the SynchroAttribute objects
  388. // for each field of the target class
  389. // Create all the SynchroAttribute records
  390. $oAttributeSet = $this->Get('attribute_list');
  391. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  392. {
  393. if ($oAttDef->IsScalar() && $oAttDef->IsWritable())
  394. {
  395. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  396. if ($oAttDef->IsExternalKey())
  397. {
  398. $oAttribute = new SynchroAttExtKey();
  399. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  400. }
  401. else
  402. {
  403. $oAttribute = new SynchroAttribute();
  404. }
  405. $oAttribute->Set('sync_source_id', $this->GetKey());
  406. $oAttribute->Set('attcode', $sAttCode);
  407. $oAttribute->Set('reconcile', MetaModel::IsReconcKey($this->GetTargetClass(), $sAttCode) ? 1 : 0);
  408. $oAttribute->Set('update', 1);
  409. $oAttribute->Set('update_policy', 'master_locked');
  410. $oAttributeSet->AddObject($oAttribute);
  411. }
  412. }
  413. $this->Set('attribute_list', $oAttributeSet);
  414. }
  415. // Check that there is at least one reconciliation key defined
  416. if ($this->Get('reconciliation_policy') == 'use_attributes')
  417. {
  418. $oSet = $this->Get('attribute_list');
  419. $oSynchroAttributeList = $oSet->ToArray();
  420. $bReconciliationKey = false;
  421. foreach($oSynchroAttributeList as $oSynchroAttribute)
  422. {
  423. if ($oSynchroAttribute->Get('reconcile') == 1)
  424. {
  425. $bReconciliationKey = true; // At least one key is defined
  426. }
  427. }
  428. if (!$bReconciliationKey)
  429. {
  430. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:AtLeastOneReconciliationKeyMustBeSpecified');
  431. }
  432. }
  433. // If 'update_then_delete' is specified there must be a delete_retention_period
  434. if (($this->Get('delete_policy') == 'update_then_delete') && ($this->Get('delete_policy_retention') == 0))
  435. {
  436. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:DeleteRetentionDurationMustBeSpecified');
  437. }
  438. // If update is specified, then something to update must be defined
  439. if ((($this->Get('delete_policy') == 'update_then_delete') || ($this->Get('delete_policy') == 'update'))
  440. && ($this->Get('delete_policy_update') == ''))
  441. {
  442. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:DeletePolicyUpdateMustBeSpecified');
  443. }
  444. }
  445. public function GetTargetClass()
  446. {
  447. return $this->Get('scope_class');
  448. }
  449. public function GetDataTable()
  450. {
  451. $sName = strtolower($this->GetTargetClass());
  452. $sName = str_replace('\'"&@|\\/ ', '_', $sName); // Remove forbidden characters from the table name
  453. $sName .= '_'.$this->GetKey(); // Add a suffix for unicity
  454. $sTable = MetaModel::GetConfig()->GetDBSubName()."synchro_data_$sName"; // Add the prefix if any
  455. return $sTable;
  456. }
  457. /**
  458. * When the new datasource has been created, let's create the synchro_data table
  459. * that will hold the data records and the correspoding triggers which will maintain
  460. * both tables in sync
  461. */
  462. protected function AfterInsert()
  463. {
  464. parent::AfterInsert();
  465. $sTable = $this->GetDataTable();
  466. $aColumns = $this->GetSQLColumns();
  467. $aFieldDefs = array();
  468. // Allow '0', otherwise mysql will render an error when the id is not given
  469. // (the trigger is expected to set the value, but it is not executed soon enough)
  470. $aFieldDefs[] = "id INTEGER(11) NOT NULL DEFAULT 0 ";
  471. $aFieldDefs[] = "`primary_key` VARCHAR(255) NULL DEFAULT NULL";
  472. foreach($aColumns as $sColumn => $ColSpec)
  473. {
  474. $aFieldDefs[] = "`$sColumn` $ColSpec NULL DEFAULT NULL";
  475. }
  476. $aFieldDefs[] = "INDEX (id)";
  477. $aFieldDefs[] = "INDEX (primary_key)";
  478. $sFieldDefs = implode(', ', $aFieldDefs);
  479. $sCreateTable = "CREATE TABLE `$sTable` ($sFieldDefs) ENGINE = innodb;";
  480. CMDBSource::Query($sCreateTable);
  481. $sTriggerInsert = "CREATE TRIGGER `{$sTable}_bi` BEFORE INSERT ON $sTable";
  482. $sTriggerInsert .= " FOR EACH ROW";
  483. $sTriggerInsert .= " BEGIN";
  484. $sTriggerInsert .= " INSERT INTO priv_sync_replica (sync_source_id, status_last_seen, `status`) VALUES ({$this->GetKey()}, NOW(), 'new');";
  485. $sTriggerInsert .= " SET NEW.id = LAST_INSERT_ID();";
  486. $sTriggerInsert .= " END;";
  487. CMDBSource::Query($sTriggerInsert);
  488. $aModified = array();
  489. foreach($aColumns as $sColumn => $ColSpec)
  490. {
  491. // <=> is a null-safe 'EQUALS' operator (there is no equivalent for "DIFFERS FROM")
  492. $aModified[] = "NOT(NEW.`$sColumn` <=> OLD.`$sColumn`)";
  493. }
  494. $sIsModified = '('.implode(') OR (', $aModified).')';
  495. // Update the replica
  496. //
  497. // status is forced to "new" if the replica was obsoleted directly from the state "new" (dest_id = null)
  498. // otherwise, if status was either 'obsolete' or 'synchronized' it is turned into 'modified' or 'synchronized' depending on the changes
  499. // otherwise, the status is left as is
  500. $sTriggerUpdate = "CREATE TRIGGER `{$sTable}_bu` BEFORE UPDATE ON $sTable";
  501. $sTriggerUpdate .= " FOR EACH ROW";
  502. $sTriggerUpdate .= " BEGIN";
  503. $sTriggerUpdate .= " IF @itopuser is null THEN";
  504. $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;";
  505. $sTriggerUpdate .= " SET NEW.id = OLD.id;"; // make sure this id won't change
  506. $sTriggerUpdate .= " END IF;";
  507. $sTriggerUpdate .= " END;";
  508. CMDBSource::Query($sTriggerUpdate);
  509. $sTriggerInsert = "CREATE TRIGGER `{$sTable}_ad` AFTER DELETE ON $sTable";
  510. $sTriggerInsert .= " FOR EACH ROW";
  511. $sTriggerInsert .= " BEGIN";
  512. $sTriggerInsert .= " DELETE FROM priv_sync_replica WHERE id = OLD.id;";
  513. $sTriggerInsert .= " END;";
  514. CMDBSource::Query($sTriggerInsert);
  515. }
  516. protected function AfterDelete()
  517. {
  518. parent::AfterDelete();
  519. $sTable = $this->GetDataTable();
  520. $sDropTable = "DROP TABLE `$sTable`";
  521. CMDBSource::Query($sDropTable);
  522. // TO DO - check that triggers get dropped with the table
  523. }
  524. /**
  525. * Perform a synchronization between the data stored in the replicas (&synchro_data_xxx_xx table)
  526. * and the iTop objects. If the lastFullLoadStartDate is NOT specified then the full_load_periodicity
  527. * is used to determine which records are obsolete.
  528. * @param Hash $aTraces Debugs/Trace information, one or more entries per replica
  529. * @param DateTime $oLastFullLoadStartDate Date of the last full load (start date/time), if known
  530. * @return void
  531. */
  532. public function Synchronize($oLastFullLoadStartDate = null)
  533. {
  534. // Create a change used for logging all the modifications/creations happening during the synchro
  535. $oMyChange = MetaModel::NewObject("CMDBChange");
  536. $oMyChange->Set("date", time());
  537. $sUserString = CMDBChange::GetCurrentUserName();
  538. $oMyChange->Set("userinfo", $sUserString);
  539. $iChangeId = $oMyChange->DBInsert();
  540. // Start logging this execution (stats + protection against reentrance)
  541. //
  542. $oStatLog = new SynchroLog();
  543. $oStatLog->Set('sync_source_id', $this->GetKey());
  544. $oStatLog->Set('start_date', time());
  545. $oStatLog->Set('status', 'running');
  546. $oStatLog->Set('stats_nb_replica_seen', 0);
  547. $oStatLog->Set('stats_nb_replica_total', 0);
  548. $oStatLog->Set('stats_nb_obj_deleted', 0);
  549. $oStatLog->Set('stats_nb_obj_deleted_errors', 0);
  550. $oStatLog->Set('stats_nb_obj_obsoleted', 0);
  551. $oStatLog->Set('stats_nb_obj_obsoleted_errors', 0);
  552. $oStatLog->Set('stats_nb_obj_created', 0);
  553. $oStatLog->Set('stats_nb_obj_created_errors', 0);
  554. $oStatLog->Set('stats_nb_obj_updated', 0);
  555. $oStatLog->Set('stats_nb_obj_updated_errors', 0);
  556. // $oStatLog->Set('stats_nb_replica_reconciled', 0);
  557. $oStatLog->Set('stats_nb_replica_reconciled_errors', 0);
  558. $oStatLog->Set('stats_nb_replica_disappeared_no_action', 0);
  559. $oStatLog->Set('stats_nb_obj_new_updated', 0);
  560. $oStatLog->Set('stats_nb_obj_new_unchanged',0);
  561. $sSelectTotal = "SELECT SynchroReplica WHERE sync_source_id = :source_id";
  562. $oSetTotal = new DBObjectSet(DBObjectSearch::FromOQL($sSelectTotal), array() /* order by*/, array('source_id' => $this->GetKey()));
  563. $oStatLog->Set('stats_nb_replica_total', $oSetTotal->Count());
  564. $oStatLog->DBInsertTracked($oMyChange);
  565. try
  566. {
  567. $this->DoSynchronize($oLastFullLoadStartDate, $oMyChange, $oStatLog);
  568. $oStatLog->Set('end_date', time());
  569. $oStatLog->Set('status', 'completed');
  570. $oStatLog->DBUpdateTracked($oMyChange);
  571. }
  572. catch (SynchroExceptionNotStarted $e)
  573. {
  574. // Set information for reporting... but delete the object in DB
  575. $oStatLog->Set('end_date', time());
  576. $oStatLog->Set('status', 'error');
  577. $oStatLog->Set('last_error', $e->getMessage());
  578. $oStatLog->DBDeleteTracked($oMyChange);
  579. }
  580. catch (Exception $e)
  581. {
  582. $oStatLog->Set('end_date', time());
  583. $oStatLog->Set('status', 'error');
  584. $oStatLog->Set('last_error', $e->getMessage());
  585. $oStatLog->DBUpdateTracked($oMyChange);
  586. }
  587. return $oStatLog;
  588. }
  589. protected function DoSynchronize($oLastFullLoadStartDate, $oMyChange, &$oStatLog)
  590. {
  591. if ($this->Get('status') == 'obsolete')
  592. {
  593. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncDataSourceObsolete'));
  594. }
  595. if (!UserRights::IsAdministrator() && $this->Get('user_id') != UserRights::GetUserId())
  596. {
  597. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncDataSourceAccessRestriction'));
  598. }
  599. $sDeletePolicy = $this->Get('delete_policy');
  600. // Count the replicas
  601. $sSelectAll = "SELECT SynchroReplica WHERE sync_source_id = :source_id";
  602. $oSetAll = new DBObjectSet(DBObjectSearch::FromOQL($sSelectAll), array() /* order by*/, array('source_id' => $this->GetKey()));
  603. $iCountAllReplicas = $oSetAll->Count();
  604. $oStatLog->Set('stats_nb_replica_total', $iCountAllReplicas);
  605. // Get all the replicas that were not seen in the last import and mark them as obsolete
  606. if ($oLastFullLoadStartDate == null)
  607. {
  608. // No previous import known, use the full_load_periodicity value... and the current date
  609. $oLastFullLoadStartDate = new DateTime(); // Now
  610. $iLoadPeriodicity = $this->Get('full_load_periodicity'); // Duration in seconds
  611. if ($iLoadPeriodicity > 0)
  612. {
  613. $sInterval = "-$iLoadPeriodicity seconds";
  614. $oLastFullLoadStartDate->Modify($sInterval);
  615. }
  616. }
  617. $sLimitDate = $oLastFullLoadStartDate->Format('Y-m-d H:i:s');
  618. $oStatLog->AddTrace("Limit Date: $sLimitDate");
  619. $sSelectToObsolete = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen < :last_import";
  620. $oSetToObsolete = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToObsolete), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sLimitDate));
  621. if (($iCountAllReplicas > 10) && ($iCountAllReplicas == $oSetToObsolete->Count()))
  622. {
  623. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncTooManyMissingReplicas'));
  624. }
  625. while($oReplica = $oSetToObsolete->Fetch())
  626. {
  627. switch ($sDeletePolicy)
  628. {
  629. case 'update':
  630. case 'update_then_delete':
  631. $oStatLog->AddTrace("Destination object to be updated", $oReplica);
  632. $aToUpdate = array();
  633. $aToUpdate = explode(';', $this->Get('delete_policy_update')); //ex: 'status:obsolete;description:stopped',
  634. foreach($aToUpdate as $sUpdateSpec)
  635. {
  636. $aUpdateSpec = explode(':', $sUpdateSpec);
  637. if (count($aUpdateSpec) == 2)
  638. {
  639. $sAttCode = $aUpdateSpec[0];
  640. $sValue = $aUpdateSpec[1];
  641. $aToUpdate[$sAttCode] = $sValue;
  642. }
  643. }
  644. $oReplica->UpdateDestObject($aToUpdate, $oMyChange, $oStatLog);
  645. $oReplica->Set('status', 'obsolete');
  646. $oReplica->DBUpdateTracked($oMyChange);
  647. break;
  648. case 'delete':
  649. default:
  650. $oStatLog->AddTrace("Destination object to be DELETED", $oReplica);
  651. $oReplica->DeleteDestObject($oMyChange, $oStatLog);
  652. }
  653. }
  654. //Count "seen" objects
  655. $sSelectSeen = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen >= :last_import";
  656. $oSetSeen = new DBObjectSet(DBObjectSearch::FromOQL($sSelectSeen), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sLimitDate));
  657. $oStatLog->Set('stats_nb_replica_seen', $oSetSeen->Count());
  658. // Get all the replicas that are 'new' or modified
  659. //
  660. // Get the list of SQL columns
  661. $sClass = $this->GetTargetClass();
  662. $aAttCodesExpected = array();
  663. $aAttCodesToReconcile = array();
  664. $aAttCodesToUpdate = array();
  665. $sSelectAtt = "SELECT SynchroAttribute WHERE sync_source_id = :source_id AND (update = 1 OR reconcile = 1)";
  666. $oSetAtt = new DBObjectSet(DBObjectSearch::FromOQL($sSelectAtt), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */);
  667. while ($oSyncAtt = $oSetAtt->Fetch())
  668. {
  669. if ($oSyncAtt->Get('update'))
  670. {
  671. $aAttCodesToUpdate[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  672. }
  673. if ($oSyncAtt->Get('reconcile'))
  674. {
  675. $aAttCodesToReconcile[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  676. }
  677. $aAttCodesExpected[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  678. }
  679. $aColumns = $this->GetSQLColumns(array_keys($aAttCodesExpected));
  680. $aExtDataFields = array_keys($aColumns);
  681. $aExtDataFields[] = 'primary_key';
  682. $aExtDataSpec = array(
  683. 'table' => $this->GetDataTable(),
  684. 'join_key' => 'id',
  685. 'fields' => $aExtDataFields
  686. );
  687. // Get the list of reconciliation keys
  688. if ($this->Get('reconciliation_policy') == 'use_attributes')
  689. {
  690. $aReconciliationKeys = $aAttCodesToReconcile;
  691. }
  692. elseif ($this->Get('reconciliation_policy') == 'use_primary_key')
  693. {
  694. // Override the setings made at the attribute level !
  695. $aReconciliationKeys = array("primary_key" => null);
  696. }
  697. $oStatLog->AddTrace("Update of: {".implode(', ', array_keys($aAttCodesToUpdate))."}");
  698. $oStatLog->AddTrace("Reconciliation on: {".implode(', ', array_keys($aReconciliationKeys))."}");
  699. $aAttributes = array();
  700. foreach($aAttCodesToUpdate as $sAttCode => $oSyncAtt)
  701. {
  702. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  703. if ($oAttDef->IsWritable() && $oAttDef->IsScalar())
  704. {
  705. $aAttributes[$sAttCode] = $oSyncAtt;
  706. }
  707. }
  708. $sSelectToSync = "SELECT SynchroReplica WHERE (status = 'new' OR status = 'modified') AND sync_source_id = :source_id";
  709. $oSetToSync = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToSync), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, $aExtDataSpec, 0 /* limitCount */, 0 /* limitStart */);
  710. while($oReplica = $oSetToSync->Fetch())
  711. {
  712. $oReplica->Synchro($this, $aReconciliationKeys, $aAttributes, $oMyChange, $oStatLog);
  713. }
  714. // Get all the replicas that are to be deleted
  715. //
  716. if ($sDeletePolicy == 'update_then_delete')
  717. {
  718. $oDeletionDate = $oLastFullLoadStartDate;
  719. $iDeleteRetention = $this->Get('delete_policy_retention'); // Duration in seconds
  720. if ($iDeleteRetention > 0)
  721. {
  722. $sInterval = "-$iDeleteRetention seconds";
  723. $oDeletionDate->Modify($sInterval);
  724. }
  725. $sDeletionDate = $oDeletionDate->Format('Y-m-d H:i:s');
  726. $oStatLog->AddTrace("Deletion date: $sDeletionDate");
  727. $sSelectToDelete = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('obsolete') AND status_last_seen < :last_import";
  728. $oSetToDelete = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToDelete), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sDeletionDate));
  729. while($oReplica = $oSetToDelete->Fetch())
  730. {
  731. $oStatLog->AddTrace("Destination object to be DELETED", $oReplica);
  732. $oReplica->DeleteDestObject($oMyChange, $oStatLog);
  733. }
  734. }
  735. }
  736. /**
  737. * Get the list of SQL columns corresponding to a particular list of attribute codes
  738. * Defaults to the whole list of columns for the current class
  739. */
  740. public function GetSQLColumns($aAttributeCodes = null)
  741. {
  742. $aColumns = array();
  743. $sClass = $this->GetTargetClass();
  744. if (is_null($aAttributeCodes))
  745. {
  746. $aAttributeCodes = array();
  747. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  748. {
  749. if ($sAttCode == 'finalclass') continue;
  750. $aAttributeCodes[] = $sAttCode;
  751. }
  752. }
  753. foreach($aAttributeCodes as $sAttCode)
  754. {
  755. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  756. if ($oAttDef->IsExternalKey())
  757. {
  758. // The pkey might be used as well as any other key column
  759. $aColumns[$sAttCode] = 'VARCHAR (255)';
  760. }
  761. else
  762. {
  763. foreach($oAttDef->GetSQLColumns() as $sField => $sDBFieldType)
  764. {
  765. $aColumns[$sField] = $sDBFieldType;
  766. }
  767. }
  768. }
  769. return $aColumns;
  770. }
  771. public function IsRunning()
  772. {
  773. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id AND status='running'";
  774. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 1 /* limitCount */, 0 /* limitStart */);
  775. if ($oSet->Count() < 1)
  776. {
  777. $bRet = false;
  778. }
  779. else
  780. {
  781. $bRet = true;
  782. }
  783. return $bRet;
  784. }
  785. public function GetLatestLog()
  786. {
  787. $oLog = null;
  788. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id";
  789. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 1 /* limitCount */, 0 /* limitStart */);
  790. if ($oSet->Count() >= 1)
  791. {
  792. $oLog = $oSet->Fetch();
  793. }
  794. return $oLog;
  795. }
  796. // TO DO: remove if still unused
  797. /**
  798. * Retrieve from the log, the date of the last completed import
  799. * @return DateTime
  800. */
  801. public function GetLastCompletedImportDate()
  802. {
  803. $date = null;
  804. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id AND status='completed'";
  805. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('end_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 0 /* limitCount */, 0 /* limitStart */);
  806. if ($oSet->Count() >= 1)
  807. {
  808. $oLog = $oSet->Fetch();
  809. $date = $oLog->Get('end_date');
  810. }
  811. else
  812. {
  813. // TO DO: remove trace
  814. echo "<p>No completed log found</p>\n";
  815. }
  816. return $date;
  817. }
  818. }
  819. class SynchroAttribute extends cmdbAbstractObject
  820. {
  821. public static function Init()
  822. {
  823. $aParams = array
  824. (
  825. "category" => "core/cmdb,view_in_gui",
  826. "key_type" => "autoincrement",
  827. "name_attcode" => "",
  828. "state_attcode" => "",
  829. "reconc_keys" => array(),
  830. "db_table" => "priv_sync_att",
  831. "db_key_field" => "id",
  832. "db_finalclass_field" => "",
  833. "display_template" => "",
  834. );
  835. MetaModel::Init_Params($aParams);
  836. MetaModel::Init_InheritAttributes();
  837. 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())));
  838. MetaModel::Init_AddAttribute(new AttributeString("attcode", array("allowed_values"=>null, "sql"=>"attcode", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  839. MetaModel::Init_AddAttribute(new AttributeBoolean("update", array("allowed_values"=>null, "sql"=>"update", "default_value"=>true, "is_null_allowed"=>false, "depends_on"=>array())));
  840. MetaModel::Init_AddAttribute(new AttributeBoolean("reconcile", array("allowed_values"=>null, "sql"=>"reconcile", "default_value"=>false, "is_null_allowed"=>false, "depends_on"=>array())));
  841. MetaModel::Init_AddAttribute(new AttributeEnum("update_policy", array("allowed_values"=>new ValueSetEnum('master_locked,master_unlocked,write_if_empty'), "sql"=>"update_policy", "default_value"=>"master_locked", "is_null_allowed"=>false, "depends_on"=>array())));
  842. // Display lists
  843. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for the complete details
  844. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  845. // Search criteria
  846. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  847. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  848. }
  849. }
  850. class SynchroAttExtKey extends SynchroAttribute
  851. {
  852. public static function Init()
  853. {
  854. $aParams = array
  855. (
  856. "category" => "core/cmdb,view_in_gui",
  857. "key_type" => "autoincrement",
  858. "name_attcode" => "",
  859. "state_attcode" => "",
  860. "reconc_keys" => array(),
  861. "db_table" => "priv_sync_att_extkey",
  862. "db_key_field" => "id",
  863. "db_finalclass_field" => "",
  864. "display_template" => "",
  865. );
  866. MetaModel::Init_Params($aParams);
  867. MetaModel::Init_InheritAttributes();
  868. MetaModel::Init_AddAttribute(new AttributeString("reconciliation_attcode", array("allowed_values"=>null, "sql"=>"reconciliation_attcode", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  869. // Display lists
  870. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy', 'reconciliation_attcode')); // Attributes to be displayed for the complete details
  871. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  872. // Search criteria
  873. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  874. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  875. }
  876. }
  877. class SynchroAttLinkSet extends SynchroAttribute
  878. {
  879. public static function Init()
  880. {
  881. $aParams = array
  882. (
  883. "category" => "core/cmdb,view_in_gui",
  884. "key_type" => "autoincrement",
  885. "name_attcode" => "",
  886. "state_attcode" => "",
  887. "reconc_keys" => array(),
  888. "db_table" => "priv_sync_att_linkset",
  889. "db_key_field" => "id",
  890. "db_finalclass_field" => "",
  891. "display_template" => "",
  892. );
  893. MetaModel::Init_Params($aParams);
  894. MetaModel::Init_InheritAttributes();
  895. MetaModel::Init_AddAttribute(new AttributeString("row_separator", array("allowed_values"=>null, "sql"=>"row_separator", "default_value"=>'|', "is_null_allowed"=>true, "depends_on"=>array())));
  896. MetaModel::Init_AddAttribute(new AttributeString("attribute_separator", array("allowed_values"=>null, "sql"=>"attribute_separator", "default_value"=>';', "is_null_allowed"=>true, "depends_on"=>array())));
  897. // Display lists
  898. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy', 'row_separator', 'attribute_separator')); // Attributes to be displayed for the complete details
  899. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  900. // Search criteria
  901. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  902. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  903. }
  904. }
  905. //class SynchroLog extends Event
  906. class SynchroLog extends DBObject
  907. {
  908. public static function Init()
  909. {
  910. $aParams = array
  911. (
  912. "category" => "core/cmdb,view_in_gui",
  913. "key_type" => "autoincrement",
  914. "name_attcode" => "",
  915. "state_attcode" => "",
  916. "reconc_keys" => array(),
  917. "db_table" => "priv_sync_log",
  918. "db_key_field" => "id",
  919. "db_finalclass_field" => "",
  920. "display_template" => "",
  921. );
  922. MetaModel::Init_Params($aParams);
  923. MetaModel::Init_InheritAttributes();
  924. // MetaModel::Init_AddAttribute(new AttributeString("userinfo", array("allowed_values"=>null, "sql"=>"userinfo", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  925. 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())));
  926. MetaModel::Init_AddAttribute(new AttributeDateTime("start_date", array("allowed_values"=>null, "sql"=>"start_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  927. MetaModel::Init_AddAttribute(new AttributeDateTime("end_date", array("allowed_values"=>null, "sql"=>"end_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  928. MetaModel::Init_AddAttribute(new AttributeEnum("status", array("allowed_values"=>new ValueSetEnum('running,completed,error'), "sql"=>"status", "default_value"=>"running", "is_null_allowed"=>false, "depends_on"=>array())));
  929. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_replica_seen", array("allowed_values"=>null, "sql"=>"stats_nb_replica_seen", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  930. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_replica_total", array("allowed_values"=>null, "sql"=>"stats_nb_replica_total", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  931. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_deleted", array("allowed_values"=>null, "sql"=>"stats_nb_obj_deleted", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  932. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_deleted_errors", array("allowed_values"=>null, "sql"=>"stats_deleted_errors", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  933. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_obsoleted", array("allowed_values"=>null, "sql"=>"stats_nb_obj_obsoleted", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  934. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_obsoleted_errors", array("allowed_values"=>null, "sql"=>"stats_nb_obj_obsoleted_errors", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  935. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_created", array("allowed_values"=>null, "sql"=>"stats_nb_obj_created", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  936. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_created_errors", array("allowed_values"=>null, "sql"=>"stats_nb_obj_created_errors", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  937. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_updated", array("allowed_values"=>null, "sql"=>"stats_nb_obj_updated", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  938. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_updated_errors", array("allowed_values"=>null, "sql"=>"stats_nb_obj_updated_errors", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  939. // MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_replica_reconciled", array("allowed_values"=>null, "sql"=>"stats_nb_replica_reconciled", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  940. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_replica_reconciled_errors", array("allowed_values"=>null, "sql"=>"stats_nb_replica_reconciled_errors", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  941. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_replica_disappeared_no_action", array("allowed_values"=>null, "sql"=>"stats_nb_replica_disappeared_no_action", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  942. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_new_updated", array("allowed_values"=>null, "sql"=>"stats_nb_obj_new_updated", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  943. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_new_unchanged", array("allowed_values"=>null, "sql"=>"stats_nb_obj_new_unchanged", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  944. MetaModel::Init_AddAttribute(new AttributeString("last_error", array("allowed_values"=>null, "sql"=>"last_error", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  945. MetaModel::Init_AddAttribute(new AttributeLongText("traces", array("allowed_values"=>null, "sql"=>"traces", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  946. // Display lists
  947. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'start_date', 'end_date', 'status', 'stats_nb_replica_total', 'stats_nb_replica_seen', 'stats_nb_obj_created', /*'stats_nb_replica_reconciled',*/ 'stats_nb_obj_updated', 'stats_nb_obj_obsoleted', 'stats_nb_obj_deleted',
  948. 'stats_nb_obj_created_errors', 'stats_nb_replica_reconciled_errors', 'stats_nb_replica_disappeared_no_action', 'stats_nb_obj_updated_errors', 'stats_nb_obj_obsoleted_errors', 'stats_nb_obj_deleted_errors', 'stats_nb_obj_new_unchanged', 'stats_nb_obj_new_updated', 'dump')); // Attributes to be displayed for the complete details
  949. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'start_date', 'end_date', 'status', 'stats_nb_replica_seen')); // Attributes to be displayed for a list
  950. // Search criteria
  951. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  952. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  953. }
  954. /**
  955. * Increments a statistics counter
  956. */
  957. function Inc($sCode)
  958. {
  959. $this->Set($sCode, 1+$this->Get($sCode));
  960. }
  961. /**
  962. * Implement traces management
  963. */
  964. protected $m_aTraces = array();
  965. public function AddTrace($sMsg, $oReplica = null)
  966. {
  967. if (MetaModel::GetConfig()->Get('synchro_trace') == 'none')
  968. {
  969. return;
  970. }
  971. if ($oReplica)
  972. {
  973. $sDestClass = $oReplica->Get('dest_class');
  974. if (!empty($sDestClass))
  975. {
  976. $sPrefix = $oReplica->GetKey().','.$sDestClass.'::'.$oReplica->Get('dest_id').',';
  977. }
  978. else
  979. {
  980. $sPrefix = $oReplica->GetKey().',,';
  981. }
  982. }
  983. else
  984. {
  985. $sPrefix = ',,';
  986. }
  987. $this->m_aTraces[] = $sPrefix.$sMsg;
  988. }
  989. public function GetTraces()
  990. {
  991. return $this->m_aTraces;
  992. }
  993. protected function TraceToText()
  994. {
  995. if (MetaModel::GetConfig()->Get('synchro_trace') != 'save')
  996. {
  997. // none, or display only
  998. return;
  999. }
  1000. $oAttDef = MetaModel::GetAttributeDef(get_class($this), 'traces');
  1001. $iMaxSize = $oAttDef->GetMaxSize();
  1002. $sTrace = implode("\n", $this->m_aTraces);
  1003. if (strlen($sTrace) >= $iMaxSize)
  1004. {
  1005. $sTrace = substr($sTrace, 0, $iMaxSize - 255)."...\nTruncated (size: ".strlen($sTrace).')';
  1006. }
  1007. $this->Set('traces', $sTrace);
  1008. }
  1009. protected function OnInsert()
  1010. {
  1011. $this->TraceToText();
  1012. parent::OnInsert();
  1013. }
  1014. protected function OnUpdate()
  1015. {
  1016. $this->TraceToText();
  1017. parent::OnUpdate();
  1018. }
  1019. }
  1020. class SynchroReplica extends DBObject implements iDisplay
  1021. {
  1022. static $aSearches = array(); // Cache of OQL queries used for reconciliation (per data source)
  1023. public static function Init()
  1024. {
  1025. $aParams = array
  1026. (
  1027. "category" => "core/cmdb,view_in_gui",
  1028. "key_type" => "autoincrement",
  1029. "name_attcode" => "",
  1030. "state_attcode" => "",
  1031. "reconc_keys" => array(),
  1032. "db_table" => "priv_sync_replica",
  1033. "db_key_field" => "id",
  1034. "db_finalclass_field" => "",
  1035. "display_template" => "",
  1036. );
  1037. MetaModel::Init_Params($aParams);
  1038. MetaModel::Init_InheritAttributes();
  1039. 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())));
  1040. MetaModel::Init_AddAttribute(new AttributeInteger("dest_id", array("allowed_values"=>null, "sql"=>"dest_id", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  1041. MetaModel::Init_AddAttribute(new AttributeClass("dest_class", array("class_category"=>"bizmodel", "more_values"=>"", "sql"=>"dest_class", "default_value"=>'Organization', "is_null_allowed"=>true, "depends_on"=>array())));
  1042. 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())));
  1043. 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())));
  1044. 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())));
  1045. 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())));
  1046. 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())));
  1047. 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())));
  1048. 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())));
  1049. // Display lists
  1050. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'dest_id', 'dest_class', '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
  1051. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'dest_id', 'dest_class', 'status_last_seen', 'status', 'status_dest_creator', 'status_last_error')); // Attributes to be displayed for a list
  1052. // Search criteria
  1053. MetaModel::Init_SetZListItems('standard_search', array('sync_source_id', 'status_last_seen', 'status', 'status_dest_creator', 'dest_class', 'dest_id', 'status_last_error')); // Criteria of the std search form
  1054. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1055. }
  1056. public function DBInsert()
  1057. {
  1058. throw new CoreException('A synchronization replica must be created only by the mean of triggers');
  1059. }
  1060. // Overload the deletion -> the replica has been created by the mean of a trigger,
  1061. // it will be deleted by the mean of a trigger too
  1062. public function DBDelete()
  1063. {
  1064. $oDataSource = MetaModel::GetObject('SynchroDataSource', $this->Get('sync_source_id'));
  1065. $sTable = $oDataSource->GetDataTable();
  1066. $sSQL = "DELETE FROM `$sTable` WHERE id = '{$this->GetKey()}'";
  1067. CMDBSource::Query($sSQL);
  1068. $this->m_bIsInDB = false;
  1069. $this->m_iKey = null;
  1070. }
  1071. public function SetLastError($sMessage, $oException = null)
  1072. {
  1073. if ($oException)
  1074. {
  1075. $sText = $sMessage.$oException->getMessage();
  1076. }
  1077. else
  1078. {
  1079. $sText = $sMessage;
  1080. }
  1081. if (strlen($sText) > 255)
  1082. {
  1083. $sText = substr($sText, 0, 200).'...('.strlen($sText).' chars)...';
  1084. }
  1085. $this->Set('status_last_error', $sText);
  1086. }
  1087. public function Synchro($oDataSource, $aReconciliationKeys, $aAttributes, $oChange, &$oStatLog)
  1088. {
  1089. switch($this->Get('status'))
  1090. {
  1091. case 'new':
  1092. $this->Set('status_dest_creator', false);
  1093. // If needed, construct the query used for the reconciliation
  1094. if (!isset(self::$aSearches[$oDataSource->GetKey()]))
  1095. {
  1096. foreach($aReconciliationKeys as $sFilterCode => $oSyncAtt)
  1097. {
  1098. $aCriterias[] = ($sFilterCode == 'primary_key' ? 'id' : $sFilterCode).' = :'.$sFilterCode;
  1099. }
  1100. $sOQL = "SELECT ".$oDataSource->GetTargetClass()." WHERE ".implode(' AND ', $aCriterias);
  1101. self::$aSearches[$oDataSource->GetKey()] = DBObjectSearch::FromOQL($sOQL);
  1102. }
  1103. // Get the criterias for the search
  1104. $aFilterValues = array();
  1105. foreach($aReconciliationKeys as $sFilterCode => $oSyncAtt)
  1106. {
  1107. $value = $this->GetValueFromExtData($sFilterCode, $oSyncAtt, $oStatLog);
  1108. if (!is_null($value))
  1109. {
  1110. $aFilterValues[$sFilterCode] = $value;
  1111. }
  1112. else
  1113. {
  1114. // Reconciliation could not be performed - log and EXIT
  1115. $oStatLog->AddTrace("Could not reconcile on null value: ".$sFilterCode, $this);
  1116. $this->SetLastError('Could not reconcile on null value: '.$sFilterCode);
  1117. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1118. return;
  1119. }
  1120. }
  1121. $oDestSet = new DBObjectSet(self::$aSearches[$oDataSource->GetKey()], array(), $aFilterValues);
  1122. $iCount = $oDestSet->Count();
  1123. $aConditions = array();
  1124. foreach($aFilterValues as $sCode => $sValue)
  1125. {
  1126. $aConditions[] = $sCode.'='.$sValue;
  1127. }
  1128. $sConditionDesc = implode(' AND ', $aConditions);
  1129. // How many objects match the reconciliation criterias
  1130. switch($iCount)
  1131. {
  1132. case 0:
  1133. $oStatLog->AddTrace("Nothing found on: $sConditionDesc", $this);
  1134. if ($oDataSource->Get('action_on_zero') == 'create')
  1135. {
  1136. $this->CreateObjectFromReplica($oDataSource->GetTargetClass(), $aAttributes, $oChange, $oStatLog);
  1137. }
  1138. else // assumed to be 'error'
  1139. {
  1140. $oStatLog->AddTrace("Failed to reconcile (no match)", $this);
  1141. $this->SetLastError('Could not find a match for reconciliation');
  1142. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1143. }
  1144. break;
  1145. case 1:
  1146. $oStatLog->AddTrace("Found 1 object on: $sConditionDesc", $this);
  1147. if ($oDataSource->Get('action_on_one') == 'update')
  1148. {
  1149. $oDestObj = $oDestSet->Fetch();
  1150. $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj_new', 'stats_nb_replica_reconciled_errors');
  1151. $this->Set('dest_id', $oDestObj->GetKey());
  1152. $this->Set('dest_class', get_class($oDestObj));
  1153. }
  1154. else
  1155. {
  1156. // assumed to be 'error'
  1157. $oStatLog->AddTrace("Failed to reconcile (1 match)", $this);
  1158. $this->SetLastError('Found a match while expecting several');
  1159. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1160. }
  1161. break;
  1162. default:
  1163. $oStatLog->AddTrace("Found $iCount objects on: $sConditionDesc", $this);
  1164. if ($oDataSource->Get('action_on_multiple') == 'error')
  1165. {
  1166. $oStatLog->AddTrace("Failed to reconcile (N>1 matches)", $this);
  1167. $this->SetLastError($iCount.' destination objects match the reconciliation criterias: '.$sConditionDesc);
  1168. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1169. }
  1170. elseif ($oDataSource->Get('action_on_multiple') == 'create')
  1171. {
  1172. $this->CreateObjectFromReplica($oDataSource->GetTargetClass(), $aAttributes, $oChange, $oStatLog);
  1173. }
  1174. else
  1175. {
  1176. // assumed to be 'take_first'
  1177. $oDestObj = $oDestSet->Fetch();
  1178. $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj_new', 'stats_nb_replica_reconciled_errors');
  1179. $this->Set('dest_id', $oDestObj->GetKey());
  1180. $this->Set('dest_class', get_class($oDestObj));
  1181. }
  1182. }
  1183. break;
  1184. case 'modified':
  1185. $oDestObj = MetaModel::GetObject($oDataSource->GetTargetClass(), $this->Get('dest_id'));
  1186. if ($oDestObj == null)
  1187. {
  1188. $this->Set('status', 'orphan'); // The destination object has been deleted !
  1189. $this->SetLastError('Destination object deleted unexpectedly');
  1190. $oStatLog->Inc('stats_nb_obj_updated_errors');
  1191. }
  1192. else
  1193. {
  1194. $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj', 'stats_nb_obj_updated_errors');
  1195. }
  1196. break;
  1197. default: // Do nothing in all other cases
  1198. }
  1199. $this->DBUpdateTracked($oChange);
  1200. }
  1201. /**
  1202. * Updates the destination object with the Extended data found in the synchro_data_XXXX table
  1203. */
  1204. protected function UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, &$oStatLog, $sStatsCode, $sStatsCodeError)
  1205. {
  1206. $aValueTrace = array();
  1207. foreach($aAttributes as $sAttCode => $oSyncAtt)
  1208. {
  1209. $value = $this->GetValueFromExtData($sAttCode, $oSyncAtt, $oStatLog);
  1210. if (!is_null($value))
  1211. {
  1212. $oDestObj->Set($sAttCode, $value);
  1213. $aValueTrace[] = "$sAttCode: $value";
  1214. }
  1215. }
  1216. try
  1217. {
  1218. // Really modified ?
  1219. if ($oDestObj->IsModified())
  1220. {
  1221. $oDestObj->DBUpdateTracked($oChange);
  1222. $oStatLog->AddTrace('Updated object - Values: {'.implode(', ', $aValueTrace).'}', $this);
  1223. $oStatLog->Inc($sStatsCode.'_updated');
  1224. }
  1225. else
  1226. {
  1227. $oStatLog->AddTrace('Unchanged object', $this);
  1228. $oStatLog->Inc($sStatsCode.'_unchanged');
  1229. }
  1230. $this->Set('status_last_error', '');
  1231. $this->Set('status', 'synchronized');
  1232. }
  1233. catch(Exception $e)
  1234. {
  1235. $oStatLog->AddTrace("Failed to update destination object: {$e->getMessage()}", $this);
  1236. $this->SetLastError('Unable to update destination object: ', $e);
  1237. $oStatLog->Inc($sStatsCodeError);
  1238. }
  1239. }
  1240. /**
  1241. * Creates the destination object populating it with the Extended data found in the synchro_data_XXXX table
  1242. */
  1243. protected function CreateObjectFromReplica($sClass, $aAttributes, $oChange, &$oStatLog)
  1244. {
  1245. $oDestObj = MetaModel::NewObject($sClass);
  1246. try
  1247. {
  1248. $aValueTrace = array();
  1249. foreach($aAttributes as $sAttCode => $oSyncAtt)
  1250. {
  1251. $value = $this->GetValueFromExtData($sAttCode, $oSyncAtt, $oStatLog);
  1252. if (!is_null($value))
  1253. {
  1254. $oDestObj->Set($sAttCode, $value);
  1255. $aValueTrace[] = "$sAttCode: $value";
  1256. }
  1257. }
  1258. $iNew = $oDestObj->DBInsertTracked($oChange);
  1259. $this->Set('dest_id', $oDestObj->GetKey());
  1260. $this->Set('dest_class', get_class($oDestObj));
  1261. $this->Set('status_dest_creator', true);
  1262. $this->Set('status_last_error', '');
  1263. $this->Set('status', 'synchronized');
  1264. $oStatLog->AddTrace("Created (".implode(', ', $aValueTrace).")", $this);
  1265. $oStatLog->Inc('stats_nb_obj_created');
  1266. }
  1267. catch(Exception $e)
  1268. {
  1269. $oStatLog->AddTrace("Failed to create $sClass ({$e->getMessage()})", $this);
  1270. $this->SetLastError('Unable to create destination object: ', $e);
  1271. $oStatLog->Inc('stats_nb_obj_created_errors');
  1272. }
  1273. }
  1274. /**
  1275. * Update the destination object with given values
  1276. */
  1277. public function UpdateDestObject($aValues, $oChange, &$oStatLog)
  1278. {
  1279. try
  1280. {
  1281. if ($this->Get('dest_class') == '')
  1282. {
  1283. $this->SetLastError('No destination object to update');
  1284. $oStatLog->Inc('stats_nb_obj_obsoleted_errors');
  1285. }
  1286. else
  1287. {
  1288. $oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'));
  1289. foreach($aValues as $sAttCode => $value)
  1290. {
  1291. $oDestObj->Set($sAttCode, $value);
  1292. }
  1293. $oDestObj->DBUpdateTracked($oChange);
  1294. $oStatLog->AddTrace("Replica marked as obsolete", $this);
  1295. $oStatLog->Inc('stats_nb_obj_obsoleted');
  1296. }
  1297. }
  1298. catch(Exception $e)
  1299. {
  1300. $this->SetLastError('Unable to update the destination object: ', $e);
  1301. $oStatLog->Inc('stats_nb_obj_obsoleted_errors');
  1302. }
  1303. }
  1304. /**
  1305. * Delete the destination object
  1306. */
  1307. public function DeleteDestObject($oChange, &$oStatLog)
  1308. {
  1309. if($this->Get('status_dest_creator'))
  1310. {
  1311. try
  1312. {
  1313. $oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'));
  1314. $oDestObj->DBDeleteTracked($oChange);
  1315. $this->DBDeleteTracked($oChange);
  1316. $oStatLog->Inc('stats_nb_obj_deleted');
  1317. }
  1318. catch(Exception $e)
  1319. {
  1320. $this->SetLastError('Unable to delete the destination object: ', $e);
  1321. $this->Set('status', 'obsolete');
  1322. $this->DBUpdateTracked($oChange);
  1323. $oStatLog->Inc('stats_nb_obj_deleted_errors');
  1324. }
  1325. }
  1326. else
  1327. {
  1328. $this->DBDeleteTracked($oChange);
  1329. $oStatLog->Inc('stats_nb_replica_disappeared_no_action');
  1330. }
  1331. }
  1332. /**
  1333. * Get the value from the 'Extended Data' located in the synchro_data_xxx table for this replica
  1334. */
  1335. protected function GetValueFromExtData($sColumnName, $oSyncAtt, &$oStatLog)
  1336. {
  1337. // $aData should contain attributes defined either for reconciliation or create/update
  1338. $aData = $this->GetExtendedData();
  1339. // In any case, a null column means "ignore this column"
  1340. //
  1341. if (is_null($aData[$sColumnName]))
  1342. {
  1343. return null;
  1344. }
  1345. if (!is_null($oSyncAtt) && ($oSyncAtt instanceof SynchroAttExtKey))
  1346. {
  1347. $sReconcAttCode = $oSyncAtt->Get('reconciliation_attcode');
  1348. if (!empty($sReconcAttCode))
  1349. {
  1350. $oDataSource = MetaModel::GetObject('SynchroDataSource', $this->Get('sync_source_id'));
  1351. $sClass = $oDataSource->GetTargetClass();
  1352. $oAttDef = MetaModel::GetAttributeDef($sClass, $sColumnName);
  1353. $sRemoteClass = $oAttDef->GetTargetClass();
  1354. $oObj = MetaModel::GetObjectByColumn($sRemoteClass, $sReconcAttCode, $aData[$sColumnName], false);
  1355. if ($oObj)
  1356. {
  1357. return $oObj->GetKey();
  1358. }
  1359. else
  1360. {
  1361. // Note: differs from null (in which case the value would be left unchanged)
  1362. $oStatLog->AddTrace("Could not find [unique] object for '$sColumnName': searched on $sReconcAttCode = '$aData[$sColumnName]'", $this);
  1363. return 0;
  1364. }
  1365. }
  1366. }
  1367. return $aData[$sColumnName];
  1368. }
  1369. /**
  1370. * Maps the given context parameter name to the appropriate filter/search code for this class
  1371. * @param string $sContextParam Name of the context parameter, i.e. 'org_id'
  1372. * @return string Filter code, i.e. 'customer_id'
  1373. */
  1374. public static function MapContextParam($sContextParam)
  1375. {
  1376. if ($sContextParam == 'menu')
  1377. {
  1378. return null;
  1379. }
  1380. else
  1381. {
  1382. return $sContextParam;
  1383. }
  1384. }
  1385. /**
  1386. * This function returns a 'hilight' CSS class, used to hilight a given row in a table
  1387. * There are currently (i.e defined in the CSS) 4 possible values HILIGHT_CLASS_CRITICAL,
  1388. * HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  1389. * To Be overridden by derived classes
  1390. * @param void
  1391. * @return String The desired higlight class for the object/row
  1392. */
  1393. public function GetHilightClass()
  1394. {
  1395. // Possible return values are:
  1396. // HILIGHT_CLASS_CRITICAL, HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  1397. return HILIGHT_CLASS_NONE; // Not hilighted by default
  1398. }
  1399. public static function GetUIPage()
  1400. {
  1401. return '../synchro/replica.php';
  1402. }
  1403. function DisplayDetails(WebPage $oPage, $bEditMode = false)
  1404. {
  1405. // Object's details
  1406. //$this->DisplayBareHeader($oPage, $bEditMode);
  1407. $oPage->AddTabContainer(OBJECT_PROPERTIES_TAB);
  1408. $oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB);
  1409. $oPage->SetCurrentTab(Dict::S('UI:PropertiesTab'));
  1410. $this->DisplayBareProperties($oPage, $bEditMode);
  1411. }
  1412. function DisplayBareProperties(WebPage $oPage, $bEditMode = false)
  1413. {
  1414. if ($bEditMode) return; // Not editable
  1415. $oPage->add('<table style="vertical-align:top"><tr style="vertical-align:top"><td>');
  1416. $aDetails = array();
  1417. $sClass = get_class($this);
  1418. $oPage->add('<fieldset>');
  1419. $oPage->add('<legend>'.Dict::S('Core:SynchroReplica:PrivateDetails').'</legend>');
  1420. $aZList = MetaModel::GetZListItems($sClass, 'details');
  1421. foreach( $aZList as $sAttCode)
  1422. {
  1423. $sDisplayValue = $this->GetAsHTML($sAttCode);
  1424. $aDetails[] = array('label' => '<span title="'.MetaModel::GetDescription($sClass, $sAttCode).'">'.MetaModel::GetLabel($sClass, $sAttCode).'</span>', 'value' => $sDisplayValue);
  1425. }
  1426. $oPage->Details($aDetails);
  1427. $oPage->add('</fieldset>');
  1428. $oPage->add('</td><td>');
  1429. $oPage->add('<fieldset>');
  1430. $oPage->add('<legend>'.Dict::S('Core:SynchroReplica:PublicData').'</legend>');
  1431. $oSource = MetaModel::GetObject('SynchroDataSource', $this->Get('sync_source_id'));
  1432. $sSQLTable = $oSource->GetDataTable();
  1433. $sSQL = "SELECT * FROM $sSQLTable WHERE id=".$this->GetKey();
  1434. $rQuery = CMDBSource::Query($sSQL);
  1435. $aData = CMDBSource::FetchArray($rQuery);
  1436. $aHeaders = array('attcode' => array('label' => 'Attribute Code', 'description' => ''),
  1437. 'data' => array('label' => 'Value', 'description' => ''));
  1438. $aRows = array();
  1439. foreach($aData as $sKey => $value)
  1440. {
  1441. $aRows[] = array('attcode' => $sKey, 'data' => $value);
  1442. }
  1443. $oPage->Table($aHeaders, $aRows);
  1444. $oPage->add('</fieldset>');
  1445. $oPage->add('</td></tr></table>');
  1446. }
  1447. }
  1448. // TO DO: finalize.... admins only ? which options ? troubleshoot WebPageMenuNode::__construct(.... sEnableClass...) ?
  1449. //if (UserRights::IsAdministrator())
  1450. {
  1451. $oAdminMenu = new MenuGroup('AdminTools', 80 /* fRank */);
  1452. new OQLMenuNode('DataSources', 'SELECT SynchroDataSource', $oAdminMenu->GetIndex(), 12 /* fRank */, true, 'SynchroDataSource', UR_ACTION_MODIFY, UR_ALLOWED_YES);
  1453. new OQLMenuNode('Replicas', 'SELECT SynchroReplica', $oAdminMenu->GetIndex(), 12 /* fRank */, true, 'SynchroReplica', UR_ACTION_MODIFY, UR_ALLOWED_YES);
  1454. new WebPageMenuNode('Test:RunSynchro', '../synchro/synchro_exec.php', $oAdminMenu->GetIndex(), 13 /* fRank */, 'SynchroDataSource');
  1455. }
  1456. ?>