synchrodatasource.class.inc.php 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650
  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 function DisplayBareRelations(WebPage $oPage, $bEditMode = false)
  87. {
  88. if (!$this->IsNew())
  89. {
  90. $oPage->SetCurrentTab(Dict::S('Core:SynchroAttributes'));
  91. $oAttributeSet = $this->Get('attribute_list');
  92. $aAttributes = array();
  93. while($oAttribute = $oAttributeSet->Fetch())
  94. {
  95. $aAttributes[$oAttribute->Get('attcode')] = $oAttribute;
  96. }
  97. // Columns of the form
  98. $aAttribs = array();
  99. foreach(array('attcode', 'reconciliation', 'update', 'update_policy', 'reconciliation_attcode') as $s )
  100. {
  101. $aAttribs[$s] = array( 'label' => Dict::S("Core:SynchroAtt:$s"), "description" => Dict::S("Core:SynchroAtt:$s+"));
  102. }
  103. // Rows of the form
  104. $aValues = array();
  105. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  106. {
  107. if ($oAttDef->IsWritable())
  108. {
  109. if (isset($aAttributes[$sAttCode]))
  110. {
  111. $oAttribute = $aAttributes[$sAttCode];
  112. }
  113. else
  114. {
  115. if ($oAttDef->IsExternalKey())
  116. {
  117. $oAttribute = new SynchroAttExtKey();
  118. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  119. }
  120. elseif ($oAttDef->IsLinkSet() && $oAttDef->IsIndirect())
  121. {
  122. $oAttribute = new SynchroAttLinkSet();
  123. // Todo - add these settings into the form
  124. $oAttribute->Set('row_separator', MetaModel::GetConfig()->Get('link_set_item_separator'));
  125. $oAttribute->Set('attribute_separator', MetaModel::GetConfig()->Get('link_set_attribute_separator'));
  126. $oAttribute->Set('value_separator', MetaModel::GetConfig()->Get('link_set_value_separator'));
  127. $oAttribute->Set('attribute_qualifier', MetaModel::GetConfig()->Get('link_set_attribute_qualifier'));
  128. }
  129. elseif ($oAttDef->IsScalar())
  130. {
  131. $oAttribute = new SynchroAttribute();
  132. }
  133. else
  134. {
  135. $oAttribute = null;
  136. }
  137. if (!is_null($oAttribute))
  138. {
  139. $oAttribute->Set('sync_source_id', $this->GetKey());
  140. $oAttribute->Set('attcode', $sAttCode);
  141. $oAttribute->Set('reconcile', MetaModel::IsReconcKey($this->GetTargetClass(), $sAttCode) ? 1 : 0);
  142. $oAttribute->Set('update', 1);
  143. $oAttribute->Set('update_policy', 'master_locked');
  144. }
  145. }
  146. if (!is_null($oAttribute))
  147. {
  148. if (!$bEditMode)
  149. {
  150. // Read-only mode
  151. $aRow['reconciliation'] = $oAttribute->Get('reconcile') == 1 ? Dict::S('Core:SynchroReconcile:Yes') : Dict::S('Core:SynchroReconcile:No');
  152. $aRow['update'] = $oAttribute->Get('update') == 1 ? Dict::S('Core:SynchroUpdate:Yes') : Dict::S('Core:SynchroUpdate:No');
  153. $aRow['attcode'] = MetaModel::GetLabel($this->GetTargetClass(), $oAttribute->Get('attcode'));
  154. $aRow['update_policy'] = $oAttribute->GetAsHTML('update_policy');
  155. if ($oAttDef->IsExternalKey())
  156. {
  157. $aRow['reconciliation_attcode'] = $oAttribute->GetAsHTML('reconciliation_attcode');
  158. }
  159. else
  160. {
  161. $aRow['reconciliation_attcode'] = '&nbsp;';
  162. }
  163. }
  164. else
  165. {
  166. // Edit mode
  167. $sAttCode = $oAttribute->Get('attcode');
  168. $sChecked = $oAttribute->Get('reconcile') == 1 ? 'checked' : '';
  169. $aRow['reconciliation'] = "<input type=\"checkbox\" name=\"reconciliation[$sAttCode]\" $sChecked/>";
  170. $sChecked = $oAttribute->Get('update') == 1 ? 'checked' : '';
  171. $aRow['update'] = "<input type=\"checkbox\" name=\"update[$sAttCode]\" $sChecked/>";
  172. $aRow['attcode'] = MetaModel::GetLabel($this->GetTargetClass(), $oAttribute->Get('attcode'));
  173. $oUpdateAttDef = MetaModel::GetAttributeDef(get_class($oAttribute), 'update_policy');
  174. $aRow['update_policy'] = cmdbAbstractObject::GetFormElementForField($oPage, get_class($oAttribute), 'update_policy', $oUpdateAttDef, $oAttribute->Get('update_policy'), '', 'update_policy_'.$sAttCode, "[$sAttCode]");
  175. if ($oAttDef->IsExternalKey())
  176. {
  177. $aRow['reconciliation_attcode'] = $oAttribute->GetReconciliationFormElement($oAttDef->GetTargetClass(), "attr_reconciliation_attcode[$sAttCode]");
  178. }
  179. else
  180. {
  181. $aRow['reconciliation_attcode'] = '&nbsp;';
  182. }
  183. }
  184. $aValues[] = $aRow;
  185. }
  186. }
  187. }
  188. $oPage->p(Dict::Format('Class:SynchroDataSource:DataTable', $this->GetDataTable()));
  189. $oPage->Table($aAttribs, $aValues);
  190. $this->DisplayStatusTab($oPage);
  191. }
  192. parent::DisplayBareRelations($oPage, $bEditMode);
  193. }
  194. /**
  195. * Displays the status (SynchroLog) of the datasource in a graphical manner
  196. * @param $oPage WebPage
  197. * @return void
  198. */
  199. protected function DisplayStatusTab(WebPage $oPage)
  200. {
  201. $oPage->SetCurrentTab(Dict::S('Core:SynchroStatus'));
  202. $sSelectSynchroLog = 'SELECT SynchroLog WHERE sync_source_id = :source_id';
  203. $oSetSynchroLog = new CMDBObjectSet(DBObjectSearch::FromOQL($sSelectSynchroLog), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()));
  204. if ($oSetSynchroLog->Count() > 0)
  205. {
  206. $oLastLog = $oSetSynchroLog->Fetch();
  207. $sStartDate = $oLastLog->Get('start_date');
  208. $oLastLog->Get('stats_nb_replica_seen');
  209. $iLastLog = 0;
  210. $iDSid = $this->GetKey();
  211. if ($oLastLog->Get('status') == 'running')
  212. {
  213. // Still running !
  214. $oPage->p('<h2>'.Dict::Format('Core:Synchro:SynchroRunningStartedOn_Date', $sStartDate).'</h2>');
  215. }
  216. else
  217. {
  218. $sEndDate = $oLastLog->Get('end_date');
  219. $iLastLog = $oLastLog->GetKey();
  220. $oPage->p('<h2>'.Dict::Format('Core:Synchro:SynchroEndedOn_Date', $sEndDate).'</h2>');
  221. $sOQL = "SELECT SynchroReplica WHERE sync_source_id=$iDSid";
  222. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL));
  223. $iCountAllReplicas = $oSet->Count();
  224. $sAllReplicas = "<a href=\"../synchro/replica.php?operation=oql&datasource=$iDSid&oql=$sOQL\">$iCountAllReplicas</a>";
  225. $sOQL = "SELECT SynchroReplica WHERE sync_source_id=$iDSid AND status_last_error !=''";
  226. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL));
  227. $iCountAllErrors = $oSet->Count();
  228. $sAllErrors = "<a href=\"../synchro/replica.php?operation=oql&datasource=$iDSid&oql=$sOQL\">$iCountAllErrors</a>";
  229. $sOQL = "SELECT SynchroReplica WHERE sync_source_id=$iDSid AND status_last_warning !=''";
  230. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL));
  231. $iCountAllWarnings = $oSet->Count();
  232. $sAllWarnings = "<a href=\"../synchro/replica.php?operation=oql&datasource=$iDSid&oql=$sOQL\">$iCountAllWarnings</a>";
  233. $oPage->p('<h2>'.Dict::Format('Core:Synchro:ListReplicas_AllReplicas_Errors_Warnings', $sAllReplicas, $sAllErrors, $sAllWarnings).'</h2>');
  234. }
  235. $oPage->add('<table class="synoptics"><tr><td style="color:#333;vertical-align:top">');
  236. // List all the log entries for the user to select
  237. $oPage->add('<h2 style="line-height:55px;">'.Dict::S('Core:Synchro:History').'</h2>');
  238. $oSetSynchroLog->Rewind();
  239. $oPage->add('<select size="25" onChange="UpdateSynoptics(this.value);">');
  240. $sSelected = ' selected'; // First log is selected by default
  241. $sScript = "var aSynchroLog = {\n";
  242. while($oLog = $oSetSynchroLog->Fetch())
  243. {
  244. $sLogTitle = Dict::Format('Core:SynchroLogTitle', $oLog->Get('status'), $oLog->Get('start_date'));
  245. $oPage->add('<option value="'.$oLog->GetKey().'"'.$sSelected.'>'.$sLogTitle.'</option>');
  246. $sSelected = ''; // only the first log is selected by default
  247. $aData = $this->ProcessLog($oLog);
  248. $sScript .= '"'.$oLog->GetKey().'": '.json_encode($aData).",\n";
  249. }
  250. $sScript .= "end: 'Done'";
  251. $sScript .= "};\n";
  252. $sScript .= <<<EOF
  253. var sLastLog = '$iLastLog';
  254. function ToggleSynoptics(sId, bShow)
  255. {
  256. if (bShow)
  257. {
  258. $(sId).show();
  259. }
  260. else
  261. {
  262. $(sId).hide();
  263. }
  264. }
  265. function UpdateSynoptics(id)
  266. {
  267. var aValues = aSynchroLog[id];
  268. for (var sKey in aValues)
  269. {
  270. $('#c_'+sKey).html(aValues[sKey]);
  271. var fOpacity = (aValues[sKey] == 0) ? 0.3 : 1;
  272. $('#'+sKey).fadeTo("slow", fOpacity);
  273. }
  274. //alert('id = '+id+', lastLog='+sLastLog+', id==sLastLog: '+(id==sLastLog)+' obj_updated_errors: '+aValues['obj_updated_errors']);
  275. if ( (id == sLastLog) && (aValues['obj_new_errors'] > 0) )
  276. {
  277. $('#new_errors_link').show();
  278. }
  279. else
  280. {
  281. $('#new_errors_link').hide();
  282. }
  283. if ( (id == sLastLog) && (aValues['obj_updated_errors'] > 0) )
  284. {
  285. $('#updated_errors_link').show();
  286. }
  287. else
  288. {
  289. $('#updated_errors_link').hide();
  290. }
  291. if ( (id == sLastLog) && (aValues['obj_disappeared_errors'] > 0) )
  292. {
  293. $('#disappeared_errors_link').show();
  294. }
  295. else
  296. {
  297. $('#disappeared_errors_link').hide();
  298. }
  299. ToggleSynoptics('#cw_obj_created_warnings', aValues['obj_created_warnings'] > 0);
  300. ToggleSynoptics('#cw_obj_new_updated_warnings', aValues['obj_new_updated_warnings'] > 0);
  301. ToggleSynoptics('#cw_obj_new_unchanged_warnings', aValues['obj_new_unchanged_warnings'] > 0);
  302. ToggleSynoptics('#cw_obj_updated_warnings', aValues['obj_updated_warnings'] > 0);
  303. ToggleSynoptics('#cw_obj_unchanged_warnings', aValues['obj_unchanged_warnings'] > 0);
  304. }
  305. EOF
  306. ;
  307. $oPage->add_script($sScript);
  308. $oPage->add('</select>');
  309. $oPage->add('</td><td style="vertical-align:top;">');
  310. // Now build the big "synoptics" view
  311. $aData = $this->ProcessLog($oLastLog);
  312. $sNbReplica = $this->GetIcon()."&nbsp;".Dict::Format('Core:Synchro:Nb_Replica', "<span id=\"c_nb_replica_total\">{$aData['nb_replica_total']}</span>");
  313. $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>");
  314. $oPage->add(
  315. <<<EOF
  316. <table class="synoptics">
  317. <tr class="synoptics_header">
  318. <td>$sNbReplica</td><td>&nbsp;</td><td>$sNbObjects</td>
  319. </tr>
  320. <tr>
  321. EOF
  322. );
  323. $sBaseOQL = "SELECT SynchroReplica WHERE sync_source_id=".$this->GetKey()." AND status_last_error!=''";
  324. $oPage->add($this->HtmlBox('repl_ignored', $aData, '#999').'<td colspan="2">&nbsp;</td>');
  325. $oPage->add("</tr>\n<tr>");
  326. $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'));
  327. $oPage->add("</tr>\n<tr>");
  328. $oPage->add($this->HtmlBox('obj_deleted', $aData, '#000'));
  329. $oPage->add("</tr>\n<tr>");
  330. $oPage->add($this->HtmlBox('obj_obsoleted', $aData, '#630'));
  331. $oPage->add("</tr>\n<tr>");
  332. $sOQL = urlencode($sBaseOQL." AND status='obsolete'");
  333. $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>"));
  334. $oPage->add("</tr>\n<tr>");
  335. $oPage->add($this->HtmlBox('repl_existing', $aData, '#093', 'rowspan="3"').'<td rowspan="3" class="arrow">=&gt;</td>'.$this->HtmlBox('obj_unchanged', $aData, '#393'));
  336. $oPage->add("</tr>\n<tr>");
  337. $oPage->add($this->HtmlBox('obj_updated', $aData, '#3C3'));
  338. $oPage->add("</tr>\n<tr>");
  339. $sOQL = urlencode($sBaseOQL." AND status='modified'");
  340. $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>"));
  341. $oPage->add("</tr>\n<tr>");
  342. $oPage->add($this->HtmlBox('repl_new', $aData, '#339', 'rowspan="4"').'<td rowspan="4" class="arrow">=&gt;</td>'.$this->HtmlBox('obj_new_unchanged', $aData, '#393'));
  343. $oPage->add("</tr>\n<tr>");
  344. $oPage->add($this->HtmlBox('obj_new_updated', $aData, '#3C3'));
  345. $oPage->add("</tr>\n<tr>");
  346. $oPage->add($this->HtmlBox('obj_created', $aData, '#339'));
  347. $oPage->add("</tr>\n<tr>");
  348. $sOQL = urlencode($sBaseOQL." AND status='new'");
  349. $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>"));
  350. $oPage->add("</tr>\n</table>\n");
  351. $oPage->add('</td></tr></table>');
  352. $oPage->add_ready_script("UpdateSynoptics('$iLastLog')");
  353. }
  354. else
  355. {
  356. $oPage->p('<h2>'.Dict::S('Core:Synchro:NeverRun').'</h2>');
  357. }
  358. }
  359. protected function HtmlBox($sId, $aData, $sColor, $sHTMLAttribs = '', $sErrorLink = '')
  360. {
  361. $iCount = $aData[$sId];
  362. $sCount = "<span id=\"c_{$sId}\">$iCount</span>";
  363. $sLabel = Dict::Format('Core:Synchro:label_'.$sId, $sCount);
  364. $sOpacity = ($iCount==0) ? "opacity:0.3;" : "";
  365. if (isset($aData[$sId.'_warnings']))
  366. {
  367. $sLabel .= " <span id=\"cw_{$sId}_warnings\"><img src=\"../images/error.png\" style=\"vertical-align:middle\"/> (<span id=\"c_{$sId}_warnings\">".$aData[$sId.'_warnings']."</span>)</span>";
  368. }
  369. return "<td id=\"$sId\" style=\"background-color:$sColor;$sOpacity;\" {$sHTMLAttribs}>{$sLabel}{$sErrorLink}</td>";
  370. }
  371. protected function ProcessLog($oLastLog)
  372. {
  373. $aData = array(
  374. 'obj_deleted' => $oLastLog->Get('stats_nb_obj_deleted'),
  375. 'obj_obsoleted' => $oLastLog->Get('stats_nb_obj_obsoleted'),
  376. 'obj_disappeared_errors' => $oLastLog->Get('stats_nb_obj_obsoleted_errors') + $oLastLog->Get('stats_nb_obj_deleted_errors'),
  377. 'obj_disappeared_no_action' => $oLastLog->Get('stats_nb_replica_disappeared_no_action'),
  378. 'obj_updated' => $oLastLog->Get('stats_nb_obj_updated'),
  379. 'obj_updated_warnings' => $oLastLog->Get('stats_nb_obj_updated_warnings'),
  380. 'obj_updated_errors' => $oLastLog->Get('stats_nb_obj_updated_errors'),
  381. 'obj_new_updated' => $oLastLog->Get('stats_nb_obj_new_updated'),
  382. 'obj_new_updated_warnings' => $oLastLog->Get('stats_nb_obj_new_updated_warnings'),
  383. 'obj_new_unchanged' => $oLastLog->Get('stats_nb_obj_new_unchanged'),
  384. 'obj_created' => $oLastLog->Get('stats_nb_obj_created'),
  385. 'obj_created_warnings' => $oLastLog->Get('stats_nb_obj_created_warnings'),
  386. 'obj_created_errors' => $oLastLog->Get('stats_nb_obj_created_errors'),
  387. 'obj_unchanged_warnings' => $oLastLog->Get('stats_nb_obj_unchanged_warnings'),
  388. );
  389. $iReconciledErrors = $oLastLog->Get('stats_nb_replica_reconciled_errors');
  390. $iDisappeared = $aData['obj_disappeared_errors'] + $aData['obj_obsoleted'] + $aData['obj_deleted'] + $aData['obj_disappeared_no_action'];
  391. $aData['repl_disappeared'] = $iDisappeared;
  392. $iNewErrors = $aData['obj_created_errors'] + $oLastLog->Get('stats_nb_replica_reconciled_errors');
  393. $aData['obj_new_errors'] = $iNewErrors;
  394. $iNew = $aData['obj_created'] + $iNewErrors + $aData['obj_new_updated'] + $aData['obj_new_unchanged'];
  395. $aData['repl_new'] = $iNew;
  396. $iExisting = $oLastLog->Get('stats_nb_replica_seen') - $iNew;
  397. $aData['repl_existing'] = $iExisting;
  398. $aData['obj_unchanged'] = $iExisting - $aData['obj_updated'] - $aData['obj_updated_errors'];
  399. $iIgnored = $oLastLog->Get('stats_nb_replica_total') - $iNew - $iExisting - $iDisappeared;
  400. $aData['repl_ignored'] = $iIgnored;
  401. $aData['nb_obj_total'] = $iNew + $iExisting + $iDisappeared;
  402. $aData['nb_replica_total'] = $aData['nb_obj_total'] + $iIgnored;
  403. return $aData;
  404. }
  405. public function GetIcon($bImgTag = true, $sMoreStyles = '')
  406. {
  407. if ($this->Get('url_icon') == '') return MetaModel::GetClassIcon(get_class($this), $bImgTag);
  408. if ($bImgTag)
  409. {
  410. return "<img src=\"".$this->Get('url_icon')."\" style=\"vertical-align:middle;$sMoreStyles\"/>";
  411. }
  412. return $this->Get('url_icon');
  413. }
  414. /**
  415. * Get the actual hyperlink to the remote application for the given replica and dest object
  416. */
  417. public function GetApplicationUrl(DBObject $oDestObj, SynchroReplica $oReplica)
  418. {
  419. if ($this->Get('url_application') == '') return '';
  420. $aSearches = array();
  421. $aReplacements = array();
  422. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  423. {
  424. if ($oAttDef->IsScalar())
  425. {
  426. $aSearches[] = '$this->'.$sAttCode.'$';
  427. $aReplacements[] = $oDestObj->Get($sAttCode);
  428. }
  429. }
  430. $aData = $oReplica->LoadExtendedDataFromTable($this->GetDataTable());
  431. foreach($aData as $sColumn => $value)
  432. {
  433. $aSearches[] = '$replica->'.$sColumn.'$';
  434. $aReplacements[] = $value;
  435. }
  436. return str_replace($aSearches, $aReplacements, $this->Get('url_application'));
  437. }
  438. public function GetAttributeFlags($sAttCode, &$aReasons = array(), $sTargetState = '')
  439. {
  440. if (($sAttCode == 'scope_class') && (!$this->IsNew()))
  441. {
  442. return OPT_ATT_READONLY;
  443. }
  444. return parent::GetAttributeFlags($sAttCode, $aReasons, $sTargetState);
  445. }
  446. public function UpdateObjectFromPostedForm($sFormPrefix = '', $sAttList = null, $sTargetState = '')
  447. {
  448. parent::UpdateObjectFromPostedForm($sFormPrefix, $sAttList, $sTargetState);
  449. // And now read the other post parameters...
  450. $oAttributeSet = $this->Get('attribute_list');
  451. $aAttributes = array();
  452. while($oAttribute = $oAttributeSet->Fetch())
  453. {
  454. $aAttributes[$oAttribute->Get('attcode')] = $oAttribute;
  455. }
  456. $aReconcile = utils::ReadPostedParam('reconciliation', array());
  457. $aUpdate = utils::ReadPostedParam('update', array());
  458. $aUpdatePolicy = utils::ReadPostedParam('attr_update_policy', array());
  459. $aReconciliation = utils::ReadPostedParam('attr_reconciliation_attcode', array());
  460. // update_policy cannot be empty, so there is one entry per attribute, use this to iterate
  461. // through all the writable attributes
  462. foreach($aUpdatePolicy as $sAttCode => $sValue)
  463. {
  464. if(!isset($aAttributes[$sAttCode]))
  465. {
  466. $oAttribute = $this->CreateSynchroAtt($sAttCode);
  467. }
  468. else
  469. {
  470. $oAttribute = $aAttributes[$sAttCode];
  471. }
  472. $bReconcile = 0;
  473. if (isset($aReconcile[$sAttCode]))
  474. {
  475. $bReconcile = $aReconcile[$sAttCode] == 'on' ? 1 : 0;
  476. }
  477. $bUpdate = 0 ; // Default / initial value
  478. if (isset($aUpdate[$sAttCode]))
  479. {
  480. $bUpdate = $aUpdate[$sAttCode] == 'on' ? 1 : 0;
  481. }
  482. $oAttribute->Set('reconcile', $bReconcile);
  483. $oAttribute->Set('update', $bUpdate);
  484. $oAttribute->Set('update_policy', $sValue);
  485. if ($oAttribute instanceof SynchroAttExtKey)
  486. {
  487. $oAttribute->Set('reconciliation_attcode', $aReconciliation[$sAttCode]);
  488. }
  489. elseif ($oAttribute instanceof SynchroAttLinkSet)
  490. {
  491. }
  492. $oAttributeSet->AddObject($oAttribute);
  493. }
  494. $this->Set('attribute_list', $oAttributeSet);
  495. }
  496. /**
  497. * Creates a new SynchroAttXXX object in memory with the default values
  498. */
  499. protected function CreateSynchroAtt($sAttCode)
  500. {
  501. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  502. if ($oAttDef->IsExternalKey())
  503. {
  504. $oAttribute = new SynchroAttExtKey();
  505. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  506. }
  507. elseif ($oAttDef->IsLinkSet() && $oAttDef->IsIndirect())
  508. {
  509. $oAttribute = new SynchroAttLinkSet();
  510. // Todo - set those value from the form
  511. $oAttribute->Set('row_separator', MetaModel::GetConfig()->Get('link_set_item_separator'));
  512. $oAttribute->Set('attribute_separator', MetaModel::GetConfig()->Get('link_set_attribute_separator'));
  513. $oAttribute->Set('value_separator', MetaModel::GetConfig()->Get('link_set_value_separator'));
  514. $oAttribute->Set('attribute_qualifier', MetaModel::GetConfig()->Get('link_set_attribute_qualifier'));
  515. }
  516. else
  517. {
  518. $oAttribute = new SynchroAttribute();
  519. }
  520. $oAttribute->Set('sync_source_id', $this->GetKey());
  521. $oAttribute->Set('attcode', $sAttCode);
  522. $oAttribute->Set('reconcile', 0);
  523. $oAttribute->Set('update', 0);
  524. $oAttribute->Set('update_policy', 'master_locked');
  525. return $oAttribute;
  526. }
  527. /**
  528. * Overload the standard behavior
  529. */
  530. public function ComputeValues()
  531. {
  532. parent::ComputeValues();
  533. if ($this->IsNew())
  534. {
  535. // When inserting a new datasource object, also create the SynchroAttribute objects
  536. // for each field of the target class
  537. // Create all the SynchroAttribute records
  538. $oAttributeSet = $this->Get('attribute_list');
  539. if ($oAttributeSet->Count() == 0)
  540. {
  541. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode=>$oAttDef)
  542. {
  543. if ($oAttDef->IsWritable())
  544. {
  545. $oAttDef = MetaModel::GetAttributeDef($this->GetTargetClass(), $sAttCode);
  546. if ($oAttDef->IsExternalKey())
  547. {
  548. $oAttribute = new SynchroAttExtKey();
  549. $oAttribute->Set('reconciliation_attcode', ''); // Blank means by pkey
  550. }
  551. elseif ($oAttDef->IsLinkSet() && $oAttDef->IsIndirect())
  552. {
  553. $oAttribute = new SynchroAttLinkSet();
  554. // Todo - set those value from the form
  555. $oAttribute->Set('row_separator', MetaModel::GetConfig()->Get('link_set_item_separator'));
  556. $oAttribute->Set('attribute_separator', MetaModel::GetConfig()->Get('link_set_attribute_separator'));
  557. $oAttribute->Set('value_separator', MetaModel::GetConfig()->Get('link_set_value_separator'));
  558. $oAttribute->Set('attribute_qualifier', MetaModel::GetConfig()->Get('link_set_attribute_qualifier'));
  559. }
  560. elseif ($oAttDef->IsScalar())
  561. {
  562. $oAttribute = new SynchroAttribute();
  563. }
  564. else
  565. {
  566. $oAttribute = null;
  567. }
  568. if (!is_null($oAttribute))
  569. {
  570. $oAttribute->Set('sync_source_id', $this->GetKey());
  571. $oAttribute->Set('attcode', $sAttCode);
  572. $oAttribute->Set('reconcile', MetaModel::IsReconcKey($this->GetTargetClass(), $sAttCode) ? 1 : 0);
  573. $oAttribute->Set('update', 1);
  574. $oAttribute->Set('update_policy', 'master_locked');
  575. $oAttributeSet->AddObject($oAttribute);
  576. }
  577. }
  578. }
  579. $this->Set('attribute_list', $oAttributeSet);
  580. }
  581. }
  582. }
  583. public function DoCheckToWrite()
  584. {
  585. parent::DoCheckToWrite();
  586. // Check that there is at least one reconciliation key defined
  587. if ($this->Get('reconciliation_policy') == 'use_attributes')
  588. {
  589. $oSet = $this->Get('attribute_list');
  590. $oSynchroAttributeList = $oSet->ToArray();
  591. $bReconciliationKey = false;
  592. foreach($oSynchroAttributeList as $oSynchroAttribute)
  593. {
  594. if ($oSynchroAttribute->Get('reconcile') == 1)
  595. {
  596. $bReconciliationKey = true; // At least one key is defined
  597. break;
  598. }
  599. }
  600. if (!$bReconciliationKey)
  601. {
  602. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:AtLeastOneReconciliationKeyMustBeSpecified');
  603. }
  604. }
  605. // If 'update_then_delete' is specified there must be a delete_retention_period
  606. if (($this->Get('delete_policy') == 'update_then_delete') && ($this->Get('delete_policy_retention') == 0))
  607. {
  608. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:DeleteRetentionDurationMustBeSpecified');
  609. }
  610. // If update is specified, then something to update must be defined
  611. if ((($this->Get('delete_policy') == 'update_then_delete') || ($this->Get('delete_policy') == 'update'))
  612. && ($this->Get('delete_policy_update') == ''))
  613. {
  614. $this->m_aCheckIssues[] = Dict::Format('Class:SynchroDataSource/Error:DeletePolicyUpdateMustBeSpecified');
  615. }
  616. }
  617. public function GetTargetClass()
  618. {
  619. return $this->Get('scope_class');
  620. }
  621. public function GetDataTable()
  622. {
  623. $sName = strtolower($this->GetTargetClass());
  624. $sName = str_replace('\'"&@|\\/ ', '_', $sName); // Remove forbidden characters from the table name
  625. $sName .= '_'.$this->GetKey(); // Add a suffix for unicity
  626. $sTable = MetaModel::GetConfig()->GetDBSubName()."synchro_data_$sName"; // Add the prefix if any
  627. return $sTable;
  628. }
  629. /**
  630. * When the new datasource has been created, let's create the synchro_data table
  631. * that will hold the data records and the correspoding triggers which will maintain
  632. * both tables in sync
  633. */
  634. protected function AfterInsert()
  635. {
  636. parent::AfterInsert();
  637. $sTable = $this->GetDataTable();
  638. $sReplicaTable = MetaModel::DBGetTable('SynchroReplica');
  639. $aColumns = $this->GetSQLColumns();
  640. $aFieldDefs = array();
  641. // Allow '0', otherwise mysql will render an error when the id is not given
  642. // (the trigger is expected to set the value, but it is not executed soon enough)
  643. $aFieldDefs[] = "id INTEGER(11) NOT NULL DEFAULT 0 ";
  644. $aFieldDefs[] = "`primary_key` VARCHAR(255) NULL DEFAULT NULL";
  645. foreach($aColumns as $sColumn => $ColSpec)
  646. {
  647. $aFieldDefs[] = "`$sColumn` $ColSpec NULL DEFAULT NULL";
  648. }
  649. $aFieldDefs[] = "INDEX (id)";
  650. $aFieldDefs[] = "INDEX (primary_key)";
  651. $sFieldDefs = implode(', ', $aFieldDefs);
  652. $sCreateTable = "CREATE TABLE `$sTable` ($sFieldDefs) ENGINE = innodb;";
  653. CMDBSource::Query($sCreateTable);
  654. $aTriggers = $this->GetTriggersDefinition();
  655. foreach($aTriggers as $key => $sTriggerSQL)
  656. {
  657. CMDBSource::Query($sTriggerSQL);
  658. }
  659. }
  660. /**
  661. * Gets the definitions of the 3 triggers: before insert, before update and after delete
  662. * @return array An array with 3 entries, one for each of the SQL queries
  663. */
  664. protected function GetTriggersDefinition()
  665. {
  666. $sTable = $this->GetDataTable();
  667. $sReplicaTable = MetaModel::DBGetTable('SynchroReplica');
  668. $aColumns = $this->GetSQLColumns();
  669. $aResult = array();
  670. $sTriggerInsert = "CREATE TRIGGER `{$sTable}_bi` BEFORE INSERT ON `$sTable`";
  671. $sTriggerInsert .= " FOR EACH ROW";
  672. $sTriggerInsert .= " BEGIN";
  673. $sTriggerInsert .= " INSERT INTO `{$sReplicaTable}` (`sync_source_id`, `status_last_seen`, `status`) VALUES ({$this->GetKey()}, NOW(), 'new');";
  674. $sTriggerInsert .= " SET NEW.id = LAST_INSERT_ID();";
  675. $sTriggerInsert .= " END;";
  676. $aResult['bi'] = $sTriggerInsert;
  677. $aModified = array();
  678. foreach($aColumns as $sColumn => $ColSpec)
  679. {
  680. // <=> is a null-safe 'EQUALS' operator (there is no equivalent for "DIFFERS FROM")
  681. $aModified[] = "NOT(NEW.`$sColumn` <=> OLD.`$sColumn`)";
  682. }
  683. $sIsModified = '('.implode(') OR (', $aModified).')';
  684. // Update the replica
  685. //
  686. // status is forced to "new" if the replica was obsoleted directly from the state "new" (dest_id = null)
  687. // otherwise, if status was either 'obsolete' or 'synchronized' it is turned into 'modified' or 'synchronized' depending on the changes
  688. // otherwise, the status is left as is
  689. $sTriggerUpdate = "CREATE TRIGGER `{$sTable}_bu` BEFORE UPDATE ON `$sTable`";
  690. $sTriggerUpdate .= " FOR EACH ROW";
  691. $sTriggerUpdate .= " BEGIN";
  692. $sTriggerUpdate .= " IF @itopuser is null THEN";
  693. $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;";
  694. $sTriggerUpdate .= " SET NEW.id = OLD.id;"; // make sure this id won't change
  695. $sTriggerUpdate .= " END IF;";
  696. $sTriggerUpdate .= " END;";
  697. $aResult['bu'] = $sTriggerUpdate;
  698. $sTriggerDelete = "CREATE TRIGGER `{$sTable}_ad` AFTER DELETE ON `$sTable`";
  699. $sTriggerDelete .= " FOR EACH ROW";
  700. $sTriggerDelete .= " BEGIN";
  701. $sTriggerDelete .= " DELETE FROM `{$sReplicaTable}` WHERE id = OLD.id;";
  702. $sTriggerDelete .= " END;";
  703. $aResult['ad'] = $sTriggerDelete;
  704. return $aResult;
  705. }
  706. protected function AfterDelete()
  707. {
  708. parent::AfterDelete();
  709. $sTable = $this->GetDataTable();
  710. $sDropTable = "DROP TABLE `$sTable`";
  711. CMDBSource::Query($sDropTable);
  712. // TO DO - check that triggers get dropped with the table
  713. }
  714. /**
  715. * Checks if the data source definition is consistent with the schema of the target class
  716. * @param $bDiagnostics boolean True to only diagnose the consistency, false to actually apply some changes
  717. * @param $bVerbose boolean True to get some information in the std output (echo)
  718. * @return bool Whether or not the database needs fixing for this data source
  719. */
  720. public function CheckDBConsistency($bDiagnostics, $bVerbose, $oChange = null)
  721. {
  722. $bFixNeeded = false;
  723. $aMissingFields = array();
  724. $oAttributeSet = $this->Get('attribute_list');
  725. $aAttributes = array();
  726. while($oAttribute = $oAttributeSet->Fetch())
  727. {
  728. $sAttCode = $oAttribute->Get('attcode');
  729. if (MetaModel::IsValidAttCode($this->GetTargetClass(), $sAttCode))
  730. {
  731. $aAttributes[$sAttCode] = $oAttribute;
  732. }
  733. else
  734. {
  735. // Old field remaining
  736. if ($bVerbose)
  737. {
  738. echo "Irrelevant field description for the field '$sAttCode', for the data synchro task ".$this->GetName()." (".$this->GetKey()."), will be removed.\n";
  739. }
  740. $bFixNeeded = true;
  741. if (!$bDiagnostics)
  742. {
  743. if ($oChange == null)
  744. {
  745. $oChange = MetaModel::NewObject("CMDBChange");
  746. $oChange->Set("date", time());
  747. $sUserString = CMDBChange::GetCurrentUserName();
  748. $oChange->Set("userinfo", $sUserString);
  749. $oChange->DBInsert();
  750. }
  751. // Fix the issue
  752. $oAttribute->DBDeleteTracked($oChange);
  753. }
  754. }
  755. }
  756. foreach($this->ListTargetAttributes() as $sAttCode=>$oAttDef)
  757. {
  758. if (!isset($aAttributes[$sAttCode]))
  759. {
  760. $bFixNeeded = true;
  761. $aMissingFields[] = $sAttCode;
  762. // New field missing...
  763. if ($bVerbose)
  764. {
  765. echo "Missing field description for the field '$sAttCode', for the data synchro task ".$this->GetName()." (".$this->GetKey()."), will be created with default values.\n";
  766. }
  767. if (!$bDiagnostics)
  768. {
  769. if ($oChange == null)
  770. {
  771. $oChange = MetaModel::NewObject("CMDBChange");
  772. $oChange->Set("date", time());
  773. $sUserString = CMDBChange::GetCurrentUserName();
  774. $oChange->Set("userinfo", $sUserString);
  775. $oChange->DBInsert();
  776. }
  777. // Fix the issue
  778. $oAttribute = $this->CreateSynchroAtt($sAttCode);
  779. $oAttribute->DBInsertTracked($oChange);
  780. }
  781. }
  782. }
  783. $sTable = $this->GetDataTable();
  784. if ($bFixNeeded && (count($aMissingFields) > 0))
  785. {
  786. $aRepairQueries = array();
  787. // The structure of the table needs adjusting
  788. $aColumns = $this->GetSQLColumns($aMissingFields);
  789. $aFieldDefs = array();
  790. foreach($aColumns as $sAttCode => $sColumnDef)
  791. {
  792. if (CMDBSource::IsField($sTable, $sAttCode))
  793. {
  794. $aRepairQueries[] = "ALTER TABLE `$sTable` DROP COLUMN `$sAttCode`;";
  795. }
  796. $aFieldDefs[] = "`$sAttCode` $sColumnDef";
  797. }
  798. $aRepairQueries[] = "ALTER TABLE `$sTable` ADD (".implode(',', $aFieldDefs).");";
  799. // The triggers as well must be adjusted
  800. $aTriggersDefs = $this->GetTriggersDefinition();
  801. $aRepairQueries[] = "DROP TRIGGER IF EXISTS `{$sTable}_bi`;";
  802. $aRepairQueries[] = $aTriggersDefs['bi'];
  803. $aRepairQueries[] = "DROP TRIGGER IF EXISTS `{$sTable}_bu`;";
  804. $aRepairQueries[] = $aTriggersDefs['bu'];
  805. $aRepairQueries[] = "DROP TRIGGER IF EXISTS `{$sTable}_ad`;";
  806. $aRepairQueries[] = $aTriggersDefs['ad'];
  807. if ($bDiagnostics)
  808. {
  809. if ($bVerbose)
  810. {
  811. // Report the issue
  812. echo "The structure of the table $sTable for the data synchro task ".$this->GetName()." (".$this->GetKey().") must be altered (missing fields: ".implode(',', $aMissingFields).").\n";
  813. echo "The trigger {$sTable}_bi, {$sTable}_bu, {$sTable}_ad for the data synchro task ".$this->GetName()." (".$this->GetKey().") must be re-created.\n";
  814. echo implode("\n", $aRepairQueries)."\n";
  815. }
  816. }
  817. else
  818. {
  819. // Fix the issue
  820. foreach($aRepairQueries as $sSQL)
  821. {
  822. CMDBSource::Query($sSQL);
  823. if ($bVerbose)
  824. {
  825. echo "$sSQL\n";
  826. }
  827. }
  828. }
  829. }
  830. return $bFixNeeded;
  831. }
  832. public function SendNotification($sSubject, $sBody)
  833. {
  834. $iContact = $this->Get('notify_contact_id');
  835. if ($iContact == 0)
  836. {
  837. // Leave silently...
  838. return;
  839. }
  840. $oContact = MetaModel::GetObject('Contact', $iContact);
  841. // Determine the email attribute (the first one will be our choice)
  842. $sEmailAttCode = null;
  843. foreach (MetaModel::ListAttributeDefs(get_class($oContact)) as $sAttCode => $oAttDef)
  844. {
  845. if ($oAttDef instanceof AttributeEmailAddress)
  846. {
  847. $sEmailAttCode = $sAttCode;
  848. // we've got one, exit the loop
  849. break;
  850. }
  851. }
  852. if (is_null($sEmailAttCode))
  853. {
  854. // Leave silently...
  855. return;
  856. }
  857. $sTo = $oContact->Get($sEmailAttCode);
  858. $sFrom = $sTo;
  859. $sBody = '<p>Data synchronization: '.$this->GetHyperlink().'</p>'.$sBody;
  860. $sSubject = 'iTop Data Sync - '.$this->GetName().' - '.$sSubject;
  861. $oEmail = new Email();
  862. $oEmail->SetRecipientTO($sTo);
  863. $oEmail->SetRecipientFrom($sFrom);
  864. $oEmail->SetSubject($sSubject);
  865. $oEmail->SetBody($sBody);
  866. if ($oEmail->Send($aIssues) == EMAIL_SEND_ERROR)
  867. {
  868. // mmmm, what can I do?
  869. }
  870. }
  871. /**
  872. * Get the list of attributes eligible to the synchronization
  873. */
  874. public function ListTargetAttributes()
  875. {
  876. $aRet = array();
  877. foreach(MetaModel::ListAttributeDefs($this->GetTargetClass()) as $sAttCode => $oAttDef)
  878. {
  879. if ($sAttCode == 'finalclass') continue;
  880. if (!$oAttDef->IsWritable()) continue;
  881. if ($oAttDef->IsLinkSet() && !$oAttDef->IsIndirect()) continue;
  882. $aRet[$sAttCode] = $oAttDef;
  883. }
  884. return $aRet;
  885. }
  886. /**
  887. * Get the list of SQL columns corresponding to a particular list of attribute codes
  888. * Defaults to the whole list of columns for the current class
  889. */
  890. public function GetSQLColumns($aAttributeCodes = null)
  891. {
  892. $aColumns = array();
  893. $sClass = $this->GetTargetClass();
  894. if (is_null($aAttributeCodes))
  895. {
  896. $aAttributeCodes = array();
  897. foreach($this->ListTargetAttributes() as $sAttCode => $oAttDef)
  898. {
  899. $aAttributeCodes[] = $sAttCode;
  900. }
  901. }
  902. foreach($aAttributeCodes as $sAttCode)
  903. {
  904. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  905. if ($oAttDef->IsExternalKey())
  906. {
  907. // The pkey might be used as well as any other key column
  908. $aColumns[$sAttCode] = 'VARCHAR (255)';
  909. }
  910. else
  911. {
  912. foreach($oAttDef->GetImportColumns() as $sField => $sDBFieldType)
  913. {
  914. $aColumns[$sField] = $sDBFieldType;
  915. }
  916. }
  917. }
  918. return $aColumns;
  919. }
  920. /**
  921. * Get the list of Date and Datetime SQL columns
  922. */
  923. public function GetDateSQLColumns()
  924. {
  925. $aDateAttributes = array();
  926. $sClass = $this->GetTargetClass();
  927. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  928. {
  929. if ($oAttDef instanceof AttributeDateTime)
  930. {
  931. $aDateAttributes[] = $sAttCode;
  932. }
  933. }
  934. return $this->GetSQLColumns($aDateAttributes);
  935. }
  936. public function IsRunning()
  937. {
  938. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id AND status='running'";
  939. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 1 /* limitCount */, 0 /* limitStart */);
  940. if ($oSet->Count() < 1)
  941. {
  942. $bRet = false;
  943. }
  944. else
  945. {
  946. $bRet = true;
  947. }
  948. return $bRet;
  949. }
  950. public function GetLatestLog()
  951. {
  952. $oLog = null;
  953. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id";
  954. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('start_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 1 /* limitCount */, 0 /* limitStart */);
  955. if ($oSet->Count() >= 1)
  956. {
  957. $oLog = $oSet->Fetch();
  958. }
  959. return $oLog;
  960. }
  961. // TO DO: remove if still unused
  962. /**
  963. * Retrieve from the log, the date of the last completed import
  964. * @return DateTime
  965. */
  966. public function GetLastCompletedImportDate()
  967. {
  968. $date = null;
  969. $sOQL = "SELECT SynchroLog WHERE sync_source_id = :source_id AND status='completed'";
  970. $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array('end_date' => false) /* order by*/, array('source_id' => $this->GetKey()) /* aArgs */, array(), 0 /* limitCount */, 0 /* limitStart */);
  971. if ($oSet->Count() >= 1)
  972. {
  973. $oLog = $oSet->Fetch();
  974. $date = $oLog->Get('end_date');
  975. }
  976. return $date;
  977. }
  978. }
  979. class SynchroAttribute extends cmdbAbstractObject
  980. {
  981. public static function Init()
  982. {
  983. $aParams = array
  984. (
  985. "category" => "core/cmdb,view_in_gui",
  986. "key_type" => "autoincrement",
  987. "name_attcode" => "",
  988. "state_attcode" => "",
  989. "reconc_keys" => array(),
  990. "db_table" => "priv_sync_att",
  991. "db_key_field" => "id",
  992. "db_finalclass_field" => "",
  993. "display_template" => "",
  994. );
  995. MetaModel::Init_Params($aParams);
  996. MetaModel::Init_InheritAttributes();
  997. 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())));
  998. MetaModel::Init_AddAttribute(new AttributeExternalField("sync_source_name", array("allowed_values"=>null, "extkey_attcode"=> 'sync_source_id', "target_attcode"=>"name")));
  999. MetaModel::Init_AddAttribute(new AttributeString("attcode", array("allowed_values"=>null, "sql"=>"attcode", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  1000. MetaModel::Init_AddAttribute(new AttributeBoolean("update", array("allowed_values"=>null, "sql"=>"update", "default_value"=>true, "is_null_allowed"=>false, "depends_on"=>array())));
  1001. MetaModel::Init_AddAttribute(new AttributeBoolean("reconcile", array("allowed_values"=>null, "sql"=>"reconcile", "default_value"=>false, "is_null_allowed"=>false, "depends_on"=>array())));
  1002. 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())));
  1003. // Display lists
  1004. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for the complete details
  1005. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  1006. // Search criteria
  1007. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  1008. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1009. }
  1010. }
  1011. class SynchroAttExtKey extends SynchroAttribute
  1012. {
  1013. public static function Init()
  1014. {
  1015. $aParams = array
  1016. (
  1017. "category" => "core/cmdb,view_in_gui",
  1018. "key_type" => "autoincrement",
  1019. "name_attcode" => "",
  1020. "state_attcode" => "",
  1021. "reconc_keys" => array(),
  1022. "db_table" => "priv_sync_att_extkey",
  1023. "db_key_field" => "id",
  1024. "db_finalclass_field" => "",
  1025. "display_template" => "",
  1026. );
  1027. MetaModel::Init_Params($aParams);
  1028. MetaModel::Init_InheritAttributes();
  1029. MetaModel::Init_AddAttribute(new AttributeString("reconciliation_attcode", array("allowed_values"=>null, "sql"=>"reconciliation_attcode", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  1030. // Display lists
  1031. MetaModel::Init_SetZListItems('details', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy', 'reconciliation_attcode')); // Attributes to be displayed for the complete details
  1032. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  1033. // Search criteria
  1034. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  1035. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1036. }
  1037. public function GetReconciliationFormElement($sTargetClass, $sFieldName)
  1038. {
  1039. $sHtml = "<select name=\"$sFieldName\">\n";
  1040. $sSelected = (''== $this->Get('reconciliation_attcode')) ? ' selected' : '';
  1041. $sHtml .= "<option value=\"\" $sSelected>".Dict::S('Core:SynchroAttExtKey:ReconciliationById')."</option>\n";
  1042. foreach(MetaModel::ListAttributeDefs($sTargetClass) as $sAttCode => $oAttDef)
  1043. {
  1044. if ($oAttDef->IsScalar())
  1045. {
  1046. $sSelected = ($sAttCode == $this->Get('reconciliation_attcode')) ? ' selected' : '';
  1047. $sHtml .= "<option value=\"$sAttCode\" $sSelected>".MetaModel::GetLabel($sTargetClass, $sAttCode)."</option>\n";
  1048. }
  1049. }
  1050. $sHtml .= "</select>\n";
  1051. return $sHtml;
  1052. }
  1053. }
  1054. class SynchroAttLinkSet extends SynchroAttribute
  1055. {
  1056. public static function Init()
  1057. {
  1058. $aParams = array
  1059. (
  1060. "category" => "core/cmdb,view_in_gui",
  1061. "key_type" => "autoincrement",
  1062. "name_attcode" => "",
  1063. "state_attcode" => "",
  1064. "reconc_keys" => array(),
  1065. "db_table" => "priv_sync_att_linkset",
  1066. "db_key_field" => "id",
  1067. "db_finalclass_field" => "",
  1068. "display_template" => "",
  1069. );
  1070. MetaModel::Init_Params($aParams);
  1071. MetaModel::Init_InheritAttributes();
  1072. MetaModel::Init_AddAttribute(new AttributeString("row_separator", array("allowed_values"=>null, "sql"=>"row_separator", "default_value"=>'|', "is_null_allowed"=>true, "depends_on"=>array())));
  1073. MetaModel::Init_AddAttribute(new AttributeString("attribute_separator", array("allowed_values"=>null, "sql"=>"attribute_separator", "default_value"=>';', "is_null_allowed"=>true, "depends_on"=>array())));
  1074. MetaModel::Init_AddAttribute(new AttributeString("value_separator", array("allowed_values"=>null, "sql"=>"value_separator", "default_value"=>':', "is_null_allowed"=>true, "depends_on"=>array())));
  1075. MetaModel::Init_AddAttribute(new AttributeString("attribute_qualifier", array("allowed_values"=>null, "sql"=>"attribute_qualifier", "default_value"=>'\'', "is_null_allowed"=>true, "depends_on"=>array())));
  1076. // Display lists
  1077. 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
  1078. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'attcode', 'update', 'reconcile', 'update_policy')); // Attributes to be displayed for a list
  1079. // Search criteria
  1080. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  1081. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1082. }
  1083. }
  1084. //class SynchroLog extends Event
  1085. class SynchroLog extends DBObject
  1086. {
  1087. public static function Init()
  1088. {
  1089. $aParams = array
  1090. (
  1091. "category" => "core/cmdb,view_in_gui",
  1092. "key_type" => "autoincrement",
  1093. "name_attcode" => "",
  1094. "state_attcode" => "",
  1095. "reconc_keys" => array(),
  1096. "db_table" => "priv_sync_log",
  1097. "db_key_field" => "id",
  1098. "db_finalclass_field" => "",
  1099. "display_template" => "",
  1100. );
  1101. MetaModel::Init_Params($aParams);
  1102. MetaModel::Init_InheritAttributes();
  1103. // MetaModel::Init_AddAttribute(new AttributeString("userinfo", array("allowed_values"=>null, "sql"=>"userinfo", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  1104. 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())));
  1105. MetaModel::Init_AddAttribute(new AttributeDateTime("start_date", array("allowed_values"=>null, "sql"=>"start_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  1106. MetaModel::Init_AddAttribute(new AttributeDateTime("end_date", array("allowed_values"=>null, "sql"=>"end_date", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  1107. 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())));
  1108. MetaModel::Init_AddAttribute(new AttributeInteger("status_curr_job", array("allowed_values"=>null, "sql"=>"status_curr_job", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  1109. MetaModel::Init_AddAttribute(new AttributeInteger("status_curr_pos", array("allowed_values"=>null, "sql"=>"status_curr_pos", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  1110. 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())));
  1111. 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())));
  1112. 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())));
  1113. 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())));
  1114. 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())));
  1115. 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())));
  1116. 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())));
  1117. 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())));
  1118. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_created_warnings", array("allowed_values"=>null, "sql"=>"stats_nb_obj_created_warnings", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  1119. 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())));
  1120. 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())));
  1121. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_updated_warnings", array("allowed_values"=>null, "sql"=>"stats_nb_obj_updated_warnings", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  1122. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_unchanged_warnings", array("allowed_values"=>null, "sql"=>"stats_nb_obj_unchanged_warnings", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  1123. // 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())));
  1124. 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())));
  1125. 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())));
  1126. 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())));
  1127. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_new_updated_warnings", array("allowed_values"=>null, "sql"=>"stats_nb_obj_new_updated_warnings", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  1128. 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())));
  1129. MetaModel::Init_AddAttribute(new AttributeInteger("stats_nb_obj_new_unchanged_warnings", array("allowed_values"=>null, "sql"=>"stats_nb_obj_new_unchanged_warnings", "default_value"=>0, "is_null_allowed"=>false, "depends_on"=>array())));
  1130. MetaModel::Init_AddAttribute(new AttributeText("last_error", array("allowed_values"=>null, "sql"=>"last_error", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  1131. MetaModel::Init_AddAttribute(new AttributeLongText("traces", array("allowed_values"=>null, "sql"=>"traces", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  1132. // Display lists
  1133. 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',
  1134. '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
  1135. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'start_date', 'end_date', 'status', 'stats_nb_replica_seen')); // Attributes to be displayed for a list
  1136. // Search criteria
  1137. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  1138. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1139. }
  1140. /**
  1141. * Helper
  1142. */
  1143. function GetErrorCount()
  1144. {
  1145. return $this->Get('stats_nb_obj_deleted_errors')
  1146. + $this->Get('stats_nb_obj_obsoleted_errors')
  1147. + $this->Get('stats_nb_obj_created_errors')
  1148. + $this->Get('stats_nb_obj_updated_errors')
  1149. + $this->Get('stats_nb_replica_reconciled_errors');
  1150. }
  1151. /**
  1152. * Increments a statistics counter
  1153. */
  1154. function Inc($sCode)
  1155. {
  1156. $this->Set($sCode, 1+$this->Get($sCode));
  1157. }
  1158. /**
  1159. * Implement traces management
  1160. */
  1161. protected $m_aTraces = array();
  1162. public function AddTrace($sMsg, $oReplica = null)
  1163. {
  1164. if (MetaModel::GetConfig()->Get('synchro_trace') == 'none')
  1165. {
  1166. return;
  1167. }
  1168. if ($oReplica)
  1169. {
  1170. $sDestClass = $oReplica->Get('dest_class');
  1171. if (!empty($sDestClass))
  1172. {
  1173. $sPrefix = $oReplica->GetKey().','.$sDestClass.'::'.$oReplica->Get('dest_id').',';
  1174. }
  1175. else
  1176. {
  1177. $sPrefix = $oReplica->GetKey().',,';
  1178. }
  1179. }
  1180. else
  1181. {
  1182. $sPrefix = ',,';
  1183. }
  1184. $this->m_aTraces[] = $sPrefix.$sMsg;
  1185. }
  1186. public function GetTraces()
  1187. {
  1188. return $this->m_aTraces;
  1189. }
  1190. protected function TraceToText()
  1191. {
  1192. if (MetaModel::GetConfig()->Get('synchro_trace') != 'save')
  1193. {
  1194. // none, or display only
  1195. return;
  1196. }
  1197. $sPrevTrace = $this->Get('traces');
  1198. $oAttDef = MetaModel::GetAttributeDef(get_class($this), 'traces');
  1199. $iMaxSize = $oAttDef->GetMaxSize();
  1200. if (strlen($sPrevTrace) > 0)
  1201. {
  1202. $sTrace = $sPrevTrace."\n".implode("\n", $this->m_aTraces);
  1203. }
  1204. else
  1205. {
  1206. $sTrace = implode("\n", $this->m_aTraces);
  1207. }
  1208. if (strlen($sTrace) >= $iMaxSize)
  1209. {
  1210. $sTrace = substr($sTrace, 0, $iMaxSize - 255)."...\nTruncated (size: ".strlen($sTrace).')';
  1211. }
  1212. $this->Set('traces', $sTrace);
  1213. //DBUpdate may be called many times... the operation should not be repeated
  1214. $this->m_aTraces = array();
  1215. }
  1216. protected function OnInsert()
  1217. {
  1218. $this->TraceToText();
  1219. parent::OnInsert();
  1220. }
  1221. protected function OnUpdate()
  1222. {
  1223. $this->TraceToText();
  1224. parent::OnUpdate();
  1225. }
  1226. }
  1227. class SynchroReplica extends DBObject implements iDisplay
  1228. {
  1229. static $aSearches = array(); // Cache of OQL queries used for reconciliation (per data source)
  1230. protected $aWarnings;
  1231. public static function Init()
  1232. {
  1233. $aParams = array
  1234. (
  1235. "category" => "core/cmdb,view_in_gui",
  1236. "key_type" => "autoincrement",
  1237. "name_attcode" => "",
  1238. "state_attcode" => "",
  1239. "reconc_keys" => array(),
  1240. "db_table" => "priv_sync_replica",
  1241. "db_key_field" => "id",
  1242. "db_finalclass_field" => "",
  1243. "display_template" => "",
  1244. );
  1245. MetaModel::Init_Params($aParams);
  1246. MetaModel::Init_InheritAttributes();
  1247. 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())));
  1248. MetaModel::Init_AddAttribute(new AttributeExternalField("base_class", array("allowed_values"=>null, "extkey_attcode"=> 'sync_source_id', "target_attcode"=>"scope_class")));
  1249. MetaModel::Init_AddAttribute(new AttributeInteger("dest_id", array("allowed_values"=>null, "sql"=>"dest_id", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  1250. 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())));
  1251. 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())));
  1252. 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())));
  1253. 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())));
  1254. 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())));
  1255. MetaModel::Init_AddAttribute(new AttributeString("status_last_warning", array("allowed_values"=>null, "sql"=>"status_last_warning", "default_value"=>'', "is_null_allowed"=>true, "depends_on"=>array())));
  1256. 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())));
  1257. 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())));
  1258. // Display lists
  1259. MetaModel::Init_SetZListItems('details', array('' .
  1260. 'col:0'=> array(
  1261. 'fieldset:SynchroDataSource:Definition' => array('sync_source_id','dest_id','dest_class'),
  1262. 'fieldset:SynchroDataSource:Status' => array('status','status_last_seen','status_dest_creator','status_last_error','status_last_warning'),
  1263. 'fieldset:SynchroDataSource:Information' => array('info_creation_date','info_last_modified'))
  1264. )
  1265. );
  1266. MetaModel::Init_SetZListItems('list', array('sync_source_id', 'dest_id', 'dest_class', 'status_last_seen', 'status', 'status_dest_creator', 'status_last_error', 'status_last_warning')); // Attributes to be displayed for a list
  1267. // Search criteria
  1268. MetaModel::Init_SetZListItems('standard_search', array('sync_source_id', 'status_last_seen', 'status', 'status_dest_creator', 'dest_class', 'dest_id', 'status_last_error', 'status_last_warning')); // Criteria of the std search form
  1269. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  1270. }
  1271. public function __construct($aRow = null, $sClassAlias = '', $aAttToLoad = null, $aExtendedDataSpec = null)
  1272. {
  1273. parent::__construct($aRow, $sClassAlias, $aAttToLoad, $aExtendedDataSpec);
  1274. $this->aWarnings = array();
  1275. }
  1276. protected function AddWarning($sWarningMessage)
  1277. {
  1278. $this->aWarnings[] = $sWarningMessage;
  1279. }
  1280. protected function ResetWarnings()
  1281. {
  1282. $this->aWarnings = array();
  1283. }
  1284. protected function HasWarnings()
  1285. {
  1286. return (count($this->aWarnings) > 0);
  1287. }
  1288. protected function RecordWarnings()
  1289. {
  1290. $sWarningMessage = '';
  1291. $MAX_WARNING_LENGTH = 255;
  1292. switch(count($this->aWarnings))
  1293. {
  1294. case 0:
  1295. $sWarningMessage = '';
  1296. break;
  1297. case 1:
  1298. $sWarningMessage = $this->aWarnings[0];
  1299. break;
  1300. default:
  1301. $sWarningMessage = count($this->aWarnings)." warnings: ".implode(' ', $this->aWarnings);
  1302. break;
  1303. }
  1304. if (strlen($sWarningMessage) > $MAX_WARNING_LENGTH)
  1305. {
  1306. $sWarningMessage = substr($sWarningMessage, 0, $MAX_WARNING_LENGTH - 3).'...';
  1307. }
  1308. $this->Set('status_last_warning', $sWarningMessage);
  1309. }
  1310. public function DBInsert()
  1311. {
  1312. throw new CoreException('A synchronization replica must be created only by the mean of triggers');
  1313. }
  1314. // Overload the deletion -> the replica has been created by the mean of a trigger,
  1315. // it will be deleted by the mean of a trigger too
  1316. protected function DBDeleteSingleObject()
  1317. {
  1318. $this->OnDelete();
  1319. if (!MetaModel::DBIsReadOnly())
  1320. {
  1321. $oDataSource = MetaModel::GetObject('SynchroDataSource', $this->Get('sync_source_id'));
  1322. $sTable = $oDataSource->GetDataTable();
  1323. $sSQL = "DELETE FROM `$sTable` WHERE id = '{$this->GetKey()}'";
  1324. CMDBSource::Query($sSQL);
  1325. }
  1326. $this->AfterDelete();
  1327. $this->m_bIsInDB = false;
  1328. $this->m_iKey = null;
  1329. }
  1330. public function SetLastError($sMessage, $oException = null)
  1331. {
  1332. if ($oException)
  1333. {
  1334. $sText = $sMessage.$oException->getMessage();
  1335. }
  1336. else
  1337. {
  1338. $sText = $sMessage;
  1339. }
  1340. if (strlen($sText) > 255)
  1341. {
  1342. $sText = substr($sText, 0, 200).'...('.strlen($sText).' chars)...';
  1343. }
  1344. $this->Set('status_last_error', $sText);
  1345. }
  1346. public function Synchro($oDataSource, $aReconciliationKeys, $aAttributes, $oChange, &$oStatLog)
  1347. {
  1348. $this->ResetWarnings();
  1349. switch($this->Get('status'))
  1350. {
  1351. case 'new':
  1352. $this->Set('status_dest_creator', false);
  1353. // If needed, construct the query used for the reconciliation
  1354. if (!isset(self::$aSearches[$oDataSource->GetKey()]))
  1355. {
  1356. $aCriterias = array();
  1357. foreach($aReconciliationKeys as $sFilterCode => $oSyncAtt)
  1358. {
  1359. $aCriterias[] = ($sFilterCode == 'primary_key' ? 'id' : $sFilterCode).' = :'.$sFilterCode;
  1360. }
  1361. $sOQL = "SELECT ".$oDataSource->GetTargetClass()." WHERE ".implode(' AND ', $aCriterias);
  1362. self::$aSearches[$oDataSource->GetKey()] = DBObjectSearch::FromOQL($sOQL);
  1363. }
  1364. // Get the criterias for the search
  1365. $aFilterValues = array();
  1366. foreach($aReconciliationKeys as $sFilterCode => $oSyncAtt)
  1367. {
  1368. $value = $this->GetValueFromExtData($sFilterCode, $oSyncAtt, $oStatLog);
  1369. if (!is_null($value))
  1370. {
  1371. $aFilterValues[$sFilterCode] = $value;
  1372. }
  1373. else
  1374. {
  1375. // TO DO: can we retry this ??
  1376. // Reconciliation could not be performed - log and EXIT
  1377. $oStatLog->AddTrace("Could not reconcile on null value for attribute '$sFilterCode'", $this);
  1378. $this->SetLastError("Could not reconcile on null value for attribute '$sFilterCode'");
  1379. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1380. return;
  1381. }
  1382. }
  1383. $oDestSet = new DBObjectSet(self::$aSearches[$oDataSource->GetKey()], array(), $aFilterValues);
  1384. $iCount = $oDestSet->Count();
  1385. $aConditions = array();
  1386. foreach($aFilterValues as $sCode => $sValue)
  1387. {
  1388. $aConditions[] = $sCode.'='.$sValue;
  1389. }
  1390. $sConditionDesc = implode(' AND ', $aConditions);
  1391. // How many objects match the reconciliation criterias
  1392. switch($iCount)
  1393. {
  1394. case 0:
  1395. $oStatLog->AddTrace("Nothing found on: $sConditionDesc", $this);
  1396. if ($oDataSource->Get('action_on_zero') == 'create')
  1397. {
  1398. $bCreated = $this->CreateObjectFromReplica($oDataSource->GetTargetClass(), $aAttributes, $oChange, $oStatLog);
  1399. if ($bCreated)
  1400. {
  1401. if ($this->HasWarnings())
  1402. {
  1403. $oStatLog->Inc('stats_nb_obj_created_warnings');
  1404. }
  1405. }
  1406. else
  1407. {
  1408. // Creation error has precedence over any warning
  1409. $this->ResetWarnings();
  1410. }
  1411. }
  1412. else // assumed to be 'error'
  1413. {
  1414. $oStatLog->AddTrace("Failed to reconcile (no match)", $this);
  1415. // Recoverable error
  1416. $this->SetLastError('Could not find a match for reconciliation');
  1417. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1418. }
  1419. break;
  1420. case 1:
  1421. $oStatLog->AddTrace("Found 1 object on: $sConditionDesc", $this);
  1422. if ($oDataSource->Get('action_on_one') == 'update')
  1423. {
  1424. $oDestObj = $oDestSet->Fetch();
  1425. $bModified = $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj_new', 'stats_nb_replica_reconciled_errors');
  1426. $this->Set('dest_id', $oDestObj->GetKey());
  1427. $this->Set('dest_class', get_class($oDestObj));
  1428. if ($this->HasWarnings())
  1429. {
  1430. if ($bModified)
  1431. {
  1432. $oStatLog->Inc('stats_nb_obj_new_updated_warnings');
  1433. }
  1434. else
  1435. {
  1436. $oStatLog->Inc('stats_nb_obj_new_unchanged_warnings');
  1437. }
  1438. }
  1439. }
  1440. else
  1441. {
  1442. // assumed to be 'error'
  1443. $oStatLog->AddTrace("Failed to reconcile (1 match)", $this);
  1444. // Recoverable error
  1445. $this->SetLastError('Found a match while expecting several');
  1446. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1447. }
  1448. break;
  1449. default:
  1450. $oStatLog->AddTrace("Found $iCount objects on: $sConditionDesc", $this);
  1451. if ($oDataSource->Get('action_on_multiple') == 'error')
  1452. {
  1453. $oStatLog->AddTrace("Failed to reconcile (N>1 matches)", $this);
  1454. // Recoverable error
  1455. $this->SetLastError($iCount.' destination objects match the reconciliation criterias: '.$sConditionDesc);
  1456. $oStatLog->Inc('stats_nb_replica_reconciled_errors');
  1457. }
  1458. elseif ($oDataSource->Get('action_on_multiple') == 'create')
  1459. {
  1460. $bCreated = $this->CreateObjectFromReplica($oDataSource->GetTargetClass(), $aAttributes, $oChange, $oStatLog);
  1461. if ($bCreated)
  1462. {
  1463. if ($this->HasWarnings())
  1464. {
  1465. $oStatLog->Inc('stats_nb_obj_created_warnings');
  1466. }
  1467. }
  1468. else
  1469. {
  1470. // Creation error has precedence over any warning
  1471. $this->ResetWarnings();
  1472. }
  1473. }
  1474. else
  1475. {
  1476. // assumed to be 'take_first'
  1477. $oDestObj = $oDestSet->Fetch();
  1478. $bModified = $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj_new', 'stats_nb_replica_reconciled_errors');
  1479. $this->Set('dest_id', $oDestObj->GetKey());
  1480. $this->Set('dest_class', get_class($oDestObj));
  1481. if ($this->HasWarnings())
  1482. {
  1483. if ($bModified)
  1484. {
  1485. $oStatLog->Inc('stats_nb_obj_new_updated_warnings');
  1486. }
  1487. else
  1488. {
  1489. $oStatLog->Inc('stats_nb_obj_new_unchanged_warnings');
  1490. }
  1491. }
  1492. }
  1493. }
  1494. $this->RecordWarnings();
  1495. break;
  1496. case 'synchronized': // try to recover synchronized replicas with warnings
  1497. case 'modified':
  1498. $oDestObj = MetaModel::GetObject($oDataSource->GetTargetClass(), $this->Get('dest_id'));
  1499. if ($oDestObj == null)
  1500. {
  1501. $this->Set('status', 'orphan'); // The destination object has been deleted !
  1502. $this->SetLastError('Destination object deleted unexpectedly');
  1503. $oStatLog->Inc('stats_nb_obj_updated_errors');
  1504. }
  1505. else
  1506. {
  1507. $bModified = $this->UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, $oStatLog, 'stats_nb_obj', 'stats_nb_obj_updated_errors');
  1508. if ($this->HasWarnings())
  1509. {
  1510. if ($bModified)
  1511. {
  1512. $oStatLog->Inc('stats_nb_obj_updated_warnings');
  1513. }
  1514. else
  1515. {
  1516. $oStatLog->Inc('stats_nb_obj_unchanged_warnings');
  1517. }
  1518. }
  1519. }
  1520. $this->RecordWarnings();
  1521. break;
  1522. default: // Do nothing in all other cases
  1523. }
  1524. }
  1525. /**
  1526. * Updates the destination object with the Extended data found in the synchro_data_XXXX table
  1527. */
  1528. protected function UpdateObjectFromReplica($oDestObj, $aAttributes, $oChange, &$oStatLog, $sStatsCode, $sStatsCodeError)
  1529. {
  1530. $aValueTrace = array();
  1531. $bModified = false;
  1532. try
  1533. {
  1534. foreach($aAttributes as $sAttCode => $oSyncAtt)
  1535. {
  1536. $value = $this->GetValueFromExtData($sAttCode, $oSyncAtt, $oStatLog);
  1537. if (!is_null($value))
  1538. {
  1539. $oDestObj->Set($sAttCode, $value);
  1540. $aValueTrace[] = "$sAttCode: $value";
  1541. }
  1542. }
  1543. // Really modified ?
  1544. if ($oDestObj->IsModified())
  1545. {
  1546. $oDestObj->DBUpdateTracked($oChange);
  1547. $bModified = true;
  1548. $oStatLog->AddTrace('Updated object - Values: {'.implode(', ', $aValueTrace).'}', $this);
  1549. if (($sStatsCode != '') &&(MetaModel::IsValidAttCode(get_class($oStatLog), $sStatsCode.'_updated')))
  1550. {
  1551. $oStatLog->Inc($sStatsCode.'_updated');
  1552. }
  1553. $this->Set('info_last_modified', date('Y-m-d H:i:s'));
  1554. }
  1555. else
  1556. {
  1557. $oStatLog->AddTrace('Unchanged object', $this);
  1558. if (($sStatsCode != '') &&(MetaModel::IsValidAttCode(get_class($oStatLog), $sStatsCode.'_unchanged')))
  1559. {
  1560. $oStatLog->Inc($sStatsCode.'_unchanged');
  1561. }
  1562. }
  1563. $this->Set('status_last_error', '');
  1564. $this->Set('status', 'synchronized');
  1565. }
  1566. catch(Exception $e)
  1567. {
  1568. $oStatLog->AddTrace("Failed to update destination object: {$e->getMessage()}", $this);
  1569. $this->SetLastError('Unable to update destination object: ', $e);
  1570. $oStatLog->Inc($sStatsCodeError);
  1571. }
  1572. return $bModified;
  1573. }
  1574. /**
  1575. * Creates the destination object populating it with the Extended data found in the synchro_data_XXXX table
  1576. * @return bool Whether or not the object was created
  1577. */
  1578. protected function CreateObjectFromReplica($sClass, $aAttributes, $oChange, &$oStatLog)
  1579. {
  1580. $bCreated = false;
  1581. $oDestObj = MetaModel::NewObject($sClass);
  1582. try
  1583. {
  1584. $aValueTrace = array();
  1585. foreach($aAttributes as $sAttCode => $oSyncAtt)
  1586. {
  1587. $value = $this->GetValueFromExtData($sAttCode, $oSyncAtt, $oStatLog);
  1588. if (!is_null($value))
  1589. {
  1590. $oDestObj->Set($sAttCode, $value);
  1591. $aValueTrace[] = "$sAttCode: $value";
  1592. }
  1593. }
  1594. $iNew = $oDestObj->DBInsertTracked($oChange);
  1595. $this->Set('dest_id', $oDestObj->GetKey());
  1596. $this->Set('dest_class', get_class($oDestObj));
  1597. $this->Set('status_dest_creator', true);
  1598. $this->Set('status_last_error', '');
  1599. $this->Set('status', 'synchronized');
  1600. $this->Set('info_creation_date', date('Y-m-d H:i:s'));
  1601. $bCreated = true;
  1602. $oStatLog->AddTrace("Created (".implode(', ', $aValueTrace).")", $this);
  1603. $oStatLog->Inc('stats_nb_obj_created');
  1604. }
  1605. catch(Exception $e)
  1606. {
  1607. $oStatLog->AddTrace("Failed to create $sClass ({$e->getMessage()})", $this);
  1608. $this->SetLastError('Unable to create destination object: ', $e);
  1609. $oStatLog->Inc('stats_nb_obj_created_errors');
  1610. }
  1611. return $bCreated;
  1612. }
  1613. /**
  1614. * Update the destination object with given values
  1615. */
  1616. public function UpdateDestObject($aValues, $oChange, &$oStatLog)
  1617. {
  1618. try
  1619. {
  1620. if ($this->Get('dest_class') == '')
  1621. {
  1622. $this->SetLastError('No destination object to update');
  1623. $oStatLog->Inc('stats_nb_obj_obsoleted_errors');
  1624. }
  1625. else
  1626. {
  1627. $oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'));
  1628. foreach($aValues as $sAttCode => $value)
  1629. {
  1630. if (!MetaModel::IsValidAttCode(get_class($oDestObj), $sAttCode))
  1631. {
  1632. throw new Exception("Unknown attribute code '$sAttCode'");
  1633. }
  1634. $oDestObj->Set($sAttCode, $value);
  1635. }
  1636. $this->Set('info_last_modified', date('Y-m-d H:i:s'));
  1637. $oDestObj->DBUpdateTracked($oChange);
  1638. $oStatLog->AddTrace("Replica marked as obsolete", $this);
  1639. $oStatLog->Inc('stats_nb_obj_obsoleted');
  1640. }
  1641. }
  1642. catch(Exception $e)
  1643. {
  1644. $this->SetLastError('Unable to update the destination object: ', $e);
  1645. $oStatLog->Inc('stats_nb_obj_obsoleted_errors');
  1646. }
  1647. }
  1648. /**
  1649. * Delete the destination object
  1650. */
  1651. public function DeleteDestObject($oChange, &$oStatLog)
  1652. {
  1653. if($this->Get('status_dest_creator'))
  1654. {
  1655. try
  1656. {
  1657. $oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'));
  1658. $oCheckDeletionPlan = new DeletionPlan();
  1659. if ($oDestObj->CheckToDelete($oCheckDeletionPlan))
  1660. {
  1661. $oActualDeletionPlan = new DeletionPlan();
  1662. $oDestObj->DBDeleteTracked($oChange, null, $oActualDeletionPlan);
  1663. $this->DBDeleteTracked($oChange);
  1664. $oStatLog->Inc('stats_nb_obj_deleted');
  1665. }
  1666. else
  1667. {
  1668. $sIssues = implode("\n", $oCheckDeletionPlan->GetIssues());
  1669. throw(new Exception($sIssues));
  1670. }
  1671. }
  1672. catch(Exception $e)
  1673. {
  1674. $this->SetLastError('Unable to delete the destination object: ', $e);
  1675. $this->Set('status', 'obsolete');
  1676. $this->DBUpdateTracked($oChange);
  1677. $oStatLog->Inc('stats_nb_obj_deleted_errors');
  1678. }
  1679. }
  1680. else
  1681. {
  1682. $this->DBDeleteTracked($oChange);
  1683. $oStatLog->Inc('stats_nb_replica_disappeared_no_action');
  1684. }
  1685. }
  1686. /**
  1687. * Get the value from the 'Extended Data' located in the synchro_data_xxx table for this replica
  1688. * Note: sExtAttCode could be a standard attcode, or 'primary_key'
  1689. */
  1690. protected function GetValueFromExtData($sExtAttCode, $oSyncAtt, &$oStatLog)
  1691. {
  1692. // $aData should contain attributes defined either for reconciliation or create/update
  1693. $aData = $this->GetExtendedData();
  1694. if ($sExtAttCode == 'primary_key')
  1695. {
  1696. return $aData['primary_key'];
  1697. }
  1698. // $sExtAttCode is a valid attribute code
  1699. //
  1700. $sClass = $this->Get('base_class');
  1701. $oAttDef = MetaModel::GetAttributeDef($sClass, $sExtAttCode);
  1702. if (!is_null($oSyncAtt) && ($oSyncAtt instanceof SynchroAttExtKey))
  1703. {
  1704. $rawValue = $aData[$sExtAttCode];
  1705. if (is_null($rawValue))
  1706. {
  1707. // Null means "ignore" this attribute
  1708. return null;
  1709. }
  1710. $sReconcAttCode = $oSyncAtt->Get('reconciliation_attcode');
  1711. if (!empty($sReconcAttCode))
  1712. {
  1713. $sRemoteClass = $oAttDef->GetTargetClass();
  1714. $oObj = MetaModel::GetObjectByColumn($sRemoteClass, $sReconcAttCode, $rawValue, false);
  1715. if ($oObj)
  1716. {
  1717. $retValue = $oObj->GetKey();
  1718. }
  1719. else
  1720. {
  1721. if ($rawValue != '')
  1722. {
  1723. // Note: differs from null (in which case the value would be left unchanged)
  1724. $oStatLog->AddTrace("Could not find [unique] object for '$sExtAttCode': searched on $sReconcAttCode = '$rawValue'", $this);
  1725. $this->AddWarning("Could not find [unique] object for '$sExtAttCode': searched on $sReconcAttCode = '$rawValue'");
  1726. }
  1727. $retValue = 0;
  1728. }
  1729. }
  1730. else
  1731. {
  1732. $retValue = $rawValue;
  1733. }
  1734. }
  1735. elseif (!is_null($oSyncAtt) && ($oSyncAtt instanceof SynchroAttLinkSet))
  1736. {
  1737. $rawValue = $aData[$sExtAttCode];
  1738. if (is_null($rawValue))
  1739. {
  1740. // Null means "ignore" this attribute
  1741. return null;
  1742. }
  1743. // MakeValueFromString() throws an exception in case of failure
  1744. $retValue = $oAttDef->MakeValueFromString($rawValue, $oSyncAtt->Get('row_separator'), $oSyncAtt->Get('attribute_separator'), $oSyncAtt->Get('value_separator'), $oSyncAtt->Get('attribute_qualifier'));
  1745. }
  1746. else
  1747. {
  1748. $aColumns = $oAttDef->GetImportColumns();
  1749. foreach($aColumns as $sColumn => $sFormat)
  1750. {
  1751. // In any case, a null column means "ignore this attribute"
  1752. //
  1753. if (is_null($aData[$sColumn]))
  1754. {
  1755. return null;
  1756. }
  1757. }
  1758. $retValue = $oAttDef->FromImportToValue($aData, $sExtAttCode);
  1759. }
  1760. return $retValue;
  1761. }
  1762. /**
  1763. * Maps the given context parameter name to the appropriate filter/search code for this class
  1764. * @param string $sContextParam Name of the context parameter, i.e. 'org_id'
  1765. * @return string Filter code, i.e. 'customer_id'
  1766. */
  1767. public static function MapContextParam($sContextParam)
  1768. {
  1769. if ($sContextParam == 'menu')
  1770. {
  1771. return null;
  1772. }
  1773. else
  1774. {
  1775. return $sContextParam;
  1776. }
  1777. }
  1778. /**
  1779. * This function returns a 'hilight' CSS class, used to hilight a given row in a table
  1780. * There are currently (i.e defined in the CSS) 4 possible values HILIGHT_CLASS_CRITICAL,
  1781. * HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  1782. * To Be overridden by derived classes
  1783. * @param void
  1784. * @return String The desired higlight class for the object/row
  1785. */
  1786. public function GetHilightClass()
  1787. {
  1788. // Possible return values are:
  1789. // HILIGHT_CLASS_CRITICAL, HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  1790. return HILIGHT_CLASS_NONE; // Not hilighted by default
  1791. }
  1792. public static function GetUIPage()
  1793. {
  1794. return '../synchro/replica.php';
  1795. }
  1796. function DisplayDetails(WebPage $oPage, $bEditMode = false)
  1797. {
  1798. // Object's details
  1799. //$this->DisplayBareHeader($oPage, $bEditMode);
  1800. $oPage->AddTabContainer(OBJECT_PROPERTIES_TAB);
  1801. $oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB);
  1802. $oPage->SetCurrentTab(Dict::S('UI:PropertiesTab'));
  1803. $this->DisplayBareProperties($oPage, $bEditMode);
  1804. }
  1805. function DisplayBareProperties(WebPage $oPage, $bEditMode = false, $sPrefix = '', $aExtraParams = array())
  1806. {
  1807. if ($bEditMode) return; // Not editable
  1808. $oPage->add('<table style="vertical-align:top"><tr style="vertical-align:top"><td>');
  1809. $aDetails = array();
  1810. $sClass = get_class($this);
  1811. $oPage->add('<fieldset>');
  1812. $oPage->add('<legend>'.Dict::S('Core:SynchroReplica:PrivateDetails').'</legend>');
  1813. $aZList = MetaModel::FlattenZlist(MetaModel::GetZListItems($sClass, 'details'));
  1814. foreach( $aZList as $sAttCode)
  1815. {
  1816. $sDisplayValue = $this->GetAsHTML($sAttCode);
  1817. $aDetails[] = array('label' => '<span title="'.MetaModel::GetDescription($sClass, $sAttCode).'">'.MetaModel::GetLabel($sClass, $sAttCode).'</span>', 'value' => $sDisplayValue);
  1818. }
  1819. $oPage->Details($aDetails);
  1820. $oPage->add('</fieldset>');
  1821. $oDestObj = MetaModel::GetObject($this->Get('dest_class'), $this->Get('dest_id'), false);
  1822. if (is_object($oDestObj))
  1823. {
  1824. $oPage->add('<fieldset>');
  1825. $oPage->add('<legend>'.Dict::Format('Core:SynchroReplica:TargetObject', $oDestObj->GetHyperlink()).'</legend>');
  1826. $oDestObj->DisplayBareProperties($oPage, false, $sPrefix, $aExtraParams);
  1827. $oPage->add('<fieldset>');
  1828. }
  1829. $oPage->add('</td><td>');
  1830. $oPage->add('<fieldset>');
  1831. $oPage->add('<legend>'.Dict::S('Core:SynchroReplica:PublicData').'</legend>');
  1832. $oSource = MetaModel::GetObject('SynchroDataSource', $this->Get('sync_source_id'));
  1833. $sSQLTable = $oSource->GetDataTable();
  1834. $aData = $this->LoadExtendedDataFromTable($sSQLTable);
  1835. $aHeaders = array('attcode' => array('label' => 'Attribute Code', 'description' => ''),
  1836. 'data' => array('label' => 'Value', 'description' => ''));
  1837. $aRows = array();
  1838. foreach($aData as $sKey => $value)
  1839. {
  1840. $aRows[] = array('attcode' => $sKey, 'data' => $value);
  1841. }
  1842. $oPage->Table($aHeaders, $aRows);
  1843. $oPage->add('</fieldset>');
  1844. $oPage->add('</td></tr></table>');
  1845. }
  1846. public function LoadExtendedDataFromTable($sSQLTable)
  1847. {
  1848. $sSQL = "SELECT * FROM $sSQLTable WHERE id=".$this->GetKey();
  1849. $rQuery = CMDBSource::Query($sSQL);
  1850. return CMDBSource::FetchArray($rQuery);
  1851. }
  1852. }
  1853. /**
  1854. * Context of an ongoing synchronization
  1855. * Two usages:
  1856. * 1) Public usage: execute the synchronization
  1857. * $oSynchroExec = new SynchroExecution($oDataSource[, $iLastFullLoad]);
  1858. * $oSynchroExec->Process($iMaxChunkSize);
  1859. *
  1860. * 2) Internal usage: continue the synchronization (split into chunks, each performed in a separate process)
  1861. * This is implemented in the page priv_sync_chunk.php
  1862. * $oSynchroExec = SynchroExecution::Resume($oDataSource, $iLastFullLoad, $iSynchroLog, $iChange, $iMaxToProcess, $iJob, $iNextInJob);
  1863. * $oSynchroExec->Process()
  1864. */
  1865. class SynchroExecution
  1866. {
  1867. protected $m_oDataSource = null;
  1868. protected $m_oLastFullLoadStartDate = null;
  1869. protected $m_oChange = null;
  1870. protected $m_oStatLog = null;
  1871. // Context computed one for optimization and report inconsistencies ASAP
  1872. protected $m_aExtDataSpec = array();
  1873. protected $m_aReconciliationKeys = array();
  1874. protected $m_aAttributes = array();
  1875. protected $m_iCountAllReplicas = 0;
  1876. /**
  1877. * Constructor
  1878. * @param SynchroDataSource $oDataSource Synchronization task
  1879. * @param DateTime $oLastFullLoadStartDate Date of the last full load (start date/time), if known
  1880. * @return void
  1881. */
  1882. public function __construct($oDataSource, $oLastFullLoadStartDate = null)
  1883. {
  1884. $this->m_oDataSource = $oDataSource;
  1885. $this->m_oLastFullLoadStartDate = $oLastFullLoadStartDate;
  1886. }
  1887. /**
  1888. * Create the persistant information records, for the current synchronization
  1889. * In fact, those records ARE defining what is the "current" synchronization
  1890. */
  1891. protected function PrepareLogs()
  1892. {
  1893. if (!is_null($this->m_oChange))
  1894. {
  1895. return;
  1896. }
  1897. // Create a change used for logging all the modifications/creations happening during the synchro
  1898. $this->m_oChange = MetaModel::NewObject("CMDBChange");
  1899. $this->m_oChange->Set("date", time());
  1900. $sUserString = CMDBChange::GetCurrentUserName();
  1901. $this->m_oChange->Set("userinfo", $sUserString.' '.Dict::S('Core:SyncDataExchangeComment'));
  1902. $iChangeId = $this->m_oChange->DBInsert();
  1903. // Start logging this execution (stats + protection against reentrance)
  1904. //
  1905. $this->m_oStatLog = new SynchroLog();
  1906. $this->m_oStatLog->Set('sync_source_id', $this->m_oDataSource->GetKey());
  1907. $this->m_oStatLog->Set('start_date', time());
  1908. $this->m_oStatLog->Set('status', 'running');
  1909. $this->m_oStatLog->Set('stats_nb_replica_seen', 0);
  1910. $this->m_oStatLog->Set('stats_nb_replica_total', 0);
  1911. $this->m_oStatLog->Set('stats_nb_obj_deleted', 0);
  1912. $this->m_oStatLog->Set('stats_nb_obj_deleted_errors', 0);
  1913. $this->m_oStatLog->Set('stats_nb_obj_obsoleted', 0);
  1914. $this->m_oStatLog->Set('stats_nb_obj_obsoleted_errors', 0);
  1915. $this->m_oStatLog->Set('stats_nb_obj_created', 0);
  1916. $this->m_oStatLog->Set('stats_nb_obj_created_errors', 0);
  1917. $this->m_oStatLog->Set('stats_nb_obj_created_warnings', 0);
  1918. $this->m_oStatLog->Set('stats_nb_obj_updated', 0);
  1919. $this->m_oStatLog->Set('stats_nb_obj_updated_warnings', 0);
  1920. $this->m_oStatLog->Set('stats_nb_obj_updated_errors', 0);
  1921. $this->m_oStatLog->Set('stats_nb_obj_unchanged_warnings', 0);
  1922. // $this->m_oStatLog->Set('stats_nb_replica_reconciled', 0);
  1923. $this->m_oStatLog->Set('stats_nb_replica_reconciled_errors', 0);
  1924. $this->m_oStatLog->Set('stats_nb_replica_disappeared_no_action', 0);
  1925. $this->m_oStatLog->Set('stats_nb_obj_new_updated', 0);
  1926. $this->m_oStatLog->Set('stats_nb_obj_new_updated_warnings', 0);
  1927. $this->m_oStatLog->Set('stats_nb_obj_new_unchanged',0);
  1928. $this->m_oStatLog->Set('stats_nb_obj_new_unchanged_warnings',0);
  1929. $sSelectTotal = "SELECT SynchroReplica WHERE sync_source_id = :source_id";
  1930. $oSetTotal = new DBObjectSet(DBObjectSearch::FromOQL($sSelectTotal), array() /* order by*/, array('source_id' => $this->m_oDataSource->GetKey()));
  1931. $this->m_oStatLog->Set('stats_nb_replica_total', $oSetTotal->Count());
  1932. $this->m_oStatLog->DBInsertTracked($this->m_oChange);
  1933. }
  1934. /**
  1935. * Prevent against the reentrance... or allow the current task to do things forbidden by the others !
  1936. */
  1937. public static $m_oCurrentTask = null;
  1938. public static function GetCurrentTaskId()
  1939. {
  1940. if (is_object(self::$m_oCurrentTask))
  1941. {
  1942. return self::$m_oCurrentTask->GetKey();
  1943. }
  1944. else
  1945. {
  1946. return null;
  1947. }
  1948. }
  1949. /**
  1950. * Prepare structures in memory, to speedup the processing of a given replica
  1951. */
  1952. public function PrepareProcessing($bFirstPass = true)
  1953. {
  1954. if ($this->m_oDataSource->Get('status') == 'obsolete')
  1955. {
  1956. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncDataSourceObsolete'));
  1957. }
  1958. if (!UserRights::IsAdministrator() && $this->m_oDataSource->Get('user_id') != UserRights::GetUserId())
  1959. {
  1960. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncDataSourceAccessRestriction'));
  1961. }
  1962. // Get the list of SQL columns
  1963. $sClass = $this->m_oDataSource->GetTargetClass();
  1964. $aAttCodesExpected = array();
  1965. $aAttCodesToReconcile = array();
  1966. $aAttCodesToUpdate = array();
  1967. $sSelectAtt = "SELECT SynchroAttribute WHERE sync_source_id = :source_id AND (update = 1 OR reconcile = 1)";
  1968. $oSetAtt = new DBObjectSet(DBObjectSearch::FromOQL($sSelectAtt), array() /* order by*/, array('source_id' => $this->m_oDataSource->GetKey()) /* aArgs */);
  1969. while ($oSyncAtt = $oSetAtt->Fetch())
  1970. {
  1971. if ($oSyncAtt->Get('update'))
  1972. {
  1973. $aAttCodesToUpdate[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  1974. }
  1975. if ($oSyncAtt->Get('reconcile'))
  1976. {
  1977. $aAttCodesToReconcile[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  1978. }
  1979. $aAttCodesExpected[$oSyncAtt->Get('attcode')] = $oSyncAtt;
  1980. }
  1981. $aColumns = $this->m_oDataSource->GetSQLColumns(array_keys($aAttCodesExpected));
  1982. $aExtDataFields = array_keys($aColumns);
  1983. $aExtDataFields[] = 'primary_key';
  1984. $this->m_aExtDataSpec = array(
  1985. 'table' => $this->m_oDataSource->GetDataTable(),
  1986. 'join_key' => 'id',
  1987. 'fields' => $aExtDataFields
  1988. );
  1989. // Get the list of attributes, determine reconciliation keys and update targets
  1990. //
  1991. if ($this->m_oDataSource->Get('reconciliation_policy') == 'use_attributes')
  1992. {
  1993. $this->m_aReconciliationKeys = $aAttCodesToReconcile;
  1994. }
  1995. elseif ($this->m_oDataSource->Get('reconciliation_policy') == 'use_primary_key')
  1996. {
  1997. // Override the settings made at the attribute level !
  1998. $this->m_aReconciliationKeys = array("primary_key" => null);
  1999. }
  2000. if ($bFirstPass)
  2001. {
  2002. $this->m_oStatLog->AddTrace("Update of: {".implode(', ', array_keys($aAttCodesToUpdate))."}");
  2003. $this->m_oStatLog->AddTrace("Reconciliation on: {".implode(', ', array_keys($this->m_aReconciliationKeys))."}");
  2004. }
  2005. if (count($aAttCodesToUpdate) == 0)
  2006. {
  2007. $this->m_oStatLog->AddTrace("No attribute to update");
  2008. throw new SynchroExceptionNotStarted('There is no attribute to update');
  2009. }
  2010. if (count($this->m_aReconciliationKeys) == 0)
  2011. {
  2012. $this->m_oStatLog->AddTrace("No attribute for reconciliation");
  2013. throw new SynchroExceptionNotStarted('No attribute for reconciliation');
  2014. }
  2015. $this->m_aAttributes = array();
  2016. foreach($aAttCodesToUpdate as $sAttCode => $oSyncAtt)
  2017. {
  2018. $oAttDef = MetaModel::GetAttributeDef($this->m_oDataSource->GetTargetClass(), $sAttCode);
  2019. if ($oAttDef->IsWritable())
  2020. {
  2021. $this->m_aAttributes[$sAttCode] = $oSyncAtt;
  2022. }
  2023. }
  2024. // Count the replicas
  2025. $sSelectAll = "SELECT SynchroReplica WHERE sync_source_id = :source_id";
  2026. $oSetAll = new DBObjectSet(DBObjectSearch::FromOQL($sSelectAll), array() /* order by*/, array('source_id' => $this->m_oDataSource->GetKey()));
  2027. $this->m_iCountAllReplicas = $oSetAll->Count();
  2028. $this->m_oStatLog->Set('stats_nb_replica_total', $this->m_iCountAllReplicas);
  2029. // Compute and keep track of the limit date taken into account for obsoleting replicas
  2030. //
  2031. if ($this->m_oLastFullLoadStartDate == null)
  2032. {
  2033. // No previous import known, use the full_load_periodicity value... and the current date
  2034. $this->m_oLastFullLoadStartDate = new DateTime(); // Now
  2035. $iLoadPeriodicity = $this->m_oDataSource->Get('full_load_periodicity'); // Duration in seconds
  2036. if ($iLoadPeriodicity > 0)
  2037. {
  2038. $sInterval = "-$iLoadPeriodicity seconds";
  2039. $this->m_oLastFullLoadStartDate->Modify($sInterval);
  2040. }
  2041. else
  2042. {
  2043. $this->m_oLastFullLoadStartDate = new DateTime('1970-01-01');
  2044. }
  2045. }
  2046. if ($bFirstPass)
  2047. {
  2048. $this->m_oStatLog->AddTrace("Limit Date: ".$this->m_oLastFullLoadStartDate->Format('Y-m-d H:i:s'));
  2049. }
  2050. }
  2051. /**
  2052. * Perform a synchronization between the data stored in the replicas (&synchro_data_xxx_xx table)
  2053. * and the iTop objects. If the lastFullLoadStartDate is NOT specified then the full_load_periodicity
  2054. * is used to determine which records are obsolete.
  2055. * @return void
  2056. */
  2057. public function Process()
  2058. {
  2059. $this->PrepareLogs();
  2060. self::$m_oCurrentTask = $this->m_oDataSource;
  2061. try
  2062. {
  2063. $this->DoSynchronize();
  2064. $this->m_oStatLog->Set('end_date', time());
  2065. $this->m_oStatLog->Set('status', 'completed');
  2066. $this->m_oStatLog->DBUpdateTracked($this->m_oChange);
  2067. $iErrors = $this->m_oStatLog->GetErrorCount();
  2068. if ($iErrors > 0)
  2069. {
  2070. $sIssuesOQL = "SELECT SynchroReplica WHERE sync_source_id=".$this->m_oDataSource->GetKey()." AND status_last_error!=''";
  2071. $sAbsoluteUrl = utils::GetAbsoluteUrlAppRoot();
  2072. $sIssuesURL = "{$sAbsoluteUrl}synchro/replica.php?operation=oql&datasource=".$this->m_oDataSource->GetKey()."&oql=".urlencode($sIssuesOQL);
  2073. $sSeeIssues = "<p></p>";
  2074. $sStatistics = "<h1>Statistics</h1>\n";
  2075. $sStatistics .= "<ul>\n";
  2076. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('start_date').": ".$this->m_oStatLog->Get('start_date')."</li>\n";
  2077. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('end_date').": ".$this->m_oStatLog->Get('end_date')."</li>\n";
  2078. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_replica_seen').": ".$this->m_oStatLog->Get('stats_nb_replica_seen')."</li>\n";
  2079. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_replica_total').": ".$this->m_oStatLog->Get('stats_nb_replica_total')."</li>\n";
  2080. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_deleted').": ".$this->m_oStatLog->Get('stats_nb_obj_deleted')."</li>\n";
  2081. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_deleted_errors').": ".$this->m_oStatLog->Get('stats_nb_obj_deleted_errors')."</li>\n";
  2082. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_obsoleted').": ".$this->m_oStatLog->Get('stats_nb_obj_obsoleted')."</li>\n";
  2083. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_obsoleted_errors').": ".$this->m_oStatLog->Get('stats_nb_obj_obsoleted_errors')."</li>\n";
  2084. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_created').": ".$this->m_oStatLog->Get('stats_nb_obj_created')." (".$this->m_oStatLog->Get('stats_nb_obj_created_warnings')." warnings)"."</li>\n";
  2085. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_created_errors').": ".$this->m_oStatLog->Get('stats_nb_obj_created_errors')."</li>\n";
  2086. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_updated').": ".$this->m_oStatLog->Get('stats_nb_obj_updated')." (".$this->m_oStatLog->Get('stats_nb_obj_updated_warnings')." warnings)"."</li>\n";
  2087. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_updated_errors').": ".$this->m_oStatLog->Get('stats_nb_obj_updated_errors')."</li>\n";
  2088. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_replica_reconciled_errors').": ".$this->m_oStatLog->Get('stats_nb_replica_reconciled_errors')."</li>\n";
  2089. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_replica_disappeared_no_action').": ".$this->m_oStatLog->Get('stats_nb_replica_disappeared_no_action')."</li>\n";
  2090. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_new_updated').": ".$this->m_oStatLog->Get('stats_nb_obj_new_updated')." (".$this->m_oStatLog->Get('stats_nb_obj_new_updated_warnings')." warnings)"."</li>\n";
  2091. $sStatistics .= "<li>".$this->m_oStatLog->GetLabel('stats_nb_obj_new_unchanged').": ".$this->m_oStatLog->Get('stats_nb_obj_new_unchanged')." (".$this->m_oStatLog->Get('stats_nb_obj_new_unchanged_warnings')." warnings)"."</li>\n";
  2092. $sStatistics .= "</ul>\n";
  2093. $this->m_oDataSource->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);
  2094. }
  2095. else
  2096. {
  2097. //$this->m_oDataSource->SendNotification('success', '<p>The synchronization has been successfully executed.</p>');
  2098. }
  2099. }
  2100. catch (SynchroExceptionNotStarted $e)
  2101. {
  2102. // Set information for reporting... but delete the object in DB
  2103. $this->m_oStatLog->Set('end_date', time());
  2104. $this->m_oStatLog->Set('status', 'error');
  2105. $this->m_oStatLog->Set('last_error', $e->getMessage());
  2106. $this->m_oStatLog->DBDeleteTracked($this->m_oChange);
  2107. $this->m_oDataSource->SendNotification('fatal error', '<p>The synchronization could not start: \''.$e->getMessage().'\'</p><p>Please check its configuration</p>');
  2108. }
  2109. catch (Exception $e)
  2110. {
  2111. $this->m_oStatLog->Set('end_date', time());
  2112. $this->m_oStatLog->Set('status', 'error');
  2113. $this->m_oStatLog->Set('last_error', $e->getMessage());
  2114. $this->m_oStatLog->DBUpdateTracked($this->m_oChange);
  2115. $this->m_oDataSource->SendNotification('exception', '<p>The synchronization has been interrupted: \''.$e->getMessage().'\'</p><p>Please contact the application support team</p>');
  2116. }
  2117. self::$m_oCurrentTask = null;
  2118. return $this->m_oStatLog;
  2119. }
  2120. /**
  2121. * Do the entire synchronization job
  2122. */
  2123. protected function DoSynchronize()
  2124. {
  2125. $this->m_oStatLog->Set('status_curr_job', 1);
  2126. $this->m_oStatLog->Set('status_curr_pos', -1);
  2127. $iMaxChunkSize = utils::ReadParam('max_chunk_size', 0, true /* allow CLI */);
  2128. if ($iMaxChunkSize > 0)
  2129. {
  2130. // Split the execution into several processes
  2131. // Each process will call DoSynchronizeChunk()
  2132. // The loop will end when a process does not reply "continue" on the last line of its output
  2133. if (!utils::IsModeCLI())
  2134. {
  2135. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncSplitModeCLIOnly'));
  2136. }
  2137. $aArguments = array();
  2138. $aArguments['source'] = $this->m_oDataSource->GetKey();
  2139. $aArguments['log'] = $this->m_oStatLog->GetKey();
  2140. $aArguments['change'] = $this->m_oChange->GetKey();
  2141. $aArguments['chunk'] = $iMaxChunkSize;
  2142. if ($this->m_oLastFullLoadStartDate)
  2143. {
  2144. $aArguments['last_full_load'] = $this->m_oLastFullLoadStartDate->Format('Y-m-d H:i:s');
  2145. }
  2146. else
  2147. {
  2148. $aArguments['last_full_load'] = '';
  2149. }
  2150. $this->m_oStatLog->DBUpdate($this->m_oChange);
  2151. $iStepCount = 0;
  2152. do
  2153. {
  2154. $aArguments['step_count'] = $iStepCount;
  2155. $iStepCount++;
  2156. list ($iRes, $aOut) = utils::ExecITopScript('synchro/priv_sync_chunk.php', $aArguments);
  2157. $sLastRes = strtolower(trim(end($aOut)));
  2158. $bContinue = ($sLastRes == 'continue');
  2159. }
  2160. while ($bContinue);
  2161. // Reload the log that has been modified by the processes
  2162. $this->m_oStatLog->Reload();
  2163. }
  2164. else
  2165. {
  2166. $this->PrepareProcessing(/* first pass */);
  2167. $this->DoJob1();
  2168. $this->DoJob2();
  2169. $this->DoJob3();
  2170. }
  2171. }
  2172. /**
  2173. * Do the synchronization job, limited to some amount of work
  2174. * This verb has been designed to be called from within a separate process
  2175. * @return true if the process has to be continued
  2176. */
  2177. public function DoSynchronizeChunk($oLog, $oChange, $iMaxChunkSize)
  2178. {
  2179. // Initialize the structures...
  2180. self::$m_oCurrentTask = $this->m_oDataSource;
  2181. $this->m_oStatLog = $oLog;
  2182. $this->m_oChange = $oChange;
  2183. // Prepare internal structures (not the first pass)
  2184. $this->PrepareProcessing(false);
  2185. $iCurrJob = $this->m_oStatLog->Get('status_curr_job');
  2186. $iCurrPos = $this->m_oStatLog->Get('status_curr_pos');
  2187. $this->m_oStatLog->AddTrace("Synchronizing chunk - curr_job:$iCurrJob, curr_pos:$iCurrPos, max_chunk_size:$iMaxChunkSize");
  2188. $bContinue = false;
  2189. switch ($iCurrJob)
  2190. {
  2191. case 1:
  2192. default:
  2193. $this->DoJob1($iMaxChunkSize, $iCurrPos);
  2194. $bContinue = true;
  2195. break;
  2196. case 2:
  2197. $this->DoJob2($iMaxChunkSize, $iCurrPos);
  2198. $bContinue = true;
  2199. break;
  2200. case 3:
  2201. $bContinue = $this->DoJob3($iMaxChunkSize, $iCurrPos);
  2202. break;
  2203. }
  2204. $this->m_oStatLog->DBUpdate($this->m_oChange);
  2205. self::$m_oCurrentTask = null;
  2206. return $bContinue;
  2207. }
  2208. /**
  2209. * Do the synchronization job #1: Obsolete replica "untouched" for some time
  2210. * @param integer $iMaxReplica Limit the number of replicas to process
  2211. * @param integer $iCurrPos Current position where to resume the processing
  2212. * @return true if the process must be continued
  2213. */
  2214. protected function DoJob1($iMaxReplica = null, $iCurrPos = -1)
  2215. {
  2216. $sLimitDate = $this->m_oLastFullLoadStartDate->Format('Y-m-d H:i:s');
  2217. // Get all the replicas that were not seen in the last import and mark them as obsolete
  2218. $sDeletePolicy = $this->m_oDataSource->Get('delete_policy');
  2219. if ($sDeletePolicy != 'ignore')
  2220. {
  2221. $sSelectToObsolete = "SELECT SynchroReplica WHERE id > :curr_pos AND sync_source_id = :source_id AND status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen < :last_import";
  2222. $oSetScope = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToObsolete), array() /* order by*/, array('source_id' => $this->m_oDataSource->GetKey(), 'last_import' => $sLimitDate, 'curr_pos' => $iCurrPos));
  2223. $iCountScope = $oSetScope->Count();
  2224. if (($this->m_iCountAllReplicas > 10) && ($this->m_iCountAllReplicas == $iCountScope))
  2225. {
  2226. throw new SynchroExceptionNotStarted(Dict::S('Core:SyncTooManyMissingReplicas'));
  2227. }
  2228. if ($iMaxReplica)
  2229. {
  2230. // Re-build the object set and set a LIMIT
  2231. $oSetToProcess = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToObsolete), array() /* order by*/, array('source_id' => $this->m_oDataSource->GetKey(), 'last_import' => $sLimitDate, 'curr_pos' => $iCurrPos));
  2232. $oSetToProcess->SetLimit($iMaxReplica);
  2233. }
  2234. else
  2235. {
  2236. $oSetToProcess = $oSetScope;
  2237. }
  2238. $iLastReplicaProcessed = -1;
  2239. while($oReplica = $oSetToProcess->Fetch())
  2240. {
  2241. $iLastReplicaProcessed = $oReplica->GetKey();
  2242. switch ($sDeletePolicy)
  2243. {
  2244. case 'update':
  2245. case 'update_then_delete':
  2246. $this->m_oStatLog->AddTrace("Destination object to be updated", $oReplica);
  2247. $aToUpdate = array();
  2248. $aToUpdateSpec = explode(';', $this->m_oDataSource->Get('delete_policy_update')); //ex: 'status:obsolete;description:stopped',
  2249. foreach($aToUpdateSpec as $sUpdateSpec)
  2250. {
  2251. $aUpdateSpec = explode(':', $sUpdateSpec);
  2252. if (count($aUpdateSpec) == 2)
  2253. {
  2254. $sAttCode = $aUpdateSpec[0];
  2255. $sValue = $aUpdateSpec[1];
  2256. $aToUpdate[$sAttCode] = $sValue;
  2257. }
  2258. }
  2259. $oReplica->Set('status_last_error', '');
  2260. if ($oReplica->Get('dest_id') == '')
  2261. {
  2262. $oReplica->Set('status', 'obsolete');
  2263. $this->m_oStatLog->Inc('stats_nb_replica_disappeared_no_action');
  2264. }
  2265. else
  2266. {
  2267. $oReplica->UpdateDestObject($aToUpdate, $this->m_oChange, $this->m_oStatLog);
  2268. if ($oReplica->Get('status_last_error') == '')
  2269. {
  2270. // Change the status of the replica IIF
  2271. $oReplica->Set('status', 'obsolete');
  2272. }
  2273. }
  2274. $oReplica->DBUpdateTracked($this->m_oChange);
  2275. break;
  2276. case 'delete':
  2277. default:
  2278. $this->m_oStatLog->AddTrace("Destination object to be DELETED", $oReplica);
  2279. $oReplica->DeleteDestObject($this->m_oChange, $this->m_oStatLog);
  2280. }
  2281. }
  2282. if ($iMaxReplica)
  2283. {
  2284. if ($iMaxReplica < $iCountScope)
  2285. {
  2286. // Continue with this job!
  2287. $this->m_oStatLog->Set('status_curr_pos', $iLastReplicaProcessed);
  2288. return true;
  2289. }
  2290. }
  2291. } // if ($sDeletePolicy != 'ignore'
  2292. //Count "seen" objects
  2293. $sSelectSeen = "SELECT SynchroReplica WHERE sync_source_id = :source_id AND status IN ('new', 'synchronized', 'modified', 'orphan') AND status_last_seen >= :last_import";
  2294. $oSetSeen = new DBObjectSet(DBObjectSearch::FromOQL($sSelectSeen), array() /* order by*/, array('source_id' => $this->m_oDataSource->GetKey(), 'last_import' => $sLimitDate));
  2295. $this->m_oStatLog->Set('stats_nb_replica_seen', $oSetSeen->Count());
  2296. // Job complete!
  2297. $this->m_oStatLog->Set('status_curr_job', 2);
  2298. $this->m_oStatLog->Set('status_curr_pos', -1);
  2299. return false;
  2300. }
  2301. /**
  2302. * Do the synchronization job #2: Create and modify object for new/modified replicas
  2303. * @param integer $iMaxReplica Limit the number of replicas to process
  2304. * @param integer $iCurrPos Current position where to resume the processing
  2305. * @return true if the process must be continued
  2306. */
  2307. protected function DoJob2($iMaxReplica = null, $iCurrPos = -1)
  2308. {
  2309. $sLimitDate = $this->m_oLastFullLoadStartDate->Format('Y-m-d H:i:s');
  2310. // Get all the replicas that are 'new' or modified or synchronized with a warning
  2311. //
  2312. $sSelectToSync = "SELECT SynchroReplica WHERE id > :curr_pos AND (status = 'new' OR status = 'modified' OR (status = 'synchronized' AND status_last_warning != '')) AND sync_source_id = :source_id AND status_last_seen >= :last_import";
  2313. $oSetScope = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToSync), array() /* order by*/, array('source_id' => $this->m_oDataSource->GetKey(), 'last_import' => $sLimitDate, 'curr_pos' => $iCurrPos), $this->m_aExtDataSpec);
  2314. $iCountScope = $oSetScope->Count();
  2315. if ($iMaxReplica)
  2316. {
  2317. // Re-build the object set and set a LIMIT
  2318. $oSetToProcess = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToSync), array() /* order by*/, array('source_id' => $this->m_oDataSource->GetKey(), 'last_import' => $sLimitDate, 'curr_pos' => $iCurrPos), $this->m_aExtDataSpec);
  2319. $oSetToProcess->SetLimit($iMaxReplica);
  2320. }
  2321. else
  2322. {
  2323. $oSetToProcess = $oSetScope;
  2324. }
  2325. $iLastReplicaProcessed = -1;
  2326. while($oReplica = $oSetToProcess->Fetch())
  2327. {
  2328. $iLastReplicaProcessed = $oReplica->GetKey();
  2329. $oReplica->Synchro($this->m_oDataSource, $this->m_aReconciliationKeys, $this->m_aAttributes, $this->m_oChange, $this->m_oStatLog);
  2330. $oReplica->DBUpdateTracked($this->m_oChange);
  2331. }
  2332. if ($iMaxReplica)
  2333. {
  2334. if ($iMaxReplica < $iCountScope)
  2335. {
  2336. // Continue with this job!
  2337. $this->m_oStatLog->Set('status_curr_pos', $iLastReplicaProcessed);
  2338. return true;
  2339. }
  2340. }
  2341. // Job complete!
  2342. $this->m_oStatLog->Set('status_curr_job', 3);
  2343. $this->m_oStatLog->Set('status_curr_pos', -1);
  2344. return false;
  2345. }
  2346. /**
  2347. * Do the synchronization job #3: Delete replica depending on the obsolescence scheme
  2348. * @param integer $iMaxReplica Limit the number of replicas to process
  2349. * @param integer $iCurrPos Current position where to resume the processing
  2350. * @return true if the process must be continued
  2351. */
  2352. protected function DoJob3($iMaxReplica = null, $iCurrPos = -1)
  2353. {
  2354. $sDeletePolicy = $this->m_oDataSource->Get('delete_policy');
  2355. if ($sDeletePolicy != 'update_then_delete')
  2356. {
  2357. // Job complete!
  2358. $this->m_oStatLog->Set('status_curr_job', 0);
  2359. $this->m_oStatLog->Set('status_curr_pos', -1);
  2360. return false;
  2361. }
  2362. $bFirstPass = ($iCurrPos == -1);
  2363. // Get all the replicas that are to be deleted
  2364. //
  2365. $oDeletionDate = $this->m_oLastFullLoadStartDate;
  2366. $iDeleteRetention = $this->m_oDataSource->Get('delete_policy_retention'); // Duration in seconds
  2367. if ($iDeleteRetention > 0)
  2368. {
  2369. $sInterval = "-$iDeleteRetention seconds";
  2370. $oDeletionDate->Modify($sInterval);
  2371. }
  2372. $sDeletionDate = $oDeletionDate->Format('Y-m-d H:i:s');
  2373. if ($bFirstPass)
  2374. {
  2375. $this->m_oStatLog->AddTrace("Deletion date: $sDeletionDate");
  2376. }
  2377. $sSelectToDelete = "SELECT SynchroReplica WHERE id > :curr_pos AND sync_source_id = :source_id AND status IN ('obsolete') AND status_last_seen < :last_import";
  2378. $oSetScope = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToDelete), array() /* order by*/, array('source_id' => $this->m_oDataSource->GetKey(), 'last_import' => $sDeletionDate, 'curr_pos' => $iCurrPos));
  2379. $iCountScope = $oSetScope->Count();
  2380. if ($iMaxReplica)
  2381. {
  2382. // Re-build the object set and set a LIMIT
  2383. $oSetToProcess = new DBObjectSet(DBObjectSearch::FromOQL($sSelectToDelete), array() /* order by*/, array('source_id' => $this->m_oDataSource->GetKey(), 'last_import' => $sDeletionDate, 'curr_pos' => $iCurrPos));
  2384. $oSetToProcess->SetLimit($iMaxReplica);
  2385. }
  2386. else
  2387. {
  2388. $oSetToProcess = $oSetScope;
  2389. }
  2390. $iLastReplicaProcessed = -1;
  2391. while($oReplica = $oSetToProcess->Fetch())
  2392. {
  2393. $iLastReplicaProcessed = $oReplica->GetKey();
  2394. $this->m_oStatLog->AddTrace("Destination object to be DELETED", $oReplica);
  2395. $oReplica->DeleteDestObject($this->m_oChange, $this->m_oStatLog);
  2396. }
  2397. if ($iMaxReplica)
  2398. {
  2399. if ($iMaxReplica < $iCountScope)
  2400. {
  2401. // Continue with this job!
  2402. $this->m_oStatLog->Set('status_curr_pos', $iLastReplicaProcessed);
  2403. return true;
  2404. }
  2405. }
  2406. // Job complete!
  2407. $this->m_oStatLog->Set('status_curr_job', 0);
  2408. $this->m_oStatLog->Set('status_curr_pos', -1);
  2409. return false;
  2410. }
  2411. }
  2412. $oAdminMenu = new MenuGroup('AdminTools', 80 /* fRank */, 'SynchroDataSource', UR_ACTION_MODIFY, UR_ALLOWED_YES);
  2413. new OQLMenuNode('DataSources', 'SELECT SynchroDataSource', $oAdminMenu->GetIndex(), 12 /* fRank */, true, 'SynchroDataSource', UR_ACTION_MODIFY, UR_ALLOWED_YES);
  2414. // new OQLMenuNode('Replicas', 'SELECT SynchroReplica', $oAdminMenu->GetIndex(), 12 /* fRank */, true, 'SynchroReplica', UR_ACTION_MODIFY, UR_ALLOWED_YES);
  2415. // new WebPageMenuNode('Test:RunSynchro', '../synchro/synchro_exec.php', $oAdminMenu->GetIndex(), 13 /* fRank */, 'SynchroDataSource');
  2416. ?>