Beispiel #1
0
        protected virtual void InitializeAdapters()
        {
            SpSyncAdapter adapter1 = new SpSyncAdapter("Contacts");

            adapter1.TableName = "Contacts";

            foreach (TypeMapping m in typeConnection)
            {
                adapter1.TypeMappings.Add(m);
            }

            adapter1.TypeMappings.DefaultMapping = new TypeMapping("*", typeof(String), "nvarchar", 100);

            adapter1.DataColumns.Add("ID");
            adapter1.DataColumns.Add("GUID");
            adapter1.DataColumns.Add("ContentType");
            adapter1.DataColumns.Add("Title");
            adapter1.DataColumns.Add("owshiddenversion");
            adapter1.DataColumns.Add("FullName");


            // adapter1.RowGuidColumn = "GUID";
            // adapter1.FilterClause = "<Query><Where><Eq><FieldRef Name='ContentType' /> <Value Type='Text'>Person</Value></Eq></Where></Query>";

            (RemoteProvider as SpServerSyncProvider).SyncAdapters.Add(adapter1);
        }
Beispiel #2
0
        /// <summary>
        /// Gets a SyncSchema object created by consulting the database, namely by filling the datatables from the SyncAdapters
        /// </summary>
        /// <param name="tableNames">the names of the tables to be included in the schema</param>
        /// <param name="missingTables">the names of the missing tables not found in the set of the SyncAdapters</param>
        /// <returns>the SyncSchema object</returns>
        protected virtual SyncSchema GetSchemaFromDatabase(Collection <string> tableNames, out Collection <string> missingTables)
        {
            SyncSchema schema = new SyncSchema();

            schema.SchemaDataSet        = new DataSet();
            schema.SchemaDataSet.Locale = CultureInfo.InvariantCulture;
            missingTables = new Collection <string>();

            Connection.Open();

            foreach (string tableName in tableNames)
            {
                SpSyncAdapter adapter = null;
                if (SyncAdapters.Contains(tableName))
                {
                    adapter = SyncAdapters[tableName];
                }

                if (adapter != null)
                {
                    DataTable dataTable = null;

                    try
                    {
                        dataTable           = adapter.FillSchema(dataTable, Connection);
                        dataTable.TableName = tableName;
                        schema.SchemaDataSet.Tables.Add(dataTable);
                    }
                    catch (Exception e)
                    {
                        missingTables.Add(tableName);
                        if (SyncTracer.IsErrorEnabled())
                        {
                            SyncTracer.Error(e.ToString());
                        }
                    }
                }
                else
                {
                    missingTables.Add(tableName);
                }
            }

            Connection.Close();

            if (missingTables.Count == 0)
            {
                missingTables = null;
            }

            return(schema);
        }
Beispiel #3
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;
            }
        }
Beispiel #4
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);
            }
        }