예제 #1
0
        /// <summary>
        /// Enumerates the changes from the server and puts them to the synccontext object
        /// </summary>
        /// <param name="groupMetadata">the metadata about the synchronization tables</param>
        /// <param name="syncSession">the object that contains synchronization variables</param>
        /// <param name="syncContext">the synchronization context to be changed</param>
        /// <param name="schema">the schema of the synchronization tables</param>
        private void EnumerateChanges(SyncGroupMetadata groupMetadata, SyncSession syncSession, SyncContext syncContext, SyncSchema schema)
        {//#DOWNLOAD a batch 2
            SyncStage syncStage = SyncStage.DownloadingChanges;

            SpSyncGroupAnchor newSyncAnchor = new SpSyncGroupAnchor();

            bool hasMoreData = false;

            foreach (SyncTableMetadata tableMetadata in groupMetadata.TablesMetadata)
            {
                SpSyncAdapter adapter = null;

                if (this.SyncAdapters.Contains(tableMetadata.TableName))
                {
                    adapter = this.SyncAdapters[tableMetadata.TableName];
                }

                if (adapter == null)
                {
                    throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
                                                              Messages.InvalidTableName, tableMetadata.TableName));
                }

                if (!schema.SchemaDataSet.Tables.Contains(tableMetadata.TableName))
                {
                    throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
                                                              Messages.TableNotInSchema, tableMetadata.TableName));
                }

                DataTable dataTable = schema.SchemaDataSet.Tables[tableMetadata.TableName].Clone();

                SyncTableProgress tableProgress = syncContext.GroupProgress.FindTableProgress(tableMetadata.TableName);

                SpSyncAnchor tableAnchor = SpSyncAnchor.Empty;

                if (tableMetadata.LastReceivedAnchor != null &&
                    tableMetadata.LastReceivedAnchor.Anchor != null)
                {
                    SpSyncGroupAnchor anchors = SpSyncGroupAnchor.Deserialize(tableMetadata.LastReceivedAnchor.Anchor);
                    if (anchors != null)
                    {
                        if (anchors.Contains(connString, tableMetadata.TableName))
                        {
                            tableAnchor = anchors[connString, tableMetadata.TableName];
                        }
                        newSyncAnchor = anchors;
                    }
                }

                SpSyncAnchor newAnchor = SpSyncAnchor.Empty;

                try
                {
                    if (tableMetadata.SyncDirection == SyncDirection.Snapshot)
                    {
                        newAnchor = adapter.SelectAll(tableAnchor, BatchSize, dataTable, Connection);
                    }
                    else
                    {
                        newAnchor = adapter.SelectIncremental(tableAnchor, BatchSize, Connection, dataTable);
                    }

                    hasMoreData = hasMoreData || newAnchor.HasMoreData;

                    if (syncContext.DataSet.Tables.Contains(tableMetadata.TableName))
                    {
                        DataTable contextTable = syncContext.DataSet.Tables[tableMetadata.TableName];
                        foreach (DataRow row in dataTable.Rows)
                        {
                            contextTable.ImportRow(row);
                        }
                    }
                    else
                    {
                        dataTable.TableName = tableMetadata.TableName;
                        syncContext.DataSet.Tables.Add(dataTable);
                    }
                }
                catch (Exception e)
                {
                    var e2 = new Microsoft.Synchronization.SyncException(e.Message, e);
                    throw e2;
                }
                finally
                {
                    newSyncAnchor[connString, tableMetadata.TableName] = newAnchor;
                }

                tableProgress.DataTable = dataTable;
                SyncProgressEventArgs args = new SyncProgressEventArgs(tableMetadata, tableProgress, groupMetadata, syncContext.GroupProgress, syncStage);//SYNC TODO
                OnSyncProgress(args);
                tableProgress.DataTable = null;
            }

            syncContext.NewAnchor = new SyncAnchor();

            syncContext.NewAnchor.Anchor = SpSyncGroupAnchor.Serialize(newSyncAnchor);

            int batchCount = groupMetadata.BatchCount == 0 ? 1 : groupMetadata.BatchCount;

            if (hasMoreData)
            {
                syncContext.BatchCount = batchCount + 1;
            }
            else
            {
                syncContext.BatchCount = batchCount;
            }
        }
예제 #2
0
        /// #UPLOAD 2
        private void ApplyChangesInternal(SyncGroupMetadata groupMetadata, DataSet dataSet, SyncSession syncSession, SyncContext syncContext)
        {
            SyncStage syncStage = SyncStage.UploadingChanges;

            foreach (SyncTableMetadata tableMetadata in groupMetadata.TablesMetadata)
            {
                SpSyncAdapter adapter = null;

                if (this.SyncAdapters.Contains(tableMetadata.TableName))
                {
                    adapter = this.SyncAdapters[tableMetadata.TableName];
                }

                if (adapter == null)
                {
                    throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
                                                              Messages.InvalidTableName, tableMetadata.TableName));
                }


                // SpSyncAnchor anchor
                if (!dataSet.Tables.Contains(tableMetadata.TableName))
                {
                    throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
                                                              Messages.TableNotInSchema, tableMetadata.TableName));
                }

                SyncTableProgress tableProgress = syncContext.GroupProgress.FindTableProgress(tableMetadata.TableName);

                DataTable dataTable = dataSet.Tables[tableMetadata.TableName];

                try
                {
                    Collection <SyncConflict> conflicts;
                    int changesCount = dataTable.Rows.Count;
                    adapter.Update(dataTable, Connection, out conflicts);

                    if (conflicts != null)
                    {
                        foreach (SyncConflict conflict in conflicts)
                        {
                            ApplyChangeFailedEventArgs failureArgs = new ApplyChangeFailedEventArgs(tableMetadata, conflict, null, syncSession, syncContext, Connection, null);
                            OnApplyChangeFailed(failureArgs);

                            if (failureArgs.Action == ApplyAction.Continue)
                            {
                                if (conflict != null)
                                {
                                    tableProgress.ChangesFailed++;
                                    tableProgress.Conflicts.Add(conflict);
                                }
                            }
                        }
                    }
                    tableProgress.ChangesApplied = changesCount - tableProgress.ChangesFailed;
                }
                catch (Exception e)
                {
                    SyncConflict conflict = new SyncConflict(ConflictType.ErrorsOccurred, SyncStage.UploadingChanges)
                    {
                        ErrorMessage = e.Message + ", InnerException:" + e.InnerException.ToString(), ServerChange = dataTable, ClientChange = dataTable
                    };
                    ApplyChangeFailedEventArgs failureArgs = new ApplyChangeFailedEventArgs(tableMetadata, conflict, null, syncSession, syncContext, Connection, null);
                    OnApplyChangeFailed(failureArgs);
                    // handle errors?
                    if (SyncTracer.IsErrorEnabled())
                    {
                        SyncTracer.Error(e.ToString());
                    }
                }

                SyncProgressEventArgs args = new SyncProgressEventArgs(tableMetadata, tableProgress, groupMetadata, syncContext.GroupProgress, syncStage);
                OnSyncProgress(args);
            }
        }