public DateTime LastPublished(string[] schemaUris)
        {
            TcmUri schemaTcmUri = new TcmUri(schemaUris.First());

            PublicationCriteria publicationCriteria = new PublicationCriteria(schemaTcmUri.PublicationId);

            Criteria[] schemaCriterias = schemaUris
                                         .Select(uri => new ItemSchemaCriteria(new TcmUri(uri).ItemId))
                                         .ToArray();

            Criteria basedOnSchema = CriteriaFactory.Or(schemaCriterias);
            Criteria basedOnSchemaAndInPublication = CriteriaFactory.And(publicationCriteria, basedOnSchema);

            Query q = new Query(basedOnSchemaAndInPublication);

            SortParameter sortParameter = new SortParameter(SortParameter.ItemLastPublishedDate, SortParameter.Descending);

            q.AddSorting(sortParameter);
            q.AddLimitFilter(new LimitFilter(1));

            string[] foundUris = q.ExecuteQuery();

            if (foundUris.Length > 0)
            {
                using (Tridion.ContentDelivery.Meta.ComponentMetaFactory fac = new Tridion.ContentDelivery.Meta.ComponentMetaFactory(schemaTcmUri.PublicationId))
                {
                    Tridion.ContentDelivery.Meta.IComponentMeta meta = fac.GetMeta(foundUris[0]);
                    return(meta.LastPublicationDate);
                }
            }

            return(DateTime.MinValue);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the raw string (xml) from the broker db by URL
        /// </summary>
        /// <param name="Url">URL of the page</param>
        /// <returns>String with page xml or empty string if no page was found</returns>
        public string GetContentByUrl(string Url)
        {
            string retVal = string.Empty;

            Query            pageQuery = new Query();
            ItemTypeCriteria isPage    = new ItemTypeCriteria(64); // TODO There must be an enum of these somewhere
            PageURLCriteria  pageUrl   = new PageURLCriteria(Url);

            Criteria allCriteria = CriteriaFactory.And(isPage, pageUrl);

            if (this.PublicationId != 0)
            {
                PublicationCriteria correctSite = new PublicationCriteria(this.PublicationId);
                allCriteria.AddCriteria(correctSite);
            }
            pageQuery.Criteria = allCriteria;

            string[] resultUris = pageQuery.ExecuteQuery();

            if (resultUris.Length > 0)
            {
                PageContentAssembler loadPage = new PageContentAssembler();
                retVal = loadPage.GetContent(resultUris[0]);
            }
            return(retVal);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the raw string (xml) from the broker db by URL
        /// </summary>
        /// <param name="Url">URL of the page</param>
        /// <returns>String with page xml or empty string if no page was found</returns>
        private string GetStringContentFromBrokerByUrl(string Url)
        {
            string retVal = string.Empty;

            Query               pageQuery   = new Query();
            ItemTypeCriteria    isPage      = new ItemTypeCriteria(64);               // TODO There must be an enum of these somewhere
            PageURLCriteria     pageUrl     = new PageURLCriteria(Url);
            PublicationCriteria correctSite = new PublicationCriteria(PublicationId); //Todo: add logic to determine site on url



            Criteria allCriteria = CriteriaFactory.And(isPage, pageUrl);

            allCriteria.AddCriteria(correctSite);

            pageQuery.Criteria = allCriteria;

            string[] resultUris = pageQuery.ExecuteQuery();

            if (resultUris.Length > 0)
            {
                PageContentAssembler loadPage = new PageContentAssembler();
                retVal = loadPage.GetContent(resultUris[0]);
            }
            return(retVal);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the raw string (xml) from the broker db by URL
        /// </summary>
        /// <param name="Url">URL of the page</param>
        /// <returns>String with page xml or empty string if no page was found</returns>
        public string GetContentByUrl(string Url)
        {
            LoggerService.Debug(">>GetContentByUrl({0})", LoggingCategory.Performance, Url);
            string retVal = string.Empty;

            LoggerService.Debug("GetContentByUrl: about to create query", LoggingCategory.Performance);
            Query pageQuery = new Query();

            LoggerService.Debug("GetContentByUrl: created query", LoggingCategory.Performance);
            ItemTypeCriteria isPage  = new ItemTypeCriteria(64); // TODO There must be an enum of these somewhere
            PageURLCriteria  pageUrl = new PageURLCriteria(Url);

            Criteria allCriteria = CriteriaFactory.And(isPage, pageUrl);

            if (this.PublicationId != 0)
            {
                PublicationCriteria correctSite = new PublicationCriteria(this.PublicationId);
                allCriteria.AddCriteria(correctSite);
            }
            pageQuery.Criteria = allCriteria;
            LoggerService.Debug("GetContentByUrl: added criteria to query", LoggingCategory.Performance);

            LoggerService.Debug("GetContentByUrl: about to execute query", LoggingCategory.Performance);
            string[] resultUris = pageQuery.ExecuteQuery();
            LoggerService.Debug("GetContentByUrl: executed query", LoggingCategory.Performance);


            if (resultUris.Length > 0)
            {
                retVal = PageContentAssembler.GetContent(resultUris[0]);
                LoggerService.Debug("GetContentByUrl: executed PageContentAssembler", LoggingCategory.Performance);
            }
            LoggerService.Debug("<<GetContentByUrl({0})", LoggingCategory.Performance, Url);
            return(retVal);
        }
Exemplo n.º 5
0
        public byte[] GetBinaryByUrl(string url)
        {
            string encodedUrl = HttpUtility.UrlPathEncode(url); // ?? why here? why now?

            Query findBinary = new Query();
            PublicationURLCriteria urlCriteria = new PublicationURLCriteria(url);
            //MultimediaCriteria isBinary = new MultimediaCriteria(true);

            //Criteria allCriteria = CriteriaFactory.And(isBinary, urlCriteria);
            Criteria allCriteria = urlCriteria;

            findBinary.Criteria = allCriteria;
            if (this.PublicationId != 0)
            {
                PublicationCriteria correctSite = new PublicationCriteria(this.PublicationId);
                allCriteria.AddCriteria(correctSite);
            }

            string[] binaryUri = findBinary.ExecuteQuery();

            if (binaryUri.Length == 0)
            {
                // TODO: find out how to retrieve binary data
            }

            throw new NotImplementedException();
        }
        public byte[] GetBinaryByUrl(string url)
        {
            string encodedUrl = HttpUtility.UrlPathEncode(url); // ?? why here? why now?
            
            Query findBinary = new Query();
            PublicationURLCriteria urlCriteria = new PublicationURLCriteria(url);
            //MultimediaCriteria isBinary = new MultimediaCriteria(true);

            //Criteria allCriteria = CriteriaFactory.And(isBinary, urlCriteria);
            Criteria allCriteria = urlCriteria;
            findBinary.Criteria = allCriteria;
            if (this.PublicationId != 0)
            {
                PublicationCriteria correctSite = new PublicationCriteria(this.PublicationId);
                allCriteria.AddCriteria(correctSite);
            }

            string[] binaryUri = findBinary.ExecuteQuery();

            if (binaryUri.Length == 0)
            {
                
                // TODO: find out how to retrieve binary data
            }

            throw new NotImplementedException();
        }
        public IItem GetPageIdByIshLogicalReference(int publicationId, string ishLogicalRefValue)
        {
            try
            {
                Criteria dateCriteria = new ItemLastPublishedDateCriteria(DefaultPublishData, Criteria.GreaterThanOrEqual);
                CustomMetaKeyCriteria metaKeyCriteria = new CustomMetaKeyCriteria(RefFieldName);
                Criteria refCriteria = new CustomMetaValueCriteria(metaKeyCriteria, ishLogicalRefValue);
                Criteria pubCriteria = new PublicationCriteria(publicationId);
                Criteria itemType    = new ItemTypeCriteria((int)ItemType.Page);
                Criteria composite   = new AndCriteria(new[] { dateCriteria, refCriteria, itemType, pubCriteria });

                Query   query = new Query(composite);
                IItem[] items = query.ExecuteEntityQuery();
                if (items == null || items.Length == 0)
                {
                    return(new ItemImpl());
                }

                if (items.Length > 1)
                {
                    throw new TridionDocsApiException($"Too many page Ids found in publication with logical ref value {ishLogicalRefValue}");
                }

                return(items[0]);
            }
            catch (Exception)
            {
                throw new DxaItemNotFoundException($"Page reference by ishlogicalref.object.id = {ishLogicalRefValue} not found in publication {publicationId}.");
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Get all urls of published pages
        /// </summary>
        /// <param name="includeExtensions"></param>
        /// <param name="pathStarts"></param>
        /// <param name="publicationID"></param>
        /// <returns></returns>
        public string[] GetAllPublishedPageUrls(string[] includeExtensions, string[] pathStarts)
        {
            Query               pageQuery          = new Query();
            ItemTypeCriteria    isPage             = new ItemTypeCriteria(64);               // TODO There must be an enum of these somewhere
            PublicationCriteria currentPublication = new PublicationCriteria(PublicationId); //Todo: add logic to determine site on url

            Criteria pageInPublication = CriteriaFactory.And(isPage, currentPublication);

            if (includeExtensions.Length > 0)
            {
                PageURLCriteria[] extensionsCriteria = new PageURLCriteria[includeExtensions.Length];
                int criteriaCount = 0;
                foreach (string pageExtension in includeExtensions)
                {
                    extensionsCriteria.SetValue(new PageURLCriteria("%" + pageExtension, Criteria.Like), criteriaCount);
                    criteriaCount++;
                }

                Criteria allExtensions = CriteriaFactory.Or(extensionsCriteria);
                pageInPublication = CriteriaFactory.And(pageInPublication, allExtensions);
            }

            if (pathStarts.Length > 0)
            {
                PageURLCriteria[] pathCriteria = new PageURLCriteria[pathStarts.Length];
                int criteriaCount = 0;
                foreach (string requiredPath in pathStarts)
                {
                    pathCriteria.SetValue(new PageURLCriteria(requiredPath + "%", Criteria.Like), criteriaCount);
                    criteriaCount++;
                }

                Criteria allPaths = CriteriaFactory.Or(pathCriteria);
                pageInPublication = CriteriaFactory.And(pageInPublication, allPaths);
            }

            Query findPages = new Query(pageInPublication);

            string[] pageUris = findPages.ExecuteQuery();

            // Need to get PageMeta data to find all the urls
            List <string> pageUrls = new List <string>();

            foreach (string uri in pageUris)
            {
                TcmUri          tcmUri      = new TcmUri(uri);
                PageMetaFactory metaFactory = GetPageMetaFactory(tcmUri.PublicationId);
                IPageMeta       currentMeta = metaFactory.GetMeta(uri);
                pageUrls.Add(currentMeta.UrlPath);
            }
            return(pageUrls.ToArray());
        }
        /// <summary>
        /// Get all urls of published pages
        /// </summary>
        /// <param name="includeExtensions"></param>
        /// <param name="pathStarts"></param>
        /// <param name="publicationID"></param>
        /// <returns></returns>
        public string[] GetAllPublishedPageUrls(string[] includeExtensions, string[] pathStarts)
        {
            Query pageQuery = new Query();
            ItemTypeCriteria isPage = new ItemTypeCriteria(64);  // TODO There must be an enum of these somewhere
            PublicationCriteria currentPublication = new PublicationCriteria(PublicationId); //Todo: add logic to determine site on url

            Criteria pageInPublication = CriteriaFactory.And(isPage, currentPublication);

            if (includeExtensions.Length > 0)
            {
                PageURLCriteria[] extensionsCriteria = new PageURLCriteria[includeExtensions.Length];
                int criteriaCount = 0;
                foreach (string pageExtension in includeExtensions)
                {
                    extensionsCriteria.SetValue(new PageURLCriteria("%" + pageExtension, Criteria.Like), criteriaCount);
                    criteriaCount++;
                }

                Criteria allExtensions = CriteriaFactory.Or(extensionsCriteria);
                pageInPublication = CriteriaFactory.And(pageInPublication, allExtensions);
            }

            if (pathStarts.Length > 0)
            {
                PageURLCriteria[] pathCriteria = new PageURLCriteria[pathStarts.Length];
                int criteriaCount = 0;
                foreach (string requiredPath in pathStarts)
                {
                    pathCriteria.SetValue(new PageURLCriteria(requiredPath + "%", Criteria.Like), criteriaCount);
                    criteriaCount++;
                }

                Criteria allPaths = CriteriaFactory.Or(pathCriteria);
                pageInPublication = CriteriaFactory.And(pageInPublication, allPaths);
            }

            Query findPages = new Query(pageInPublication);
            string[] pageUris = findPages.ExecuteQuery();

            // Need to get PageMeta data to find all the urls
            List<string> pageUrls = new List<string>();
            foreach (string uri in pageUris)
            {
                TcmUri tcmUri = new TcmUri(uri);
                PageMetaFactory metaFactory = GetPageMetaFactory(tcmUri.PublicationId);
                IPageMeta currentMeta = metaFactory.GetMeta(uri);
                pageUrls.Add(currentMeta.UrlPath);
            }
            return pageUrls.ToArray();
        }
Exemplo n.º 10
0
        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);
        }
Exemplo n.º 11
0
        public bool TryFindBinary(string url, out IBinary binary)
        {
            string encodedUrl = HttpUtility.UrlPathEncode(url);

            binary = null;
            //using (var sqlBinMetaHome = new Com.Tridion.Broker.Binaries.Meta.SQLBinaryMetaHome())
            //{

            //    Com.Tridion.Meta.BinaryMeta binaryMeta = sqlBinMetaHome.FindByURL(PublicationId, encodedUrl); // "/Images/anubis_pecunia160_tcm70-520973.jpg"
            //    if (binaryMeta != null)
            //    {
            //        using (var sqlBinaryHome = new Com.Tridion.Broker.Binaries.SQLBinaryHome())
            //        {
            //            Com.Tridion.Data.BinaryData binData = sqlBinaryHome.FindByPrimaryKey(PublicationId, (int)binaryMeta.GetId());
            //            if (binData != null)
            //            {
            //                binary = new Binary(this)
            //                {
            //                    Id = String.Format("tcm:{0}-{1}", binData.GetPublicationId(), binData.GetId()),
            //                    Url = url,
            //                    LastPublishedDate = DateTime.Now,
            //                    Multimedia = null,
            //                    VariantId = binData.GetVariantId()
            //                };
            //                return true;
            //            }
            //        }
            //    }
            //    return false;
            //}

            Query            binaryQuery  = new Query();
            ItemTypeCriteria isMultiMedia = new ItemTypeCriteria(32);

            PublicationCriteria inPub       = new PublicationCriteria(PublicationId);
            Criteria            allCriteria = CriteriaFactory.And(isMultiMedia, inPub);

            string[] results = binaryQuery.ExecuteQuery();
            binary = null;
            return(true);

            foreach (var item in results)
            {
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Gets the raw string (xml) from the broker db by URL
        /// </summary>
        /// <param name="Url">URL of the page</param>
        /// <returns>String with page xml or empty string if no page was found</returns>
        public string GetContentByUrl(string Url)
        {
            LoggerService.Debug(">>GetContentByUrl({0})", LoggingCategory.Performance, Url);
            string retVal = string.Empty;

            LoggerService.Debug("GetContentByUrl: about to create query", LoggingCategory.Performance);
            using (var pageQuery = new Query())
            {
                LoggerService.Debug("GetContentByUrl: created query", LoggingCategory.Performance);
                using (var isPage = new ItemTypeCriteria(64))
                {
                    using (var pageUrl = new PageURLCriteria(Url))
                    {
                        using (var allCriteria = CriteriaFactory.And(isPage, pageUrl))
                        {
                            if (this.PublicationId != 0)
                            {
                                using (var correctSite = new PublicationCriteria(this.PublicationId))
                                {
                                    allCriteria.AddCriteria(correctSite);
                                }
                            }

                            pageQuery.Criteria = allCriteria;
                        }
                    }
                }
                LoggerService.Debug("GetContentByUrl: added criteria to query", LoggingCategory.Performance);

                LoggerService.Debug("GetContentByUrl: about to execute query", LoggingCategory.Performance);
                string[] resultUris = pageQuery.ExecuteQuery();
                pageQuery.Dispose();
                LoggerService.Debug("GetContentByUrl: executed query", LoggingCategory.Performance);


                if (resultUris.Length > 0)
                {
                    retVal = PageContentAssembler.GetContent(resultUris[0]);
                    LoggerService.Debug("GetContentByUrl: executed PageContentAssembler", LoggingCategory.Performance);
                }
                LoggerService.Debug("<<GetContentByUrl({0})", LoggingCategory.Performance, Url);
                return(retVal);
            }
        }
Exemplo n.º 13
0
        public static IPageMeta GetPageMetaFromSEO(HttpRequestBase request, string url)
        {
            PageMetaFactory pageMetaFactory = new PageMetaFactory(SiteGlobal.PublicationId);
            IPageMeta       pageMeta        = pageMetaFactory.GetMetaByUrl(SiteGlobal.PublicationId, url);

            if (pageMeta == null)
            {
                // Check if it is a Vanity URL
                var publicationCriteria = new PublicationCriteria(SiteGlobal.PublicationId);
                var pageCriteria        = new ItemTypeCriteria((int)TridionItemType.Page);
                var vanityUrlCriteria   = new CustomMetaValueCriteria(new CustomMetaKeyCriteria("SEOUrl", Criteria.Equal), url);

                // --

                Query query = new Query();
                query.Criteria = CriteriaFactory.And(new Criteria[] { publicationCriteria, pageCriteria, vanityUrlCriteria });
                IEnumerable <string> pages = query.ExecuteQuery();

                //If no result, we try taking the ApplicationPath ['/en'] fro the url
                if (!pages.Any() && url.ToLower().StartsWith(request.ApplicationPath.ToLower()))
                {
                    var urlWithoutAppPath  = url.Substring(request.ApplicationPath.Length);
                    var vanityUrlCriteria2 = new CustomMetaValueCriteria(new CustomMetaKeyCriteria("SEOUrl", Criteria.Equal), urlWithoutAppPath);
                    query.Criteria = CriteriaFactory.And(new Criteria[] { publicationCriteria, pageCriteria, vanityUrlCriteria2 });
                    pages          = query.ExecuteQuery();

                    //If no result, we try taking the '/' from the begining of the url.
                    if (!pages.Any())
                    {
                        urlWithoutAppPath  = urlWithoutAppPath.TrimStart('/');
                        vanityUrlCriteria2 = new CustomMetaValueCriteria(new CustomMetaKeyCriteria("SEOUrl", Criteria.Equal), urlWithoutAppPath);
                        query.Criteria     = CriteriaFactory.And(new Criteria[] { publicationCriteria, pageCriteria, vanityUrlCriteria2 });
                        pages = query.ExecuteQuery();
                    }
                }

                if (pages.Any())
                {
                    pageMeta = pageMetaFactory.GetMeta(pages.First());
                }
            }
            return(pageMeta);
        }
Exemplo n.º 14
0
        public void TryRedirect(Object s, EventArgs e)
        {
            HttpContext context = HttpContext.Current;

            int    publicationId = int.Parse(ConfigurationManager.AppSettings["GlobalPubId"]);
            string redirectField = ConfigurationManager.AppSettings["RedirectUrlField"];

            string url = context.Request.Url.PathAndQuery;
            int    pos = url.IndexOf("?");

            if (pos > 0)
            {
                url = url.Substring(0, pos);
            }

            PublicationCriteria     publicationCriteria = new PublicationCriteria(publicationId);
            ItemTypeCriteria        itemCriteria        = new ItemTypeCriteria(64);
            CustomMetaValueCriteria cmvCriteria         = new CustomMetaValueCriteria(new CustomMetaKeyCriteria(redirectField), url);
            AndCriteria             andCriteria         = new AndCriteria(new Criteria[] { publicationCriteria, itemCriteria, cmvCriteria });

            Query query = new Query(andCriteria);

            string[] results = query.ExecuteQuery();

            if (results.Length > 0)
            {
                PageLink pageLink = new PageLink(publicationId);
                Link     link     = pageLink.GetLink(results[0]);

                if (link.IsResolved)
                {
                    // Redirect
                    HttpResponse response = context.Response;
                    response.Clear();
                    response.RedirectLocation  = link.Url;
                    response.StatusCode        = 301;
                    response.StatusDescription = "301 Moved Permanently";
                    response.Write("Page has moved to " + link.Url);
                    response.End();
                }
            }
        }
Exemplo n.º 15
0
        public void TryRedirect(Object s, EventArgs e)
        {
            HttpContext context = HttpContext.Current;

            int publicationId = int.Parse(ConfigurationManager.AppSettings["GlobalPubId"]);
            string redirectField = ConfigurationManager.AppSettings["RedirectUrlField"];

            string url = context.Request.Url.PathAndQuery;
            int pos = url.IndexOf("?");
            if (pos > 0) url = url.Substring(0, pos);

            PublicationCriteria publicationCriteria = new PublicationCriteria(publicationId);
            ItemTypeCriteria itemCriteria = new ItemTypeCriteria(64);
            CustomMetaValueCriteria cmvCriteria = new CustomMetaValueCriteria(new CustomMetaKeyCriteria(redirectField), url);
            AndCriteria andCriteria = new AndCriteria(new Criteria[] { publicationCriteria, itemCriteria, cmvCriteria });

            Query query = new Query(andCriteria);

            string[] results = query.ExecuteQuery();

            if (results.Length > 0)
            {
                PageLink pageLink = new PageLink(publicationId);
                Link link = pageLink.GetLink(results[0]);

                if (link.IsResolved)
                {
                    // Redirect
                    HttpResponse response = context.Response;
                    response.Clear();
                    response.RedirectLocation = link.Url;
                    response.StatusCode = 301;
                    response.StatusDescription = "301 Moved Permanently";
                    response.Write("Page has moved to " + link.Url);
                    response.End();
                }
            }
        }
        private string[] FindComponentUrisBySchemas(string[] schemaUris, int?limit, DateTime?sinceLastPublished)
        {
            TcmUri schemaTcmUri = new TcmUri(schemaUris.First());

            PublicationCriteria publicationCriteria = new PublicationCriteria(schemaTcmUri.PublicationId);

            Criteria[] schemaCriterias = schemaUris
                                         .Select(uri => new ItemSchemaCriteria(new TcmUri(uri).ItemId))
                                         .ToArray();

            Criteria basedOnSchema = CriteriaFactory.Or(schemaCriterias);
            Criteria basedOnSchemaAndInPublication = CriteriaFactory.And(publicationCriteria, basedOnSchema);

            if (sinceLastPublished.HasValue)
            {
                ItemLastPublishedDateCriteria dateLastPublished = new ItemLastPublishedDateCriteria(sinceLastPublished.Value.ToString("yyyy-MM-dd hh:mm:ss"), Criteria.GreaterThanOrEqual);
                basedOnSchemaAndInPublication = CriteriaFactory.And(basedOnSchemaAndInPublication, dateLastPublished);
            }

            Query q = new Query(basedOnSchemaAndInPublication);

            SortParameter sortParameter = new SortParameter(SortParameter.ItemLastPublishedDate, SortParameter.Descending);

            q.AddSorting(sortParameter);

            if (limit.HasValue && limit.Value > 0)
            {
                q.AddLimitFilter(new LimitFilter(limit.Value));
            }
            else
            {
                q.AddLimitFilter(new LimitFilter(maximumComponent));
            }

            return(q.ExecuteQuery());
        }
        /// <summary>
        /// Gets the raw string (xml) from the broker db by URL
        /// </summary>
        /// <param name="Url">URL of the page</param>
        /// <returns>String with page xml or empty string if no page was found</returns>
        public string GetContentByUrl(string Url)
        {

            LoggerService.Debug(">>GetContentByUrl({0})", LoggingCategory.Performance, Url);
            string retVal = string.Empty;

            LoggerService.Debug("GetContentByUrl: about to create query", LoggingCategory.Performance);
            using (var pageQuery = new Query())
            {
                LoggerService.Debug("GetContentByUrl: created query", LoggingCategory.Performance);
                using (var isPage = new ItemTypeCriteria(64))
                {
                    using (var pageUrl = new PageURLCriteria(Url))
                    {
                        using (var allCriteria = CriteriaFactory.And(isPage, pageUrl))
                        {
                            if (this.PublicationId != 0)
                            {
                                using (var correctSite = new PublicationCriteria(this.PublicationId))
                                {
                                    allCriteria.AddCriteria(correctSite);
                                }
                            }

                            pageQuery.Criteria = allCriteria;
                        }
                    }
                }
                LoggerService.Debug("GetContentByUrl: added criteria to query", LoggingCategory.Performance);

                LoggerService.Debug("GetContentByUrl: about to execute query", LoggingCategory.Performance);
                string[] resultUris = pageQuery.ExecuteQuery();
                pageQuery.Dispose();
                LoggerService.Debug("GetContentByUrl: executed query", LoggingCategory.Performance);


                if (resultUris.Length > 0)
                {
                    retVal = PageContentAssembler.GetContent(resultUris[0]);
                    LoggerService.Debug("GetContentByUrl: executed PageContentAssembler", LoggingCategory.Performance);
                }
                LoggerService.Debug("<<GetContentByUrl({0})", LoggingCategory.Performance, Url);
                return retVal;
            }
        }
        public bool TryFindBinary(string url, out IBinary binary)
        {
            string encodedUrl = HttpUtility.UrlPathEncode(url);
            binary = null;
            //using (var sqlBinMetaHome = new Com.Tridion.Broker.Binaries.Meta.SQLBinaryMetaHome())
            //{

            //    Com.Tridion.Meta.BinaryMeta binaryMeta = sqlBinMetaHome.FindByURL(PublicationId, encodedUrl); // "/Images/anubis_pecunia160_tcm70-520973.jpg"
            //    if (binaryMeta != null)
            //    {
            //        using (var sqlBinaryHome = new Com.Tridion.Broker.Binaries.SQLBinaryHome())
            //        {
            //            Com.Tridion.Data.BinaryData binData = sqlBinaryHome.FindByPrimaryKey(PublicationId, (int)binaryMeta.GetId());
            //            if (binData != null)
            //            {
            //                binary = new Binary(this)
            //                {
            //                    Id = String.Format("tcm:{0}-{1}", binData.GetPublicationId(), binData.GetId()),
            //                    Url = url,
            //                    LastPublishedDate = DateTime.Now,
            //                    Multimedia = null,
            //                    VariantId = binData.GetVariantId()
            //                };
            //                return true;
            //            }
            //        }
            //    }
            //    return false;
            //}

            Query binaryQuery = new Query();
            ItemTypeCriteria isMultiMedia = new ItemTypeCriteria(32);

            PublicationCriteria inPub = new PublicationCriteria(PublicationId);
            Criteria allCriteria = CriteriaFactory.And(isMultiMedia, inPub);

            string[] results =  binaryQuery.ExecuteQuery();
            binary = null;
            return true;

            foreach (var item in results) {

            }
        }
Exemplo n.º 19
0
        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;
        }
        /// <summary>
        /// Gets the raw string (xml) from the broker db by URL
        /// </summary>
        /// <param name="Url">URL of the page</param>
        /// <returns>String with page xml or empty string if no page was found</returns>
        private string GetStringContentFromBrokerByUrl(string Url)
        {
            string retVal = string.Empty;

            Query pageQuery = new Query();
            ItemTypeCriteria isPage = new ItemTypeCriteria(64);  // TODO There must be an enum of these somewhere
            PageURLCriteria pageUrl = new PageURLCriteria(Url);
            PublicationCriteria correctSite = new PublicationCriteria(PublicationId); //Todo: add logic to determine site on url

            Criteria allCriteria = CriteriaFactory.And(isPage, pageUrl);
            allCriteria.AddCriteria(correctSite);

            pageQuery.Criteria = allCriteria;

            string[] resultUris = pageQuery.ExecuteQuery();

            if (resultUris.Length > 0)
            {
                PageContentAssembler loadPage = new PageContentAssembler();
                retVal = loadPage.GetContent(resultUris[0]);
            }
            return retVal;
        }
        /// <summary>
        /// Gets the raw string (xml) from the broker db by URL
        /// </summary>
        /// <param name="Url">URL of the page</param>
        /// <returns>String with page xml or empty string if no page was found</returns>
        public string GetContentByUrl(string Url)
        {
            string retVal = string.Empty;

            Query pageQuery = new Query();
            ItemTypeCriteria isPage = new ItemTypeCriteria(64);  // TODO There must be an enum of these somewhere
            PageURLCriteria pageUrl = new PageURLCriteria(Url);

            Criteria allCriteria = CriteriaFactory.And(isPage, pageUrl);
            if (this.PublicationId != 0)
            {
                PublicationCriteria correctSite = new PublicationCriteria(this.PublicationId);
                allCriteria.AddCriteria(correctSite);
            }
            pageQuery.Criteria = allCriteria;

            string[] resultUris = pageQuery.ExecuteQuery();

            if (resultUris.Length > 0)
            {
                PageContentAssembler loadPage = new PageContentAssembler();
                retVal = loadPage.GetContent(resultUris[0]);
            }
            return retVal;
        }
        public DateTime LastPublished(string[] schemaUris)
        {
            TcmUri schemaTcmUri = new TcmUri(schemaUris.First());

            PublicationCriteria publicationCriteria = new PublicationCriteria(schemaTcmUri.PublicationId);

            Criteria[] schemaCriterias = schemaUris
                .Select(uri => new ItemSchemaCriteria(new TcmUri(uri).ItemId))
                .ToArray();

            Criteria basedOnSchema = CriteriaFactory.Or(schemaCriterias);
            Criteria basedOnSchemaAndInPublication = CriteriaFactory.And(publicationCriteria, basedOnSchema);

            Query q = new Query(basedOnSchemaAndInPublication);

            SortParameter sortParameter = new SortParameter(SortParameter.ItemLastPublishedDate, SortParameter.Descending);
            q.AddSorting(sortParameter);
            q.AddLimitFilter(new LimitFilter(1));

            string[] foundUris = q.ExecuteQuery();

            if (foundUris.Length > 0)
            {
                using (Tridion.ContentDelivery.Meta.ComponentMetaFactory fac = new Tridion.ContentDelivery.Meta.ComponentMetaFactory(schemaTcmUri.PublicationId))
                {
                    Tridion.ContentDelivery.Meta.IComponentMeta meta = fac.GetMeta(foundUris[0]);
                    return meta.LastPublicationDate;
                }
            }

            return DateTime.MinValue;
        }
        /// <summary>
        /// Gets the raw string (xml) from the broker db by URL
        /// </summary>
        /// <param name="Url">URL of the page</param>
        /// <returns>String with page xml or empty string if no page was found</returns>
        public string GetContentByUrl(string Url)
        {

            LoggerService.Debug(">>GetContentByUrl({0})", LoggingCategory.Performance, Url);
            string retVal = string.Empty;

            LoggerService.Debug("GetContentByUrl: about to create query", LoggingCategory.Performance);
            Query pageQuery = new Query();
            LoggerService.Debug("GetContentByUrl: created query", LoggingCategory.Performance);
            ItemTypeCriteria isPage = new ItemTypeCriteria(64);  // TODO There must be an enum of these somewhere
            PageURLCriteria pageUrl = new PageURLCriteria(Url);

            Criteria allCriteria = CriteriaFactory.And(isPage, pageUrl);
            if (this.PublicationId != 0)
            {
                PublicationCriteria correctSite = new PublicationCriteria(this.PublicationId);
                allCriteria.AddCriteria(correctSite);
            }
            pageQuery.Criteria = allCriteria;
            LoggerService.Debug("GetContentByUrl: added criteria to query", LoggingCategory.Performance);

            LoggerService.Debug("GetContentByUrl: about to execute query", LoggingCategory.Performance);
            string[] resultUris = pageQuery.ExecuteQuery();
            LoggerService.Debug("GetContentByUrl: executed query", LoggingCategory.Performance);


            if (resultUris.Length > 0)
            {
                retVal = PageContentAssembler.GetContent(resultUris[0]);
                LoggerService.Debug("GetContentByUrl: executed PageContentAssembler", LoggingCategory.Performance);
            }
            LoggerService.Debug("<<GetContentByUrl({0})", LoggingCategory.Performance, Url);
            return retVal;
        }
        private string[] FindComponentUrisBySchemas(string[] schemaUris, int? limit, DateTime? sinceLastPublished)
        {
            TcmUri schemaTcmUri = new TcmUri(schemaUris.First());

            PublicationCriteria publicationCriteria = new PublicationCriteria(schemaTcmUri.PublicationId);

            Criteria[] schemaCriterias = schemaUris
                .Select(uri => new ItemSchemaCriteria(new TcmUri(uri).ItemId))
                .ToArray();

            Criteria basedOnSchema = CriteriaFactory.Or(schemaCriterias);
            Criteria basedOnSchemaAndInPublication = CriteriaFactory.And(publicationCriteria, basedOnSchema);

            if (sinceLastPublished.HasValue)
            {
                ItemLastPublishedDateCriteria dateLastPublished = new ItemLastPublishedDateCriteria(sinceLastPublished.Value.ToString("yyyy-MM-dd hh:mm:ss"), Criteria.GreaterThanOrEqual);
                basedOnSchemaAndInPublication = CriteriaFactory.And(basedOnSchemaAndInPublication, dateLastPublished);
            }

            Query q = new Query(basedOnSchemaAndInPublication);

            SortParameter sortParameter = new SortParameter(SortParameter.ItemLastPublishedDate, SortParameter.Descending);
            q.AddSorting(sortParameter);

            if (limit.HasValue && limit.Value > 0)
            {
                q.AddLimitFilter(new LimitFilter(limit.Value));
            }
            else
            {
                q.AddLimitFilter(new LimitFilter(maximumComponent));
            }

            return q.ExecuteQuery();
        }