private static List <XmlDocument> BuildAndExecute(int contentRepositoryId, Criteria[] criteria, int nbrOfRecords) { Query query; List <XmlDocument> results; PublicationCriteria publicationCriteria = new PublicationCriteria(contentRepositoryId); Criteria searchCriteria = CriteriaFactory.And(criteria); searchCriteria = CriteriaFactory.And(searchCriteria, publicationCriteria); query = new Query { Criteria = searchCriteria }; SortParameter sortParameter = new SortParameter(SortParameter.ItemTitle, SortParameter.Ascending); LimitFilter limitFilter = new LimitFilter(nbrOfRecords); results = ExecuteQuery(query, limitFilter, sortParameter); return(results); }
/// <summary> /// Execute Query will execute the query and will return the list of Xml if the component /// </summary> /// <param name="query"></param> /// <param name="limitFilter"></param> /// <param name="sortParameter"></param> /// <returns></returns> public static List <XmlDocument> ExecuteQuery(Query query, LimitFilter limitFilter, SortParameter sortParameter) { List <XmlDocument> results = null; query.AddSorting(sortParameter); query.AddLimitFilter(limitFilter); string[] compURIList = query.ExecuteQuery(); if (compURIList != null && compURIList.Length > 0) { results = new List <XmlDocument>(); for (int componentCount = 0; componentCount < compURIList.Length; componentCount++) { results.Add(GetComponent(compURIList[componentCount])); } } return(results); }
public Tridion.ContentDelivery.DynamicContent.Query.Query ToTridionQuery() { string[] basedOnSchemas = QuerySchemas; DateTime lastPublishedDate = LastPublishedDate; IList <MetaQueryItem> metaQueryItems = MetaQueryValues; QueryLogic metaQueryLogic = MetaQueryLogic; int maxmimumComponents = MaximumComponents; Query q = null; //PublicationCriteria publicationAndLastPublishedDateCriteria = new PublicationCriteria(PublicationId); PublicationCriteria publicationAndLastPublishedDateCriteria = new PublicationCriteria(PublicationId); //format DateTime // 00:00:00.000 ItemLastPublishedDateCriteria dateLastPublished = new ItemLastPublishedDateCriteria(lastPublishedDate.ToString("yyyy-MM-dd HH:mm:ss.fff"), Criteria.GreaterThanOrEqual); //publicationAndLastPublishedDateCriteria.AddCriteria(dateLastPublished); Criteria basedOnSchemaAndInPublication; if (basedOnSchemas.Length > 0) { Criteria[] schemaCriterias = new Criteria[basedOnSchemas.Length]; int i = 0; foreach (var schema in basedOnSchemas) { TcmUri schemaUri = new TcmUri(schema); schemaCriterias.SetValue(new ItemSchemaCriteria(schemaUri.ItemId), i); i++; } Criteria basedOnSchema = CriteriaFactory.Or(schemaCriterias); basedOnSchemaAndInPublication = CriteriaFactory.And(publicationAndLastPublishedDateCriteria, basedOnSchema); } else { basedOnSchemaAndInPublication = publicationAndLastPublishedDateCriteria; } // Add filtering for meta data Criteria schemasAndMetaData; if (metaQueryItems.Count > 0) { Criteria metaQuery; Criteria[] metaCriterias = new Criteria[metaQueryItems.Count]; int metaCount = 0; foreach (MetaQueryItem queryItem in metaQueryItems) { CustomMetaKeyCriteria metaField = new CustomMetaKeyCriteria(queryItem.MetaField); CustomMetaValueCriteria metaCriteria; FieldOperator metaOperator = typeof(Criteria).GetField(queryItem.MetaOperator.ToString()).GetValue(null) as FieldOperator; switch (queryItem.MetaValue.GetType().Name) { case "DateTime": DateTime tempDate = (DateTime)queryItem.MetaValue; metaCriteria = new CustomMetaValueCriteria(metaField, tempDate.ToString("yyyy-MM-dd HH:mm:ss.fff"), "yyyy-MM-dd HH:mm:ss.SSS", metaOperator); break; case "Float": metaCriteria = new CustomMetaValueCriteria(metaField, (float)queryItem.MetaValue, metaOperator); break; case "String": metaCriteria = new CustomMetaValueCriteria(metaField, queryItem.MetaValue as string, metaOperator); break; default: throw new System.Exception("Unexpected query item data type; " + queryItem.MetaValue.GetType().Name); } metaCriterias.SetValue(metaCriteria, metaCount); metaCount++; } if (MetaQueryLogic == QueryLogic.AllCriteriaMatch) { metaQuery = CriteriaFactory.And(metaCriterias); } else { metaQuery = CriteriaFactory.Or(metaCriterias); } schemasAndMetaData = CriteriaFactory.And(basedOnSchemaAndInPublication, metaQuery); } else { schemasAndMetaData = basedOnSchemaAndInPublication; } Criteria allConditions; if (KeywordValues.Count > 0) { Criteria[] keywordCriterias = new Criteria[KeywordValues.Count]; int keywordCount = 0; foreach (KeywordItem keyCriteria in KeywordValues) { TaxonomyKeywordCriteria keywordField = new TaxonomyKeywordCriteria(keyCriteria.CategoryUri, keyCriteria.KeywordUri, false); keywordCriterias.SetValue(keywordField, keywordCount); keywordCount++; } Criteria keyQuery; if (KeywordQueryLogic == QueryLogic.AllCriteriaMatch) { keyQuery = CriteriaFactory.And(keywordCriterias); } else { keyQuery = CriteriaFactory.Or(keywordCriterias); } allConditions = CriteriaFactory.And(schemasAndMetaData, keyQuery); } else { allConditions = schemasAndMetaData; } q = new Query(allConditions); if (maxmimumComponents != 0 && maxmimumComponents != int.MaxValue) { LimitFilter limitResults = new LimitFilter(maxmimumComponents); q.SetResultFilter(limitResults); } // Sort column should either be a standard or custom metaData field SortColumn paramSort; if (typeof(SortParameter).GetField(QuerySortField) != null) { paramSort = typeof(SortParameter).GetField(QuerySortField).GetValue(null) as SortColumn; } else { // Why do we need to tell Tridion what data type the field is! Its in the database already! /* FIX: Does not work (null pointer exception): var sType = typeof(MetadataType).GetField(SortType.ToString()).GetValue(null) as MetadataType;*/ paramSort = new CustomMetaKeyColumn(QuerySortField, SortType); } SortDirection paramSortDirection = typeof(SortParameter).GetField(QuerySortOrder.ToString()).GetValue(null) as SortDirection; SortParameter sortParameter = new SortParameter(paramSort, paramSortDirection); q.AddSorting(sortParameter); return(q); }
public Tridion.ContentDelivery.DynamicContent.Query.Query ToTridionQuery() { string[] basedOnSchemas = QuerySchemas; DateTime lastPublishedDate = LastPublishedDate; IList<MetaQueryItem> metaQueryItems = MetaQueryValues; QueryLogic metaQueryLogic = MetaQueryLogic; int maxmimumComponents = MaximumComponents; Query q = null; //PublicationCriteria publicationAndLastPublishedDateCriteria = new PublicationCriteria(PublicationId); PublicationCriteria publicationAndLastPublishedDateCriteria = new PublicationCriteria(PublicationId); //format DateTime // 00:00:00.000 ItemLastPublishedDateCriteria dateLastPublished = new ItemLastPublishedDateCriteria(lastPublishedDate.ToString("yyyy-MM-dd HH:mm:ss.fff"), Criteria.GreaterThanOrEqual); //publicationAndLastPublishedDateCriteria.AddCriteria(dateLastPublished); Criteria basedOnSchemaAndInPublication; if (basedOnSchemas.Length > 0) { Criteria[] schemaCriterias = new Criteria[basedOnSchemas.Length]; int i = 0; foreach (var schema in basedOnSchemas) { TcmUri schemaUri = new TcmUri(schema); schemaCriterias.SetValue(new ItemSchemaCriteria(schemaUri.ItemId), i); i++; } Criteria basedOnSchema = CriteriaFactory.Or(schemaCriterias); basedOnSchemaAndInPublication = CriteriaFactory.And(publicationAndLastPublishedDateCriteria, basedOnSchema); } else { basedOnSchemaAndInPublication = publicationAndLastPublishedDateCriteria; } // Add filtering for meta data Criteria schemasAndMetaData; if (metaQueryItems.Count > 0) { Criteria metaQuery; Criteria[] metaCriterias = new Criteria[metaQueryItems.Count]; int metaCount = 0; foreach (MetaQueryItem queryItem in metaQueryItems) { CustomMetaKeyCriteria metaField = new CustomMetaKeyCriteria(queryItem.MetaField); CustomMetaValueCriteria metaCriteria; FieldOperator metaOperator = typeof(Criteria).GetField(queryItem.MetaOperator.ToString()).GetValue(null) as FieldOperator; switch (queryItem.MetaValue.GetType().Name) { case "DateTime": DateTime tempDate = (DateTime)queryItem.MetaValue; metaCriteria = new CustomMetaValueCriteria(metaField, tempDate.ToString("yyyy-MM-dd HH:mm:ss.fff"), "yyyy-MM-dd HH:mm:ss.SSS", metaOperator); break; case "Float": metaCriteria = new CustomMetaValueCriteria(metaField, (float)queryItem.MetaValue, metaOperator); break; case "String": metaCriteria = new CustomMetaValueCriteria(metaField, queryItem.MetaValue as string, metaOperator); break; default: throw new System.Exception("Unexpected query item data type; " + queryItem.MetaValue.GetType().Name); } metaCriterias.SetValue(metaCriteria, metaCount); metaCount++; } if (MetaQueryLogic == QueryLogic.AllCriteriaMatch) { metaQuery = CriteriaFactory.And(metaCriterias); } else { metaQuery = CriteriaFactory.Or(metaCriterias); } schemasAndMetaData = CriteriaFactory.And(basedOnSchemaAndInPublication, metaQuery); } else { schemasAndMetaData = basedOnSchemaAndInPublication; } Criteria allConditions; if (KeywordValues.Count > 0) { Criteria[] keywordCriterias = new Criteria[KeywordValues.Count]; int keywordCount = 0; foreach (KeywordItem keyCriteria in KeywordValues) { TaxonomyKeywordCriteria keywordField = new TaxonomyKeywordCriteria(keyCriteria.CategoryUri, keyCriteria.KeywordUri, false); keywordCriterias.SetValue(keywordField, keywordCount); keywordCount++; } Criteria keyQuery; if (KeywordQueryLogic == QueryLogic.AllCriteriaMatch) { keyQuery = CriteriaFactory.And(keywordCriterias); } else { keyQuery = CriteriaFactory.Or(keywordCriterias); } allConditions = CriteriaFactory.And(schemasAndMetaData, keyQuery); } else { allConditions = schemasAndMetaData; } q = new Query(allConditions); if (maxmimumComponents != 0 && maxmimumComponents != int.MaxValue) { LimitFilter limitResults = new LimitFilter(maxmimumComponents); q.SetResultFilter(limitResults); } // Sort column should either be a standard or custom metaData field SortColumn paramSort; if (typeof(SortParameter).GetField(QuerySortField) != null) { paramSort = typeof(SortParameter).GetField(QuerySortField).GetValue(null) as SortColumn; } else { // Why do we need to tell Tridion what data type the field is! Its in the database already! paramSort = new CustomMetaKeyColumn(QuerySortField, typeof(MetadataType).GetField(SortType.ToString()).GetValue(null) as MetadataType); } SortDirection paramSortDirection = typeof(SortParameter).GetField(QuerySortOrder.ToString()).GetValue(null) as SortDirection; SortParameter sortParameter = new SortParameter(paramSort, paramSortDirection); q.AddSorting(sortParameter); return q; }
public void Apply(LimitFilter filter) => _limit = filter.Value;
/// <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[] { }; }