Ejemplo n.º 1
0
        /// <summary>
        /// Enqueue a Web Service record into the record queue
        /// </summary>
        public static void EnqueueRequestRecord(string queueName, RequestRecord newRecord)
        {
            bool enableQueueOptimization = false;  // turn off the queue optimization (doesn't work with introduction of tags)

            List <RequestRecord>       requests = new List <RequestRecord>();
            DataContractJsonSerializer dc       = new DataContractJsonSerializer(requests.GetType());

            if (newRecord.SerializedBody == null)
            {
                newRecord.SerializeBody();
            }

            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                lock (fileLocks[queueName])
                {
                    // if the file opens, read the contents
                    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(QueueFilename(queueName), FileMode.OpenOrCreate, file))
                    {
                        try
                        {
                            // if the file opened, read the record queue
                            requests = dc.ReadObject(stream) as List <RequestRecord>;
                            if (requests == null)
                            {
                                requests = new List <RequestRecord>();
                            }
                        }
                        catch (Exception ex)
                        {
                            stream.Position = 0;
                            string s = new StreamReader(stream).ReadToEnd();
                            TraceHelper.AddMessage(String.Format("Exception in deserializing RequestRQueue: {0}; record: {1}", ex.Message, s));
                        }

                        if (enableQueueOptimization == true)
                        {
                            OptimizeQueue(newRecord, requests);
                        }
                        else
                        {
                            // this is a new record so add the new record at the end
                            requests.Add(newRecord);
                        }

                        // reset the stream and write the new record queue back to the file
                        stream.SetLength(0);
                        stream.Position = 0;
                        dc.WriteObject(stream, requests);
                        stream.Flush();
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Helper method to update an existing queue record with new information
        /// </summary>
        /// <param name="requests"></param>
        /// <param name="existingRecord"></param>
        /// <param name="newRecord"></param>
        /// <returns>Whether an update was performed</returns>
        private static bool UpdateExistingRecord(List <RequestRecord> requests, RequestRecord existingRecord, RequestRecord newRecord)
        {
            switch (existingRecord.ReqType)
            {
            case RequestRecord.RequestType.Delete:
                // the existing record in the queue is a delete - this situation can't happen unless there is local corruption (but not necessarily queue corruption)
                // there can be no further action
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    throw new Exception("trying to do something to an already deleted entry");
                }
                return(false);

            case RequestRecord.RequestType.Insert:
                // the existing record in the queue is an insert
                switch (newRecord.ReqType)
                {
                case RequestRecord.RequestType.Delete:
                    // the entity was created and deleted while offline
                    // the existing record needs to be removed
                    requests.Remove(existingRecord);
                    return(true);

                case RequestRecord.RequestType.Insert:
                    // this doesn't make sense because it violates the Guid uniqueness principle
                    // since the record already exists, we're not going to insert a new one - take no action
                    if (System.Diagnostics.Debugger.IsAttached)
                    {
                        throw new Exception("insert after an insert");
                    }
                    return(false);

                case RequestRecord.RequestType.Update:
                    // an update on top of an insert
                    // replace the new value in the existing insert record with the "new" value in the update
                    switch (existingRecord.BodyTypeName)
                    {
                    case "Tag":
                        List <Tag> newRecordTagList = (List <Tag>)newRecord.Body;
                        existingRecord.Body = newRecordTagList[1];
                        break;

                    case "Item":
                        List <Item> newRecordFolder = (List <Item>)newRecord.Body;
                        existingRecord.Body = newRecordFolder[1];
                        break;

                    case "Folder":
                        List <Folder> newRecordFolderList = (List <Folder>)newRecord.Body;
                        existingRecord.Body = newRecordFolderList[1];
                        break;
                    }
                    // reserialize the body
                    existingRecord.SerializeBody();
                    return(true);
                }
                break;

            case RequestRecord.RequestType.Update:
                // the existing record in the queue is an update
                switch (newRecord.ReqType)
                {
                case RequestRecord.RequestType.Delete:
                    // the entity was updated and now deleted while offline
                    // the existing record needs to be amended to a delete record
                    existingRecord.Copy(newRecord);
                    return(true);

                case RequestRecord.RequestType.Insert:
                    // this doesn't make sense because it violates the Guid uniqueness principle
                    // since the record already exists, we're not going to insert a new one - take no action
                    if (System.Diagnostics.Debugger.IsAttached)
                    {
                        throw new Exception("insert after an update");
                    }
                    return(false);

                case RequestRecord.RequestType.Update:
                    // an update on top of an update
                    // replace the new value in the existing record with the "new" new value
                    switch (existingRecord.BodyTypeName)
                    {
                    case "Tag":
                        List <Tag> existingRecordTagList = (List <Tag>)existingRecord.Body;
                        List <Tag> newRecordTagList      = (List <Tag>)newRecord.Body;
                        existingRecordTagList[1] = newRecordTagList[1];
                        break;

                    case "Item":
                        List <Item> existingRecordFolder = (List <Item>)existingRecord.Body;
                        List <Item> newRecordFolder      = (List <Item>)newRecord.Body;
                        existingRecordFolder[1] = newRecordFolder[1];
                        break;

                    case "Folder":
                        List <Folder> existingRecordFolderList = (List <Folder>)existingRecord.Body;
                        List <Folder> newRecordFolderList      = (List <Folder>)newRecord.Body;
                        existingRecordFolderList[1] = newRecordFolderList[1];
                        break;
                    }
                    // reserialize the body
                    existingRecord.SerializeBody();
                    return(true);
                }
                break;
            }

            // no case was triggered - this is an exceptional situation
            if (System.Diagnostics.Debugger.IsAttached)
            {
                throw new Exception("queue corrupted");
            }

            return(false);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Enqueue a Web Service record into the record queue
        /// </summary>
        public static void EnqueueRequestRecord(RequestRecord newRecord)
        {
            bool enableQueueOptimization = false;  // turn off the queue optimization (doesn't work with introduction of tags)

            List<RequestRecord> requests = new List<RequestRecord>();
            DataContractJsonSerializer dc = new DataContractJsonSerializer(requests.GetType());

            if (newRecord.SerializedBody == null)
                newRecord.SerializeBody();

            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                lock (fileLock)
                {
                    // if the file opens, read the contents
                    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"RequestRecords.xml", FileMode.OpenOrCreate, file))
                    {
                        try
                        {
                            // if the file opened, read the record queue
                            requests = dc.ReadObject(stream) as List<RequestRecord>;
                            if (requests == null)
                                requests = new List<RequestRecord>();
                        }
                        catch (Exception)
                        {
                            stream.Position = 0;
                            string s = new StreamReader(stream).ReadToEnd();
                        }

                        if (enableQueueOptimization == true)
                        {
                            OptimizeQueue(newRecord, requests);
                        }
                        else
                        {
                            // this is a new record so add the new record at the end
                            requests.Add(newRecord);
                        }

                        // reset the stream and write the new record queue back to the file
                        stream.SetLength(0);
                        stream.Position = 0;
                        dc.WriteObject(stream, requests);
                        stream.Flush();
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Helper method to update an existing queue record with new information
        /// </summary>
        /// <param name="requests"></param>
        /// <param name="existingRecord"></param>
        /// <param name="newRecord"></param>
        /// <returns>Whether an update was performed</returns>
        private static bool UpdateExistingRecord(List<RequestRecord> requests, RequestRecord existingRecord, RequestRecord newRecord)
        {
            switch (existingRecord.ReqType)
            {
                case RequestRecord.RequestType.Delete:
                    // the existing record in the queue is a delete - this situation can't happen unless there is local corruption (but not necessarily queue corruption)
                    // there can be no further action
                    if (System.Diagnostics.Debugger.IsAttached)
                        throw new Exception("trying to do something to an already deleted entry");
                    return false;
                case RequestRecord.RequestType.Insert:
                    // the existing record in the queue is an insert
                    switch (newRecord.ReqType)
                    {
                        case RequestRecord.RequestType.Delete:
                            // the entity was created and deleted while offline
                            // the existing record needs to be removed
                            requests.Remove(existingRecord);
                            return true;
                        case RequestRecord.RequestType.Insert:
                            // this doesn't make sense because it violates the Guid uniqueness principle
                            // since the record already exists, we're not going to insert a new one - take no action
                            if (System.Diagnostics.Debugger.IsAttached)
                                throw new Exception("insert after an insert");
                            return false;
                        case RequestRecord.RequestType.Update:
                            // an update on top of an insert
                            // replace the new value in the existing insert record with the "new" value in the update
                            switch (existingRecord.BodyTypeName)
                            {
                                case "Tag":
                                    List<Tag> newRecordTagList = (List<Tag>)newRecord.Body;
                                    existingRecord.Body = newRecordTagList[1];
                                    break;
                                case "Task":
                                    List<Task> newRecordTaskList = (List<Task>)newRecord.Body;
                                    existingRecord.Body = newRecordTaskList[1];
                                    break;
                                case "TaskList":
                                    List<TaskList> newRecordTaskListList = (List<TaskList>)newRecord.Body;
                                    existingRecord.Body = newRecordTaskListList[1];
                                    break;
                            }
                            // reserialize the body
                            existingRecord.SerializeBody();
                            return true;
                    }
                    break;
                case RequestRecord.RequestType.Update:
                    // the existing record in the queue is an update
                    switch (newRecord.ReqType)
                    {
                        case RequestRecord.RequestType.Delete:
                            // the entity was updated and now deleted while offline
                            // the existing record needs to be amended to a delete record
                            existingRecord.Copy(newRecord);
                            return true;
                        case RequestRecord.RequestType.Insert:
                            // this doesn't make sense because it violates the Guid uniqueness principle
                            // since the record already exists, we're not going to insert a new one - take no action
                            if (System.Diagnostics.Debugger.IsAttached)
                                throw new Exception("insert after an update");
                            return false;
                        case RequestRecord.RequestType.Update:
                            // an update on top of an update
                            // replace the new value in the existing record with the "new" new value
                            switch (existingRecord.BodyTypeName)
                            {
                                case "Tag":
                                    List<Tag> existingRecordTagList = (List<Tag>)existingRecord.Body;
                                    List<Tag> newRecordTagList = (List<Tag>)newRecord.Body;
                                    existingRecordTagList[1] = newRecordTagList[1];
                                    break;
                                case "Task":
                                    List<Task> existingRecordTaskList = (List<Task>)existingRecord.Body;
                                    List<Task> newRecordTaskList = (List<Task>)newRecord.Body;
                                    existingRecordTaskList[1] = newRecordTaskList[1];
                                    break;
                                case "TaskList":
                                    List<TaskList> existingRecordTaskListList = (List<TaskList>)existingRecord.Body;
                                    List<TaskList> newRecordTaskListList = (List<TaskList>)newRecord.Body;
                                    existingRecordTaskListList[1] = newRecordTaskListList[1];
                                    break;
                            }
                            // reserialize the body
                            existingRecord.SerializeBody();
                            return true;
                    }
                    break;
            }

            // no case was triggered - this is an exceptional situation
            if (System.Diagnostics.Debugger.IsAttached)
                throw new Exception("queue corrupted");

            return false;
        }