Example #1
0
        private ContentItem Load(ContentItem contentItem)
        {
            if (!_contentManagerSession.RecallVersionId(contentItem.Id, out var loaded))
            {
                // store in session prior to loading to avoid some problems with simple circular dependencies
                _contentManagerSession.Store(contentItem);

                // create a context with a new instance to load
                var context = new LoadContentContext(contentItem);

                // invoke handlers to acquire state, or at least establish lazy loading callbacks
                Handlers.Invoke(handler => handler.Loading(context), _logger);
                ReversedHandlers.Invoke(handler => handler.Loaded(context), _logger);

                loaded = context.ContentItem;
            }

            return(loaded);
        }
        public async Task <ContentItem> GetAsync(string contentItemId, VersionOptions options)
        {
            ContentItem contentItem = null;

            if (options.IsLatest)
            {
                contentItem = await _session
                              .Query <ContentItem, ContentItemIndex>()
                              .Where(x => x.ContentItemId == contentItemId && x.Latest == true)
                              .FirstOrDefaultAsync();
            }
            else if (options.IsDraft && !options.IsDraftRequired)
            {
                contentItem = await _session
                              .Query <ContentItem, ContentItemIndex>()
                              .Where(x =>
                                     x.ContentItemId == contentItemId &&
                                     x.Published == false &&
                                     x.Latest == true)
                              .FirstOrDefaultAsync();
            }
            else if (options.IsDraft || options.IsDraftRequired)
            {
                // Loaded whatever is the latest as it will be cloned
                contentItem = await _session
                              .Query <ContentItem, ContentItemIndex>()
                              .Where(x =>
                                     x.ContentItemId == contentItemId &&
                                     x.Latest == true)
                              .FirstOrDefaultAsync();
            }
            else if (options.IsPublished)
            {
                // If the published version is requested and is already loaded, we can
                // return it right away
                if (_contentManagerSession.RecallPublishedItemId(contentItemId, out contentItem))
                {
                    return(contentItem);
                }

                contentItem = await _session
                              .Query <ContentItem, ContentItemIndex>()
                              .Where(x => x.ContentItemId == contentItemId && x.Published == true)
                              .FirstOrDefaultAsync();
            }

            if (contentItem == null)
            {
                if (!options.IsDraftRequired)
                {
                    return(null);
                }
            }

            // Return item if obtained earlier in session
            // If IsPublished is required then the test has already been checked before
            ContentItem recalled = null;

            if (!_contentManagerSession.RecallVersionId(contentItem.Id, out recalled))
            {
                // store in session prior to loading to avoid some problems with simple circular dependencies
                _contentManagerSession.Store(contentItem);

                // create a context with a new instance to load
                var context = new LoadContentContext(contentItem);

                // invoke handlers to acquire state, or at least establish lazy loading callbacks
                Handlers.Invoke(handler => handler.Loading(context), _logger);
                ReversedHandlers.Invoke(handler => handler.Loaded(context), _logger);

                contentItem = context.ContentItem;
            }
            else
            {
                contentItem = recalled;
            }

            if (options.IsDraftRequired)
            {
                // When draft is required and latest is published a new version is added
                if (contentItem.Published)
                {
                    // Save the previous version
                    _session.Save(contentItem);

                    contentItem = await BuildNewVersionAsync(contentItem);
                }

                // Save the new version
                _session.Save(contentItem);
            }

            return(contentItem);
        }