public void Store(ContentItem item)
        {
            _itemByVersionRecordId.Add(item.VersionRecord.Id, item);
            _itemByVersionNumber.Add(Tuple.Create(item.Id, item.Version), item);

            // is it the Published version ?
            if (item.VersionRecord.Latest && item.VersionRecord.Published) {
                _publishedItemsByContentRecordId[item.Id] = item;
            }
        }
        public virtual void Create(ContentItem contentItem, VersionOptions options)
        {
            if (contentItem.VersionRecord == null) {
                // produce root record to determine the model id
                contentItem.VersionRecord = new ContentItemVersionRecord {
                    ContentItemRecord = new ContentItemRecord(),
                    Number = 1,
                    Latest = true,
                    Published = true
                };
            }

            // add to the collection manually for the created case
            contentItem.VersionRecord.ContentItemRecord.Versions.Add(contentItem.VersionRecord);

            // version may be specified
            if (options.VersionNumber != 0) {
                contentItem.VersionRecord.Number = options.VersionNumber;
            }

            // draft flag on create is required for explicitly-published content items
            if (options.IsDraft) {
                contentItem.VersionRecord.Published = false;
            }

            _contentItemStore.Store(contentItem);

            // build a context with the initialized instance to create
            var context = new CreateContentContext(contentItem);

            // invoke handlers to add information to persistent stores
            Handlers.Invoke(handler => handler.Creating(context));

            // deferring the assignment of ContentType as loading a Record might force NHibernate to AutoFlush
            // the ContentPart, and needs the ContentItemRecord to be created before (created in previous statement)
            contentItem.VersionRecord.ContentItemRecord.ContentType = AcquireContentTypeRecord(contentItem.ContentType);

            Handlers.Invoke(handler => handler.Created(context));

            if (options.IsPublished) {
                var publishContext = new PublishContentContext(contentItem, null);

                // invoke handlers to acquire state, or at least establish lazy loading callbacks
                Handlers.Invoke(handler => handler.Publishing(publishContext));

                // invoke handlers to acquire state, or at least establish lazy loading callbacks
                Handlers.Invoke(handler => handler.Published(publishContext));
            }
        }
 public void Store(ContentItem contentItem) {
     _contentStorageManager.Store(contentItem.Record);
     _contentStorageManager.Store(contentItem.VersionRecord);
 }
 public virtual void Create(ContentItem contentItem)
 {
     Create(contentItem, VersionOptions.Published);
 }
        //    public virtual void Remove(ContentItem contentItem) {
        //        var activeVersions = _contentItemVersionRepository.Fetch(x => x.ContentItemRecord == contentItem.Record && (x.Published || x.Latest));
        //        var context = new RemoveContentContext(contentItem);
        //        Handlers.Invoke(handler => handler.Removing(context), Logger);
        //        foreach (var version in activeVersions) {
        //            if (version.Published) {
        //                version.Published = false;
        //            }
        //            if (version.Latest) {
        //                version.Latest = false;
        //            }
        //        }
        //        Handlers.Invoke(handler => handler.Removed(context), Logger);
        //    }
        //    public virtual void Destroy(ContentItem contentItem) {
        //        var session = _sessionLocator.Value.For(typeof(ContentItemVersionRecord));
        //        var context = new DestroyContentContext(contentItem);
        //        // Give storage filters a chance to delete content part records.
        //        Handlers.Invoke(handler => handler.Destroying(context), Logger);
        //        // Delete content item version and content item records.
        //        session
        //            .CreateQuery("delete from Orchard.ContentManagement.Records.ContentItemVersionRecord civ where civ.ContentItemRecord.Id = (:id)")
        //            .SetParameter("id", contentItem.Id)
        //            .ExecuteUpdate();
        //        // Delete the content item record itself.
        //        session
        //            .CreateQuery("delete from Orchard.ContentManagement.Records.ContentItemRecord ci where ci.Id = (:id)")
        //            .SetParameter("id", contentItem.Id)
        //            .ExecuteUpdate();
        //        Handlers.Invoke(handler => handler.Destroyed(context), Logger);
        //    }
        protected virtual ContentItem BuildNewVersion(ContentItem existingContentItem)
        {
            var contentItemRecord = existingContentItem.Record;

            // locate the existing and the current latest versions, allocate building version
            var existingItemVersionRecord = existingContentItem.VersionRecord;
            var buildingItemVersionRecord = new ContentItemVersionRecord {
                ContentItemRecord = contentItemRecord,
                Latest = true,
                Published = false,
                Data = existingItemVersionRecord.Data,
            };

            var latestVersion = contentItemRecord.Versions.SingleOrDefault(x => x.Latest);

            if (latestVersion != null) {
                latestVersion.Latest = false;
                buildingItemVersionRecord.Number = latestVersion.Number + 1;
            }
            else {
                buildingItemVersionRecord.Number = contentItemRecord.Versions.Max(x => x.Number) + 1;
            }

            contentItemRecord.Versions.Add(buildingItemVersionRecord);
            _contentStorageManager.Store(buildingItemVersionRecord);

            var buildingContentItem = New(existingContentItem.ContentType);
            buildingContentItem.VersionRecord = buildingItemVersionRecord;

            var context = new VersionContentContext {
                Id = existingContentItem.Id,
                ContentType = existingContentItem.ContentType,
                ContentItemRecord = contentItemRecord,
                ExistingContentItem = existingContentItem,
                BuildingContentItem = buildingContentItem,
                ExistingItemVersionRecord = existingItemVersionRecord,
                BuildingItemVersionRecord = buildingItemVersionRecord,
            };
            Handlers.Invoke(handler => handler.Versioning(context));
            Handlers.Invoke(handler => handler.Versioned(context));

            return context.BuildingContentItem;
        }
        public virtual void Unpublish(ContentItem contentItem)
        {
            ContentItem publishedItem;
            if (contentItem.VersionRecord.Published) {
                // the version passed in is the published one
                publishedItem = contentItem;
            }
            else {
                // try to locate the published version of this item
                publishedItem = Get(contentItem.Id, VersionOptions.Published);
            }

            if (publishedItem == null) {
                // no published version exists. no work to perform.
                return;
            }

            // create a context for the item. the publishing version is null in this case
            // and the previous version is the one active prior to unpublishing. handlers
            // should take this null check into account
            var context = new PublishContentContext(contentItem, publishedItem.VersionRecord) {
                PublishingItemVersionRecord = null
            };

            Handlers.Invoke(handler => handler.Unpublishing(context));

            publishedItem.VersionRecord.Published = false;

            Handlers.Invoke(handler => handler.Unpublished(context));
        }
        //    public virtual IEnumerable<ContentItem> GetAllVersions(int id) {
        //        return _contentItemVersionRepository
        //            .Fetch(x => x.ContentItemRecord.Id == id)
        //            .OrderBy(x => x.Number)
        //            .Select(x => GetAsync(x.Id, VersionOptions.VersionRecord(x.Id)));
        //    }
        //    public IEnumerable<T> GetManyAsync<T>(IEnumerable<int> ids, VersionOptions options) where T : class, IContent {
        //        var contentItemVersionRecords = GetManyImplementation(hints, (contentItemCriteria, contentItemVersionCriteria) => {
        //            contentItemCriteria.Add(Restrictions.In("Id", ids.ToArray()));
        //            if (options.IsPublished) {
        //                contentItemVersionCriteria.Add(Restrictions.Eq("Published", true));
        //            }
        //            else if (options.IsLatest) {
        //                contentItemVersionCriteria.Add(Restrictions.Eq("Latest", true));
        //            }
        //            else if (options.IsDraft && !options.IsDraftRequired) {
        //                contentItemVersionCriteria.Add(
        //                    Restrictions.And(Restrictions.Eq("Published", false),
        //                                    Restrictions.Eq("Latest", true)));
        //            }
        //            else if (options.IsDraft || options.IsDraftRequired) {
        //                contentItemVersionCriteria.Add(Restrictions.Eq("Latest", true));
        //            }
        //        });
        //        var itemsById = contentItemVersionRecords
        //            .Select(r => GetAsync(r.ContentItemRecord.Id, options.IsDraftRequired ? options : VersionOptions.VersionRecord(r.Id)))
        //            .GroupBy(ci => ci.Id)
        //            .ToDictionary(g => g.Key);
        //        return ids.SelectMany(id => {
        //                IGrouping<int, ContentItem> values;
        //                return itemsById.TryGetValue(id, out values) ? values : Enumerable.Empty<ContentItem>();
        //            }).AsPart<T>().ToArray();
        //    }
        //    public IEnumerable<ContentItem> GetManyByVersionId(IEnumerable<int> versionRecordIds) {
        //        var contentItemVersionRecords = GetManyImplementation((contentItemCriteria, contentItemVersionCriteria) =>
        //            contentItemVersionCriteria.Add(Restrictions.In("Id", versionRecordIds.ToArray())));
        //        var itemsById = contentItemVersionRecords
        //            .Select(r => GetAsync(r.ContentItemRecord.Id, VersionOptions.VersionRecord(r.Id)))
        //            .GroupBy(ci => ci.VersionRecord.Id)
        //            .ToDictionary(g => g.Key);
        //        return versionRecordIds.SelectMany(id => {
        //            IGrouping<int, ContentItem> values;
        //            return itemsById.TryGetValue(id, out values) ? values : Enumerable.Empty<ContentItem>();
        //        }).ToArray();
        //    }
        //    public IEnumerable<T> GetManyByVersionId<T>(IEnumerable<int> versionRecordIds) where T : class, IContent {
        //        return GetManyByVersionId(versionRecordIds).AsPart<T>();
        //    }
        //    private IEnumerable<ContentItemVersionRecord> GetManyImplementation(Action<ICriteria, ICriteria> predicate) {
        //        var session = _sessionLocator.Value.For(typeof (ContentItemRecord));
        //        var contentItemVersionCriteria = session.CreateCriteria(typeof (ContentItemVersionRecord));
        //        var contentItemCriteria = contentItemVersionCriteria.CreateCriteria("ContentItemRecord");
        //        predicate(contentItemCriteria, contentItemVersionCriteria);
        //        var contentItemMetadata = session.SessionFactory.GetClassMetadata(typeof (ContentItemRecord));
        //        var contentItemVersionMetadata = session.SessionFactory.GetClassMetadata(typeof (ContentItemVersionRecord));
        //        if (hints != QueryHints.Empty) {
        //            // break apart and group hints by their first segment
        //            var hintDictionary = hints.Records
        //                .Select(hint => new { Hint = hint, Segments = hint.Split('.') })
        //                .GroupBy(item => item.Segments.FirstOrDefault())
        //                .ToDictionary(grouping => grouping.Key, StringComparer.InvariantCultureIgnoreCase);
        //            // locate hints that match properties in the ContentItemVersionRecord
        //            foreach (var hit in contentItemVersionMetadata.PropertyNames.Where(hintDictionary.ContainsKey).SelectMany(key => hintDictionary[key])) {
        //                contentItemVersionCriteria.SetFetchMode(hit.Hint, FetchMode.Eager);
        //                hit.Segments.Take(hit.Segments.Count() - 1).Aggregate(contentItemVersionCriteria, ExtendCriteria);
        //            }
        //            // locate hints that match properties in the ContentItemRecord
        //            foreach (var hit in contentItemMetadata.PropertyNames.Where(hintDictionary.ContainsKey).SelectMany(key => hintDictionary[key])) {
        //                contentItemVersionCriteria.SetFetchMode("ContentItemRecord." + hit.Hint, FetchMode.Eager);
        //                hit.Segments.Take(hit.Segments.Count() - 1).Aggregate(contentItemCriteria, ExtendCriteria);
        //            }
        //            if (hintDictionary.SelectMany(x => x.Value).Any(x => x.Segments.Count() > 1))
        //                contentItemVersionCriteria.SetResultTransformer(new DistinctRootEntityResultTransformer());
        //        }
        //        contentItemCriteria.SetCacheable(true);
        //        return contentItemVersionCriteria.List<ContentItemVersionRecord>();
        //    }
        //    private static ICriteria ExtendCriteria(ICriteria criteria, string segment) {
        //        return criteria.GetCriteriaByPath(segment) ?? criteria.CreateCriteria(segment, JoinType.LeftOuterJoin);
        //    }
        public virtual void Publish(ContentItem contentItem)
        {
            if (contentItem.VersionRecord.Published) {
                return;
            }
            // create a context for the item and it's previous published record
            var previous = contentItem.Record.Versions.SingleOrDefault(x => x.Published);
            var context = new PublishContentContext(contentItem, previous);

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

            if (context.Cancel) {
                return;
            }

            if (previous != null) {
                previous.Published = false;
            }
            contentItem.VersionRecord.Published = true;

            Handlers.Invoke(handler => handler.Published(context));
        }
 public bool RecallVersionRecordId(int id, out ContentItem item)
 {
     return _itemByVersionRecordId.TryGetValue(id, out item);
 }
 public bool RecallVersionNumber(int id, int version, out ContentItem item)
 {
     return _itemByVersionNumber.TryGetValue(Tuple.Create(id, version), out item);
 }
 public bool RecallContentRecordId(int id, out ContentItem item)
 {
     return _publishedItemsByContentRecordId.TryGetValue(id, out item);
 }
 public bool RecallContentRecordId(int id, out ContentItem item)
 {
     return(_publishedItemsByContentRecordId.TryGetValue(id, out item));
 }
 public bool RecallVersionNumber(int id, int version, out ContentItem item)
 {
     return(_itemByVersionNumber.TryGetValue(Tuple.Create(id, version), out item));
 }
 public bool RecallVersionRecordId(int id, out ContentItem item)
 {
     return(_itemByVersionRecordId.TryGetValue(id, out item));
 }
 public virtual void Create(ContentItem contentItem)
 {
     Create(contentItem, VersionOptions.Published);
 }