protected SyncSchema GetSchemaInternal(Collection <string> tableNames, out Collection <string> missingTables)
        {
            SyncSchema          schema         = null;
            Collection <string> missingTables2 = null;

            missingTables = new Collection <string>();

            if (Schema != null)
            {
                schema = GetSchemaFromSchemaDataset(tableNames, out missingTables2);

                if (missingTables2 != null)
                {
                    SyncSchema schema2 = GetSchemaFromDatabase(missingTables2, out missingTables);
                    schema.Merge(schema2);
                }
                if (missingTables.Count == 0)
                {
                    missingTables = null;
                }
            }
            else
            {
                schema = GetSchemaFromDatabase(tableNames, out missingTables);
            }
            return(schema);
        }
        /// <summary>
        /// Selects for a table in the server database the inserts, updates, and deletes to apply to the client database for a synchronization group.
        /// </summary>
        /// <param name="groupMetadata">A SyncGroupMetadata object that contains metadata about the synchronization group.</param>
        /// <param name="syncSession">A SyncSession object that contains synchronization session variables, such as the ID of the client that is synchronizing.</param>
        /// <returns>A SyncContext object that contains synchronization data and metadata.</returns>
        public override SyncContext GetChanges(SyncGroupMetadata groupMetadata, SyncSession syncSession)
        {//#DOWNLOAD a batch 1
            if (groupMetadata == null)
            {
                throw new ArgumentNullException("groupMetadata");
            }
            if (syncSession == null)
            {
                throw new ArgumentNullException("syncSession");
            }

            SyncContext syncContext = new SyncContext();
            DataSet     dataSet     = new DataSet();

            groupMetadata = InitializeMetadata(groupMetadata, dataSet, syncContext);

            SelectingChangesEventArgs selectingArgs = new SelectingChangesEventArgs(groupMetadata, syncSession, syncContext, Connection, null);

            OnSelectingChanges(selectingArgs);

            SyncSchema          schema        = new SyncSchema();
            Collection <string> tables        = new Collection <string>();
            Collection <string> missingTables = new Collection <string>();

            foreach (var tableMetadata in groupMetadata.TablesMetadata)
            {
                tables.Add(tableMetadata.TableName);
            }

            if (tables.Count > 0)
            {
                schema = GetSchemaInternal(tables, out missingTables);
            }

            if (missingTables != null)
            {
                string[] tableArray = new string[missingTables.Count];
                missingTables.CopyTo(tableArray, 0);
                SchemaException e = new SchemaException(String.Format(CultureInfo.CurrentCulture,
                                                                      Messages.MissingTables, String.Join(", ", tableArray)));
                e.SyncStage   = SyncStage.ReadingSchema;
                e.ErrorNumber = SyncErrorNumber.MissingTableSchema;
                throw e;
            }

            // FIX: Get schema from somewhere (possibly the adapter or a temporary schema)
            // Possible performance hit when in mobile

            EnumerateChanges(groupMetadata, syncSession, syncContext, schema);

            ChangesSelectedEventArgs selectedArgs = new ChangesSelectedEventArgs(groupMetadata, syncSession, syncContext, Connection, null);//sYNC tODO

            OnChangesSelected(selectedArgs);

            return(syncContext);
        }
        /// <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);
        }
        /// <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 EnumerateChanges2(SyncGroupMetadata groupMetadata, SyncSession syncSession, SyncContext syncContext, SyncSchema schema)
         * {
         *  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);
         *
         *
         *
         *      DataTable insertTable = dataTable.Clone();
         *      DataTable updateTable = dataTable.Clone();
         *      DataTable deleteTable = dataTable.Clone();
         *
         *      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(tableMetadata.TableName))
         *                  tableAnchor = anchors[tableMetadata.TableName];
         *              else
         *                  throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
         *                      Messages.AnchorNotValidForTable, tableMetadata.TableName));
         *          }
         *      }
         *
         *      SpSyncAnchor newAnchor = SpSyncAnchor.Empty;
         *
         *      try
         *      {
         *          if (tableMetadata.SyncDirection == SyncDirection.Snapshot)
         *          {
         *              newAnchor = adapter.SelectAll(tableAnchor, BatchSize, updateTable, Connection);
         *          }
         *          else
         *          {
         *              newAnchor = adapter.SelectIncremental(tableAnchor, BatchSize, Connection, insertTable, updateTable, deleteTable);
         *          }
         *
         *          hasMoreData = hasMoreData || newAnchor.HasMoreData;
         *
         *          foreach (DataRow row in insertTable.Rows)
         *          {
         *              row.SetModified();
         *              dataTable.ImportRow(row);
         *              tableProgress.Inserts++;
         *          }
         *
         *          foreach (DataRow row in deleteTable.Rows)
         *          {
         *              row.Delete();
         *              dataTable.ImportRow(row);
         *              tableProgress.Deletes++;
         *          }
         *
         *          foreach (DataRow row in updateTable.Rows)
         *          {
         *              row.SetModified();
         *              dataTable.ImportRow(row);
         *              tableProgress.Updates++;
         *          }
         *
         *          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);
         *          throw e2;
         *      }
         *      finally
         *      {
         *          newSyncAnchor[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;
         *
         * }
         */

        /// <summary>
        /// Returns a SyncSchema object that contains the schema for each table specified.
        /// </summary>
        /// <param name="tableNames">A collection of table names for which the server provider should get the schema.</param>
        /// <param name="syncSession">A SyncSession object that contains synchronization session variables, such as the ID of the client that is synchronizing.</param>
        /// <returns>A SyncSchema object that contains the schema for each table that is specified.</returns>
        public override SyncSchema GetSchema(Collection <string> tableNames, SyncSession syncSession)
        {
            Collection <string> missingTables = null;

            SyncSchema schema = GetSchemaInternal(tableNames, out missingTables);

            if (missingTables != null)
            {
                string[] tableArray = new string[missingTables.Count];
                missingTables.CopyTo(tableArray, 0);
                SchemaException e = new SchemaException(String.Format(CultureInfo.CurrentCulture,
                                                                      Messages.MissingTables, String.Join(", ", tableArray)));
                e.SyncStage   = SyncStage.ReadingSchema;
                e.ErrorNumber = SyncErrorNumber.MissingTableSchema;
                throw e;
            }

            return(schema);
        }
        // Meet Code For Buddylist Implementation.
        #region DataBase Creation Function
        // public static string ClientConnectionString = @"Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "AllocateConferenceNumber.sdf";

        /// <summary>
        /// this function will create Node_Status table if it is not created. Buddy_Status will contains all the users with his/her status.
        /// </summary>
        void fncCreateBuddyStatusTable()
        {
            try
            {

               if (false == File.Exists(strLocalDBPath))
                {
                    SqlCeEngine clientEngine = new SqlCeEngine(ClientConnectionString);
                    clientEngine.CreateDatabase();
                }
                if (!IsTableExits("Node_Status"))
                {
                    SqlCeClientSyncProvider sync = new SqlCeClientSyncProvider(ClientConnectionString);

                    SyncTable tblNode_Status = new SyncTable("Node_Status");

                    SyncSchema syncSchemaLead = new SyncSchema();
                    syncSchemaLead.Tables.Add("Node_Status");
                    syncSchemaLead.Tables["Node_Status"].Columns.Add("Buddy_ID");
                    syncSchemaLead.Tables["Node_Status"].Columns["Buddy_ID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaLead.Tables["Node_Status"].Columns["Buddy_ID"].AutoIncrement = true;
                    syncSchemaLead.Tables["Node_Status"].Columns["Buddy_ID"].AutoIncrementSeed = 1;
                    syncSchemaLead.Tables["Node_Status"].Columns["Buddy_ID"].AutoIncrementStep = 1;
                    syncSchemaLead.Tables["Node_Status"].PrimaryKey = new string[] { "Buddy_ID" };

                    syncSchemaLead.Tables["Node_Status"].Columns.Add("Buddy_Name");
                    syncSchemaLead.Tables["Node_Status"].Columns["Buddy_Name"].ProviderDataType = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["Node_Status"].Columns["Buddy_Name"].MaxLength = 30;

                    syncSchemaLead.Tables["Node_Status"].Columns.Add("Buddy_Status");
                    syncSchemaLead.Tables["Node_Status"].Columns["Buddy_Status"].ProviderDataType = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["Node_Status"].Columns["Buddy_Status"].MaxLength = 30;

                    syncSchemaLead.Tables["Node_Status"].Columns.Add("Buddy_TimeStamp");
                    syncSchemaLead.Tables["Node_Status"].Columns["Buddy_TimeStamp"].ProviderDataType = SqlDbType.DateTime.ToString();
                    syncSchemaLead.Tables["Node_Status"].Columns["Buddy_TimeStamp"].MaxLength = 30;


                    sync.CreateSchema(tblNode_Status, syncSchemaLead);
                }
                if (VMuktiInfo.CurrentPeer.DisplayName != null && VMuktiInfo.CurrentPeer.DisplayName != string.Empty)
                {
                    fncSNInsertBuddy(VMuktiInfo.CurrentPeer.DisplayName, "Online");
                }
            }
            catch (Exception ex)
            {
                VMuktiHelper.ExceptionHandler(ex, "fncCreateBuddyStatusTable()", "Domains\\BootstrapServiceDomain.cs");
            }
        }
        private SyncTable CreateCallBackTable()
        {
            SqlCeConnection ce = null;
            try
            {

                //   if (!IsTableExits("CallBack"))
                {
                    //        SqlCeClientSyncProvider sync = new SqlCeClientSyncProvider(ClientConnectionString);
                    ce = new SqlCeConnection(strClientConnectionString);
                    ce.Open();
                   // OpenConnection(true);
                    tblCallBackTable = new SyncTable("CallBack");
                    tblCallBackTable.SyncDirection = SyncDirection.UploadOnly;

                    SyncSchema syncSchemaCallBack = new SyncSchema();
                    syncSchemaCallBack.Tables.Add("CallBack");

                    syncSchemaCallBack.Tables["CallBack"].Columns.Add("ID");
                    syncSchemaCallBack.Tables["CallBack"].Columns["ID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCallBack.Tables["CallBack"].PrimaryKey = new string[] { "ID" };
                    syncSchemaCallBack.Tables["CallBack"].Columns["ID"].AutoIncrement = true;
                    syncSchemaCallBack.Tables["CallBack"].Columns["ID"].AutoIncrementSeed = 1;
                    syncSchemaCallBack.Tables["CallBack"].Columns["ID"].AutoIncrementStep = 1;
                    syncSchemaCallBack.Tables["CallBack"].Columns["ID"].AllowNull = false;

                    syncSchemaCallBack.Tables["CallBack"].Columns.Add("CallID");
                    syncSchemaCallBack.Tables["CallBack"].Columns["CallID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCallBack.Tables["CallBack"].Columns["CallID"].AllowNull = false;


                    syncSchemaCallBack.Tables["CallBack"].Columns.Add("CallBackDate");
                    syncSchemaCallBack.Tables["CallBack"].Columns["CallBackDate"].ProviderDataType = SqlDbType.DateTime.ToString();
                    syncSchemaCallBack.Tables["CallBack"].Columns["CallBackDate"].AllowNull = false;

                    syncSchemaCallBack.Tables["CallBack"].Columns.Add("Comment");
                    syncSchemaCallBack.Tables["CallBack"].Columns["Comment"].ProviderDataType = SqlDbType.NVarChar.ToString();
                    syncSchemaCallBack.Tables["CallBack"].Columns["Comment"].MaxLength = 50;
                    syncSchemaCallBack.Tables["CallBack"].Columns["Comment"].AllowNull = true;

                    syncSchemaCallBack.Tables["CallBack"].Columns.Add("IsPublic");
                    syncSchemaCallBack.Tables["CallBack"].Columns["IsPublic"].ProviderDataType = SqlDbType.Bit.ToString();
                    syncSchemaCallBack.Tables["CallBack"].Columns["IsPublic"].AllowNull = false;

                    syncSchemaCallBack.Tables["CallBack"].Columns.Add("IsDeleted");
                    syncSchemaCallBack.Tables["CallBack"].Columns["IsDeleted"].ProviderDataType = SqlDbType.Bit.ToString();
                    syncSchemaCallBack.Tables["CallBack"].Columns["IsDeleted"].AllowNull = false;

                    // sync.CreateSchema(tblCallBackTable, syncSchemaCallBack);
                    clientSyncProvider.CreateSchema(tblCallBackTable, syncSchemaCallBack);
                    return tblCallBackTable;
                }
            }
            catch (Exception ex)
            {
                VMuktiAPI.VMuktiHelper.ExceptionHandler(ex, "CreateCallBackTable()", "ClsUserDataService.cs");
                //MessageBox.Show("CreateCallBackTable: " + ex.Message);
                return null;
            }
            finally
            {
                //  OpenConnection(false);
                if (ce != null && ce.State == ConnectionState.Open)
                {
                    ce.Close();
                }
            }
        }
        private SyncTable CreateLeadsTable()
        {
            SqlCeConnection ce = null;
            try
            {

                //  if (!IsTableExits("Leads"))
                {
                    //      SqlCeClientSyncProvider sync = new SqlCeClientSyncProvider(ClientConnectionString);
                    //OpenConnection(true);
                    
                    ce = new SqlCeConnection(strClientConnectionString);
                    ce.Open();
                    tblLeadsTable = new SyncTable("Leads");
                    tblLeadsTable.SyncDirection = SyncDirection.UploadOnly;

                    SyncSchema syncSchemaLead = new SyncSchema();
                    syncSchemaLead.Tables.Add("Leads");

                    syncSchemaLead.Tables["Leads"].Columns.Add("ID");
                    syncSchemaLead.Tables["Leads"].Columns["ID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaLead.Tables["Leads"].PrimaryKey = new string[] { "ID" };
                    //syncSchemaLead.Tables["Leads"].Columns["ID"].AutoIncrement = true;
                    //syncSchemaLead.Tables["Leads"].Columns["ID"].AutoIncrementSeed = 1;
                    //syncSchemaLead.Tables["Leads"].Columns["ID"].AutoIncrementStep = 1;
                    syncSchemaLead.Tables["Leads"].Columns["ID"].AllowNull = false;

                    syncSchemaLead.Tables["Leads"].Columns.Add("PhoneNo");
                    syncSchemaLead.Tables["Leads"].Columns["PhoneNo"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaLead.Tables["Leads"].Columns["PhoneNo"].AllowNull = false;


                    syncSchemaLead.Tables["Leads"].Columns.Add("LeadFormatID");
                    syncSchemaLead.Tables["Leads"].Columns["LeadFormatID"].ProviderDataType = SqlDbType.Int.ToString();
                    syncSchemaLead.Tables["Leads"].Columns["LeadFormatID"].AllowNull = false;

                    syncSchemaLead.Tables["Leads"].Columns.Add("CreatedDate");
                    syncSchemaLead.Tables["Leads"].Columns["CreatedDate"].ProviderDataType = SqlDbType.DateTime.ToString();
                    syncSchemaLead.Tables["Leads"].Columns["CreatedDate"].AllowNull = true;

                    syncSchemaLead.Tables["Leads"].Columns.Add("CreatedBy");
                    syncSchemaLead.Tables["Leads"].Columns["CreatedBy"].ProviderDataType = SqlDbType.Int.ToString();
                    syncSchemaLead.Tables["Leads"].Columns["CreatedBy"].AllowNull = false;

                    syncSchemaLead.Tables["Leads"].Columns.Add("DeletedDate");
                    syncSchemaLead.Tables["Leads"].Columns["DeletedDate"].ProviderDataType = SqlDbType.DateTime.ToString();
                    syncSchemaLead.Tables["Leads"].Columns["DeletedDate"].AllowNull = true;

                    syncSchemaLead.Tables["Leads"].Columns.Add("DeletedBy");
                    syncSchemaLead.Tables["Leads"].Columns["DeletedBy"].ProviderDataType = SqlDbType.Int.ToString();
                    syncSchemaLead.Tables["Leads"].Columns["DeletedBy"].AllowNull = true;

                    syncSchemaLead.Tables["Leads"].Columns.Add("IsDeleted");
                    syncSchemaLead.Tables["Leads"].Columns["IsDeleted"].ProviderDataType = SqlDbType.Bit.ToString();
                    syncSchemaLead.Tables["Leads"].Columns["IsDeleted"].AllowNull = false;

                    syncSchemaLead.Tables["Leads"].Columns.Add("ModifiedDate");
                    syncSchemaLead.Tables["Leads"].Columns["ModifiedDate"].ProviderDataType = SqlDbType.DateTime.ToString();
                    syncSchemaLead.Tables["Leads"].Columns["ModifiedDate"].AllowNull = false;

                    syncSchemaLead.Tables["Leads"].Columns.Add("ModifiedBy");
                    syncSchemaLead.Tables["Leads"].Columns["ModifiedBy"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaLead.Tables["Leads"].Columns["ModifiedBy"].AllowNull = false;

                    syncSchemaLead.Tables["Leads"].Columns.Add("DNCFlag");
                    syncSchemaLead.Tables["Leads"].Columns["DNCFlag"].ProviderDataType = SqlDbType.Bit.ToString();
                    syncSchemaLead.Tables["Leads"].Columns["DNCFlag"].AllowNull = false;

                    syncSchemaLead.Tables["Leads"].Columns.Add("DNCBy");
                    syncSchemaLead.Tables["Leads"].Columns["DNCBy"].ProviderDataType = SqlDbType.Int.ToString();
                    syncSchemaLead.Tables["Leads"].Columns["DNCBy"].AllowNull = true;

                    syncSchemaLead.Tables["Leads"].Columns.Add("ListID");
                    syncSchemaLead.Tables["Leads"].Columns["ListID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaLead.Tables["Leads"].Columns["ListID"].AllowNull = false;

                    syncSchemaLead.Tables["Leads"].Columns.Add("LocationID");
                    syncSchemaLead.Tables["Leads"].Columns["LocationID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaLead.Tables["Leads"].Columns["LocationID"].AllowNull = false;

                    syncSchemaLead.Tables["Leads"].Columns.Add("RecycleCount");
                    syncSchemaLead.Tables["Leads"].Columns["RecycleCount"].ProviderDataType = SqlDbType.Int.ToString();
                    syncSchemaLead.Tables["Leads"].Columns["RecycleCount"].AllowNull = false;

                    syncSchemaLead.Tables["Leads"].Columns.Add("Status");
                    syncSchemaLead.Tables["Leads"].Columns["Status"].ProviderDataType = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["Leads"].Columns["Status"].MaxLength = 50;
                    syncSchemaLead.Tables["Leads"].Columns["Status"].AllowNull = true;

                    syncSchemaLead.Tables["Leads"].Columns.Add("IsGlobalDNC");
                    syncSchemaLead.Tables["Leads"].Columns["IsGlobalDNC"].ProviderDataType = SqlDbType.Bit.ToString();
                    syncSchemaLead.Tables["Leads"].Columns["IsGlobalDNC"].AllowNull = true;

                    //syncSchemaLead.Tables["Leads"].Columns.Add("LastEditDate");
                    //syncSchemaLead.Tables["Leads"].Columns["LastEditDate"].ProviderDataType = SqlDbType.DateTime.ToString();
                    //syncSchemaLead.Tables["Leads"].Columns["LastEditDate"].AllowNull = false;

                    //syncSchemaLead.Tables["Leads"].Columns.Add("CreationDate");
                    //syncSchemaLead.Tables["Leads"].Columns["CreationDate"].ProviderDataType = SqlDbType.DateTime.ToString();
                    //syncSchemaLead.Tables["Leads"].Columns["CreationDate"].AllowNull = false;


                    //  sync.CreateSchema(tblLeadsTable, syncSchemaLead);
                    clientSyncProvider.CreateSchema(tblLeadsTable, syncSchemaLead);
                    return tblLeadsTable;
                }
            }
            catch (Exception ex)
            {
                VMuktiAPI.VMuktiHelper.ExceptionHandler(ex, "CreateLeadsTable()", "ClsUserDataService.cs");
                //MessageBox.Show("CreateLeadsTable: " + ex.Message);
                return null;
            }
            finally
            {
                //OpenConnection(false);
                if (ce != null && ce.State == ConnectionState.Open)
                {
                    ce.Close();
                }
            }
        }
        public SyncTable CreateCallTable()
        {
            SqlCeConnection ce = null;
            try
            {
                //  if (!IsTableExits("Call"))
                {
                  //  OpenConnection(true);
                    
                    ce = new SqlCeConnection(strClientConnectionString);
                    ce.Open();

                    tblCallTable = new SyncTable("Call");
                    tblCallTable.SyncDirection = SyncDirection.UploadOnly;


                    SyncSchema syncSchemaCall = new SyncSchema();
                    syncSchemaCall.Tables.Add("Call");

                    syncSchemaCall.Tables["Call"].Columns.Add("ID");
                    syncSchemaCall.Tables["Call"].Columns["ID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCall.Tables["Call"].PrimaryKey = new string[] { "ID" };
                    syncSchemaCall.Tables["Call"].Columns["ID"].AutoIncrement = true;
                    syncSchemaCall.Tables["Call"].Columns["ID"].AutoIncrementSeed = 1;
                    syncSchemaCall.Tables["Call"].Columns["ID"].AutoIncrementStep = 1;
                    syncSchemaCall.Tables["Call"].Columns["ID"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("LeadID");
                    syncSchemaCall.Tables["Call"].Columns["LeadID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCall.Tables["Call"].Columns["LeadID"].AllowNull = false;


                    syncSchemaCall.Tables["Call"].Columns.Add("CalledDate");
                    syncSchemaCall.Tables["Call"].Columns["CalledDate"].ProviderDataType = SqlDbType.DateTime.ToString();
                    syncSchemaCall.Tables["Call"].Columns["CalledDate"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("ModifiedDate");
                    syncSchemaCall.Tables["Call"].Columns["ModifiedDate"].ProviderDataType = SqlDbType.DateTime.ToString();
                    syncSchemaCall.Tables["Call"].Columns["ModifiedDate"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("ModifiedBy");
                    syncSchemaCall.Tables["Call"].Columns["ModifiedBy"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCall.Tables["Call"].Columns["ModifiedBy"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("GeneratedBy");
                    syncSchemaCall.Tables["Call"].Columns["GeneratedBy"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCall.Tables["Call"].Columns["GeneratedBy"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("StartDate");
                    syncSchemaCall.Tables["Call"].Columns["StartDate"].ProviderDataType = SqlDbType.DateTime.ToString();
                    syncSchemaCall.Tables["Call"].Columns["StartDate"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("StartTime");
                    syncSchemaCall.Tables["Call"].Columns["StartTime"].ProviderDataType = SqlDbType.DateTime.ToString();
                    syncSchemaCall.Tables["Call"].Columns["StartTime"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("DurationInSecond");
                    syncSchemaCall.Tables["Call"].Columns["DurationInSecond"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCall.Tables["Call"].Columns["DurationInSecond"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("DespositionID");
                    syncSchemaCall.Tables["Call"].Columns["DespositionID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCall.Tables["Call"].Columns["DespositionID"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("CampaignID");
                    syncSchemaCall.Tables["Call"].Columns["CampaignID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCall.Tables["Call"].Columns["CampaignID"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("ConfID");
                    syncSchemaCall.Tables["Call"].Columns["ConfID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCall.Tables["Call"].Columns["ConfID"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("IsDeleted");
                    syncSchemaCall.Tables["Call"].Columns["IsDeleted"].ProviderDataType = SqlDbType.Bit.ToString();
                    syncSchemaCall.Tables["Call"].Columns["IsDeleted"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("CallNote");
                    syncSchemaCall.Tables["Call"].Columns["CallNote"].ProviderDataType = SqlDbType.NVarChar.ToString();
                    syncSchemaCall.Tables["Call"].Columns["CallNote"].MaxLength = 50;
                    syncSchemaCall.Tables["Call"].Columns["CallNote"].AllowNull = true;

                    syncSchemaCall.Tables["Call"].Columns.Add("IsDNC");
                    syncSchemaCall.Tables["Call"].Columns["IsDNC"].ProviderDataType = SqlDbType.Bit.ToString();
                    syncSchemaCall.Tables["Call"].Columns["IsDNC"].AllowNull = true;

                    syncSchemaCall.Tables["Call"].Columns.Add("IsGlobal");
                    syncSchemaCall.Tables["Call"].Columns["IsGlobal"].ProviderDataType = SqlDbType.Bit.ToString();
                    syncSchemaCall.Tables["Call"].Columns["IsGlobal"].AllowNull = true;


                    //sync.CreateSchema(tblCallTable, syncSchemaCall);
                    clientSyncProvider.CreateSchema(tblCallTable, syncSchemaCall);
                    return tblCallTable;
                }
            }
            catch (Exception ex)
            {
                VMuktiAPI.VMuktiHelper.ExceptionHandler(ex, "CreateCallTable()", "ClsUserDataService.cs");
                //System.Windows.Forms.MessageBox.Show("CreateCallTable: " + ex.Message);
                return null;
            }
            finally
            {
             //   OpenConnection(false);
                if (ce != null && ce.State == ConnectionState.Open)
                {
                    ce.Close();
                }
            }
        }
        /// <summary>
        /// Gets a SyncSchema object created from the existing DataSet defined by the user
        /// </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 dataset</param>
        /// <returns>the SyncSchema object</returns>
        protected virtual SyncSchema GetSchemaFromSchemaDataset(Collection <string> tableNames, out Collection <string> missingTables)
        {
            SyncSchema schema = new SyncSchema();

            missingTables = new Collection <string>();

            foreach (string tableName in tableNames)
            {
                if (!Schema.Tables.Contains(tableName))
                {
                    missingTables.Add(tableName);
                }
            }

            foreach (DataTable table in Schema.SchemaDataSet.Tables)
            {
                if (tableNames.Contains(table.TableName))
                {
                    DataTable table2 = table.Copy();
                    schema.SchemaDataSet.Tables.Add(table2);
                }
            }

            foreach (DataRelation relation in Schema.SchemaDataSet.Relations)
            {
                if (tableNames.Contains(relation.ParentTable.TableName) &&
                    tableNames.Contains(relation.ChildTable.TableName))
                {
                    Collection <DataColumn> parentCollection = new Collection <DataColumn>();
                    foreach (DataColumn c in relation.ParentColumns)
                    {
                        parentCollection.Add(schema.SchemaDataSet.Tables[relation.ParentTable.TableName].Columns[c.ColumnName]);
                    }

                    Collection <DataColumn> childCollection = new Collection <DataColumn>();
                    foreach (DataColumn c in relation.ParentColumns)
                    {
                        childCollection.Add(schema.SchemaDataSet.Tables[relation.ChildTable.TableName].Columns[c.ColumnName]);
                    }

                    DataColumn[] parentArray = new DataColumn[parentCollection.Count];
                    DataColumn[] childArray  = new DataColumn[childCollection.Count];
                    parentCollection.CopyTo(parentArray, 0);
                    childCollection.CopyTo(childArray, 0);
                    DataRelation relation2 = new DataRelation(relation.RelationName, parentArray, childArray);
                    schema.SchemaDataSet.Relations.Add(relation2);
                }
            }

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

            //CODE FIX TO AVOID THE DREADED NOTORIOUS "TESTINTRANET" BUG
            Connection.Open();
            Connection.Close();
            //END OF CODE FIX

            return(schema);
        }
        public SyncTable CreateCallTable()
        {
            try
            {
                //  if (!IsTableExits("Call"))
                {
                    tblCallTable = new SyncTable("Call");
                    tblCallTable.SyncDirection = SyncDirection.UploadOnly;
                    

                    SyncSchema syncSchemaCall= new SyncSchema();
                    syncSchemaCall.Tables.Add("Call");

                    syncSchemaCall.Tables["Call"].Columns.Add("ID");
                    syncSchemaCall.Tables["Call"].Columns["ID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCall.Tables["Call"].PrimaryKey = new string[] { "ID" };
                    syncSchemaCall.Tables["Call"].Columns["ID"].AutoIncrement = true;
                    syncSchemaCall.Tables["Call"].Columns["ID"].AutoIncrementSeed = 1;
                    syncSchemaCall.Tables["Call"].Columns["ID"].AutoIncrementStep = 1;
                    syncSchemaCall.Tables["Call"].Columns["ID"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("LeadID");
                    syncSchemaCall.Tables["Call"].Columns["LeadID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCall.Tables["Call"].Columns["LeadID"].AllowNull = false;


                    syncSchemaCall.Tables["Call"].Columns.Add("CalledDate");
                    syncSchemaCall.Tables["Call"].Columns["CalledDate"].ProviderDataType = SqlDbType.DateTime.ToString();
                    syncSchemaCall.Tables["Call"].Columns["CalledDate"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("ModifiedDate");
                    syncSchemaCall.Tables["Call"].Columns["ModifiedDate"].ProviderDataType = SqlDbType.DateTime.ToString();
                    syncSchemaCall.Tables["Call"].Columns["ModifiedDate"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("ModifiedBy");
                    syncSchemaCall.Tables["Call"].Columns["ModifiedBy"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCall.Tables["Call"].Columns["ModifiedBy"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("GeneratedBy");
                    syncSchemaCall.Tables["Call"].Columns["GeneratedBy"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCall.Tables["Call"].Columns["GeneratedBy"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("StartDate");
                    syncSchemaCall.Tables["Call"].Columns["StartDate"].ProviderDataType = SqlDbType.DateTime.ToString();
                    syncSchemaCall.Tables["Call"].Columns["StartDate"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("StartTime");
                    syncSchemaCall.Tables["Call"].Columns["StartTime"].ProviderDataType = SqlDbType.DateTime.ToString();
                    syncSchemaCall.Tables["Call"].Columns["StartTime"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("DurationInSecond");
                    syncSchemaCall.Tables["Call"].Columns["DurationInSecond"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCall.Tables["Call"].Columns["DurationInSecond"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("DespositionID");
                    syncSchemaCall.Tables["Call"].Columns["DespositionID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCall.Tables["Call"].Columns["DespositionID"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("CampaignID");
                    syncSchemaCall.Tables["Call"].Columns["CampaignID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCall.Tables["Call"].Columns["CampaignID"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("ConfID");
                    syncSchemaCall.Tables["Call"].Columns["ConfID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCall.Tables["Call"].Columns["ConfID"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("IsDeleted");
                    syncSchemaCall.Tables["Call"].Columns["IsDeleted"].ProviderDataType = SqlDbType.Bit.ToString();
                    syncSchemaCall.Tables["Call"].Columns["IsDeleted"].AllowNull = false;

                    syncSchemaCall.Tables["Call"].Columns.Add("CallNote");
                    syncSchemaCall.Tables["Call"].Columns["CallNote"].ProviderDataType = SqlDbType.NVarChar.ToString();
                    syncSchemaCall.Tables["Call"].Columns["CallNote"].MaxLength = 50;
                    syncSchemaCall.Tables["Call"].Columns["CallNote"].AllowNull = true;

                    syncSchemaCall.Tables["Call"].Columns.Add("IsDNC");
                    syncSchemaCall.Tables["Call"].Columns["IsDNC"].ProviderDataType = SqlDbType.Bit.ToString();
                    syncSchemaCall.Tables["Call"].Columns["IsDNC"].AllowNull = true;

                    syncSchemaCall.Tables["Call"].Columns.Add("IsGlobal");
                    syncSchemaCall.Tables["Call"].Columns["IsGlobal"].ProviderDataType = SqlDbType.Bit.ToString();
                    syncSchemaCall.Tables["Call"].Columns["IsGlobal"].AllowNull = true;

                    syncSchemaCall.Tables["Call"].Columns.Add("RecordedFileName");
                    syncSchemaCall.Tables["Call"].Columns["RecordedFileName"].ProviderDataType = SqlDbType.NVarChar.ToString();
                    syncSchemaCall.Tables["Call"].Columns["RecordedFileName"].MaxLength = 100;
                    syncSchemaCall.Tables["Call"].Columns["RecordedFileName"].AllowNull = true;

                    //sync.CreateSchema(tblCallTable, syncSchemaCall);
                    clientSyncProvider.CreateSchema(tblCallTable, syncSchemaCall);
                    return tblCallTable;
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return null;
            }

        }
        /// <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;
            }
        }
        internal SyncContext Sync(string value)
        {
            if (String.IsNullOrEmpty(value))
            {
                throw new Exception("Loading a project requires a name. Ex : dotnet sync --load project01");
            }

            Project project = DataStore.Current.LoadProject(value);

            if (project == null)
            {
                throw new Exception($"Project {value} does not exists.");
            }

            if (project.ServerProvider == null || string.IsNullOrEmpty(project.ServerProvider.ConnectionString))
            {
                throw new Exception($"Server provider for project {project.Name} is not correctly defined. See help: dotnet sync provider --help");
            }

            if (project.ClientProvider == null || string.IsNullOrEmpty(project.ClientProvider.ConnectionString))
            {
                throw new Exception($"Client provider for project {project.Name} is not correctly defined. See help: dotnet sync provider --help");
            }

            if (project.ServerProvider.ProviderType != ProviderType.Web && (project.Tables == null || project.Tables.Count <= 0))
            {
                throw new Exception($"No table configured for project {project.Name}. See help: dotnet sync table --help");
            }

            IRemoteOrchestrator remoteOrchestrator;
            ILocalOrchestrator  localOrchestrator;

            switch (project.ServerProvider.ProviderType)
            {
            case ProviderType.Sqlite:
                throw new Exception("Can't use Sqlite as a server provider");

            case ProviderType.Web:
                remoteOrchestrator = new WebClientOrchestrator(new Uri(project.ServerProvider.ConnectionString));
                break;

            case ProviderType.MySql:
                remoteOrchestrator = new RemoteOrchestrator(new MySqlSyncProvider(project.ServerProvider.ConnectionString));
                break;

            case ProviderType.SqlServer:
            default:
                remoteOrchestrator = new RemoteOrchestrator(new SqlSyncProvider(project.ServerProvider.ConnectionString));
                break;
            }

            switch (project.ClientProvider.ProviderType)
            {
            case ProviderType.Web:
                throw new Exception("Web proxy is used as a proxy server. You have to use an ASP.NET web backend. CLI uses a proxy as server provider");

            case ProviderType.Sqlite:
                localOrchestrator = new LocalOrchestrator(new SqliteSyncProvider(project.ClientProvider.ConnectionString));
                break;

            case ProviderType.MySql:
                localOrchestrator = new LocalOrchestrator(new MySqlSyncProvider(project.ClientProvider.ConnectionString));
                break;

            case ProviderType.SqlServer:
            default:
                localOrchestrator = new LocalOrchestrator(new SqlSyncProvider(project.ClientProvider.ConnectionString));
                break;
            }

            SyncAgent agent = null;

            if (project.ServerProvider.ProviderType != ProviderType.Web)
            {
                agent = new SyncAgent(localOrchestrator, remoteOrchestrator);

                var syncSchema = new SyncSchema();
                //var syncConfiguration = agent.LocalOrchestrator.Configuration;

                foreach (var t in project.Tables.OrderBy(tbl => tbl.Order))
                {
                    // Potentially user can pass something like [SalesLT].[Product]
                    // or SalesLT.Product or Product. ParserName will handle it
                    var parser = ParserName.Parse(t.Name);

                    var tableName = parser.ObjectName;
                    var schema    = string.IsNullOrEmpty(t.Schema) ? parser.SchemaName : t.Schema;

                    var dmTable = new DmTable(tableName);

                    if (!String.IsNullOrEmpty(schema))
                    {
                        dmTable.Schema = schema;
                    }

                    dmTable.SyncDirection = t.Direction;

                    agent.SetSchema(agentSchema => agentSchema.Add(dmTable));
                }

                agent.SetOptions(o => o.BatchDirectory = string.IsNullOrEmpty(project.Configuration.BatchDirectory) ? null : project.Configuration.BatchDirectory);
                agent.SetOptions(o => o.BatchSize      = (int)Math.Min(Int32.MaxValue, project.Configuration.DownloadBatchSizeInKB));
                //agent.SetOptions(o => o.SerializationFormat = project.Configuration.SerializationFormat);

                //agent.Options.UseBulkOperations = project.Configuration.UseBulkOperation;

                agent.SetSchema(agentSchema =>
                {
                    agentSchema.ConflictResolutionPolicy = project.Configuration.ConflictResolutionPolicy;
                });
            }
            else
            {
                agent = new SyncAgent(localOrchestrator, remoteOrchestrator);
            }

            agent.LocalOrchestrator.OnTableChangesSelected(tcs => Console.WriteLine($"Changes selected for table {tcs.TableChangesSelected.TableName}: {tcs.TableChangesSelected.TotalChanges}"));
            agent.LocalOrchestrator.OnTableChangesApplied(tca => Console.WriteLine($"Changes applied for table {tca.TableChangesApplied.Table.TableName}: [{tca.TableChangesApplied.State}] {tca.TableChangesApplied.Applied}"));

            // synchronous call
            var syncContext = agent.SynchronizeAsync().GetAwaiter().GetResult();

            var tsEnded   = TimeSpan.FromTicks(syncContext.CompleteTime.Ticks);
            var tsStarted = TimeSpan.FromTicks(syncContext.StartTime.Ticks);

            var durationTs  = tsEnded.Subtract(tsStarted);
            var durationstr = $"{durationTs.Hours}:{durationTs.Minutes}:{durationTs.Seconds}.{durationTs.Milliseconds}";

            Console.ForegroundColor = ConsoleColor.Green;
            var s = $"Synchronization done. " + Environment.NewLine +
                    $"\tTotal changes downloaded: {syncContext.TotalChangesDownloaded} " + Environment.NewLine +
                    $"\tTotal changes uploaded: {syncContext.TotalChangesUploaded}" + Environment.NewLine +
                    $"\tTotal duration :{durationstr} ";

            Console.WriteLine(s);
            Console.ResetColor();


            return(syncContext);
        }
        private SyncTable CreateCallBackTable()
        {
            try
            {

             //   if (!IsTableExits("CallBack"))
                {
            //        SqlCeClientSyncProvider sync = new SqlCeClientSyncProvider(ClientConnectionString);

                    tblCallBackTable = new SyncTable("CallBack");
                    tblCallBackTable.SyncDirection = SyncDirection.UploadOnly;

                    SyncSchema syncSchemaCallBack = new SyncSchema();
                    syncSchemaCallBack.Tables.Add("CallBack");

                    syncSchemaCallBack.Tables["CallBack"].Columns.Add("ID");
                    syncSchemaCallBack.Tables["CallBack"].Columns["ID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCallBack.Tables["CallBack"].PrimaryKey = new string[] { "ID" };
                    syncSchemaCallBack.Tables["CallBack"].Columns["ID"].AutoIncrement = true;
                    syncSchemaCallBack.Tables["CallBack"].Columns["ID"].AutoIncrementSeed = 1;
                    syncSchemaCallBack.Tables["CallBack"].Columns["ID"].AutoIncrementStep = 1;
                    syncSchemaCallBack.Tables["CallBack"].Columns["ID"].AllowNull = false;

                    syncSchemaCallBack.Tables["CallBack"].Columns.Add("CallID");
                    syncSchemaCallBack.Tables["CallBack"].Columns["CallID"].ProviderDataType = SqlDbType.BigInt.ToString();
                    syncSchemaCallBack.Tables["CallBack"].Columns["CallID"].AllowNull = false;


                    syncSchemaCallBack.Tables["CallBack"].Columns.Add("CallBackDate");
                    syncSchemaCallBack.Tables["CallBack"].Columns["CallBackDate"].ProviderDataType = SqlDbType.DateTime.ToString();
                    syncSchemaCallBack.Tables["CallBack"].Columns["CallBackDate"].AllowNull = false;

                    syncSchemaCallBack.Tables["CallBack"].Columns.Add("Comment");
                    syncSchemaCallBack.Tables["CallBack"].Columns["Comment"].ProviderDataType = SqlDbType.NVarChar.ToString();
                    syncSchemaCallBack.Tables["CallBack"].Columns["Comment"].MaxLength = 50;
                    syncSchemaCallBack.Tables["CallBack"].Columns["Comment"].AllowNull = true;

                    syncSchemaCallBack.Tables["CallBack"].Columns.Add("IsPublic");
                    syncSchemaCallBack.Tables["CallBack"].Columns["IsPublic"].ProviderDataType = SqlDbType.Bit.ToString();
                    syncSchemaCallBack.Tables["CallBack"].Columns["IsPublic"].AllowNull = false;

                    syncSchemaCallBack.Tables["CallBack"].Columns.Add("IsDeleted");
                    syncSchemaCallBack.Tables["CallBack"].Columns["IsDeleted"].ProviderDataType = SqlDbType.Bit.ToString();
                    syncSchemaCallBack.Tables["CallBack"].Columns["IsDeleted"].AllowNull = false;

                   // sync.CreateSchema(tblCallBackTable, syncSchemaCallBack);
                    clientSyncProvider.CreateSchema(tblCallBackTable, syncSchemaCallBack);
                    return tblCallBackTable;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error In Creating CallBack Table");
                VMuktiAPI.VMuktiHelper.ExceptionHandler(ex, "CreateCallBackTable()", "AutoDialer_ClsUserDataService.xaml.cs");
                return null;
            }
        }
        private SyncTable CreateDispositionTable()
        {
            try
            {
                tblDispositionTable = new SyncTable("Disposition");
                tblDispositionTable.SyncDirection = SyncDirection.UploadOnly;

                SyncSchema syncSchemaDisposition = new SyncSchema();
                syncSchemaDisposition.Tables.Add("Disposition");

                syncSchemaDisposition.Tables["Disposition"].Columns.Add("ID");
                syncSchemaDisposition.Tables["Disposition"].Columns["ID"].ProviderDataType = SqlDbType.BigInt.ToString();
                syncSchemaDisposition.Tables["Disposition"].PrimaryKey = new string[] { "ID" };
                //syncSchemaDisposition.Tables["Disposition"].Columns["ID"].AutoIncrement = true;
                //syncSchemaDisposition.Tables["Disposition"].Columns["ID"].AutoIncrementSeed = 1;
                //syncSchemaDisposition.Tables["Disposition"].Columns["ID"].AutoIncrementStep = 1;
                syncSchemaDisposition.Tables["Disposition"].Columns["ID"].AllowNull = false;

                syncSchemaDisposition.Tables["Disposition"].Columns.Add("DespositionName");
                syncSchemaDisposition.Tables["Disposition"].Columns["DespositionName"].ProviderDataType = SqlDbType.NVarChar.ToString();
                syncSchemaDisposition.Tables["Disposition"].Columns["DespositionName"].MaxLength = 50;
                syncSchemaDisposition.Tables["Disposition"].Columns["DespositionName"].AllowNull = false;

                syncSchemaDisposition.Tables["Disposition"].Columns.Add("Description");
                syncSchemaDisposition.Tables["Disposition"].Columns["Description"].ProviderDataType = SqlDbType.NVarChar.ToString();
                syncSchemaDisposition.Tables["Disposition"].Columns["Description"].MaxLength = 250;
                syncSchemaDisposition.Tables["Disposition"].Columns["Description"].AllowNull = true;

                syncSchemaDisposition.Tables["Disposition"].Columns.Add("IsActive");
                syncSchemaDisposition.Tables["Disposition"].Columns["IsActive"].ProviderDataType = SqlDbType.Bit.ToString();
                syncSchemaDisposition.Tables["Disposition"].Columns["IsActive"].AllowNull = true;

                syncSchemaDisposition.Tables["Disposition"].Columns.Add("IsDeleted");
                syncSchemaDisposition.Tables["Disposition"].Columns["IsDeleted"].ProviderDataType = SqlDbType.Bit.ToString();
                syncSchemaDisposition.Tables["Disposition"].Columns["IsDeleted"].AllowNull = true;

                syncSchemaDisposition.Tables["Disposition"].Columns.Add("CreatedDate");
                syncSchemaDisposition.Tables["Disposition"].Columns["CreatedDate"].ProviderDataType = SqlDbType.DateTime.ToString();
                syncSchemaDisposition.Tables["Disposition"].Columns["CreatedDate"].AllowNull = true;

                syncSchemaDisposition.Tables["Disposition"].Columns.Add("CreatedBy");
                syncSchemaDisposition.Tables["Disposition"].Columns["CreatedBy"].ProviderDataType = SqlDbType.BigInt.ToString();
                syncSchemaDisposition.Tables["Disposition"].Columns["CreatedBy"].AllowNull = true;

                syncSchemaDisposition.Tables["Disposition"].Columns.Add("ModifiedDate");
                syncSchemaDisposition.Tables["Disposition"].Columns["ModifiedDate"].ProviderDataType = SqlDbType.DateTime.ToString();
                syncSchemaDisposition.Tables["Disposition"].Columns["ModifiedDate"].AllowNull = true;

                syncSchemaDisposition.Tables["Disposition"].Columns.Add("ModifiedBy");
                syncSchemaDisposition.Tables["Disposition"].Columns["ModifiedBy"].ProviderDataType = SqlDbType.BigInt.ToString();
                syncSchemaDisposition.Tables["Disposition"].Columns["ModifiedBy"].AllowNull = true;

                clientSyncProvider.CreateSchema(tblDispositionTable,syncSchemaDisposition);
                return tblDispositionTable;
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error In Creating Disposition Table");
                VMuktiAPI.VMuktiHelper.ExceptionHandler(ex, "CreateDispositionTable()", "AutoDialer_ClsUserDataService.xaml.cs");

                return null;
            }
        }
        /// <summary>
        /// this function will use for keeping track of supernode's node list. e.g. how many number of nodes(HTTP or P2P) is connected with perticular supernode.
        /// </summary>
        void fncCreateUserSuperNode_NodeInfoTable()
        {
            try
            {
               if (false == File.Exists(strLocalDBPath))
                {
                    SqlCeEngine clientEngine = new SqlCeEngine(ClientConnectionString);
                    clientEngine.CreateDatabase();
                }
                if (!IsTableExits("SuperNode_Node_Info"))
                {
                    SqlCeClientSyncProvider sync = new SqlCeClientSyncProvider(ClientConnectionString);

                    SyncTable tblSuperNode_Node_Info = new SyncTable("SuperNode_Node_Info");

                    SyncSchema syncSchemaLead = new SyncSchema();
                    syncSchemaLead.Tables.Add("SuperNode_Node_Info");
                    syncSchemaLead.Tables["SuperNode_Node_Info"].Columns.Add("ID");
                    syncSchemaLead.Tables["SuperNode_Node_Info"].Columns["ID"].ProviderDataType = SqlDbType.Int.ToString();
                    syncSchemaLead.Tables["SuperNode_Node_Info"].Columns["ID"].AutoIncrement = true;
                    syncSchemaLead.Tables["SuperNode_Node_Info"].Columns["ID"].AutoIncrementSeed = 1;
                    syncSchemaLead.Tables["SuperNode_Node_Info"].Columns["ID"].AutoIncrementStep = 1;
                    syncSchemaLead.Tables["SuperNode_Node_Info"].PrimaryKey = new string[] { "ID" };

                    syncSchemaLead.Tables["SuperNode_Node_Info"].Columns.Add("SuperNode_Id");
                    syncSchemaLead.Tables["SuperNode_Node_Info"].Columns["SuperNode_Id"].ProviderDataType = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["SuperNode_Node_Info"].Columns["SuperNode_Id"].MaxLength = 30;
                    syncSchemaLead.Tables["SuperNode_Node_Info"].Columns["SuperNode_Id"].AllowNull = false;


                    syncSchemaLead.Tables["SuperNode_Node_Info"].Columns.Add("Node_Name");
                    syncSchemaLead.Tables["SuperNode_Node_Info"].Columns["Node_Name"].ProviderDataType = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["SuperNode_Node_Info"].Columns["Node_Name"].MaxLength = 50;
                    syncSchemaLead.Tables["SuperNode_Node_Info"].Columns["Node_Name"].AllowNull = false;


                    sync.CreateSchema(tblSuperNode_Node_Info, syncSchemaLead);
                }
            }
            catch (Exception ex)
            {
                VMuktiHelper.ExceptionHandler(ex, "fncCreateUserSuperNode_NodeInfoTable()", "Domains\\BootstrapServiceDomain.cs");
            }
        }
        public void fncActiveCallTable()
        {
            try
            {
                if (false == File.Exists(strLocalDBPath))
                {
                    SqlCeEngine clientEngine = new SqlCeEngine(ClientConnectionString);
                    clientEngine.CreateDatabase();
                }
                if (!IsTableExits("Active_call"))
                {
                    SqlCeClientSyncProvider sync = new SqlCeClientSyncProvider(ClientConnectionString);

                    SyncTable tblActive_call = new SyncTable("Active_call");

                    SyncSchema syncSchemaLead = new SyncSchema();
                    syncSchemaLead.Tables.Add("Active_call");
                    syncSchemaLead.Tables["Active_call"].Columns.Add("uName");
                    syncSchemaLead.Tables["Active_call"].Columns["uName"].ProviderDataType = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["Active_call"].Columns["uName"].AutoIncrement = false;
                    syncSchemaLead.Tables["Active_call"].Columns["uName"].AutoIncrementSeed = 0;
                    syncSchemaLead.Tables["Active_call"].Columns["uName"].AutoIncrementStep = 1;
                    syncSchemaLead.Tables["Active_call"].Columns["uName"].MaxLength = 30;
                    syncSchemaLead.Tables["Active_call"].PrimaryKey = new string[] { "uName" };

                    syncSchemaLead.Tables["Active_call"].Columns.Add("Campaign_Id");
                    syncSchemaLead.Tables["Active_call"].Columns["Campaign_Id"].ProviderDataType = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["Active_call"].Columns["Campaign_Id"].MaxLength = 30;
                    syncSchemaLead.Tables["Active_call"].Columns["Campaign_Id"].AutoIncrement = false;
                    syncSchemaLead.Tables["Active_call"].Columns["Campaign_Id"].AutoIncrementSeed = 0;
                    syncSchemaLead.Tables["Active_call"].Columns["Campaign_Id"].AutoIncrementStep = 1;

                    syncSchemaLead.Tables["Active_call"].Columns.Add("Status");
                    syncSchemaLead.Tables["Active_call"].Columns["Status"].ProviderDataType = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["Active_call"].Columns["Status"].MaxLength = 30;
                    syncSchemaLead.Tables["Active_call"].Columns["Status"].AutoIncrement = false;
                    syncSchemaLead.Tables["Active_call"].Columns["Status"].AutoIncrementSeed = 0;
                    syncSchemaLead.Tables["Active_call"].Columns["Status"].AutoIncrementStep = 1;

                    syncSchemaLead.Tables["Active_call"].Columns.Add("Group_Name");
                    syncSchemaLead.Tables["Active_call"].Columns["Group_Name"].ProviderDataType = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["Active_call"].Columns["Group_Name"].MaxLength = 30;
                    syncSchemaLead.Tables["Active_call"].Columns["Group_Name"].AutoIncrement = false;
                    syncSchemaLead.Tables["Active_call"].Columns["Group_Name"].AutoIncrementSeed = 0;
                    syncSchemaLead.Tables["Active_call"].Columns["Group_Name"].AutoIncrementStep = 1;

                    syncSchemaLead.Tables["Active_call"].Columns.Add("Phone_No");
                    syncSchemaLead.Tables["Active_call"].Columns["Phone_No"].ProviderDataType = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["Active_call"].Columns["Phone_No"].MaxLength = 30;
                    syncSchemaLead.Tables["Active_call"].Columns["Phone_No"].AutoIncrement = false;
                    syncSchemaLead.Tables["Active_call"].Columns["Phone_No"].AutoIncrementSeed = 0;
                    syncSchemaLead.Tables["Active_call"].Columns["Phone_No"].AutoIncrementStep = 1;

                    syncSchemaLead.Tables["Active_call"].Columns.Add("callDuration");
                    syncSchemaLead.Tables["Active_call"].Columns["callDuration"].ProviderDataType = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["Active_call"].Columns["callDuration"].MaxLength = 30;
                    syncSchemaLead.Tables["Active_call"].Columns["callDuration"].AutoIncrement = false;
                    syncSchemaLead.Tables["Active_call"].Columns["callDuration"].AutoIncrementSeed = 0;
                    syncSchemaLead.Tables["Active_call"].Columns["callDuration"].AutoIncrementStep = 1;
                  
                    sync.CreateSchema(tblActive_call, syncSchemaLead);
                }
            }
            catch (Exception ex)
            {
                VMuktiAPI.VMuktiHelper.ExceptionHandler(ex, "fncActivecallTable()", "ctlrptActiveCall.xaml.cs");
            }
        }
Exemple #17
0
        public void fncActiveCallTable()
        {
            try
            {
                if (false == File.Exists(strLocalDBPath))
                {
                    SqlCeEngine clientEngine = new SqlCeEngine(ClientConnectionString);
                    clientEngine.CreateDatabase();
                }
                if (!IsTableExits("Active_call"))
                {
                    SqlCeClientSyncProvider sync = new SqlCeClientSyncProvider(ClientConnectionString);

                    SyncTable tblActive_call = new SyncTable("Active_call");

                    SyncSchema syncSchemaLead = new SyncSchema();
                    syncSchemaLead.Tables.Add("Active_call");
                    syncSchemaLead.Tables["Active_call"].Columns.Add("uName");
                    syncSchemaLead.Tables["Active_call"].Columns["uName"].ProviderDataType  = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["Active_call"].Columns["uName"].AutoIncrement     = false;
                    syncSchemaLead.Tables["Active_call"].Columns["uName"].AutoIncrementSeed = 0;
                    syncSchemaLead.Tables["Active_call"].Columns["uName"].AutoIncrementStep = 1;
                    syncSchemaLead.Tables["Active_call"].Columns["uName"].MaxLength         = 30;
                    syncSchemaLead.Tables["Active_call"].PrimaryKey = new string[] { "uName" };

                    syncSchemaLead.Tables["Active_call"].Columns.Add("Campaign_Id");
                    syncSchemaLead.Tables["Active_call"].Columns["Campaign_Id"].ProviderDataType  = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["Active_call"].Columns["Campaign_Id"].MaxLength         = 30;
                    syncSchemaLead.Tables["Active_call"].Columns["Campaign_Id"].AutoIncrement     = false;
                    syncSchemaLead.Tables["Active_call"].Columns["Campaign_Id"].AutoIncrementSeed = 0;
                    syncSchemaLead.Tables["Active_call"].Columns["Campaign_Id"].AutoIncrementStep = 1;

                    syncSchemaLead.Tables["Active_call"].Columns.Add("Status");
                    syncSchemaLead.Tables["Active_call"].Columns["Status"].ProviderDataType  = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["Active_call"].Columns["Status"].MaxLength         = 30;
                    syncSchemaLead.Tables["Active_call"].Columns["Status"].AutoIncrement     = false;
                    syncSchemaLead.Tables["Active_call"].Columns["Status"].AutoIncrementSeed = 0;
                    syncSchemaLead.Tables["Active_call"].Columns["Status"].AutoIncrementStep = 1;

                    syncSchemaLead.Tables["Active_call"].Columns.Add("Group_Name");
                    syncSchemaLead.Tables["Active_call"].Columns["Group_Name"].ProviderDataType  = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["Active_call"].Columns["Group_Name"].MaxLength         = 30;
                    syncSchemaLead.Tables["Active_call"].Columns["Group_Name"].AutoIncrement     = false;
                    syncSchemaLead.Tables["Active_call"].Columns["Group_Name"].AutoIncrementSeed = 0;
                    syncSchemaLead.Tables["Active_call"].Columns["Group_Name"].AutoIncrementStep = 1;

                    syncSchemaLead.Tables["Active_call"].Columns.Add("Phone_No");
                    syncSchemaLead.Tables["Active_call"].Columns["Phone_No"].ProviderDataType  = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["Active_call"].Columns["Phone_No"].MaxLength         = 30;
                    syncSchemaLead.Tables["Active_call"].Columns["Phone_No"].AutoIncrement     = false;
                    syncSchemaLead.Tables["Active_call"].Columns["Phone_No"].AutoIncrementSeed = 0;
                    syncSchemaLead.Tables["Active_call"].Columns["Phone_No"].AutoIncrementStep = 1;

                    syncSchemaLead.Tables["Active_call"].Columns.Add("callDuration");
                    syncSchemaLead.Tables["Active_call"].Columns["callDuration"].ProviderDataType  = SqlDbType.NVarChar.ToString();
                    syncSchemaLead.Tables["Active_call"].Columns["callDuration"].MaxLength         = 30;
                    syncSchemaLead.Tables["Active_call"].Columns["callDuration"].AutoIncrement     = false;
                    syncSchemaLead.Tables["Active_call"].Columns["callDuration"].AutoIncrementSeed = 0;
                    syncSchemaLead.Tables["Active_call"].Columns["callDuration"].AutoIncrementStep = 1;

                    sync.CreateSchema(tblActive_call, syncSchemaLead);
                }
            }
            catch (Exception ex)
            {
                VMuktiAPI.VMuktiHelper.ExceptionHandler(ex, "fncActivecallTable()", "ctlrptActiveCall.xaml.cs");
            }
        }