private void PostTransaction(string existencePreconditions, string nonexistencePreconditions, string patternsToDelete, string triplesToAdd, string defaultGraphUri)
        {
            var jobInfo = _client.ExecuteTransaction(_storeName,
                                                     new UpdateTransactionData
            {
                ExistencePreconditions    = existencePreconditions,
                NonexistencePreconditions = nonexistencePreconditions,
                DeletePatterns            = patternsToDelete,
                InsertData      = triplesToAdd,
                DefaultGraphUri = defaultGraphUri
            });

            while (!(jobInfo.JobCompletedOk || jobInfo.JobCompletedWithErrors))
            {
#if PORTABLE
                // Very rudimentary synchronous wait
                var ev = new ManualResetEvent(false);
                ev.WaitOne(200);
#else
                Thread.Sleep(20);
#endif
                jobInfo = _client.GetJobInfo(_storeName, jobInfo.JobId);
            }

            if (jobInfo.JobCompletedWithErrors)
            {
                // if (jobInfo.ExceptionInfo.Type == typeof(Server.PreconditionFailedException).FullName)
                if (jobInfo.ExceptionInfo != null && jobInfo.ExceptionInfo.Type == "BrightstarDB.Server.PreconditionFailedException")
                {
                    throw TransactionPreconditionsFailedException.FromExceptionDetail(jobInfo.ExceptionInfo);
                }
                throw new BrightstarClientException("Error processing update transaction. " + jobInfo.StatusMessage);
            }
        }
        /// <summary>
        /// Commits all changes. Waits for the operation to complete.
        /// </summary>
        protected override void DoSaveChanges()
        {
            if (_optimisticLockingEnabled)
            {
                // get subject entity and see if there is a version triple
                var subjects = AddTriples.Subjects
                               .Union(DeletePatterns.Subjects)
                               .Except(new[] { Constants.WildcardUri }).ToList();
                foreach (var subject in subjects)
                {
                    var entity = LookupDataObject(subject);
                    if (entity == null)
                    {
                        throw new BrightstarClientException("No Entity Found for Subject " + subject);
                    }

                    var version = entity.GetPropertyValue(Constants.VersionPredicateUri);
                    if (version == null)
                    {
                        // no existing version information so assume this is the first time using it with 1
                        entity.SetProperty(Constants.VersionPredicateUri, 1);
                    }
                    else
                    {
                        var intVersion = Convert.ToInt32(version);
                        // inc version
                        intVersion++;
                        entity.SetProperty(Constants.VersionPredicateUri, intVersion);
                        Preconditions.Add(new Triple
                        {
                            Graph     = VersionGraphUri,
                            DataType  = RdfDatatypes.Integer,
                            IsLiteral = true,
                            LangCode  = null,
                            Object    = version.ToString(),
                            Predicate = Constants.VersionPredicateUri,
                            Subject   = subject
                        });
                    }
                }
            }

            var deleteData = new StringWriter();
            var dw         = new BrightstarTripleSinkAdapter(new NQuadsWriter(deleteData));

            foreach (var triple in DeletePatterns.Items)
            {
                dw.Triple(triple);
            }
            deleteData.Close();

            var addData = new StringWriter();
            var aw      = new BrightstarTripleSinkAdapter(new NQuadsWriter(addData));

            foreach (var triple in AddTriples.Items)
            {
                aw.Triple(triple);
            }
            addData.Close();

            var preconditionsData = new StringWriter();
            var pw = new BrightstarTripleSinkAdapter(new NQuadsWriter(preconditionsData));

            foreach (var triple in Preconditions.Items)
            {
                pw.Triple(triple);
            }
            preconditionsData.Close();

            var nePreconditionsData = new StringWriter();
            var nw = new BrightstarTripleSinkAdapter(new NQuadsWriter(nePreconditionsData));

            foreach (var triple in NonExistencePreconditions.Items)
            {
                nw.Triple(triple);
            }
            nePreconditionsData.Close();

            var jobId = _serverCore.ProcessTransaction(_storeName, preconditionsData.ToString(),
                                                       nePreconditionsData.ToString(),
                                                       deleteData.ToString(), addData.ToString(), UpdateGraphUri);
            var status = _serverCore.GetJobStatus(_storeName, jobId.ToString());

            status.WaitEvent.WaitOne();
//            while (!(status.JobStatus == JobStatus.CompletedOk || status.JobStatus == JobStatus.TransactionError))
//            {
//                // wait for completion.
//#if !PORTABLE
//                Thread.Sleep(5);
//#endif
//                status = _serverCore.GetJobStatus(_storeName, jobId.ToString());
//            }

            if (status.JobStatus == JobStatus.TransactionError)
            {
                if (status.ExceptionDetail.Type.Equals(typeof(PreconditionFailedException).FullName))
                {
                    Preconditions.Clear();
                    throw TransactionPreconditionsFailedException.FromExceptionDetail(status.ExceptionDetail);
                }
                throw new BrightstarClientException(status.ExceptionDetail != null && !String.IsNullOrEmpty(status.ExceptionDetail.Message) ? status.ExceptionDetail.Message : "The transaction encountered an error");
            }
            if (status.JobStatus != JobStatus.CompletedOk)
            {
                throw new BrightstarClientException("Unexpected job status on completion: " + status.JobStatus + ". Last message was: " + status.Information);
            }

            // reset changes
            ResetTransactionData();
        }