예제 #1
0
파일: NocsService.cs 프로젝트: ipriel/nocs2
        /// <summary>
        /// Deletes an entry with a given id.
        /// </summary>
        /// <param name="entryId">ResourceId of the entry to be deleted.</param>
        /// <param name="entryType">Type of the entry (Document or Folder).</param>
        public static void DeleteEntry(string entryId, Document.DocumentType entryType)
        {
            Working = true;

            // we will only rename documents and folders
            if (entryType != Document.DocumentType.Document && entryType != Document.DocumentType.Folder)
            {
                throw new ArgumentException(string.Format("Invalid entryType ({0})", entryType));
            }

            var doc           = entryType == Document.DocumentType.Document ? AllDocuments[entryId] : AllFolders[entryId];
            var entryToDelete = doc.DocumentEntry;

            try
            {
                _documentService.ProtocolMajor = 3;
                entryToDelete.Delete();
            }
            catch (GDataRequestException exRequest)
            {
                var response = exRequest.Response as HttpWebResponse;
                if (response != null && response.StatusCode == HttpStatusCode.PreconditionFailed &&
                    exRequest.ResponseString.ToLowerInvariant().Contains("etagsmismatch"))
                {
                    // ETags don't match -> this document has been updated outside this instance of Nocs
                    // or it was just saved -> let's update it and try renaming again
                    Debug.WriteLine(string.Format("ETags don't match, couldn't find {0} - updating it and trying delete again..", doc.ETag));
                    doc = GetUpdatedDocument(doc);
                    DeleteEntry(doc.ResourceId, doc.Type);
                }
                else
                {
                    var error = GetErrorMessage(exRequest);
                    if (exRequest.ResponseString == null && error.ToLowerInvariant().Contains("execution of request failed"))
                    {
                        throw new GDataRequestException("Couldn't delete entry, connection timed out");
                    }

                    var knownIssues = ConsecutiveKnownIssuesOccurred(DeleteEntryLock, "DeleteEntry", doc, error, ref _deleteEntryAttempts, 1);
                    if (knownIssues == KnownIssuesResult.Retry)
                    {
                        doc = GetUpdatedDocument(doc);
                        DeleteEntry(doc.ResourceId, doc.Type);
                        doc.Summary          = null;
                        _deleteEntryAttempts = 0;
                        return;
                    }
                    else if (knownIssues == KnownIssuesResult.LimitReached)
                    {
                        return;
                    }

                    Trace.WriteLine(string.Format("Couldn't delete {0}: {1} - {2}", entryType, doc.DocumentEntry.Title.Text, Tools.TrimErrorMessage(error)));
                    throw new GDataRequestException(string.Format("Couldn't delete {0}: {1} - {2}", entryType, doc.DocumentEntry.Title.Text, Tools.TrimErrorMessage(error)));
                }
            }
            catch (Exception ex)
            {
                var error = GetErrorMessage(ex);
                throw new Exception(string.Format("Couldn't delete entry: {0} - {1}", doc.DocumentEntry.Title.Text, error));
            }
            finally
            {
                Working = false;
            }

            // let's update the appropriate dictionary
            if (entryType == Document.DocumentType.Document)
            {
                AllDocuments.Remove(entryId);
            }
            else
            {
                AllFolders.Remove(entryId);
            }
        }