/// <summary> /// /// </summary> /// <param name="expression"></param> /// <returns></returns> internal tridion.Query Translate(Expression expression) { this.Visit(expression); // Add all the criteria, at least one should always be present if (criteriaNodes == null) { throw new NotSupportedException("There are no criteria present in the query, a criteria should always be present to execute the query on the broker database"); } this.q = new tridion.Query(); q.Criteria = criteriaNodes.GetQueryCriteria(); // Set the ResultFilter for the query if present if (filter != null) { q.SetResultFilter(filter); } // Set sorting for the query if present if (sort != null && sort.Count > 0) { foreach (var item in sort) { q.AddSorting(item); } } return q; }
/// <summary> /// /// </summary> /// <param name="expression"></param> /// <returns></returns> internal tridion.Query Translate(Expression expression) { this.Visit(expression); // Add all the criteria, at least one should always be present if (criteriaNodes == null) { throw new NotSupportedException("There are no criteria present in the query, a criteria should always be present to execute the query on the broker database"); } this.q = new tridion.Query(); q.Criteria = criteriaNodes.GetQueryCriteria(); // Set the ResultFilter for the query if present if (filter != null) { q.SetResultFilter(filter); } // Set sorting for the query if present if (sort != null && sort.Count > 0) { foreach (var item in sort) { q.AddSorting(item); } } return(q); }
/// <summary> /// Executes the specified <see cref="T:TcmCDService.Contracts.BrokerQuery" /> /// </summary> /// <param name="brokerQuery"><see cref="T:TcmCDService.Contracts.BrokerQuery" /></param> /// <returns><see cref="I:System.Collections.Generic.IEnumerable{System.String}" /></returns> public static IEnumerable <String> Execute(BrokerQuery brokerQuery) { if (brokerQuery != null) { List <IDisposable> disposableItems = new List <IDisposable>(); List <Criteria> criteria = new List <Criteria>(); try { // Query for ItemType: Component if (brokerQuery.ItemType != 0) { criteria.Add(new ItemTypeCriteria((int)brokerQuery.ItemType)); } // Query for Publication if (!String.IsNullOrEmpty(brokerQuery.Publication)) { criteria.Add(new PublicationCriteria(new TcmUri(brokerQuery.Publication).ItemId)); } // Query based on Schema if (brokerQuery.SchemaUris != null && brokerQuery.SchemaUris.Any()) { criteria.Add(CriteriaFactory.Or(brokerQuery.SchemaUris.Select((u) => { ItemSchemaCriteria itemSchemaCriteria = new ItemSchemaCriteria(new TcmUri(u).ItemId); disposableItems.Add(itemSchemaCriteria); return(itemSchemaCriteria); }).ToArray())); } // Query based on Component Template if (!String.IsNullOrEmpty(brokerQuery.ComponentTemplateUri)) { criteria.Add(new ItemTemplateCriteria(new TcmUri(brokerQuery.ComponentTemplateUri).ItemId)); } // Add any SubQuery entries (MetaQuery or KeywordQueries which are specified) if (brokerQuery.SubQueries != null && brokerQuery.SubQueries.Any()) { criteria.AddRange(brokerQuery.SubQueries.Where(q => q != null).Select((q) => { Criteria subCriteria = q is MetaQuery ? ToCriteria(q as MetaQuery) : ToCriteria(q as KeywordQuery); disposableItems.Add(subCriteria); return(subCriteria); })); } using (Tridion.ContentDelivery.DynamicContent.Query.Query query = new Tridion.ContentDelivery.DynamicContent.Query.Query(CriteriaFactory.And(criteria.ToArray()))) { // Limit the amount of results using (LimitFilter filter = new LimitFilter(brokerQuery.ResultLimit.GetValueOrDefault(100))) { query.SetResultFilter(filter); query.AddSorting(new SortParameter(SortParameter.ItemModificationDate, SortParameter.Descending)); return(query.ExecuteQuery()); } } } finally { // Ensure all created Java objects are disposed foreach (Criteria entry in criteria) { if (entry != null) { entry.Dispose(); } } foreach (IDisposable entry in disposableItems) { if (entry != null) { entry.Dispose(); } } } } return(new String[] { }); }
/// <summary> /// Executes the specified <see cref="T:TcmCDService.Contracts.BrokerQuery" /> /// </summary> /// <param name="brokerQuery"><see cref="T:TcmCDService.Contracts.BrokerQuery" /></param> /// <returns><see cref="I:System.Collections.Generic.IEnumerable{System.String}" /></returns> public static IEnumerable<String> Execute(BrokerQuery brokerQuery) { if (brokerQuery != null) { List<IDisposable> disposableItems = new List<IDisposable>(); List<Criteria> criteria = new List<Criteria>(); try { // Query for ItemType: Component if (brokerQuery.ItemType != 0) criteria.Add(new ItemTypeCriteria((int)brokerQuery.ItemType)); // Query for Publication if (!String.IsNullOrEmpty(brokerQuery.Publication)) criteria.Add(new PublicationCriteria(new TcmUri(brokerQuery.Publication).ItemId)); // Query based on Schema if (brokerQuery.SchemaUris != null && brokerQuery.SchemaUris.Any()) criteria.Add(CriteriaFactory.Or(brokerQuery.SchemaUris.Select((u) => { ItemSchemaCriteria itemSchemaCriteria = new ItemSchemaCriteria(new TcmUri(u).ItemId); disposableItems.Add(itemSchemaCriteria); return itemSchemaCriteria; }).ToArray())); // Query based on Component Template if (!String.IsNullOrEmpty(brokerQuery.ComponentTemplateUri)) criteria.Add(new ItemTemplateCriteria(new TcmUri(brokerQuery.ComponentTemplateUri).ItemId)); // Add any SubQuery entries (MetaQuery or KeywordQueries which are specified) if (brokerQuery.SubQueries != null && brokerQuery.SubQueries.Any()) criteria.AddRange(brokerQuery.SubQueries.Where(q => q != null).Select((q) => { Criteria subCriteria = q is MetaQuery ? ToCriteria(q as MetaQuery) : ToCriteria(q as KeywordQuery); disposableItems.Add(subCriteria); return subCriteria; })); using (Tridion.ContentDelivery.DynamicContent.Query.Query query = new Tridion.ContentDelivery.DynamicContent.Query.Query(CriteriaFactory.And(criteria.ToArray()))) { // Limit the amount of results using (LimitFilter filter = new LimitFilter(brokerQuery.ResultLimit.GetValueOrDefault(100))) { query.SetResultFilter(filter); query.AddSorting(new SortParameter(SortParameter.ItemModificationDate, SortParameter.Descending)); return query.ExecuteQuery(); } } } finally { // Ensure all created Java objects are disposed foreach (Criteria entry in criteria) { if (entry != null) entry.Dispose(); } foreach (IDisposable entry in disposableItems) { if (entry != null) entry.Dispose(); } } } return new String[] { }; }