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 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); contentItem.VersionRecord.ContentItemRecord.ContentType = AcquireContentTypeRecord(contentItem.ContentType); // 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)); 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 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)); }