synchrodatasource.class.inc.php 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  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 AttributeExternalKey("notify_contact_id", array("targetclass"=>"Contact", "jointype"=>null, "allowed_values"=>null, "sql"=>"notify_contact_id", "is_null_allowed"=>true, "on_target_delete"=>DEL_MANUAL, "depends_on"=>array())));
  51. MetaModel::Init_AddAttribute(new AttributeClass("scope_class", array("class_category"=>"bizmodel,addon/authentication", "more_values"=>"", "sql"=>"scope_class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  52. // Declared here for a future usage, but ignored so far
  53. MetaModel::Init_AddAttribute(new AttributeString("scope_restriction", array("allowed_values"=>null, "sql"=>"scope_restriction", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  54. //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())));
  55. // Format: seconds (int)
  56. MetaModel::Init_AddAttribute(new AttributeDuration("full_load_periodicity", array("allowed_values"=>null, "sql"=>"full_load_periodicity", "default_value"=>86400, "is_null_allowed"=>true, "depends_on"=>array())));
  57. // MetaModel::Init_AddAttribute(new AttributeString("reconciliation_list", array("allowed_values"=>null, "sql"=>"reconciliation_list", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  58. 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())));
  59. 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())));
  60. 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())));
  61. 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())));
  62. 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())));
  63. 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())));
  64. // Format: seconds (unsigned int)
  65. 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())));
  66. 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())));
  67. // Not used yet !
  68. MetaModel::Init_AddAttribute(new AttributeEnum("user_delete_policy", array("allowed_values"=>new ValueSetEnum('everybody,administrators,nobody'), "sql"=>"user_delete_policy", "default_value"=>"nobody", "is_null_allowed"=>true, "depends_on"=>array())));
  69. MetaModel::Init_AddAttribute(new AttributeURL("url_icon", array("allowed_values"=>null, "sql"=>"url_icon", "default_value"=>null, "is_null_allowed"=>true, "target"=> '_top', "depends_on"=>array())));
  70. // The field below is not a real URL since it can contain placeholders like $replica->primary_key$ which are not syntactically allowed in a real URL
  71. MetaModel::Init_AddAttribute(new AttributeString("url_application", array("allowed_values"=>null, "sql"=>"url_application", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  72. // Display lists
  73. MetaModel::Init_SetZListItems('details', array(
  74. 'col:0'=> array(
  75. 'fieldset:SynchroDataSource:Description' => array('name','description','status','scope_class','user_id','notify_contact_id','url_icon','url_application')),
  76. 'col:1'=> array(
  77. 'fieldset:SynchroDataSource:Reconciliation' => array('reconciliation_policy','action_on_zero','action_on_one','action_on_multiple'),
  78. 'fieldset:SynchroDataSource:Deletion' => array('user_delete_policy','full_load_periodicity','delete_policy','delete_policy_update','delete_policy_retention'))
  79. )
  80. );
  81. MetaModel::Init_SetZListItems('list', array('scope_class', 'status', 'user_id', 'full_load_periodicity')); // Attributes to be displayed for a list
  82. // Search criteria
  83. MetaModel::Init_SetZListItems('standard_search', array('name', 'status', 'scope_class', 'user_id')); // Criteria of the std search form
  84. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  85. }
  86. public static $m_oCurrentTask = null;
  87. public static function GetCurrentTaskId()
  88. {
  89. if (is_object(self::$m_oCurrentTask))
  90. {
  91. return self::$m_oCurrentTask->GetKey();
  92. }
  93. else
  94. {
  95. return null;
  96. }
  97. }
  98. public function DisplayBareRelations(WebPage $oPage, $bEditMode = false)
  99. {
  100. if (!$this->IsNew())
  101. {
  102. $oPage->SetCurrentTab(Dict::S('Core:SynchroAttributes'));
  103. $oAttributeSet = $this->Get('attribute_list');
  104. $aAttributes = array();
  105. while($oAttribute = $oAttributeSet->Fetch())
  106. {
  107. $aAttributes[$oAttribute->Get('attcode')] = $oAttribute;
  108. }
  109. // Columns of the form
  110. $aAttribs = array();
  111. foreach(array('attcode', 'reconciliation', 'update', 'update_policy', 'reconciliation_attcode') as $s )
  112. {
  113. $aAttribs[$s] = array( 'label' => Dict::S("Core:SynchroAtt:$s"), "description" => Dict::S("Core:SynchroAtt:$s+"));
  114. }
  115. // Rows of the form
  116. $aValues = array();
  117. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  118. {
  119. if ($oAttDef->IsWritable())
  120. {
  121. if (isset($aAttributes[$sAttCode]))
  122. {
  123. $oAttribute = $aAttributes[$sAttCode];
  124. }
  125. else
  126. {
  127. if ($oAttDef->IsExternalKey())
  128. {
  129. $oAttribute = new SynchroAttExtKey();
  130. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  131. }
  132. elseif ($oAttDef->IsLinkSet() && $oAttDef->IsIndirect())
  133. {
  134. $oAttribute = new SynchroAttLinkSet();
  135. // Todo - add these settings into the form
  136. $oAttribute->Set('row_separator', MetaModel::GetConfig()->Get('link_set_item_separator'));
  137. $oAttribute->Set('attribute_separator', MetaModel::GetConfig()->Get('link_set_attribute_separator'));
  138. $oAttribute->Set('value_separator', MetaModel::GetConfig()->Get('link_set_value_separator'));
  139. $oAttribute->Set('attribute_qualifier', MetaModel::GetConfig()->Get('link_set_attribute_qualifier'));
  140. }
  141. elseif ($oAttDef->IsScalar())
  142. {
  143. $oAttribute = new SynchroAttribute();
  144. }
  145. else
  146. {
  147. $oAttribute = null;
  148. }
  149. if (!is_null($oAttribute))
  150. {
  151. $oAttribute->Set('sync_source_id', $this->GetKey());
  152. $oAttribute->Set('attcode', $sAttCode);
  153. $oAttribute->Set('reconcile', MetaModel::IsReconcKey($this->GetTargetClass(), $sAttCode) ? 1 : 0);
  154. $oAttribute->Set('update', 1);
  155. $oAttribute->Set('update_policy', 'master_locked');
  156. }
  157. }
  158. if (!is_null($oAttribute))
  159. {
  160. if (!$bEditMode)
  161. {
  162. // Read-only mode
  163. $aRow['reconciliation'] = $oAttribute->Get('reconcile') == 1 ? Dict::S('Core:SynchroReconcile:Yes') : Dict::S('Core:SynchroReconcile:No');
  164. $aRow['update'] = $oAttribute->Get('update') == 1 ? Dict::S('Core:SynchroUpdate:Yes') : Dict::S('Core:SynchroUpdate:No');
  165. $aRow['attcode'] = MetaModel::GetLabel($this->GetTargetClass(), $oAttribute->Get('attcode'));
  166. $aRow['update_policy'] = $oAttribute->GetAsHTML('update_policy');
  167. if ($oAttDef->IsExternalKey())
  168. {
  169. $aRow['reconciliation_attcode'] = $oAttribute->GetAsHTML('reconciliation_attcode');
  170. }
  171. else
  172. {
  173. $aRow['reconciliation_attcode'] = '&nbsp;';
  174. }
  175. }
  176. else
  177. {
  178. // Edit mode
  179. $sAttCode = $oAttribute->Get('attcode');
  180. $sChecked = $oAttribute->Get('reconcile') == 1 ? 'checked' : '';
  181. $aRow['reconciliation'] = "<input type=\"checkbox\" name=\"reconciliation[$sAttCode]\" $sChecked/>";
  182. $sChecked = $oAttribute->Get('update') == 1 ? 'checked' : '';
  183. $aRow['update'] = "<input type=\"checkbox\" name=\"update[$sAttCode]\" $sChecked/>";
  184. $aRow['attcode'] = MetaModel::GetLabel($this->GetTargetClass(), $oAttribute->Get('attcode'));
  185. $oUpdateAttDef = MetaModel::GetAttributeDef(get_class($oAttribute), 'update_policy');
  186. $aRow['update_policy'] = cmdbAbstractObject::GetFormElementForField($oPage, get_class($oAttribute), 'update_policy', $oUpdateAttDef, $oAttribute->Get('update_policy'), '', 'update_policy_'.$sAttCode, "[$sAttCode]");
  187. if ($oAttDef->IsExternalKey())
  188. {
  189. $aRow['reconciliation_attcode'] = $oAttribute->GetReconciliationFormElement($oAttDef->GetTargetClass(), "attr_reconciliation_attcode[$sAttCode]");
  190. }
  191. else
  192. {
  193. $aRow['reconciliation_attcode'] = '&nbsp;';
  194. }
  195. }
  196. $aValues[] = $aRow;
  197. }
  198. }
  199. }
  200. $oPage->p(Dict::Format('Class:SynchroDataSource:DataTable', $this->GetDataTable()));
  201. $oPage->Table($aAttribs, $aValues);
  202. $this->DisplayStatusTab($oPage);
  203. }
  204. parent::DisplayBareRelations($oPage, $bEditMode);
  205. }
  206. /**
  207. * Displays the status (SynchroLog) of the datasource in a graphical manner
  208. * @param $oPage WebPage
  209. * @return void
  210. */
  211. protected function DisplayStatusTab(WebPage $oPage)
  212. {
  213. $oPage->SetCurrentTab(Dict::S('Core:SynchroStatus'));
  214. $sSelectSynchroLog = 'SELECT SynchroLog WHERE sync_source_id = :source_id';
  215. $oSetSynchroLog = new CMDBObjectSet(DBObjectSearch::FromOQL($sSelectSynchroLog), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()));
  216. if ($oSetSynchroLog->Count() > 0)
  217. {
  218. $oLastLog = $oSetSynchroLog->Fetch();
  219. $sStartDate = $oLastLog->Get('start_date');
  220. $oLastLog->Get('stats_nb_replica_seen');
  221. $iLastLog = 0;
  222. $iDSid = $this->GetKey();
  223. if ($oLastLog->Get('status') == 'running')
  224. {
  225. // Still running !
  226. $oPage->p('<h2>'.Dict::Format('Core:Synchro:SynchroRunningStartedOn_Date', $sStartDate).'</h2>');
  227. }
  228. else
  229. {
  230. $sEndDate = $oLastLog->Get('end_date');
  231. $iLastLog = $oLastLog->GetKey();
  232. $oPage->p('<h2>'.Dict::Format('Core:Synchro:SynchroEndedOn_Date', $sEndDate).'</h2>');
  233. }
  234. $oPage->add('<table class="synoptics"><tr><td style="color:#333;vertical-align:top">');
  235. // List all the log entries for the user to select
  236. $oPage->add('<h2 style="line-height:55px;">'.Dict::S('Core:Synchro:History').'</h2>');
  237. $oSetSynchroLog->Rewind();
  238. $oPage->add('<select size="25" onChange="UpdateSynoptics(this.value);">');
  239. $sSelected = ' selected'; // First log is selected by default
  240. $sScript = "var aSynchroLog = {\n";
  241. while($oLog = $oSetSynchroLog->Fetch())
  242. {
  243. $sLogTitle = Dict::Format('Core:SynchroLogTitle', $oLog->Get('status'), $oLog->Get('start_date'));
  244. $oPage->add('<option value="'.$oLog->GetKey().'"'.$sSelected.'>'.$sLogTitle.'</option>');
  245. $sSelected = ''; // only the first log is selected by default
  246. $aData = $this->ProcessLog($oLog);
  247. $sScript .= '"'.$oLog->GetKey().'": '.json_encode($aData).",\n";
  248. }
  249. $sScript .= "end: 'Done'";
  250. $sScript .= "};\n";
  251. $sScript .= <<<EOF
  252. var sLastLog = '$iLastLog';
  253. function UpdateSynoptics(id)
  254. {
  255. var aValues = aSynchroLog[id];
  256. for (var sKey in aValues)
  257. {
  258. $('#c_'+sKey).html(aValues[sKey]);
  259. var fOpacity = (aValues[sKey] == 0) ? 0.3 : 1;
  260. $('#'+sKey).fadeTo("slow", fOpacity);
  261. }
  262. //alert('id = '+id+', lastLog='+sLastLog+', id==sLastLog: '+(id==sLastLog)+' obj_updated_errors: '+aValues['obj_updated_errors']);
  263. if ( (id == sLastLog) && (aValues['obj_new_errors'] > 0) )
  264. {
  265. $('#new_errors_link').show();
  266. }
  267. else
  268. {
  269. $('#new_errors_link').hide();
  270. }
  271. if ( (id == sLastLog) && (aValues['obj_updated_errors'] > 0) )
  272. {
  273. $('#updated_errors_link').show();
  274. }
  275. else
  276. {
  277. $('#updated_errors_link').hide();
  278. }
  279. if ( (id == sLastLog) && (aValues['obj_disappeared_errors'] > 0) )
  280. {
  281. $('#disappeared_errors_link').show();
  282. }
  283. else
  284. {
  285. $('#disappeared_errors_link').hide();
  286. }
  287. }
  288. EOF
  289. ;
  290. $oPage->add_script($sScript);
  291. $oPage->add('</select>');
  292. $oPage->add('</td><td style="vertical-align:top;">');
  293. // Now build the big "synoptics" view
  294. $aData = $this->ProcessLog($oLastLog);
  295. $sNbReplica = $this->GetIcon()."&nbsp;".Dict::Format('Core:Synchro:Nb_Replica', "<span id=\"c_nb_replica_total\">{$aData['nb_replica_total']}</span>");
  296. $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>");
  297. $oPage->add(
  298. <<<EOF
  299. <table class="synoptics">
  300. <tr class="synoptics_header">
  301. <td>$sNbReplica</td><td>&nbsp;</td><td>$sNbObjects</td>
  302. </tr>
  303. <tr>
  304. EOF
  305. );
  306. $sBaseOQL = "SELECT SynchroReplica WHERE sync_source_id=".$this->GetKey()." AND status_last_error!=''";
  307. $oPage->add($this->HtmlBox('repl_ignored', $aData, '#999').'<td colspan="2">&nbsp;</td>');
  308. $oPage->add("</tr>\n<tr>");
  309. $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'));
  310. $oPage->add("</tr>\n<tr>");
  311. $oPage->add($this->HtmlBox('obj_deleted', $aData, '#000'));
  312. $oPage->add("</tr>\n<tr>");
  313. $oPage->add($this->HtmlBox('obj_obsoleted', $aData, '#630'));
  314. $oPage->add("</tr>\n<tr>");
  315. $sOQL = urlencode($sBaseOQL." AND status='obsolete'");
  316. $oPage->add($this->HtmlBox('obj_disappeared_errors', $aData, '#C00', '', " <a style=\"color:#fff\" href=\"../synchro/replica.php?operation=oql&datasource=$iDSid&oql=$sOQL\" id=\"disappeared_errors_link\">Show</a>"));
  317. $oPage->add("</tr>\n<tr>");
  318. $oPage->add($this->HtmlBox('repl_existing', $aData, '#093', 'rowspan="3"').'<td rowspan="3" class="arrow">=&gt;</td>'.$this->HtmlBox('obj_unchanged', $aData, '#393'));
  319. $oPage->add("</tr>\n<tr>");
  320. $oPage->add($this->HtmlBox('obj_updated', $aData, '#3C3'));
  321. $oPage->add("</tr>\n<tr>");
  322. $sOQL = urlencode($sBaseOQL." AND status='modified'");
  323. $oPage->add($this->HtmlBox('obj_updated_errors', $aData, '#C00', '', " <a style=\"color:#fff\" href=\"../synchro/replica.php?operation=oql&datasource=$iDSid&oql=$sOQL\" id=\"updated_errors_link\">Show</a>"));
  324. $oPage->add("</tr>\n<tr>");
  325. $oPage->add($this->HtmlBox('repl_new', $aData, '#339', 'rowspan="4"').'<td rowspan="4" class="arrow">=&gt;</td>'.$this->HtmlBox('obj_new_unchanged', $aData, '#393'));
  326. $oPage->add("</tr>\n<tr>");
  327. $oPage->add($this->HtmlBox('obj_new_updated', $aData, '#3C3'));
  328. $oPage->add("</tr>\n<tr>");
  329. $oPage->add($this->HtmlBox('obj_created', $aData, '#339'));
  330. $oPage->add("</tr>\n<tr>");
  331. $sOQL = urlencode($sBaseOQL." AND status='new'");
  332. $oPage->add($this->HtmlBox('obj_new_errors', $aData, '#C00', '', " <a style=\"color:#fff\" href=\"../synchro/replica.php?operation=oql&datasource=$iDSid&oql=$sOQL\" id=\"new_errors_link\">Show</a>"));
  333. $oPage->add("</tr>\n</table>\n");
  334. $oPage->add('</td></tr></table>');
  335. $oPage->add_ready_script("UpdateSynoptics('$iLastLog')");
  336. }
  337. else
  338. {
  339. $oPage->p('<h2>'.Dict::S('Core:Synchro:NeverRun').'</h2>');
  340. }
  341. }
  342. protected function HtmlBox($sId, $aData, $sColor, $sHTMLAttribs = '', $sErrorLink = '')
  343. {
  344. $iCount = $aData[$sId];
  345. $sCount = "<span id=\"c_{$sId}\">$iCount</span>";
  346. $sLabel = Dict::Format('Core:Synchro:label_'.$sId, $sCount);
  347. $sOpacity = ($iCount==0) ? "opacity:0.3;" : "";
  348. return "<td id=\"$sId\" style=\"background-color:$sColor;$sOpacity;\" {$sHTMLAttribs}>{$sLabel}{$sErrorLink}</td>";
  349. }
  350. protected function ProcessLog($oLastLog)
  351. {
  352. $aData = array(
  353. 'obj_deleted' => $oLastLog->Get('stats_nb_obj_deleted'),
  354. 'obj_obsoleted' => $oLastLog->Get('stats_nb_obj_obsoleted'),
  355. 'obj_disappeared_errors' => $oLastLog->Get('stats_nb_obj_obsoleted_errors') + $oLastLog->Get('stats_nb_obj_deleted_errors'),
  356. 'obj_disappeared_no_action' => $oLastLog->Get('stats_nb_replica_disappeared_no_action'),
  357. 'obj_updated' => $oLastLog->Get('stats_nb_obj_updated'),
  358. 'obj_updated_errors' => $oLastLog->Get('stats_nb_obj_updated_errors'),
  359. 'obj_new_updated' => $oLastLog->Get('stats_nb_obj_new_updated'),
  360. 'obj_new_unchanged' => $oLastLog->Get('stats_nb_obj_new_unchanged'),
  361. 'obj_created' => $oLastLog->Get('stats_nb_obj_created'),
  362. 'obj_created_errors' => $oLastLog->Get('stats_nb_obj_created_errors'),
  363. );
  364. $iReconciledErrors = $oLastLog->Get('stats_nb_replica_reconciled_errors');
  365. $iDisappeared = $aData['obj_disappeared_errors'] + $aData['obj_obsoleted'] + $aData['obj_deleted'] + $aData['obj_disappeared_no_action'];
  366. $aData['repl_disappeared'] = $iDisappeared;
  367. $iNewErrors = $aData['obj_created_errors'] + $oLastLog->Get('stats_nb_replica_reconciled_errors');
  368. $aData['obj_new_errors'] = $iNewErrors;
  369. $iNew = $aData['obj_created'] + $iNewErrors + $aData['obj_new_updated'] + $aData['obj_new_unchanged'];
  370. $aData['repl_new'] = $iNew;
  371. $iExisting = $oLastLog->Get('stats_nb_replica_seen') - $iNew;
  372. $aData['repl_existing'] = $iExisting;
  373. $aData['obj_unchanged'] = $iExisting - $aData['obj_updated'] - $aData['obj_updated_errors'];
  374. $iIgnored = $oLastLog->Get('stats_nb_replica_total') - $iNew - $iExisting - $iDisappeared;
  375. $aData['repl_ignored'] = $iIgnored;
  376. $aData['nb_obj_total'] = $iNew + $iExisting + $iDisappeared;
  377. $aData['nb_replica_total'] = $aData['nb_obj_total'] + $iIgnored;
  378. return $aData;
  379. }
  380. public function GetIcon($bImgTag = true, $sMoreStyles = '')
  381. {
  382. if ($this->Get('url_icon') == '') return MetaModel::GetClassIcon(get_class($this), $bImgTag);
  383. if ($bImgTag)
  384. {
  385. return "<img src=\"".$this->Get('url_icon')."\" style=\"vertical-align:middle;$sMoreStyles\"/>";
  386. }
  387. return $this->Get('url_icon');
  388. }
  389. /**
  390. * Get the actual hyperlink to the remote application for the given replica and dest object
  391. */
  392. public function GetApplicationUrl(DBObject $oDestObj, SynchroReplica $oReplica)
  393. {
  394. if ($this->Get('url_application') == '') return '';
  395. $aSearches = array();
  396. $aReplacements = array();
  397. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  398. {
  399. if ($oAttDef->IsScalar())
  400. {
  401. $aSearches[] = '$this->'.$sAttCode.'$';
  402. $aReplacements[] = $oDestObj->Get($sAttCode);
  403. }
  404. }
  405. $aData = $oReplica->LoadExtendedDataFromTable($this->GetDataTable());
  406. foreach($aData as $sColumn => $value)
  407. {
  408. $aSearches[] = '$replica->'.$sColumn.'$';
  409. $aReplacements[] = $value;
  410. }
  411. return str_replace($aSearches, $aReplacements, $this->Get('url_application'));
  412. }
  413. public function GetAttributeFlags($sAttCode, &$aReasons = array())
  414. {
  415. if (($sAttCode == 'scope_class') && (!$this->IsNew()))
  416. {
  417. return OPT_ATT_READONLY;
  418. }
  419. return parent::GetAttributeFlags($sAttCode, $aReasons);
  420. }
  421. public function UpdateObjectFromPostedForm($sFormPrefix = '', $sAttList = null)
  422. {
  423. parent::UpdateObjectFromPostedForm($sFormPrefix, $sAttList);
  424. // And now read the other post parameters...
  425. $oAttributeSet = $this->Get('attribute_list');
  426. $aAttributes = array();
  427. while($oAttribute = $oAttributeSet->Fetch())
  428. {
  429. $aAttributes[$oAttribute->Get('attcode')] = $oAttribute;
  430. }
  431. $aReconcile = utils::ReadPostedParam('reconciliation', array());
  432. $aUpdate = utils::ReadPostedParam('update', array());
  433. $aUpdatePolicy = utils::ReadPostedParam('attr_update_policy', array());
  434. $aReconciliation = utils::ReadPostedParam('attr_reconciliation_attcode', array());
  435. // update_policy cannot be empty, so there is one entry per attribute, use this to iterate
  436. // through all the writable attributes
  437. foreach($aUpdatePolicy as $sAttCode => $sValue)
  438. {
  439. if(!isset($aAttributes[$sAttCode]))
  440. {
  441. $oAttribute = $this->CreateSynchroAtt($sAttCode);
  442. }
  443. else
  444. {
  445. $oAttribute = $aAttributes[$sAttCode];
  446. }
  447. $bReconcile = 0;
  448. if (isset($aReconcile[$sAttCode]))
  449. {
  450. $bReconcile = $aReconcile[$sAttCode] == 'on' ? 1 : 0;
  451. }
  452. $bUpdate = 0 ; // Default / initial value
  453. if (isset($aUpdate[$sAttCode]))
  454. {
  455. $bUpdate = $aUpdate[$sAttCode] == 'on' ? 1 : 0;
  456. }
  457. $oAttribute->Set('reconcile', $bReconcile);
  458. $oAttribute->Set('update', $bUpdate);
  459. $oAttribute->Set('update_policy', $sValue);
  460. if ($oAttribute instanceof SynchroAttExtKey)
  461. {
  462. $oAttribute->Set('reconciliation_attcode', $aReconciliation[$sAttCode]);
  463. }
  464. elseif ($oAttribute instanceof SynchroAttLinkSet)
  465. {
  466. }
  467. $oAttributeSet->AddObject($oAttribute);
  468. }
  469. $this->Set('attribute_list', $oAttributeSet);
  470. }
  471. /**
  472. * Creates a new SynchroAttXXX object in memory with the default values
  473. */
  474. protected function CreateSynchroAtt($sAttCode)
  475. {
  476. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  477. if ($oAttDef->IsExternalKey())
  478. {
  479. $oAttribute = new SynchroAttExtKey();
  480. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  481. }
  482. elseif ($oAttDef->IsLinkSet() && $oAttDef->IsIndirect())
  483. {
  484. $oAttribute = new SynchroAttLinkSet();
  485. // Todo - set those value from the form
  486. $oAttribute->Set('row_separator', MetaModel::GetConfig()->Get('link_set_item_separator'));
  487. $oAttribute->Set('attribute_separator', MetaModel::GetConfig()->Get('link_set_attribute_separator'));
  488. $oAttribute->Set('value_separator', MetaModel::GetConfig()->Get('link_set_value_separator'));
  489. $oAttribute->Set('attribute_qualifier', MetaModel::GetConfig()->Get('link_set_attribute_qualifier'));
  490. }
  491. else
  492. {
  493. $oAttribute = new SynchroAttribute();
  494. }
  495. $oAttribute->Set('sync_source_id', $this->GetKey());
  496. $oAttribute->Set('attcode', $sAttCode);
  497. $oAttribute->Set('reconcile', 0);
  498. $oAttribute->Set('update', 0);
  499. $oAttribute->Set('update_policy', 'master_locked');
  500. return $oAttribute;
  501. }
  502. /**
  503. * Overload the standard behavior
  504. */
  505. public function ComputeValues()
  506. {
  507. parent::ComputeValues();
  508. if ($this->IsNew())
  509. {
  510. // When inserting a new datasource object, also create the SynchroAttribute objects
  511. // for each field of the target class
  512. // Create all the SynchroAttribute records
  513. $oAttributeSet = $this->Get('attribute_list');
  514. if ($oAttributeSet->Count() == 0)
  515. {
  516. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  517. {
  518. if ($oAttDef->IsWritable())
  519. {
  520. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  521. if ($oAttDef->IsExternalKey())
  522. {
  523. $oAttribute = new SynchroAttExtKey();
  524. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  525. }
  526. elseif ($oAttDef->IsLinkSet() && $oAttDef->IsIndirect())
  527. {
  528. $oAttribute = new SynchroAttLinkSet();
  529. // Todo - set those value from the form
  530. $oAttribute->Set('row_separator', MetaModel::GetConfig()->Get('link_set_item_separator'));
  531. $oAttribute->Set('attribute_separator', MetaModel::GetConfig()->Get('link_set_attribute_separator'));
  532. $oAttribute->Set('value_separator', MetaModel::GetConfig()->Get('link_set_value_separator'));
  533. $oAttribute->Set('attribute_qualifier', MetaModel::GetConfig()->Get('link_set_attribute_qualifier'));
  534. }
  535. elseif ($oAttDef->IsScalar())
  536. {
  537. $oAttribute = new SynchroAttribute();
  538. }
  539. else
  540. {
  541. $oAttribute = null;
  542. }
  543. if (!is_null($oAttribute))
  544. {
  545. $oAttribute->Set('sync_source_id', $this->GetKey());
  546. $oAttribute->Set('attcode', $sAttCode);
  547. $oAttribute->Set('reconcile', MetaModel::IsReconcKey($this->GetTargetClass(), $sAttCode) ? 1 : 0);
  548. $oAttribute->Set('update', 1);
  549. $oAttribute->Set('update_policy', 'master_locked');
  550. $oAttributeSet->AddObject($oAttribute);
  551. }
  552. }
  553. }
  554. $this->Set('attribute_list', $oAttributeSet);
  555. }
  556. }
  557. }
  558. public function DoCheckToWrite()
  559. {
  560. parent::DoCheckToWrite();
  561. // Check that there is at least one reconciliation key defined
  562. if ($this->Get('reconciliation_policy') == 'use_attributes')
  563. {
  564. $oSet = $this->Get('attribute_list');
  565. $oSynchroAttributeList = $oSet->ToArray();
  566. $bReconciliationKey = false;
  567. foreach($oSynchroAttributeList as $oSynchroAttribute)
  568. {
  569. if ($oSynchroAttribute->Get('reconcile') == 1)
  570. {
  571. $bReconciliationKey = true; // At least one key is defined
  572. break;
  573. }
  574. }
  575. if (!$bReconciliationKey)
  576. {
  577. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:AtLeastOneReconciliationKeyMustBeSpecified');
  578. }
  579. }
  580. // If 'update_then_delete' is specified there must be a delete_retention_period
  581. if (($this->Get('delete_policy') == 'update_then_delete') && ($this->Get('delete_policy_retention') == 0))
  582. {
  583. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:DeleteRetentionDurationMustBeSpecified');
  584. }
  585. // If update is specified, then something to update must be defined
  586. if ((($this->Get('delete_policy') == 'update_then_delete') || ($this->Get('delete_policy') == 'update'))
  587. && ($this->Get('delete_policy_update') == ''))
  588. {
  589. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:DeletePolicyUpdateMustBeSpecified');
  590. }
  591. }
  592. public function GetTargetClass()
  593. {
  594. return $this->Get('scope_class');
  595. }
  596. public function GetDataTable()
  597. {
  598. $sName = strtolower($this->GetTargetClass());
  599. $sName = str_replace('\'"&@|\\/ ', '_', $sName); // Remove forbidden characters from the table name
  600. $sName .= '_'.$this->GetKey(); // Add a suffix for unicity
  601. $sTable = MetaModel::GetConfig()->GetDBSubName()."synchro_data_$sName"; // Add the prefix if any
  602. return $sTable;
  603. }
  604. /**
  605. * When the new datasource has been created, let's create the synchro_data table
  606. * that will hold the data records and the correspoding triggers which will maintain
  607. * both tables in sync
  608. */
  609. protected function AfterInsert()
  610. {
  611. parent::AfterInsert();
  612. $sTable = $this->GetDataTable();
  613. $sReplicaTable = MetaModel::DBGetTable('SynchroReplica');
  614. $aColumns = $this->GetSQLColumns();
  615. $aFieldDefs = array();
  616. // Allow '0', otherwise mysql will render an error when the id is not given
  617. // (the trigger is expected to set the value, but it is not executed soon enough)
  618. $aFieldDefs[] = "id INTEGER(11) NOT NULL DEFAULT 0 ";
  619. $aFieldDefs[] = "`primary_key` VARCHAR(255) NULL DEFAULT NULL";
  620. foreach($aColumns as $sColumn => $ColSpec)
  621. {
  622. $aFieldDefs[] = "`$sColumn` $ColSpec NULL DEFAULT NULL";
  623. }
  624. $aFieldDefs[] = "INDEX (id)";
  625. $aFieldDefs[] = "INDEX (primary_key)";
  626. $sFieldDefs = implode(', ', $aFieldDefs);
  627. $sCreateTable = "CREATE TABLE `$sTable` ($sFieldDefs) ENGINE = innodb;";
  628. CMDBSource::Query($sCreateTable);
  629. $aTriggers = $this->GetTriggersDefinition();
  630. foreach($aTriggers as $key => $sTriggerSQL)
  631. {
  632. CMDBSource::Query($sTriggerSQL);
  633. }
  634. }
  635. /**
  636. * Gets the definitions of the 3 triggers: before insert, before update and after delete
  637. * @return array An array with 3 entries, one for each of the SQL queries
  638. */
  639. protected function GetTriggersDefinition()
  640. {
  641. $sTable = $this->GetDataTable();
  642. $sReplicaTable = MetaModel::DBGetTable('SynchroReplica');
  643. $aColumns = $this->GetSQLColumns();
  644. $aResult = array();
  645. $sTriggerInsert = "CREATE TRIGGER `{$sTable}_bi` BEFORE INSERT ON $sTable";
  646. $sTriggerInsert .= " FOR EACH ROW";
  647. $sTriggerInsert .= " BEGIN";
  648. $sTriggerInsert .= " INSERT INTO {$sReplicaTable} (sync_source_id, status_last_seen, `status`) VALUES ({$this->GetKey()}, NOW(), 'new');";
  649. $sTriggerInsert .= " SET NEW.id = LAST_INSERT_ID();";
  650. $sTriggerInsert .= " END;";
  651. $aResult['bi'] = $sTriggerInsert;
  652. $aModified = array();
  653. foreach($aColumns as $sColumn => $ColSpec)
  654. {
  655. // <=> is a null-safe 'EQUALS' operator (there is no equivalent for "DIFFERS FROM")
  656. $aModified[] = "NOT(NEW.`$sColumn` <=> OLD.`$sColumn`)";
  657. }
  658. $sIsModified = '('.implode(') OR (', $aModified).')';
  659. // Update the replica
  660. //
  661. // status is forced to "new" if the replica was obsoleted directly from the state "new" (dest_id = null)
  662. // otherwise, if status was either 'obsolete' or 'synchronized' it is turned into 'modified' or 'synchronized' depending on the changes
  663. // otherwise, the status is left as is
  664. $sTriggerUpdate = "CREATE TRIGGER `{$sTable}_bu` BEFORE UPDATE ON $sTable";
  665. $sTriggerUpdate .= " FOR EACH ROW";
  666. $sTriggerUpdate .= " BEGIN";
  667. $sTriggerUpdate .= " IF @itopuser is null THEN";
  668. $sTriggerUpdate .= " UPDATE {$sReplicaTable} 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;";
  669. $sTriggerUpdate .= " SET NEW.id = OLD.id;"; // make sure this id won't change
  670. $sTriggerUpdate .= " END IF;";
  671. $sTriggerUpdate .= " END;";
  672. $aResult['bu'] = $sTriggerUpdate;
  673. $sTriggerDelete = "CREATE TRIGGER `{$sTable}_ad` AFTER DELETE ON $sTable";
  674. $sTriggerDelete .= " FOR EACH ROW";
  675. $sTriggerDelete .= " BEGIN";
  676. $sTriggerDelete .= " DELETE FROM {$sReplicaTable} WHERE id = OLD.id;";
  677. $sTriggerDelete .= " END;";
  678. $aResult['ad'] = $sTriggerDelete;
  679. return $aResult;
  680. }
  681. protected function AfterDelete()
  682. {
  683. parent::AfterDelete();
  684. $sTable = $this->GetDataTable();
  685. $sDropTable = "DROP TABLE `$sTable`";
  686. CMDBSource::Query($sDropTable);
  687. // TO DO - check that triggers get dropped with the table
  688. }
  689. /**
  690. * Checks if the data source definition is consistent with the schema of the target class
  691. * @param $bDiagnostics boolean True to only diagnose the consistency, false to actually apply some changes
  692. * @param $bVerbose boolean True to get some information in the std output (echo)
  693. * @return bool Whether or not the database needs fixing for this data source
  694. */
  695. public function CheckDBConsistency($bDiagnostics, $bVerbose, $oChange = null)
  696. {
  697. $bFixNeeded = false;
  698. $aMissingFields = array();
  699. $oAttributeSet = $this->Get('attribute_list');
  700. $aAttributes = array();
  701. while($oAttribute = $oAttributeSet->Fetch())
  702. {
  703. $aAttributes[$oAttribute->Get('attcode')] = $oAttribute;
  704. }
  705. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  706. {
  707. if ($oAttDef->IsWritable())
  708. {
  709. if (!isset($aAttributes[$sAttCode]))
  710. {
  711. $bFixNeeded = true;
  712. $aMissingFields[] = $sAttCode;
  713. // New field missing...
  714. if ($bDiagnostics)
  715. {
  716. // Report the issue
  717. if ($bVerbose)
  718. {
  719. echo "Missing field description for the field '$sAttCode', for the data synchro task ".$this->GetName()." (".$this->GetKey()."), will be created with default values.\n";
  720. }
  721. }
  722. else
  723. {
  724. if ($oChange == null)
  725. {
  726. $oChange = MetaModel::NewObject("CMDBChange");
  727. $oChange->Set("date", time());
  728. $sUserString = CMDBChange::GetCurrentUserName();
  729. $oChange->Set("userinfo", $sUserString);
  730. $oChange->DBInsert();
  731. }
  732. // Fix the issue
  733. $oAttribute = $this->CreateSynchroAtt($sAttCode);
  734. $oAttribute->DBInsertTracked($oChange);
  735. }
  736. }
  737. }
  738. }
  739. $sTable = $this->GetDataTable();
  740. if ($bFixNeeded)
  741. {
  742. // The structure of the table needs adjusting
  743. $aColumns = $this->GetSQLColumns($aMissingFields);
  744. $aFieldDefs = array();
  745. foreach($aColumns as $sAttCode => $sColumnDef)
  746. {
  747. $aFieldDefs[] = "$sAttCode $sColumnDef";
  748. }
  749. $sAlterTable = "ALTER TABLE `$sTable` ADD (".implode(',', $aFieldDefs).");";
  750. // The triggers as well must be adjusted
  751. $aTriggers = array();
  752. $aTriggersDefs = $this->GetTriggersDefinition();
  753. $aTriggers[] = "DROP TRIGGER IF EXISTS {$sTable}_bi;";
  754. $aTriggers[] = $aTriggersDefs['bi'];
  755. $aTriggers[] = "DROP TRIGGER IF EXISTS {$sTable}_bu;";
  756. $aTriggers[] = $aTriggersDefs['bu'];
  757. $aTriggers[] = "DROP TRIGGER IF EXISTS {$sTable}_ad;";
  758. $aTriggers[] = $aTriggersDefs['ad'];
  759. if ($bDiagnostics)
  760. {
  761. if ($bVerbose)
  762. {
  763. // Report the issue
  764. echo "The structure of the table $sTable for the data synchro task ".$this->GetName()." (".$this->GetKey().") must be altered (missing fields: ".implode(',', $aMissingFields).").\n";
  765. echo "$sAlterTable\n";
  766. echo "The trigger {$sTable}_bi, {$sTable}_bu, {$sTable}_ad for the data synchro task ".$this->GetName()." (".$this->GetKey().") must be re-created.\n";
  767. echo implode("\n", $aTriggers)."\n";
  768. }
  769. }
  770. else
  771. {
  772. // Fix the issue
  773. CMDBSource::Query($sAlterTable);
  774. if ($bVerbose)
  775. {
  776. echo "$sAlterTable\n";
  777. }
  778. foreach($aTriggers as $sSQL)
  779. {
  780. CMDBSource::Query($sSQL);
  781. if ($bVerbose)
  782. {
  783. echo "$sSQL\n";
  784. }
  785. }
  786. }
  787. }
  788. return $bFixNeeded;
  789. }
  790. protected function SendNotification($sSubject, $sBody)
  791. {
  792. $iContact = $this->Get('notify_contact_id');
  793. if ($iContact == 0)
  794. {
  795. // Leave silently...
  796. return;
  797. }
  798. $oContact = MetaModel::GetObject('Contact', $iContact);
  799. // Determine the email attribute (the first one will be our choice)
  800. $sEmailAttCode = null;
  801. foreach (MetaModel::ListAttributeDefs(get_class($oContact)) as $sAttCode => $oAttDef)
  802. {
  803. if ($oAttDef instanceof AttributeEmailAddress)
  804. {
  805. $sEmailAttCode = $sAttCode;
  806. // we've got one, exit the loop
  807. break;
  808. }
  809. }
  810. if (is_null($sEmailAttCode))
  811. {
  812. // Leave silently...
  813. return;
  814. }
  815. $sTo = $oContact->Get($sEmailAttCode);
  816. $sFrom = $sTo;
  817. $sBody = '<p>Data synchronization: '.$this->GetHyperlink().'</p>'.$sBody;
  818. $sSubject = 'iTop Data Sync - '.$this->GetName().' - '.$sSubject;
  819. $oEmail = new Email();
  820. $oEmail->SetRecipientTO($sTo);
  821. $oEmail->SetRecipientFrom($sFrom);
  822. $oEmail->SetSubject($sSubject);
  823. $oEmail->SetBody($sBody);
  824. if ($oEmail->Send($aIssues) == EMAIL_SEND_ERROR)
  825. {
  826. // mmmm, what can I do?
  827. }
  828. }
  829. /**
  830. * Perform a synchronization between the data stored in the replicas (&synchro_data_xxx_xx table)
  831. * and the iTop objects. If the lastFullLoadStartDate is NOT specified then the full_load_periodicity
  832. * is used to determine which records are obsolete.
  833. * @param Hash $aTraces Debugs/Trace information, one or more entries per replica
  834. * @param DateTime $oLastFullLoadStartDate Date of the last full load (start date/time), if known
  835. * @return void
  836. */
  837. public function Synchronize($oLastFullLoadStartDate = null)
  838. {
  839. // Create a change used for logging all the modifications/creations happening during the synchro
  840. $oMyChange = MetaModel::NewObject("CMDBChange");
  841. $oMyChange->Set("date", time());
  842. $sUserString = CMDBChange::GetCurrentUserName();
  843. $oMyChange->Set("userinfo", $sUserString.' '.Dict::S('Core:SyncDataExchangeComment'));
  844. $iChangeId = $oMyChange->DBInsert();
  845. // Start logging this execution (stats + protection against reentrance)
  846. //
  847. $oStatLog = new SynchroLog();
  848. $oStatLog->Set('sync_source_id', $this->GetKey());
  849. $oStatLog->Set('start_date', time());
  850. $oStatLog->Set('status', 'running');
  851. $oStatLog->Set('stats_nb_replica_seen', 0);
  852. $oStatLog->Set('stats_nb_replica_total', 0);
  853. $oStatLog->Set('stats_nb_obj_deleted', 0);
  854. $oStatLog->Set('stats_nb_obj_deleted_errors', 0);
  855. $oStatLog->Set('stats_nb_obj_obsoleted', 0);
  856. $oStatLog->Set('stats_nb_obj_obsoleted_errors', 0);
  857. $oStatLog->Set('stats_nb_obj_created', 0);
  858. $oStatLog->Set('stats_nb_obj_created_errors', 0);
  859. $oStatLog->Set('stats_nb_obj_updated', 0);
  860. $oStatLog->Set('stats_nb_obj_updated_errors', 0);
  861. // $oStatLog->Set('stats_nb_replica_reconciled', 0);
  862. $oStatLog->Set('stats_nb_replica_reconciled_errors', 0);
  863. $oStatLog->Set('stats_nb_replica_disappeared_no_action', 0);
  864. $oStatLog->Set('stats_nb_obj_new_updated', 0);
  865. $oStatLog->Set('stats_nb_obj_new_unchanged',0);
  866. $sSelectTotal = "SELECT SynchroReplica WHERE sync_source_id = :source_id";
  867. $oSetTotal = new DBObjectSet(DBObjectSearch::FromOQL($sSelectTotal), array() /* order by*/, array('source_id' => $this->GetKey()));
  868. $oStatLog->Set('stats_nb_replica_total', $oSetTotal->Count());
  869. $oStatLog->DBInsertTracked($oMyChange);
  870. self::$m_oCurrentTask = $this;
  871. try
  872. {
  873. $this->DoSynchronize($oLastFullLoadStartDate, $oMyChange, $oStatLog);
  874. $oStatLog->Set('end_date', time());
  875. $oStatLog->Set('status', 'completed');
  876. $oStatLog->DBUpdateTracked($oMyChange);
  877. $iErrors = $oStatLog->GetErrorCount();
  878. if ($iErrors > 0)
  879. {
  880. $sIssuesOQL = "SELECT SynchroReplica WHERE sync_source_id=".$this->GetKey()." AND status_last_error!=''";
  881. $sAbsoluteUrl = utils::GetAbsoluteUrlAppRoot();
  882. $sIssuesURL = "{$sAbsoluteUrl}synchro/replica.php?operation=oql&datasource=".$this->GetKey()."&oql=".urlencode($sIssuesOQL);
  883. $sSeeIssues = "<p></p>";
  884. $sStatistics = "<h1>Statistics</h1>\n";
  885. $sStatistics .= "<ul>\n";
  886. $sStatistics .= "<li>".$oStatLog->GetLabel('start_date').": ".$oStatLog->Get('start_date')."</li>\n";
  887. $sStatistics .= "<li>".$oStatLog->GetLabel('end_date').": ".$oStatLog->Get('end_date')."</li>\n";
  888. $sStatistics .= "<li>".$oStatLog->GetLabel('stats_nb_replica_seen').": ".$oStatLog->Get('stats_nb_replica_seen')."</li>\n";
  889. $sStatistics .= "<li>".$oStatLog->GetLabel('stats_nb_replica_total').": ".$oStatLog->Get('stats_nb_replica_total')."</li>\n";
  890. $sStatistics .= "<li>".$oStatLog->GetLabel('stats_nb_obj_deleted').": ".$oStatLog->Get('stats_nb_obj_deleted')."</li>\n";
  891. $sStatistics .= "<li>".$oStatLog->GetLabel('stats_nb_obj_deleted_errors').": ".$oStatLog->Get('stats_nb_obj_deleted_errors')."</li>\n";
  892. $sStatistics .= "<li>".$oStatLog->GetLabel('stats_nb_obj_obsoleted').": ".$oStatLog->Get('stats_nb_obj_obsoleted')."</li>\n";
  893. $sStatistics .= "<li>".$oStatLog->GetLabel('stats_nb_obj_obsoleted_errors').": ".$oStatLog->Get('stats_nb_obj_obsoleted_errors')."</li>\n";
  894. $sStatistics .= "<li>".$oStatLog->GetLabel('stats_nb_obj_created').": ".$oStatLog->Get('stats_nb_obj_created')."</li>\n";
  895. $sStatistics .= "<li>".$oStatLog->GetLabel('stats_nb_obj_created_errors').": ".$oStatLog->Get('stats_nb_obj_created_errors')."</li>\n";
  896. $sStatistics .= "<li>".$oStatLog->GetLabel('stats_nb_obj_updated').": ".$oStatLog->Get('stats_nb_obj_updated')."</li>\n";
  897. $sStatistics .= "<li>".$oStatLog->GetLabel('stats_nb_obj_updated_errors').": ".$oStatLog->Get('stats_nb_obj_updated_errors')."</li>\n";
  898. $sStatistics .= "<li>".$oStatLog->GetLabel('stats_nb_replica_reconciled_errors').": ".$oStatLog->Get('stats_nb_replica_reconciled_errors')."</li>\n";
  899. $sStatistics .= "<li>".$oStatLog->GetLabel('stats_nb_replica_disappeared_no_action').": ".$oStatLog->Get('stats_nb_replica_disappeared_no_action')."</li>\n";
  900. $sStatistics .= "<li>".$oStatLog->GetLabel('stats_nb_obj_new_updated').": ".$oStatLog->Get('stats_nb_obj_new_updated')."</li>\n";
  901. $sStatistics .= "<li>".$oStatLog->GetLabel('stats_nb_obj_new_unchanged').": ".$oStatLog->Get('stats_nb_obj_new_unchanged')."</li>\n";
  902. $sStatistics .= "</ul>\n";
  903. $this->SendNotification("errors ($iErrors)", "<p>The synchronization has been executed, $iErrors errors have been encountered. Click <a href=\"$sIssuesURL\">here</a> to see the records being currently in error.</p>".$sStatistics);
  904. }
  905. else
  906. {
  907. //$this->SendNotification('success', '<p>The synchronization has been successfully executed.</p>');
  908. }
  909. }
  910. catch (SynchroExceptionNotStarted $e)
  911. {
  912. // Set information for reporting... but delete the object in DB
  913. $oStatLog->Set('end_date', time());
  914. $oStatLog->Set('status', 'error');
  915. $oStatLog->Set('last_error', $e->getMessage());
  916. $oStatLog->DBDeleteTracked($oMyChange);
  917. $this->SendNotification('fatal error', '<p>The synchronization could not start: \''.$e->getMessage().'\'</p><p>Please check its configuration</p>');
  918. }
  919. catch (Exception $e)
  920. {
  921. $oStatLog->Set('end_date', time());
  922. $oStatLog->Set('status', 'error');
  923. $oStatLog->Set('last_error', $e->getMessage());
  924. $oStatLog->DBUpdateTracked($oMyChange);
  925. $this->SendNotification('exception', '<p>The synchronization has been interrupted: \''.$e->getMessage().'\'</p><p>Please contact the application support team</p>');
  926. }
  927. self::$m_oCurrentTask = null;
  928. return $oStatLog;
  929. }
  930. protected function DoSynchronize($oLastFullLoadStartDate, $oMyChange, &$oStatLog)
  931. {
  932. if ($this->Get('status') == 'obsolete')
  933. {
  934. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncDataSourceObsolete'));
  935. }
  936. if (!UserRights::IsAdministrator() && $this->Get('user_id') != UserRights::GetUserId())
  937. {
  938. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncDataSourceAccessRestriction'));
  939. }
  940. // Get the list of SQL columns
  941. $sClass = $this->GetTargetClass();
  942. $aAttCodesExpected = array();
  943. $aAttCodesToReconcile = array();
  944. $aAttCodesToUpdate = array();
  945. $sSelectAtt = "SELECT SynchroAttribute WHERE sync_source_id = :source_id AND (update = 1 OR reconcile = 1)";
  946. $oSetAtt = new DBObjectSet(DBObjectSearch::FromOQL($sSelectAtt), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */);
  947. while ($oSyncAtt = $oSetAtt->Fetch())
  948. {
  949. if ($oSyncAtt->Get('update'))
  950. {
  951. $aAttCodesToUpdate[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  952. }
  953. if ($oSyncAtt->Get('reconcile'))
  954. {
  955. $aAttCodesToReconcile[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  956. }
  957. $aAttCodesExpected[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  958. }
  959. $aColumns = $this->GetSQLColumns(array_keys($aAttCodesExpected));
  960. $aExtDataFields = array_keys($aColumns);
  961. $aExtDataFields[] = 'primary_key';
  962. $aExtDataSpec = array(
  963. 'table' => $this->GetDataTable(),
  964. 'join_key' => 'id',
  965. 'fields' => $aExtDataFields
  966. );
  967. // Get the list of attributes, determine reconciliation keys and update targets
  968. //
  969. if ($this->Get('reconciliation_policy') == 'use_attributes')
  970. {
  971. $aReconciliationKeys = $aAttCodesToReconcile;
  972. }
  973. elseif ($this->Get('reconciliation_policy') == 'use_primary_key')
  974. {
  975. // Override the settings made at the attribute level !
  976. $aReconciliationKeys = array("primary_key" => null);
  977. }
  978. $oStatLog->AddTrace("Update of: {".implode(', ', array_keys($aAttCodesToUpdate))."}");
  979. $oStatLog->AddTrace("Reconciliation on: {".implode(', ', array_keys($aReconciliationKeys))."}");
  980. if (count($aAttCodesToUpdate) == 0)
  981. {
  982. $oStatLog->AddTrace("No attribute to update");
  983. throw new SynchroExceptionNotStarted('There is no attribute to update');
  984. }
  985. if (count($aReconciliationKeys) == 0)
  986. {
  987. $oStatLog->AddTrace("No attribute for reconciliation");
  988. throw new SynchroExceptionNotStarted('No attribute for reconciliation');
  989. }
  990. $aAttributes = array();
  991. foreach($aAttCodesToUpdate as $sAttCode => $oSyncAtt)
  992. {
  993. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  994. if ($oAttDef->IsWritable())
  995. {
  996. $aAttributes[$sAttCode] = $oSyncAtt;
  997. }
  998. }
  999. // Count the replicas
  1000. $sSelectAll = "SELECT SynchroReplica WHERE sync_source_id = :source_id";
  1001. $oSetAll = new DBObjectSet(DBObjectSearch::FromOQL($sSelectAll), array() /* order by*/, array('source_id' => $this->GetKey()));
  1002. $iCountAllReplicas = $oSetAll->Count();
  1003. $oStatLog->Set('stats_nb_replica_total', $iCountAllReplicas);
  1004. // Get all the replicas that were not seen in the last import and mark them as obsolete
  1005. if ($oLastFullLoadStartDate == null)
  1006. {
  1007. // No previous import known, use the full_load_periodicity value... and the current date
  1008. $oLastFullLoadStartDate = new DateTime(); // Now
  1009. $iLoadPeriodicity = $this->Get('full_load_periodicity'); // Duration in seconds
  1010. if ($iLoadPeriodicity > 0)
  1011. {
  1012. $sInterval = "-$iLoadPeriodicity seconds";
  1013. $oLastFullLoadStartDate->Modify($sInterval);
  1014. }
  1015. else
  1016. {
  1017. $oLastFullLoadStartDate = new DateTime('1970-01-01');
  1018. }
  1019. }
  1020. $sLimitDate = $oLastFullLoadStartDate->Format('Y-m-d H:i:s');
  1021. $oStatLog->AddTrace("Limit Date: $sLimitDate");
  1022. $sDeletePolicy = $this->Get('delete_policy');
  1023. if ($sDeletePolicy != 'ignore')
  1024. {
  1025. $sSelectToObsolete = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen < :last_import";
  1026. $oSetToObsolete = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToObsolete), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sLimitDate));
  1027. if (($iCountAllReplicas > 10) && ($iCountAllReplicas == $oSetToObsolete->Count()))
  1028. {
  1029. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncTooManyMissingReplicas'));
  1030. }
  1031. while($oReplica = $oSetToObsolete->Fetch())
  1032. {
  1033. switch ($sDeletePolicy)
  1034. {
  1035. case 'update':
  1036. case 'update_then_delete':
  1037. $oStatLog->AddTrace("Destination object to be updated", $oReplica);
  1038. $aToUpdate = array();
  1039. $aToUpdateSpec = explode(';', $this->Get('delete_policy_update')); //ex: 'status:obsolete;description:stopped',
  1040. foreach($aToUpdateSpec as $sUpdateSpec)
  1041. {
  1042. $aUpdateSpec = explode(':', $sUpdateSpec);
  1043. if (count($aUpdateSpec) == 2)
  1044. {
  1045. $sAttCode = $aUpdateSpec[0];
  1046. $sValue = $aUpdateSpec[1];
  1047. $aToUpdate[$sAttCode] = $sValue;
  1048. }
  1049. }
  1050. $oReplica->Set('status_last_error', '');
  1051. $oReplica->UpdateDestObject($aToUpdate, $oMyChange, $oStatLog);
  1052. if ($oReplica->Get('status_last_error') == '')
  1053. {
  1054. // Change the status of the replica IIF
  1055. $oReplica->Set('status', 'obsolete');
  1056. }
  1057. $oReplica->DBUpdateTracked($oMyChange);
  1058. break;
  1059. case 'delete':
  1060. default:
  1061. $oStatLog->AddTrace("Destination object to be DELETED", $oReplica);
  1062. $oReplica->DeleteDestObject($oMyChange, $oStatLog);
  1063. }
  1064. }
  1065. } // if ($sDeletePolicy != 'ignore'
  1066. //Count "seen" objects
  1067. $sSelectSeen = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen >= :last_import";
  1068. $oSetSeen = new DBObjectSet(DBObjectSearch::FromOQL($sSelectSeen), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sLimitDate));
  1069. $oStatLog->Set('stats_nb_replica_seen', $oSetSeen->Count());
  1070. // Get all the replicas that are 'new' or modified
  1071. //
  1072. $sSelectToSync = "SELECT SynchroReplica WHERE (status = 'new' OR status = 'modified') AND sync_source_id = :source_id";
  1073. $oSetToSync = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToSync), array() /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, $aExtDataSpec, 0 /* limitCount */, 0 /* limitStart */);
  1074. while($oReplica = $oSetToSync->Fetch())
  1075. {
  1076. $oReplica->Synchro($this, $aReconciliationKeys, $aAttributes, $oMyChange, $oStatLog);
  1077. $oReplica->DBUpdateTracked($oMyChange);
  1078. }
  1079. // Get all the replicas that are to be deleted
  1080. //
  1081. if ($sDeletePolicy == 'update_then_delete')
  1082. {
  1083. $oDeletionDate = $oLastFullLoadStartDate;
  1084. $iDeleteRetention = $this->Get('delete_policy_retention'); // Duration in seconds
  1085. if ($iDeleteRetention > 0)
  1086. {
  1087. $sInterval = "-$iDeleteRetention seconds";
  1088. $oDeletionDate->Modify($sInterval);
  1089. }
  1090. $sDeletionDate = $oDeletionDate->Format('Y-m-d H:i:s');
  1091. $oStatLog->AddTrace("Deletion date: $sDeletionDate");
  1092. $sSelectToDelete = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('obsolete') AND status_last_seen < :last_import";
  1093. $oSetToDelete = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToDelete), array() /* order by*/, array('source_id' => $this->GetKey(), 'last_import' => $sDeletionDate));
  1094. while($oReplica = $oSetToDelete->Fetch())
  1095. {
  1096. $oStatLog->AddTrace("Destination object to be DELETED", $oReplica);
  1097. $oReplica->DeleteDestObject($oMyChange, $oStatLog);
  1098. }
  1099. }
  1100. }
  1101. /**
  1102. * Get the list of SQL columns corresponding to a particular list of attribute codes
  1103. * Defaults to the whole list of columns for the current class
  1104. */
  1105. public function GetSQLColumns($aAttributeCodes = null)
  1106. {
  1107. $aColumns = array();
  1108. $sClass = $this->GetTargetClass();
  1109. if (is_null($aAttributeCodes))
  1110. {
  1111. $aAttributeCodes = array();
  1112. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  1113. {
  1114. if ($sAttCode == 'finalclass') continue;
  1115. $aAttributeCodes[] = $sAttCode;
  1116. }
  1117. }
  1118. foreach($aAttributeCodes as $sAttCode)
  1119. {
  1120. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1121. if ($oAttDef->IsExternalKey())
  1122. {
  1123. // The pkey might be used as well as any other key column
  1124. $aColumns[$sAttCode] = 'VARCHAR (255)';
  1125. }
  1126. else
  1127. {
  1128. foreach($oAttDef->GetImportColumns() as $sField => $sDBFieldType)
  1129. {
  1130. $aColumns[$sField] = $sDBFieldType;
  1131. }
  1132. }
  1133. }
  1134. return $aColumns;
  1135. }
  1136. /**
  1137. * Get the list of Date and Datetime SQL columns
  1138. */
  1139. public function GetDateSQLColumns()
  1140. {
  1141. $aDateAttributes = array();
  1142. $sClass = $this->GetTargetClass();
  1143. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  1144. {
  1145. if ($oAttDef instanceof AttributeDateTime)
  1146. {
  1147. $aDateAttributes[] = $sAttCode;
  1148. }
  1149. }
  1150. return $this->GetSQLColumns($aDateAttributes);
  1151. }
  1152. public function IsRunning()
  1153. {
  1154. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id AND status='running'";
  1155. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 1 /* limitCount */, 0 /* limitStart */);
  1156. if ($oSet->Count() < 1)
  1157. {
  1158. $bRet = false;
  1159. }
  1160. else
  1161. {
  1162. $bRet = true;
  1163. }
  1164. return $bRet;
  1165. }
  1166. public function GetLatestLog()
  1167. {
  1168. $oLog = null;
  1169. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id";
  1170. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 1 /* limitCount */, 0 /* limitStart */);
  1171. if ($oSet->Count() >= 1)
  1172. {
  1173. $oLog = $oSet->Fetch();
  1174. }
  1175. return $oLog;
  1176. }
  1177. // TO DO: remove if still unused
  1178. /**
  1179. * Retrieve from the log, the date of the last completed import
  1180. * @return DateTime
  1181. */
  1182. public function GetLastCompletedImportDate()
  1183. {
  1184. $date = null;
  1185. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id AND status='completed'";
  1186. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('end_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 0 /* limitCount */, 0 /* limitStart */);
  1187. if ($oSet->Count() >= 1)
  1188. {
  1189. $oLog = $oSet->Fetch();
  1190. $date = $oLog->Get('end_date');
  1191. }
  1192. return $date;
  1193. }
  1194. }
  1195. class SynchroAttribute extends cmdbAbstractObject
  1196. {
  1197. public static function Init()
  1198. {
  1199. $aParams = array
  1200. (
  1201. "category" => "core/cmdb,view_in_gui",
  1202. "key_type" => "autoincrement",
  1203. "name_attcode" => "",
  1204. "state_attcode" => "",
  1205. "reconc_keys" => array(),
  1206. "db_table" => "priv_sync_att",
  1207. "db_key_field" => "id",
  1208. "db_finalclass_field" => "",
  1209. "display_template" => "",
  1210. );
  1211. MetaModel::Init_Params($aParams);
  1212. MetaModel::Init_InheritAttributes();
  1213. 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_SILENT, "depends_on"=>array())));
  1214. MetaModel::Init_AddAttribute(new AttributeString("attcode", array("allowed_values"=>null, "sql"=>"attcode", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  1215. MetaModel::Init_AddAttribute(new AttributeBoolean("update", array("allowed_values"=>null, "sql"=>"update", "default_value"=>true, "is_null_allowed"=>false, "depends_on"=>array())));
  1216. MetaModel::Init_AddAttribute(new AttributeBoolean("reconcile", array("allowed_values"=>null, "sql"=>"reconcile", "default_value"=>false, "is_null_allowed"=>false, "depends_on"=>array())));
  1217. 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())));
  1218. // Display lists
  1219. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for the complete details
  1220. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  1221. // Search criteria
  1222. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  1223. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1224. }
  1225. }
  1226. class SynchroAttExtKey extends SynchroAttribute
  1227. {
  1228. public static function Init()
  1229. {
  1230. $aParams = array
  1231. (
  1232. "category" => "core/cmdb,view_in_gui",
  1233. "key_type" => "autoincrement",
  1234. "name_attcode" => "",
  1235. "state_attcode" => "",
  1236. "reconc_keys" => array(),
  1237. "db_table" => "priv_sync_att_extkey",
  1238. "db_key_field" => "id",
  1239. "db_finalclass_field" => "",
  1240. "display_template" => "",
  1241. );
  1242. MetaModel::Init_Params($aParams);
  1243. MetaModel::Init_InheritAttributes();
  1244. MetaModel::Init_AddAttribute(new AttributeString("reconciliation_attcode", array("allowed_values"=>null, "sql"=>"reconciliation_attcode", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  1245. // Display lists
  1246. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy', 'reconciliation_attcode')); // Attributes to be displayed for the complete details
  1247. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  1248. // Search criteria
  1249. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  1250. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1251. }
  1252. public function GetReconciliationFormElement($sTargetClass, $sFieldName)
  1253. {
  1254. $sHtml = "<select name=\"$sFieldName\">\n";
  1255. $sSelected = (''== $this->Get('reconciliation_attcode')) ? ' selected' : '';
  1256. $sHtml .= "<option value=\"\" $sSelected>".Dict::S('Core:SynchroAttExtKey:ReconciliationById')."</option>\n";
  1257. foreach(MetaModel::ListAttributeDefs($sTargetClass) as $sAttCode => $oAttDef)
  1258. {
  1259. if ($oAttDef->IsScalar())
  1260. {
  1261. $sSelected = ($sAttCode == $this->Get('reconciliation_attcode')) ? ' selected' : '';
  1262. $sHtml .= "<option value=\"$sAttCode\" $sSelected>".MetaModel::GetLabel($sTargetClass, $sAttCode)."</option>\n";
  1263. }
  1264. }
  1265. $sHtml .= "</select>\n";
  1266. return $sHtml;
  1267. }
  1268. }
  1269. class SynchroAttLinkSet extends SynchroAttribute
  1270. {
  1271. public static function Init()
  1272. {
  1273. $aParams = array
  1274. (
  1275. "category" => "core/cmdb,view_in_gui",
  1276. "key_type" => "autoincrement",
  1277. "name_attcode" => "",
  1278. "state_attcode" => "",
  1279. "reconc_keys" => array(),
  1280. "db_table" => "priv_sync_att_linkset",
  1281. "db_key_field" => "id",
  1282. "db_finalclass_field" => "",
  1283. "display_template" => "",
  1284. );
  1285. MetaModel::Init_Params($aParams);
  1286. MetaModel::Init_InheritAttributes();
  1287. MetaModel::Init_AddAttribute(new AttributeString("row_separator", array("allowed_values"=>null, "sql"=>"row_separator", "default_value"=>'|', "is_null_allowed"=>true, "depends_on"=>array())));
  1288. MetaModel::Init_AddAttribute(new AttributeString("attribute_separator", array("allowed_values"=>null, "sql"=>"attribute_separator", "default_value"=>';', "is_null_allowed"=>true, "depends_on"=>array())));
  1289. MetaModel::Init_AddAttribute(new AttributeString("value_separator", array("allowed_values"=>null, "sql"=>"value_separator", "default_value"=>':', "is_null_allowed"=>true, "depends_on"=>array())));
  1290. MetaModel::Init_AddAttribute(new AttributeString("attribute_qualifier", array("allowed_values"=>null, "sql"=>"attribute_qualifier", "default_value"=>'\'', "is_null_allowed"=>true, "depends_on"=>array())));
  1291. // Display lists
  1292. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy', 'row_separator', 'attribute_separator', 'value_separator', 'attribute_qualifier')); // Attributes to be displayed for the complete details
  1293. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  1294. // Search criteria
  1295. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  1296. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1297. }
  1298. }
  1299. //class SynchroLog extends Event
  1300. class SynchroLog extends DBObject
  1301. {
  1302. public static function Init()
  1303. {
  1304. $aParams = array
  1305. (
  1306. "category" => "core/cmdb,view_in_gui",
  1307. "key_type" => "autoincrement",
  1308. "name_attcode" => "",
  1309. "state_attcode" => "",
  1310. "reconc_keys" => array(),
  1311. "db_table" => "priv_sync_log",
  1312. "db_key_field" => "id",
  1313. "db_finalclass_field" => "",
  1314. "display_template" => "",
  1315. );
  1316. MetaModel::Init_Params($aParams);
  1317. MetaModel::Init_InheritAttributes();
  1318. // MetaModel::Init_AddAttribute(new AttributeString("userinfo", array("allowed_values"=>null, "sql"=>"userinfo", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  1319. 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_SILENT, "depends_on"=>array())));
  1320. MetaModel::Init_AddAttribute(new AttributeDateTime("start_date", array("allowed_values"=>null, "sql"=>"start_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  1321. MetaModel::Init_AddAttribute(new AttributeDateTime("end_date", array("allowed_values"=>null, "sql"=>"end_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  1322. 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())));
  1323. 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())));
  1324. 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())));
  1325. 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())));
  1326. 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())));
  1327. 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())));
  1328. 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())));
  1329. 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())));
  1330. 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())));
  1331. 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())));
  1332. 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())));
  1333. // 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())));
  1334. 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())));
  1335. 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())));
  1336. 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())));
  1337. 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())));
  1338. MetaModel::Init_AddAttribute(new AttributeText("last_error", array("allowed_values"=>null, "sql"=>"last_error", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  1339. MetaModel::Init_AddAttribute(new AttributeLongText("traces", array("allowed_values"=>null, "sql"=>"traces", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  1340. // Display lists
  1341. 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',
  1342. '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', 'traces')); // Attributes to be displayed for the complete details
  1343. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'start_date', 'end_date', 'status', 'stats_nb_replica_seen')); // Attributes to be displayed for a list
  1344. // Search criteria
  1345. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  1346. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1347. }
  1348. /**
  1349. * Helper
  1350. */
  1351. function GetErrorCount()
  1352. {
  1353. return $this->Get('stats_nb_obj_deleted_errors')
  1354. + $this->Get('stats_nb_obj_obsoleted_errors')
  1355. + $this->Get('stats_nb_obj_created_errors')
  1356. + $this->Get('stats_nb_obj_updated_errors')
  1357. + $this->Get('stats_nb_replica_reconciled_errors');
  1358. }
  1359. /**
  1360. * Increments a statistics counter
  1361. */
  1362. function Inc($sCode)
  1363. {
  1364. $this->Set($sCode, 1+$this->Get($sCode));
  1365. }
  1366. /**
  1367. * Implement traces management
  1368. */
  1369. protected $m_aTraces = array();
  1370. public function AddTrace($sMsg, $oReplica = null)
  1371. {
  1372. if (MetaModel::GetConfig()->Get('synchro_trace') == 'none')
  1373. {
  1374. return;
  1375. }
  1376. if ($oReplica)
  1377. {
  1378. $sDestClass = $oReplica->Get('dest_class');
  1379. if (!empty($sDestClass))
  1380. {
  1381. $sPrefix = $oReplica->GetKey().','.$sDestClass.'::'.$oReplica->Get('dest_id').',';
  1382. }
  1383. else
  1384. {
  1385. $sPrefix = $oReplica->GetKey().',,';
  1386. }
  1387. }
  1388. else
  1389. {
  1390. $sPrefix = ',,';
  1391. }
  1392. $this->m_aTraces[] = $sPrefix.$sMsg;
  1393. }
  1394. public function GetTraces()
  1395. {
  1396. return $this->m_aTraces;
  1397. }
  1398. protected function TraceToText()
  1399. {
  1400. if (MetaModel::GetConfig()->Get('synchro_trace') != 'save')
  1401. {
  1402. // none, or display only
  1403. return;
  1404. }
  1405. $oAttDef = MetaModel::GetAttributeDef(get_class($this), 'traces');
  1406. $iMaxSize = $oAttDef->GetMaxSize();
  1407. $sTrace = implode("\n", $this->m_aTraces);
  1408. if (strlen($sTrace) >= $iMaxSize)
  1409. {
  1410. $sTrace = substr($sTrace, 0, $iMaxSize - 255)."...\nTruncated (size: ".strlen($sTrace).')';
  1411. }
  1412. $this->Set('traces', $sTrace);
  1413. }
  1414. protected function OnInsert()
  1415. {
  1416. $this->TraceToText();
  1417. parent::OnInsert();
  1418. }
  1419. protected function OnUpdate()
  1420. {
  1421. $this->TraceToText();
  1422. parent::OnUpdate();
  1423. }
  1424. }
  1425. class SynchroReplica extends DBObject implements iDisplay
  1426. {
  1427. static $aSearches = array(); // Cache of OQL queries used for reconciliation (per data source)
  1428. public static function Init()
  1429. {
  1430. $aParams = array
  1431. (
  1432. "category" => "core/cmdb,view_in_gui",
  1433. "key_type" => "autoincrement",
  1434. "name_attcode" => "",
  1435. "state_attcode" => "",
  1436. "reconc_keys" => array(),
  1437. "db_table" => "priv_sync_replica",
  1438. "db_key_field" => "id",
  1439. "db_finalclass_field" => "",
  1440. "display_template" => "",
  1441. );
  1442. MetaModel::Init_Params($aParams);
  1443. MetaModel::Init_InheritAttributes();
  1444. 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_SILENT, "depends_on"=>array())));
  1445. MetaModel::Init_AddAttribute(new AttributeExternalField("base_class", array("allowed_values"=>null, "extkey_attcode"=> 'sync_source_id', "target_attcode"=>"scope_class")));
  1446. MetaModel::Init_AddAttribute(new AttributeInteger("dest_id", array("allowed_values"=>null, "sql"=>"dest_id", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  1447. MetaModel::Init_AddAttribute(new AttributeClass("dest_class", array("class_category"=>"", "more_values"=>"", "sql"=>"dest_class", "default_value"=>'Organization', "is_null_allowed"=>true, "depends_on"=>array())));
  1448. 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())));
  1449. 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())));
  1450. 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())));
  1451. 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())));
  1452. 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())));
  1453. 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())));
  1454. // Display lists
  1455. MetaModel::Init_SetZListItems('details', array('' .
  1456. 'col:0'=> array(
  1457. 'fieldset:SynchroDataSource:Definition' => array('sync_source_id','dest_id','dest_class'),
  1458. 'fieldset:SynchroDataSource:Status' => array('status','status_last_seen','status_dest_creator','status_last_error'),
  1459. 'fieldset:SynchroDataSource:Information' => array('info_creation_date','info_last_modified'))
  1460. )
  1461. );
  1462. 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
  1463. // Search criteria
  1464. 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
  1465. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1466. }
  1467. public function DBInsert()
  1468. {
  1469. throw new CoreException('A synchronization replica must be created only by the mean of triggers');
  1470. }
  1471. // Overload the deletion -> the replica has been created by the mean of a trigger,
  1472. // it will be deleted by the mean of a trigger too
  1473. protected function DBDeleteSingleObject()
  1474. {
  1475. $this->OnDelete();
  1476. if (!MetaModel::DBIsReadOnly())
  1477. {
  1478. $oDataSource = MetaModel::GetObject('SynchroDataSource', $this->Get('sync_source_id'));
  1479. $sTable = $oDataSource->GetDataTable();
  1480. $sSQL = "DELETE FROM `$sTable` WHERE id = '{$this->GetKey()}'";
  1481. CMDBSource::Query($sSQL);
  1482. }
  1483. $this->AfterDelete();
  1484. $this->m_bIsInDB = false;
  1485. $this->m_iKey = null;
  1486. }
  1487. public function SetLastError($sMessage, $oException = null)
  1488. {
  1489. if ($oException)
  1490. {
  1491. $sText = $sMessage.$oException->getMessage();
  1492. }
  1493. else
  1494. {
  1495. $sText = $sMessage;
  1496. }
  1497. if (strlen($sText) > 255)
  1498. {
  1499. $sText = substr($sText, 0, 200).'...('.strlen($sText).' chars)...';
  1500. }
  1501. $this->Set('status_last_error', $sText);
  1502. }
  1503. public function Synchro($oDataSource, $aReconciliationKeys, $aAttributes, $oChange, &$oStatLog)
  1504. {
  1505. switch($this->Get('status'))
  1506. {
  1507. case 'new':
  1508. $this->Set('status_dest_creator', false);
  1509. // If needed, construct the query used for the reconciliation
  1510. if (!isset(self::$aSearches[$oDataSource->GetKey()]))
  1511. {
  1512. $aCriterias = array();
  1513. foreach($aReconciliationKeys as $sFilterCode => $oSyncAtt)
  1514. {
  1515. $aCriterias[] = ($sFilterCode == 'primary_key' ? 'id' : $sFilterCode).' = :'.$sFilterCode;
  1516. }
  1517. $sOQL = "SELECT ".$oDataSource->GetTargetClass()." WHERE ".implode(' AND ', $aCriterias);
  1518. self::$aSearches[$oDataSource->GetKey()] = DBObjectSearch::FromOQL($sOQL);
  1519. }
  1520. // Get the criterias for the search
  1521. $aFilterValues = array();
  1522. foreach($aReconciliationKeys as $sFilterCode => $oSyncAtt)
  1523. {
  1524. $value = $this->GetValueFromExtData($sFilterCode, $oSyncAtt, $oStatLog);
  1525. if (!is_null($value))
  1526. {
  1527. $aFilterValues[$sFilterCode] = $value;
  1528. }
  1529. else
  1530. {
  1531. // Reconciliation could not be performed - log and EXIT
  1532. $oStatLog->AddTrace("Could not reconcile on null value for attribute '$sFilterCode'", $this);
  1533. $this->SetLastError("Could not reconcile on null value for attribute '$sFilterCode'");
  1534. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1535. return;
  1536. }
  1537. }
  1538. $oDestSet = new DBObjectSet(self::$aSearches[$oDataSource->GetKey()], array(), $aFilterValues);
  1539. $iCount = $oDestSet->Count();
  1540. $aConditions = array();
  1541. foreach($aFilterValues as $sCode => $sValue)
  1542. {
  1543. $aConditions[] = $sCode.'='.$sValue;
  1544. }
  1545. $sConditionDesc = implode(' AND ', $aConditions);
  1546. // How many objects match the reconciliation criterias
  1547. switch($iCount)
  1548. {
  1549. case 0:
  1550. $oStatLog->AddTrace("Nothing found on: $sConditionDesc", $this);
  1551. if ($oDataSource->Get('action_on_zero') == 'create')
  1552. {
  1553. $this->CreateObjectFromReplica($oDataSource->GetTargetClass(), $aAttributes, $oChange, $oStatLog);
  1554. }
  1555. else // assumed to be 'error'
  1556. {
  1557. $oStatLog->AddTrace("Failed to reconcile (no match)", $this);
  1558. $this->SetLastError('Could not find a match for reconciliation');
  1559. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1560. }
  1561. break;
  1562. case 1:
  1563. $oStatLog->AddTrace("Found 1 object on: $sConditionDesc", $this);
  1564. if ($oDataSource->Get('action_on_one') == 'update')
  1565. {
  1566. $oDestObj = $oDestSet->Fetch();
  1567. $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj_new', 'stats_nb_replica_reconciled_errors');
  1568. $this->Set('dest_id', $oDestObj->GetKey());
  1569. $this->Set('dest_class', get_class($oDestObj));
  1570. }
  1571. else
  1572. {
  1573. // assumed to be 'error'
  1574. $oStatLog->AddTrace("Failed to reconcile (1 match)", $this);
  1575. $this->SetLastError('Found a match while expecting several');
  1576. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1577. }
  1578. break;
  1579. default:
  1580. $oStatLog->AddTrace("Found $iCount objects on: $sConditionDesc", $this);
  1581. if ($oDataSource->Get('action_on_multiple') == 'error')
  1582. {
  1583. $oStatLog->AddTrace("Failed to reconcile (N>1 matches)", $this);
  1584. $this->SetLastError($iCount.' destination objects match the reconciliation criterias: '.$sConditionDesc);
  1585. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1586. }
  1587. elseif ($oDataSource->Get('action_on_multiple') == 'create')
  1588. {
  1589. $this->CreateObjectFromReplica($oDataSource->GetTargetClass(), $aAttributes, $oChange, $oStatLog);
  1590. }
  1591. else
  1592. {
  1593. // assumed to be 'take_first'
  1594. $oDestObj = $oDestSet->Fetch();
  1595. $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj_new', 'stats_nb_replica_reconciled_errors');
  1596. $this->Set('dest_id', $oDestObj->GetKey());
  1597. $this->Set('dest_class', get_class($oDestObj));
  1598. }
  1599. }
  1600. break;
  1601. case 'modified':
  1602. $oDestObj = MetaModel::GetObject($oDataSource->GetTargetClass(), $this->Get('dest_id'));
  1603. if ($oDestObj == null)
  1604. {
  1605. $this->Set('status', 'orphan'); // The destination object has been deleted !
  1606. $this->SetLastError('Destination object deleted unexpectedly');
  1607. $oStatLog->Inc('stats_nb_obj_updated_errors');
  1608. }
  1609. else
  1610. {
  1611. $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, '', 'stats_nb_obj_updated_errors');
  1612. }
  1613. break;
  1614. default: // Do nothing in all other cases
  1615. }
  1616. }
  1617. /**
  1618. * Updates the destination object with the Extended data found in the synchro_data_XXXX table
  1619. */
  1620. protected function UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, &$oStatLog, $sStatsCode, $sStatsCodeError)
  1621. {
  1622. $aValueTrace = array();
  1623. try
  1624. {
  1625. foreach($aAttributes as $sAttCode => $oSyncAtt)
  1626. {
  1627. $value = $this->GetValueFromExtData($sAttCode, $oSyncAtt, $oStatLog);
  1628. if (!is_null($value))
  1629. {
  1630. $oDestObj->Set($sAttCode, $value);
  1631. $aValueTrace[] = "$sAttCode: $value";
  1632. }
  1633. }
  1634. // Really modified ?
  1635. if ($oDestObj->IsModified())
  1636. {
  1637. $oDestObj->DBUpdateTracked($oChange);
  1638. $oStatLog->AddTrace('Updated object - Values: {'.implode(', ', $aValueTrace).'}', $this);
  1639. if ($sStatsCode != '')
  1640. {
  1641. $oStatLog->Inc($sStatsCode.'_updated');
  1642. }
  1643. $this->Set('info_last_modified', date('Y-m-d H:i:s'));
  1644. }
  1645. else
  1646. {
  1647. $oStatLog->AddTrace('Unchanged object', $this);
  1648. if ($sStatsCode != '')
  1649. {
  1650. $oStatLog->Inc($sStatsCode.'_unchanged');
  1651. }
  1652. }
  1653. $this->Set('status_last_error', '');
  1654. $this->Set('status', 'synchronized');
  1655. }
  1656. catch(Exception $e)
  1657. {
  1658. $oStatLog->AddTrace("Failed to update destination object: {$e->getMessage()}", $this);
  1659. $this->SetLastError('Unable to update destination object: ', $e);
  1660. $oStatLog->Inc($sStatsCodeError);
  1661. }
  1662. }
  1663. /**
  1664. * Creates the destination object populating it with the Extended data found in the synchro_data_XXXX table
  1665. */
  1666. protected function CreateObjectFromReplica($sClass, $aAttributes, $oChange, &$oStatLog)
  1667. {
  1668. $oDestObj = MetaModel::NewObject($sClass);
  1669. try
  1670. {
  1671. $aValueTrace = array();
  1672. foreach($aAttributes as $sAttCode => $oSyncAtt)
  1673. {
  1674. $value = $this->GetValueFromExtData($sAttCode, $oSyncAtt, $oStatLog);
  1675. if (!is_null($value))
  1676. {
  1677. $oDestObj->Set($sAttCode, $value);
  1678. $aValueTrace[] = "$sAttCode: $value";
  1679. }
  1680. }
  1681. $iNew = $oDestObj->DBInsertTracked($oChange);
  1682. $this->Set('dest_id', $oDestObj->GetKey());
  1683. $this->Set('dest_class', get_class($oDestObj));
  1684. $this->Set('status_dest_creator', true);
  1685. $this->Set('status_last_error', '');
  1686. $this->Set('status', 'synchronized');
  1687. $this->Set('info_creation_date', date('Y-m-d H:i:s'));
  1688. $oStatLog->AddTrace("Created (".implode(', ', $aValueTrace).")", $this);
  1689. $oStatLog->Inc('stats_nb_obj_created');
  1690. }
  1691. catch(Exception $e)
  1692. {
  1693. $oStatLog->AddTrace("Failed to create $sClass ({$e->getMessage()})", $this);
  1694. $this->SetLastError('Unable to create destination object: ', $e);
  1695. $oStatLog->Inc('stats_nb_obj_created_errors');
  1696. }
  1697. }
  1698. /**
  1699. * Update the destination object with given values
  1700. */
  1701. public function UpdateDestObject($aValues, $oChange, &$oStatLog)
  1702. {
  1703. try
  1704. {
  1705. if ($this->Get('dest_class') == '')
  1706. {
  1707. $this->SetLastError('No destination object to update');
  1708. $oStatLog->Inc('stats_nb_obj_obsoleted_errors');
  1709. }
  1710. else
  1711. {
  1712. $oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'));
  1713. foreach($aValues as $sAttCode => $value)
  1714. {
  1715. if (!MetaModel::IsValidAttCode(get_class($oDestObj), $sAttCode))
  1716. {
  1717. throw new Exception("Unknown attribute code '$sAttCode'");
  1718. }
  1719. $oDestObj->Set($sAttCode, $value);
  1720. }
  1721. $this->Set('info_last_modified', date('Y-m-d H:i:s'));
  1722. $oDestObj->DBUpdateTracked($oChange);
  1723. $oStatLog->AddTrace("Replica marked as obsolete", $this);
  1724. $oStatLog->Inc('stats_nb_obj_obsoleted');
  1725. }
  1726. }
  1727. catch(Exception $e)
  1728. {
  1729. $this->SetLastError('Unable to update the destination object: ', $e);
  1730. $oStatLog->Inc('stats_nb_obj_obsoleted_errors');
  1731. }
  1732. }
  1733. /**
  1734. * Delete the destination object
  1735. */
  1736. public function DeleteDestObject($oChange, &$oStatLog)
  1737. {
  1738. if($this->Get('status_dest_creator'))
  1739. {
  1740. try
  1741. {
  1742. $oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'));
  1743. $oCheckDeletionPlan = new DeletionPlan();
  1744. if ($oDestObj->CheckToDelete($oCheckDeletionPlan))
  1745. {
  1746. $oActualDeletionPlan = new DeletionPlan();
  1747. $oDestObj->DBDeleteTracked($oChange, null, $oActualDeletionPlan);
  1748. $this->DBDeleteTracked($oChange);
  1749. $oStatLog->Inc('stats_nb_obj_deleted');
  1750. }
  1751. else
  1752. {
  1753. $sIssues = implode("\n", $oCheckDeletionPlan->GetIssues());
  1754. throw(new Exception($sIssues));
  1755. }
  1756. }
  1757. catch(Exception $e)
  1758. {
  1759. $this->SetLastError('Unable to delete the destination object: ', $e);
  1760. $this->Set('status', 'obsolete');
  1761. $this->DBUpdateTracked($oChange);
  1762. $oStatLog->Inc('stats_nb_obj_deleted_errors');
  1763. }
  1764. }
  1765. else
  1766. {
  1767. $this->DBDeleteTracked($oChange);
  1768. $oStatLog->Inc('stats_nb_replica_disappeared_no_action');
  1769. }
  1770. }
  1771. /**
  1772. * Get the value from the 'Extended Data' located in the synchro_data_xxx table for this replica
  1773. * Note: sExtAttCode could be a standard attcode, or 'primary_key'
  1774. */
  1775. protected function GetValueFromExtData($sExtAttCode, $oSyncAtt, &$oStatLog)
  1776. {
  1777. // $aData should contain attributes defined either for reconciliation or create/update
  1778. $aData = $this->GetExtendedData();
  1779. if ($sExtAttCode == 'primary_key')
  1780. {
  1781. return $aData['primary_key'];
  1782. }
  1783. // $sExtAttCode is a valid attribute code
  1784. //
  1785. $sClass = $this->Get('base_class');
  1786. $oAttDef = MetaModel::GetAttributeDef($sClass, $sExtAttCode);
  1787. if (!is_null($oSyncAtt) && ($oSyncAtt instanceof SynchroAttExtKey))
  1788. {
  1789. $rawValue = $aData[$sExtAttCode];
  1790. if (is_null($rawValue))
  1791. {
  1792. // Null means "ignore" this attribute
  1793. return null;
  1794. }
  1795. $sReconcAttCode = $oSyncAtt->Get('reconciliation_attcode');
  1796. if (!empty($sReconcAttCode))
  1797. {
  1798. $sRemoteClass = $oAttDef->GetTargetClass();
  1799. $oObj = MetaModel::GetObjectByColumn($sRemoteClass, $sReconcAttCode, $rawValue, false);
  1800. if ($oObj)
  1801. {
  1802. $retValue = $oObj->GetKey();
  1803. }
  1804. else
  1805. {
  1806. // Note: differs from null (in which case the value would be left unchanged)
  1807. $oStatLog->AddTrace("Could not find [unique] object for '$sExtAttCode': searched on $sReconcAttCode = '$rawValue'", $this);
  1808. $retValue = 0;
  1809. }
  1810. }
  1811. else
  1812. {
  1813. $retValue = $rawValue;
  1814. }
  1815. }
  1816. elseif (!is_null($oSyncAtt) && ($oSyncAtt instanceof SynchroAttLinkSet))
  1817. {
  1818. $rawValue = $aData[$sExtAttCode];
  1819. if (is_null($rawValue))
  1820. {
  1821. // Null means "ignore" this attribute
  1822. return null;
  1823. }
  1824. // MakeValueFromString() throws an exception in case of failure
  1825. $retValue = $oAttDef->MakeValueFromString($rawValue, $oSyncAtt->Get('row_separator'), $oSyncAtt->Get('attribute_separator'), $oSyncAtt->Get('value_separator'), $oSyncAtt->Get('attribute_qualifier'));
  1826. }
  1827. else
  1828. {
  1829. $aColumns = $oAttDef->GetImportColumns();
  1830. foreach($aColumns as $sColumn => $sFormat)
  1831. {
  1832. // In any case, a null column means "ignore this attribute"
  1833. //
  1834. if (is_null($aData[$sColumn]))
  1835. {
  1836. return null;
  1837. }
  1838. }
  1839. $retValue = $oAttDef->FromImportToValue($aData, $sExtAttCode);
  1840. }
  1841. return $retValue;
  1842. }
  1843. /**
  1844. * Maps the given context parameter name to the appropriate filter/search code for this class
  1845. * @param string $sContextParam Name of the context parameter, i.e. 'org_id'
  1846. * @return string Filter code, i.e. 'customer_id'
  1847. */
  1848. public static function MapContextParam($sContextParam)
  1849. {
  1850. if ($sContextParam == 'menu')
  1851. {
  1852. return null;
  1853. }
  1854. else
  1855. {
  1856. return $sContextParam;
  1857. }
  1858. }
  1859. /**
  1860. * This function returns a 'hilight' CSS class, used to hilight a given row in a table
  1861. * There are currently (i.e defined in the CSS) 4 possible values HILIGHT_CLASS_CRITICAL,
  1862. * HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  1863. * To Be overridden by derived classes
  1864. * @param void
  1865. * @return String The desired higlight class for the object/row
  1866. */
  1867. public function GetHilightClass()
  1868. {
  1869. // Possible return values are:
  1870. // HILIGHT_CLASS_CRITICAL, HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  1871. return HILIGHT_CLASS_NONE; // Not hilighted by default
  1872. }
  1873. public static function GetUIPage()
  1874. {
  1875. return '../synchro/replica.php';
  1876. }
  1877. function DisplayDetails(WebPage $oPage, $bEditMode = false)
  1878. {
  1879. // Object's details
  1880. //$this->DisplayBareHeader($oPage, $bEditMode);
  1881. $oPage->AddTabContainer(OBJECT_PROPERTIES_TAB);
  1882. $oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB);
  1883. $oPage->SetCurrentTab(Dict::S('UI:PropertiesTab'));
  1884. $this->DisplayBareProperties($oPage, $bEditMode);
  1885. }
  1886. function DisplayBareProperties(WebPage $oPage, $bEditMode = false, $aExtraParams = array())
  1887. {
  1888. if ($bEditMode) return; // Not editable
  1889. $oPage->add('<table style="vertical-align:top"><tr style="vertical-align:top"><td>');
  1890. $aDetails = array();
  1891. $sClass = get_class($this);
  1892. $oPage->add('<fieldset>');
  1893. $oPage->add('<legend>'.Dict::S('Core:SynchroReplica:PrivateDetails').'</legend>');
  1894. $aZList = MetaModel::FlattenZlist(MetaModel::GetZListItems($sClass, 'details'));
  1895. foreach( $aZList as $sAttCode)
  1896. {
  1897. $sDisplayValue = $this->GetAsHTML($sAttCode);
  1898. $aDetails[] = array('label' => '<span title="'.MetaModel::GetDescription($sClass, $sAttCode).'">'.MetaModel::GetLabel($sClass, $sAttCode).'</span>', 'value' => $sDisplayValue);
  1899. }
  1900. $oPage->Details($aDetails);
  1901. $oPage->add('</fieldset>');
  1902. $oPage->add('</td><td>');
  1903. $oPage->add('<fieldset>');
  1904. $oPage->add('<legend>'.Dict::S('Core:SynchroReplica:PublicData').'</legend>');
  1905. $oSource = MetaModel::GetObject('SynchroDataSource', $this->Get('sync_source_id'));
  1906. $sSQLTable = $oSource->GetDataTable();
  1907. $aData = $this->LoadExtendedDataFromTable($sSQLTable);
  1908. $aHeaders = array('attcode' => array('label' => 'Attribute Code', 'description' => ''),
  1909. 'data' => array('label' => 'Value', 'description' => ''));
  1910. $aRows = array();
  1911. foreach($aData as $sKey => $value)
  1912. {
  1913. $aRows[] = array('attcode' => $sKey, 'data' => $value);
  1914. }
  1915. $oPage->Table($aHeaders, $aRows);
  1916. $oPage->add('</fieldset>');
  1917. $oPage->add('</td></tr></table>');
  1918. }
  1919. public function LoadExtendedDataFromTable($sSQLTable)
  1920. {
  1921. $sSQL = "SELECT * FROM $sSQLTable WHERE id=".$this->GetKey();
  1922. $rQuery = CMDBSource::Query($sSQL);
  1923. return CMDBSource::FetchArray($rQuery);
  1924. }
  1925. }
  1926. $oAdminMenu = new MenuGroup('AdminTools', 80 /* fRank */, 'SynchroDataSource', UR_ACTION_MODIFY, UR_ALLOWED_YES);
  1927. new OQLMenuNode('DataSources', 'SELECT SynchroDataSource', $oAdminMenu->GetIndex(), 12 /* fRank */, true, 'SynchroDataSource', UR_ACTION_MODIFY, UR_ALLOWED_YES);
  1928. // new OQLMenuNode('Replicas', 'SELECT SynchroReplica', $oAdminMenu->GetIndex(), 12 /* fRank */, true, 'SynchroReplica', UR_ACTION_MODIFY, UR_ALLOWED_YES);
  1929. // new WebPageMenuNode('Test:RunSynchro', '../synchro/synchro_exec.php', $oAdminMenu->GetIndex(), 13 /* fRank */, 'SynchroDataSource');
  1930. ?>