Esempio n. 1
0
        /// <summary>
        /// 根据配置和指定的数据源初始化<see cref="SyncSession"/>。
        /// </summary>
        /// <param name="planName">配置文件中的计划名称</param>
        /// <param name="dataProvider"><see cref="DataProviderBase"/>的派生类型的对象,提供源数据。</param>
        /// <returns></returns>
        public static SyncSession CreateSession(string planName, DataProviderBase dataProvider)
        {
            Configuration.PermissionCenterIncomeSyncSettings config = Configuration.PermissionCenterIncomeSyncSettings.GetConfig();

            var planConfig = config.SyncPlans[planName];

            if (planConfig == null)
            {
                throw new ArgumentException("未找到同步方案 " + planName, "providerName");
            }

            HashSet <string>          comparerNames = new HashSet <string>();
            HashSet <string>          setterNames   = new HashSet <string>();
            PropertyMappingCollection mappings      = new PropertyMappingCollection();

            mappings.SourceKeyProperty = planConfig.SourceKeyProperty;

            foreach (PropertyMappingConfigurationElement item in planConfig.PropertyMappings)
            {
                comparerNames.Add(item.ComparerName);
                setterNames.Add(item.SetterName); //需要哪些比较器和设置器
                mappings.Add(item.SourceProperty, new PropertyMapping(item.SourceProperty, item.TargetProperty, item.ComparerName, item.SetterName, new WrappedNameValueCollection(item.Parameters)));
            }

            var comparers = CreateComparers(comparerNames, config.Comparers);
            var setters   = CreateSetters(setterNames, config.PropertySetters);

            IList <LogProviderBase> loggers = CreateLoggers(planConfig);

            SyncSession task = new SyncSession(planName, dataProvider, comparers, setters, loggers, mappings);

            task.batchSize = planConfig.BatchSize;

            return(task);
        }
Esempio n. 2
0
        private void InnerExecute(SyncSession session)
        {
            session.SourceProvider.Reset(); //重置
            session.WriteStartLog(session);
            session.NumerOfErrors = session.NumerOfUpdated = 0;

            bool success = false;

            try
            {
                if (session.BatchSize != 1)
                {
                    this.DoBatchExecute(session);
                }
                else
                {
                    this.DoOneByOneExecute(session);
                }

                success = true;
            }
            finally
            {
                session.SourceProvider.Close();
                session.WriteEndLog(session, success);
            }
        }
Esempio n. 3
0
        public bool AreEqual(SyncSession session, PropertyMapping mapping, NameObjectCollection srcValues, SchemaObjectBase targetObj)
        {
            string srcValue    = (srcValues[mapping.SourceProperty] as string ?? string.Empty);
            string targetValue = (string)targetObj.Properties[mapping.TargetProperty].StringValue ?? string.Empty;

            return(srcValue.Equals(targetValue));
        }
Esempio n. 4
0
        public virtual void Execute(string planName, DataProviderBase dataProvider)
        {
            SyncSession session = SyncSession.CreateSession(planName, dataProvider);

            SyncEventArgs e = new SyncEventArgs(session);

            OnBeforeExecute(e);
            SCDataOperationLockContext.Current.DoAddLockAction(true, "向权限中心同步", () => InnerExecute(session));

            OnAfterExecute(e);
        }
Esempio n. 5
0
        public override void WriteEndLog(SyncSession session, bool success)
        {
            CheckLogPresent();
            this.log.Status = IncomeSyncStatus.Completed;
            if (success == false)
            {
                this.log.Status = IncomeSyncStatus.FaultError;
            }

            this.log.EndTime = DateTime.Now;
            Adapters.SqlIncomeLogAdapter.Instance.Update(this.log);
        }
Esempio n. 6
0
        public override void WriteStartLog(SyncSession session)
        {
            var log = SqlIncomeSyncLog.CreateLogFromEnvironment();

            log.LogID      = UuidHelper.NewUuidString();
            log.StartTime  = DateTime.Now;
            log.EndTime    = new DateTime(9999, 9, 9);
            log.SourceName = this.sourceName;
            log.Status     = IncomeSyncStatus.Running;

            Adapters.SqlIncomeLogAdapter.Instance.Update(log);

            this.log = log;
        }
Esempio n. 7
0
        public override void WriteErrorLog(SyncSession syncSession, SchemaObjectBase scObj, Exception ex)
        {
            CheckLogPresent();
            this.log.NumberOfExceptions++;
            var detail = new SqlIncomeSyncLogDetail();

            detail.LogDetailID = UuidHelper.NewUuidString();
            detail.LogID       = this.log.LogID;
            detail.SCObjectID  = scObj.ID;
            detail.Summary     = ex.Message;
            detail.Detail      = ex.ToString();
            detail.ActionType  = SqlIncomeSyncLogDetailStatus.Update;
            Adapters.SqlIncomeLogDetailAdapter.Instance.Update(detail);
        }
Esempio n. 8
0
        private void DoOneByOneExecute(SyncSession session)
        {
            DataProviderBase provider = session.SourceProvider;

            provider.Reset();

            while (provider.MoveNext())
            {
                string key = (string)provider.CurrentData[session.Mappings.SourceKeyProperty];
                if (string.IsNullOrWhiteSpace(key))
                {
                    throw new SyncException("数据源提供的对象的ID为空");
                }

                SchemaObjectBase scObj = LoadSchemaObject(key);
                CompareAndChange(session, provider.CurrentData, session.Mappings.AllObjectValues, scObj);
            }
        }
Esempio n. 9
0
 internal void WriteEndLog(SyncSession session, bool success)
 {
     if (this.Loggers != null)
     {
         foreach (var item in Loggers)
         {
             try
             {
                 item.WriteEndLog(session, success);
             }
             catch
             {
                 if (System.Diagnostics.Debugger.IsAttached)
                 {
                     System.Diagnostics.Debugger.Log(1, "日志", "记录日志时发生错误。");
                 }
             }
         }
     }
 }
Esempio n. 10
0
        private void DoBatchExecute(SyncSession session)
        {
            DataProviderBase provider = session.SourceProvider;

            provider.Reset();

            List <NameObjectCollection> buffer = new List <NameObjectCollection>(session.BatchSize);
            int len;

            do
            {
                buffer.Clear();
                len = FetchToBuffer(buffer, provider, session.BatchSize);
                if (len > 0)
                {
                    string[] keys = new string[len];
                    for (int i = buffer.Count - 1; i >= 0; i--)
                    {
                        keys[i] = (string)buffer[i][session.Mappings.SourceKeyProperty];
                        if (string.IsNullOrWhiteSpace(keys[i]))
                        {
                            throw new SyncException("数据源提供的对象的ID为空");
                        }
                    }

                    SchemaObjectCollection scObjs = LoadSchemaObjects(keys);

                    foreach (NameObjectCollection item in buffer)
                    {
                        string           id    = (string)item[session.Mappings.SourceKeyProperty];
                        SchemaObjectBase scObj = scObjs[id];
                        if (scObjs != null)
                        {
                            CompareAndChange(session, item, session.Mappings.AllObjectValues, scObj);
                        }
                    }
                }
            } while (len > 0 && len != session.BatchSize);
        }
Esempio n. 11
0
        private void CompareAndChange(SyncSession session, NameObjectCollection currentData, PropertyMapping[] mappings, SchemaObjectBase scObj)
        {
            if (scObj != null && scObj.Status == Schemas.SchemaProperties.SchemaObjectStatus.Normal)
            {
                PropertyMapping mapping;
                bool            hasChange = false;
                bool            propertyChanged;

                for (int i = 0; i < mappings.Length; i++)
                {
                    mapping         = mappings[i];
                    propertyChanged = HasPropertyChange(session, session.Comparers[mapping.ComparerKey], mapping, currentData, scObj);
                    hasChange      |= propertyChanged;
                    if (propertyChanged)
                    {
                        MakeChange(session, session.Setters[mapping.SetterKey], mapping, currentData, scObj);
                    }
                }

                if (hasChange)
                {
                    //完成
                    try
                    {
                        PC.Adapters.SchemaObjectAdapter.Instance.Update(scObj);
                        session.NumerOfUpdated++;
                        session.WriteUpdateLog(scObj);
                    }
                    catch (Exception ex)
                    {
                        session.NumerOfErrors++;
                        session.WriteErrorLog(scObj, ex);
                    }
                }
            }
        }
Esempio n. 12
0
 public SyncEventArgs(SyncSession session)
 {
     this.session = session;
 }
Esempio n. 13
0
 private bool HasPropertyChange(SyncSession session, IPropertyComparer comparer, PropertyMapping mapping, NameObjectCollection srcValues, SchemaObjectBase targetObj)
 {
     return(comparer.AreEqual(session, mapping, srcValues, targetObj) == false);
 }
Esempio n. 14
0
 private void MakeChange(SyncSession session, IPropertySetter setter, PropertyMapping mapping, NameObjectCollection srcValues, SchemaObjectBase targetObj)
 {
     setter.SetValue(session, mapping, srcValues, targetObj);
 }
Esempio n. 15
0
 /// <summary>
 /// 写入更新失败的日志
 /// </summary>
 /// <param name="syncSession"></param>
 /// <param name="scObj"></param>
 /// <param name="ex"></param>
 public abstract void WriteErrorLog(SyncSession syncSession, SchemaObjectBase scObj, Exception ex);
Esempio n. 16
0
 public override void WriteLog(SyncSession syncSession, SchemaObjectBase scObj)
 {
     CheckLogPresent();
     this.log.NumberOfModifiedItems++;
 }
Esempio n. 17
0
 /// <summary>
 /// 写入结束日志
 /// </summary>
 /// <param name="session"></param>
 /// <param name="success">表示是否成功结束</param>
 public abstract void WriteEndLog(SyncSession session, bool success);
Esempio n. 18
0
 /// <summary>
 /// 写入开始更新的日志
 /// </summary>
 /// <param name="session"></param>
 public abstract void WriteStartLog(SyncSession session);
Esempio n. 19
0
        public void SetValue(SyncSession session, PropertyMapping mapping, NameObjectCollection srcValues, SchemaObjectBase targetObj)
        {
            string srcValue = srcValues[mapping.SourceProperty] as string ?? string.Empty;

            targetObj.Properties[mapping.TargetProperty].StringValue = srcValue;
        }
Esempio n. 20
0
 /// <summary>
 /// 写入已更新的对象日志
 /// </summary>
 /// <param name="syncSession"></param>
 /// <param name="scObj"></param>
 public abstract void WriteLog(SyncSession syncSession, SchemaObjectBase scObj);