예제 #1
0
        public override void LoadModelParameters(string parameters)
        {
            var url = new UrlParser(UrlUtil.Url);

            documentIdManager.Clear();

            if (url.GetQueryParam("mode") == "new")
            {
                Mode = DocumentMode.New;
                InitialiseDocument();
                Navigator    = null;
                CurrentIndex = 0;
                TotalItems   = 0;
                SetCurrentDocumentKey(null);
                ParentPathSegments.Clear();
                ParentPathSegments.Add(new PathSegment()
                {
                    Name = "Documents", Url = "/documents"
                });
                return;
            }

            var database = url.GetQueryParam("databaseid");

            if (database != null)
            {
                EditDatabaseDocument(database);
                return;
            }

            Navigator = DocumentNavigator.FromUrl(url);

            Navigator.GetDocument().ContinueOnSuccessInTheUIThread(
                result =>
            {
                if (result.Document == null)
                {
                    HandleDocumentNotFound();
                    return;
                }

                if (string.IsNullOrEmpty(result.Document.Key))
                {
                    Mode    = DocumentMode.Projection;
                    LocalId = Guid.NewGuid().ToString();
                }
                else
                {
                    Mode = DocumentMode.DocumentWithId;
                    result.Document.Key = Uri.UnescapeDataString(result.Document.Key);
                    LocalId             = result.Document.Key;
                    SetCurrentDocumentKey(result.Document.Key);
                }

                urlForFirst    = result.UrlForFirst;
                urlForPrevious = result.UrlForPrevious;
                urlForLast     = result.UrlForLast;
                urlForNext     = result.UrlForNext;

                IsLoaded       = true;
                document.Value = result.Document;
                CurrentIndex   = (int)result.Index;
                TotalItems     = (int)result.TotalDocuments;

                ParentPathSegments.Clear();
                ParentPathSegments.AddRange(result.ParentPath);

                WhenParsingComplete(dataSection.Document)
                .ContinueOnUIThread(t => ApplyOutliningMode());

                HandleDocumentChanged();
            })
            .Catch(exception =>
            {
                var conflictExeption = exception.GetBaseException() as ConflictException;

                if (conflictExeption != null)
                {
                    ApplicationModel.Current.Server.Value.SelectedDatabase.Value
                    .AsyncDatabaseCommands
                    .GetAsync(conflictExeption.ConflictedVersionIds, null)
                    .ContinueOnSuccessInTheUIThread(doc =>
                    {
                        var docs      = new List <RavenJObject>();
                        var metadatas = new List <RavenJObject>();
                        foreach (var result in doc.Results)
                        {
                            metadatas.Add(result.Value <RavenJObject>("@metadata"));
                            result.Remove("@metadata");
                            docs.Add(result);
                        }

                        ClearMetadatas(metadatas);

                        var docsConflictsResolver     = new ConflictsResolver(docs.ToArray());
                        var metadataConflictsResolver = new ConflictsResolver(metadatas.ToArray());

                        Key         = url.GetQueryParam("id");
                        DocumentKey = Key;
                        OnPropertyChanged(() => DisplayId);
                        Etag = conflictExeption.Etag;

                        ResolvingConflict = true;

                        dataSection.Document.DeleteText(TextChangeTypes.Custom, 0, dataSection.Document.CurrentSnapshot.Length);
                        dataSection.Document.AppendText(TextChangeTypes.Custom, docsConflictsResolver.Resolve());

                        metaDataSection.Document.DeleteText(TextChangeTypes.Custom, 0, metaDataSection.Document.CurrentSnapshot.Length);
                        metaDataSection.Document.AppendText(TextChangeTypes.Custom, metadataConflictsResolver.Resolve());

                        OnPropertyChanged(() => dataSection);
                        OnPropertyChanged(() => document);
                    });
                    return(true);
                }

                return(false);
            });
        }
예제 #2
0
        public override void LoadModelParameters(string parameters)
        {
            var url = new UrlParser(UrlUtil.Url);

            documentIdManager.Clear();

            if (url.GetQueryParam("mode") == "new")
            {
                Mode = DocumentMode.New;
                InitializeDocument();
                Navigator         = null;
                CurrentIndex      = 0;
                TotalItems        = 0;
                ResolvingConflict = false;
                SetCurrentDocumentKey(null);
                ParentPathSegments.Clear();
                ParentPathSegments.Add(new PathSegment {
                    Name = "Documents", Url = "/documents"
                });
                return;
            }

            var database = url.GetQueryParam("databaseid");

            if (database != null)
            {
                EditDatabaseDocument(database);
                return;
            }

            Navigator = DocumentNavigator.FromUrl(url);

            Navigator.GetDocument().ContinueOnSuccessInTheUIThread(
                result =>
            {
                if (result.IsConflicted)
                {
                    ApplicationModel.Current.Server.Value.SelectedDatabase.Value
                    .AsyncDatabaseCommands
                    .GetAsync(result.ConflictingVersionIds.ToArray(), null)
                    .ContinueOnSuccessInTheUIThread(loadResult =>
                    {
                        var docs      = new List <RavenJObject>();
                        var metadatas = new List <RavenJObject>();
                        foreach (var doc in loadResult.Results)
                        {
                            metadatas.Add(doc.Value <RavenJObject>("@metadata"));
                            doc.Remove("@metadata");
                            docs.Add(doc);
                        }

                        ClearMetadatas(metadatas);

                        var docsConflictsResolver     = new ConflictsResolver(docs.ToArray());
                        var metadataConflictsResolver = new ConflictsResolver(metadatas.ToArray());

                        SetCurrentDocumentKey(result.ConflictDocumentId);

                        OnPropertyChanged(() => DisplayId);
                        Etag = result.ConflictEtag;

                        ResolvingConflict = true;

                        JsonData     = docsConflictsResolver.Resolve();
                        JsonMetadata = metadataConflictsResolver.Resolve();

                        HasUnsavedChanges = false;

                        OnPropertyChanged(() => dataSection);
                        OnPropertyChanged(() => document);
                    });
                }
                else
                {
                    if (result.Document == null)
                    {
                        HandleDocumentNotFound();
                        return;
                    }

                    ResolvingConflict = false;

                    if (string.IsNullOrEmpty(result.Document.Key))
                    {
                        Mode    = DocumentMode.Projection;
                        LocalId = Guid.NewGuid().ToString();
                    }
                    else
                    {
                        AssertNoPropertyBeyondSize(result.Document.DataAsJson, 500 * 1000);
                        var recentQueue = ApplicationModel.Current.Server.Value.SelectedDatabase.Value.RecentDocuments;
                        ApplicationModel.Current.Server.Value.RawUrl = "databases/" +
                                                                       ApplicationModel.Current.Server.Value.SelectedDatabase
                                                                       .Value.Name +
                                                                       "/docs/" + result.Document.Key;
                        recentQueue.Add(result.Document.Key);
                        Mode = DocumentMode.DocumentWithId;
                        result.Document.Key = Uri.UnescapeDataString(result.Document.Key);
                        LocalId             = result.Document.Key;
                        SetCurrentDocumentKey(result.Document.Key);

                        HasExpiration  = Database.Value.HasExpirationBundle;
                        var expiration = result.Document.Metadata["Raven-Expiration-Date"];
                        if (expiration != null)
                        {
                            // The expiration date is always interpreted as UTC.
                            ExpireAt.Value         = DateTime.SpecifyKind(expiration.Value <DateTime>(), DateTimeKind.Utc);
                            EnableExpiration.Value = true;

                            // If there's an expiration date, show it even if the bundle is not enabled.
                            HasExpiration = true;
                        }
                        else
                        {
                            ExpireAt.Value         = null;
                            EnableExpiration.Value = false;
                        }
                    }

                    document.Value = result.Document;

                    WhenParsingComplete(dataSection.Document)
                    .ContinueOnUIThread(t => ApplyOutliningMode());

                    HandleDocumentChanged();
                }

                urlForFirst    = result.UrlForFirst;
                urlForPrevious = result.UrlForPrevious;
                urlForLast     = result.UrlForLast;
                urlForNext     = result.UrlForNext;

                CurrentIndex = (int)result.Index;
                TotalItems   = (int)result.TotalDocuments;

                ParentPathSegments.Clear();
                ParentPathSegments.AddRange(result.ParentPath);
            })
            .Catch();
        }