private IEnumerable <ContentItemVersionRecord> GetManyImplementation(QueryHints hints, Action <ICriteria, ICriteria> predicate) { var session = _transactionManager.Value.GetSession(); 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>()); }
void WithQueryHints(QueryHints hints) { if (hints == QueryHints.Empty) { return; } var contentItemVersionCriteria = BindItemVersionCriteria(); var contentItemCriteria = BindItemCriteria(); var contentItemMetadata = _session.SessionFactory.GetClassMetadata(typeof(ContentItemRecord)); var contentItemVersionMetadata = _session.SessionFactory.GetClassMetadata(typeof(ContentItemVersionRecord)); // 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()); } }
IContentQuery <T> IContentQuery <T> .WithQueryHints(QueryHints hints) { _query.WithQueryHints(hints); return(this); }
public static T Get <T>(this IContentManager manager, int id, VersionOptions options, QueryHints hints) where T : class, IContent { var contentItem = manager.Get(id, options, hints); return(contentItem == null ? null : contentItem.Get <T>()); }
public IEnumerable <T> GetManyByVersionId <T>(IEnumerable <int> versionRecordIds, QueryHints hints) where T : class, IContent { return(GetManyByVersionId(versionRecordIds, hints).AsPart <T>()); }
public IEnumerable <ContentItem> GetManyByVersionId(IEnumerable <int> versionRecordIds, QueryHints hints) { var contentItemVersionRecords = GetManyImplementation(hints, (contentItemCriteria, contentItemVersionCriteria) => contentItemVersionCriteria.Add(Restrictions.In("Id", versionRecordIds.ToArray()))); var itemsById = contentItemVersionRecords .Select(r => Get(r.ContentItemRecord.Id, VersionOptions.VersionRecord(r.Id))) .Where(ci => ci != null) .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> GetMany <T>(IEnumerable <int> ids, VersionOptions options, QueryHints hints) 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 => Get(r.ContentItemRecord.Id, options.IsDraftRequired ? options : VersionOptions.VersionRecord(r.Id))) .Where(ci => ci != null) .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 virtual ContentItem Get(int id, VersionOptions options, QueryHints hints) { var session = _contentManagerSession(); ContentItem contentItem; ContentItemVersionRecord versionRecord = null; // obtain the root records based on version options if (options.VersionRecordId != 0) { // short-circuit if item held in session if (session.RecallVersionRecordId(options.VersionRecordId, out contentItem)) { return(contentItem); } versionRecord = _contentItemVersionRepository.Get(options.VersionRecordId); } else if (options.VersionNumber != 0) { // short-circuit if item held in session if (session.RecallVersionNumber(id, options.VersionNumber, out contentItem)) { return(contentItem); } versionRecord = _contentItemVersionRepository.Get(x => x.ContentItemRecord.Id == id && x.Number == options.VersionNumber); } else if (session.RecallContentRecordId(id, out contentItem)) { // try to reload a previously loaded published content item if (options.IsPublished) { return(contentItem); } versionRecord = contentItem.VersionRecord; } else { // do a query to load the records in case Get is called directly var contentItemVersionRecords = GetManyImplementation(hints, (contentItemCriteria, contentItemVersionCriteria) => { contentItemCriteria.Add(Restrictions.Eq("Id", id)); 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)); } contentItemVersionCriteria.SetFetchMode("ContentItemRecord", FetchMode.Eager); contentItemVersionCriteria.SetFetchMode("ContentItemRecord.ContentType", FetchMode.Eager); //contentItemVersionCriteria.SetMaxResults(1); }); if (options.VersionNumber != 0) { versionRecord = contentItemVersionRecords.FirstOrDefault( x => x.Number == options.VersionNumber) ?? _contentItemVersionRepository.Get( x => x.ContentItemRecord.Id == id && x.Number == options.VersionNumber); } else { versionRecord = contentItemVersionRecords.LastOrDefault(); } } // no record means content item is not in db if (versionRecord == null) { // check in memory var record = _contentItemRepository.Get(id); if (record == null) { return(null); } versionRecord = GetVersionRecord(options, record); if (versionRecord == null) { return(null); } } // return item if obtained earlier in session if (session.RecallVersionRecordId(versionRecord.Id, out contentItem)) { if (options.IsDraftRequired && versionRecord.Published) { return(BuildNewVersion(contentItem)); } return(contentItem); } // allocate instance and set record property contentItem = New(versionRecord.ContentItemRecord.ContentType.Name); contentItem.VersionRecord = versionRecord; // store in session prior to loading to avoid some problems with simple circular dependencies session.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 && versionRecord.Published) { contentItem = BuildNewVersion(context.ContentItem); } return(contentItem); }