コード例 #1
0
        public async Task <ContentItem> Get(int contentItemId, VersionOptions options)
        {
            ContentItem contentItem;

            // obtain the root records based on version options
            if (options.VersionRecordId != 0)
            {
                if (_contentManagerSession.RecallVersionId(options.VersionRecordId, out contentItem))
                {
                    return(contentItem);
                }

                contentItem = await _session.GetAsync <ContentItem>(options.VersionRecordId);
            }
            else if (options.VersionNumber != 0)
            {
                if (_contentManagerSession.RecallContentItemId(contentItemId, options.VersionNumber, out contentItem))
                {
                    return(contentItem);
                }

                contentItem = await _session
                              .QueryAsync <ContentItem, ContentItemIndex>()
                              .Where(x =>
                                     x.ContentItemId == contentItemId &&
                                     x.Number == options.VersionNumber
                                     )
                              .FirstOrDefault();
            }
            else if (_contentManagerSession.RecallPublishedItemId(contentItemId, out contentItem))
            {
                if (options.IsPublished)
                {
                    return(contentItem);
                }
            }
            else if (options.IsLatest)
            {
                contentItem = await _session
                              .QueryAsync <ContentItem, ContentItemIndex>()
                              .Where(x => x.ContentItemId == contentItemId && x.Latest == true)
                              .FirstOrDefault();
            }
            else if (options.IsDraft && !options.IsDraftRequired)
            {
                contentItem = await _session
                              .QueryAsync <ContentItem, ContentItemIndex>()
                              .Where(x =>
                                     x.ContentItemId == contentItemId &&
                                     x.Published == false &&
                                     x.Latest == true)
                              .FirstOrDefault();
            }
            else if (options.IsDraft || options.IsDraftRequired)
            {
                contentItem = await _session
                              .QueryAsync <ContentItem, ContentItemIndex>()
                              .Where(x =>
                                     x.ContentItemId == contentItemId &&
                                     x.Latest == true)
                              .FirstOrDefault();
            }
            else if (options.IsPublished)
            {
                contentItem = await _session
                              .QueryAsync <ContentItem, ContentItemIndex>()
                              .Where(x => x.ContentItemId == contentItemId && x.Published == true)
                              .FirstOrDefault();
            }

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

            // 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);
            Handlers.Invoke(handler => handler.Loaded(context), _logger);

            // when draft is required and latest is published a new version is appended
            if (options.IsDraftRequired && contentItem.Published)
            {
                contentItem = await BuildNewVersionAsync(context.ContentItem);
            }

            return(contentItem);
        }
コード例 #2
0
        public async Task <ContentItem> GetAsync(string contentItemId, VersionOptions options)
        {
            ContentItem contentItem = null;

            // obtain the root records based on version options
            if (options.VersionRecordId != 0)
            {
                if (_contentManagerSession.RecallVersionId(options.VersionRecordId, out contentItem))
                {
                    return(contentItem);
                }

                contentItem = await _session.GetAsync <ContentItem>(options.VersionRecordId);
            }
            else if (options.VersionNumber != 0)
            {
                if (_contentManagerSession.RecallContentItemId(contentItemId, options.VersionNumber, out contentItem))
                {
                    return(contentItem);
                }

                contentItem = await _session
                              .QueryAsync <ContentItem, ContentItemIndex>()
                              .Where(x =>
                                     x.ContentItemId == contentItemId &&
                                     x.Number == options.VersionNumber
                                     )
                              .FirstOrDefault();
            }
            else if (options.IsLatest)
            {
                contentItem = await _session
                              .QueryAsync <ContentItem, ContentItemIndex>()
                              .Where(x => x.ContentItemId == contentItemId && x.Latest == true)
                              .FirstOrDefault();
            }
            else if (options.IsDraft && !options.IsDraftRequired)
            {
                contentItem = await _session
                              .QueryAsync <ContentItem, ContentItemIndex>()
                              .Where(x =>
                                     x.ContentItemId == contentItemId &&
                                     x.Published == false &&
                                     x.Latest == true)
                              .FirstOrDefault();
            }
            else if (options.IsDraft || options.IsDraftRequired)
            {
                // Loaded whatever is the latest as it will be cloned
                contentItem = await _session
                              .QueryAsync <ContentItem, ContentItemIndex>()
                              .Where(x =>
                                     x.ContentItemId == contentItemId &&
                                     x.Latest == true)
                              .FirstOrDefault();
            }
            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
                              .QueryAsync <ContentItem, ContentItemIndex>()
                              .Where(x => x.ContentItemId == contentItemId && x.Published == true)
                              .FirstOrDefault();
            }

            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);
                Handlers.Reverse().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);
        }