public void Add(string resourceKind, SyncDigestEntryInfo info)
        {
            ISyncDigestTableAdapter   syncDigestTableAdapter   = StoreEnvironment.Resolve <ISyncDigestTableAdapter>(_context);
            IResourceKindTableAdapter resourceKindTableAdapter = StoreEnvironment.Resolve <IResourceKindTableAdapter>(_context);

            using (IJetTransaction jetTransaction = _jetConnectionProvider.GetTransaction(false))
            {
                ResourceKindInfo resourceKindInfo = resourceKindTableAdapter.GetOrCreate(resourceKind, jetTransaction);

                try
                {
                    syncDigestTableAdapter.Insert(resourceKindInfo.Id, new SyncDigestEntryInfo[] { info }, jetTransaction);
                }
                catch (OleDbException exception)
                {
                    if (exception.Errors.Count == 1 && exception.Errors[0].SQLState == "3022")
                    {
                        throw new StoreException(string.Format("An error occured while adding a new sync digest entry. A sync digest entry already exists for the resource kind '{0}' and EndPoint '{1}.", resourceKind, info.EndPoint), exception);
                    }

                    throw;
                }

                jetTransaction.Commit();
            }
        }
        internal int CreateNexttick(string resourceKind)
        {
            int nexttick;
            int currenttick;

            ItickTableAdapter         tickTableAdapter         = StoreEnvironment.Resolve <ItickTableAdapter>(_context);
            IResourceKindTableAdapter resourceKindTableAdapter = StoreEnvironment.Resolve <IResourceKindTableAdapter>(_context);

            using (IJetTransaction jetTransaction = _jetConnectionProvider.GetTransaction(false))
            {
                ResourceKindInfo resourceKindInfo = resourceKindTableAdapter.GetOrCreate(resourceKind, jetTransaction);

                if (!tickTableAdapter.TryGet(resourceKindInfo.Id, out currenttick, jetTransaction))
                {
                    currenttick = STARTtick;

                    tickTableAdapter.Insert(resourceKindInfo.Id, currenttick, jetTransaction);
                    nexttick = currenttick;
                }
                else
                {
                    currenttick++;
                    tickTableAdapter.Update(resourceKindInfo.Id, currenttick, jetTransaction);

                    nexttick = currenttick;
                }

                jetTransaction.Commit();
            }

            return(nexttick);
        }
        public bool Get(string resourceKind, Type type, out object applicationBookmark)
        {
            bool result = false;

            applicationBookmark = null;
            string assemblyQualifiedName = type.AssemblyQualifiedName;

            IAppBookmarkTableAdapter  appBookmarkTableAdapter  = StoreEnvironment.Resolve <IAppBookmarkTableAdapter>(_context);
            IResourceKindTableAdapter resourceKindTableAdapter = StoreEnvironment.Resolve <IResourceKindTableAdapter>(_context);

            using (IJetTransaction jetTransaction = _jetConnectionProvider.GetTransaction(false))
            {
                ResourceKindInfo resourceKindInfo = resourceKindTableAdapter.GetOrCreate(resourceKind, jetTransaction);

                byte[] blob;
                string receivedAssemblyQualifiedName;
                if (appBookmarkTableAdapter.Get(resourceKindInfo.Id, out blob, out receivedAssemblyQualifiedName, jetTransaction))
                {
                    applicationBookmark = this.DeserializeBlob(blob, type);    // use given type not the one stored
                    result = true;
                }

                jetTransaction.Commit();
            }

            return(result);
        }
        public void Add(string resourceKind, object applicationBookmark)
        {
            string assemblyQualifiedName = applicationBookmark.GetType().AssemblyQualifiedName;
            object blob = this.SerializeApplicationBookmark(applicationBookmark);

            IAppBookmarkTableAdapter  appBookmarkTableAdapter  = StoreEnvironment.Resolve <IAppBookmarkTableAdapter>(_context);
            IResourceKindTableAdapter resourceKindTableAdapter = StoreEnvironment.Resolve <IResourceKindTableAdapter>(_context);

            using (IJetTransaction jetTransaction = _jetConnectionProvider.GetTransaction(false))
            {
                ResourceKindInfo resourceKindInfo = resourceKindTableAdapter.GetOrCreate(resourceKind, jetTransaction);

                try
                {
                    appBookmarkTableAdapter.Insert(resourceKindInfo.Id, blob, assemblyQualifiedName, jetTransaction);
                }
                catch (OleDbException exception)
                {
                    if (exception.Errors.Count == 1 && exception.Errors[0].SQLState == "3022")
                    {
                        throw new StoreException(string.Format("An error occured while adding a new application bookmark. An application bookmark already exists for the resource kind '{0}'.", resourceKind), exception);
                    }

                    throw;
                }

                jetTransaction.Commit();
            }
        }
        public ResourceKindInfo GetOrCreate(string resourceKind, IJetTransaction jetTransaction)
        {
            ResourceKindInfo resourceKindInfo = null;

            OleDbCommand oleDbCommand = jetTransaction.CreateOleCommand();

            oleDbCommand.CommandText = string.Format("SELECT [ID] FROM {0} WHERE [Name]=@Name;", _resourceKindTable.TableName);
            oleDbCommand.Parameters.AddWithValue("@Name", resourceKind);

            OleDbDataReader reader = oleDbCommand.ExecuteReader(CommandBehavior.SingleRow);

            if (!reader.HasRows)
            {
                resourceKindInfo = this.Add(resourceKind, jetTransaction);
            }
            else
            {
                reader.Read();

                int id = Convert.ToInt32(reader["ID"]);
                resourceKindInfo = new ResourceKindInfo(id, resourceKind);
            }


            return(resourceKindInfo);
        }
        /*
         * public void Update(string resourceKind, SyncDigestInfo info)
         * {
         *  ISyncDigestTableAdapter syncDigestTableAdapter = StoreEnvironment.Resolve<ISyncDigestTableAdapter>(_context);
         *  IResourceKindTableAdapter resourceKindTableAdapter = StoreEnvironment.Resolve<IResourceKindTableAdapter>(_context);
         *
         *  using (IJetTransaction jetTransaction = _jetConnectionProvider.GetTransaction(false))
         *  {
         *      ResourceKindInfo resourceKindInfo = resourceKindTableAdapter.GetOrCreate(resourceKind, jetTransaction);
         *
         *      try
         *      {
         *          syncDigestTableAdapter.Update(resourceKindInfo.Id, info.ToArray(), jetTransaction);
         *      }
         *      catch (StoreException exception)
         *      {
         *          throw new StoreException(string.Format("An error occured while updating a sync digest entry. No sync digest entry exists for the resource kind '{0}'.", resourceKind), exception);
         *      }
         *      jetTransaction.Commit();
         *  }
         * }
         */
        public bool Update(string resourceKind, SyncDigestEntryInfo info)
        {
            bool result = false;
            ISyncDigestTableAdapter   syncDigestTableAdapter   = StoreEnvironment.Resolve <ISyncDigestTableAdapter>(_context);
            IResourceKindTableAdapter resourceKindTableAdapter = StoreEnvironment.Resolve <IResourceKindTableAdapter>(_context);

            using (IJetTransaction jetTransaction = _jetConnectionProvider.GetTransaction(false))
            {
                ResourceKindInfo resourceKindInfo = resourceKindTableAdapter.GetOrCreate(resourceKind, jetTransaction);

                result = syncDigestTableAdapter.Update(resourceKindInfo.Id, info, jetTransaction);

                jetTransaction.Commit();
            }
            return(result);
        }
        public SyncDigestInfo Get(string resourceKind)
        {
            SyncDigestInfo            resultInfo               = null;
            ISyncDigestTableAdapter   syncDigestTableAdapter   = StoreEnvironment.Resolve <ISyncDigestTableAdapter>(_context);
            IResourceKindTableAdapter resourceKindTableAdapter = StoreEnvironment.Resolve <IResourceKindTableAdapter>(_context);

            using (IJetTransaction jetTransaction = _jetConnectionProvider.GetTransaction(false))
            {
                ResourceKindInfo resourceKindInfo = resourceKindTableAdapter.GetOrCreate(resourceKind, jetTransaction);

                resultInfo = syncDigestTableAdapter.Get(resourceKindInfo.Id, jetTransaction);

                jetTransaction.Commit();
            }

            return(resultInfo);
        }
        public void Update(string resourceKind, object applicationBookmark)
        {
            string assemblyQualifiedName = applicationBookmark.GetType().AssemblyQualifiedName;
            object blob = this.SerializeApplicationBookmark(applicationBookmark);

            IAppBookmarkTableAdapter  appBookmarkTableAdapter  = StoreEnvironment.Resolve <IAppBookmarkTableAdapter>(_context);
            IResourceKindTableAdapter resourceKindTableAdapter = StoreEnvironment.Resolve <IResourceKindTableAdapter>(_context);

            using (IJetTransaction jetTransaction = _jetConnectionProvider.GetTransaction(false))
            {
                ResourceKindInfo resourceKindInfo = resourceKindTableAdapter.GetOrCreate(resourceKind, jetTransaction);

                if (!appBookmarkTableAdapter.Update(resourceKindInfo.Id, blob, assemblyQualifiedName, jetTransaction))
                {
                    throw new StoreException(string.Format("No application bookmark exists for the resource kind '{0}' that can be updated.", resourceKind));
                }

                jetTransaction.Commit();
            }
        }
        public void Delete(string resourceKind)
        {
            IAppBookmarkTableAdapter  appBookmarkTableAdapter  = StoreEnvironment.Resolve <IAppBookmarkTableAdapter>(_context);
            IResourceKindTableAdapter resourceKindTableAdapter = StoreEnvironment.Resolve <IResourceKindTableAdapter>(_context);

            using (IJetTransaction jetTransaction = _jetConnectionProvider.GetTransaction(false))
            {
                ResourceKindInfo resourceKindInfo = resourceKindTableAdapter.GetOrCreate(resourceKind, jetTransaction);

                try
                {
                    appBookmarkTableAdapter.Delete(resourceKindInfo.Id, jetTransaction);
                }
                catch
                {
                    return;
                }

                jetTransaction.Commit();
            }
        }
        internal ISyncResultsTableAdapter CreateSyncResultsTableAdapter(string resourceKind, IResourceKindTableAdapter resourceKindTableAdapter)
        {
            ResourceKindInfo resourceKindInfo = null;

            using (IJetTransaction jetTransaction = _connProvider.GetTransaction(false))
            {
                resourceKindInfo = resourceKindTableAdapter.GetOrCreate(resourceKind, jetTransaction);
                jetTransaction.Commit();
            }

            ISyncResultsTable syncResultsTable = new SyncResultsTable(resourceKindInfo.Id, resourceKindTableAdapter.Table);

            using (IJetTransaction jetTransaction = _connProvider.GetTransaction(false))
            {
                if (!JetHelpers.TableExists(syncResultsTable.TableName, jetTransaction))
                {
                    syncResultsTable.CreateTable(jetTransaction);
                    jetTransaction.Commit();
                }
            }

            return(new SyncResultsTableAdapter(syncResultsTable, _context));
        }
        internal ICorrelatedResSyncTableAdapter CreateCorrelatedResSyncTableAdapter(string resourceKind, IEndpointTableAdapter endpointTableAdapter, IResourceKindTableAdapter resourceKindTableAdapter)
        {
            ResourceKindInfo resourceKindInfo = null;

            using (IJetTransaction jetTransaction = _connProvider.GetTransaction(false))
            {
                resourceKindInfo = resourceKindTableAdapter.GetOrCreate(resourceKind, jetTransaction);
                jetTransaction.Commit();
            }

            ICorrelatedResSyncTable correlatedResSyncTable = new CorrelatedResSyncTable(resourceKindInfo.Id, resourceKindTableAdapter.Table, endpointTableAdapter.Table);

            using (IJetTransaction jetTransaction = _connProvider.GetTransaction(false))
            {
                if (!JetHelpers.TableExists(correlatedResSyncTable.TableName, jetTransaction))
                {
                    correlatedResSyncTable.CreateTable(jetTransaction);
                    jetTransaction.Commit();
                }
            }

            return(new CorrelatedResSyncTableAdapter(correlatedResSyncTable, _context));
        }