public static Dynamic.Component BuildComponent(TCM.Component tcmComponent, int linkLevels, bool resolveWidthAndHeight, bool publishEmptyFields, BuildManager manager)
        {
            GeneralUtils.TimedLog("start BuildComponent");
            Dynamic.Component c = new Dynamic.Component();
            c.Title        = tcmComponent.Title;
            c.Id           = tcmComponent.Id.ToString();
            c.RevisionDate = tcmComponent.RevisionDate;
            GeneralUtils.TimedLog("component title = " + c.Title);

            c.Version = tcmComponent.Version;
            GeneralUtils.TimedLog("start building schema");
            c.Schema = manager.BuildSchema(tcmComponent.Schema);
            GeneralUtils.TimedLog("finished building schema");

            c.ComponentType = (ComponentType)Enum.Parse(typeof(ComponentType), tcmComponent.ComponentType.ToString());

            if (tcmComponent.ComponentType.Equals(TCM.ComponentType.Multimedia))
            {
                GeneralUtils.TimedLog("start building multimedia");
                Multimedia multimedia = new Multimedia();
                multimedia.MimeType = tcmComponent.BinaryContent.MultimediaType.MimeType;
                multimedia.Size     = tcmComponent.BinaryContent.FileSize;
                multimedia.FileName = tcmComponent.BinaryContent.Filename;
                // remove leading dot from extension because microsoft returns this as ".gif"
                string extension = System.IO.Path.GetExtension(multimedia.FileName);
                if (string.IsNullOrEmpty(extension))
                {
                    multimedia.FileExtension = "";
                }
                else
                {
                    multimedia.FileExtension = extension.Substring(1);
                }

                if (resolveWidthAndHeight)
                {
                    try
                    {
                        MemoryStream memstream = new MemoryStream();
                        tcmComponent.BinaryContent.WriteToStream(memstream);
                        Image image = Image.FromStream(memstream);
                        memstream.Close();

                        multimedia.Width  = image.Size.Width;
                        multimedia.Height = image.Size.Height;
                    }
                    catch (Exception e)
                    {
                        log.Warning("error retrieving width and height of image: " + e.Message);
                        multimedia.Width  = 0;
                        multimedia.Height = 0;
                    }
                }
                else
                {
                    multimedia.Width  = 0;
                    multimedia.Height = 0;
                }
                c.Multimedia = multimedia;
                GeneralUtils.TimedLog("finished building multimedia");
            }
            else
            {
                c.Multimedia = null;
            }
            c.Fields         = new Dynamic.FieldSet();
            c.MetadataFields = new Dynamic.FieldSet();
            if (linkLevels > 0)
            {
                if (tcmComponent.Content != null)
                {
                    GeneralUtils.TimedLog("start retrieving tcm fields");
                    TCM.Fields.ItemFields tcmFields = new TCM.Fields.ItemFields(tcmComponent.Content, tcmComponent.Schema);
                    log.Debug("TCM fields" + tcmFields.ToXml());
                    GeneralUtils.TimedLog("finished retrieving tcm fields");
                    GeneralUtils.TimedLog("start building fields");
                    c.Fields = manager.BuildFields(tcmFields, linkLevels, resolveWidthAndHeight, publishEmptyFields);
                    GeneralUtils.TimedLog("finished building fields");
                }

                if (tcmComponent.Metadata != null)
                {
                    GeneralUtils.TimedLog("start retrieving tcm metadata fields");
                    TCM.Fields.ItemFields tcmMetadataFields = new TCM.Fields.ItemFields(tcmComponent.Metadata, tcmComponent.MetadataSchema);
                    GeneralUtils.TimedLog("finished retrieving tcm metadata fields");
                    GeneralUtils.TimedLog("start building metadata fields");
                    c.MetadataFields = manager.BuildFields(tcmMetadataFields, linkLevels, resolveWidthAndHeight, publishEmptyFields);
                    GeneralUtils.TimedLog("finished building metadata fields");
                }
            }
            c.Publication       = manager.BuildPublication(tcmComponent.ContextRepository);
            c.OwningPublication = manager.BuildPublication(tcmComponent.OwningRepository);
            TCM.Folder folder = (TCM.Folder)tcmComponent.OrganizationalItem;
            c.Folder     = manager.BuildOrganizationalItem(folder);
            c.Categories = manager.BuildCategories(tcmComponent);

            manager.AddXpathToFields(c.Fields, "tcm:Content/custom:" + tcmComponent.Schema.RootElementName); // TODO: check if the first part of the XPath is really the root element name, or simply always 'Content'
            manager.AddXpathToFields(c.MetadataFields, "tcm:Metadata/custom:Metadata");
            return(c);
        }
        /// <summary>
        /// Gets an imported template's content. Works with tcm uri's, web dav urls, or physical file paths.
        /// </summary>
        /// <param name="path">The path to check.</param>
        /// <param name="engine"></param>
        /// <returns></returns>
        private string GetImportTemplateContent(string path)
        {
            TcmUri templateID = new TcmUri(_templateID);

            if (path.ToLower().StartsWith("tcm:") || path.ToLower().StartsWith("/webdav/") || !path.Contains("\\"))
            {
                if (!path.ToLower().StartsWith("tcm:") && !path.ToLower().StartsWith("/webdav/"))
                {
                    path = GetRelativeImportPath(path);
                }

                TemplateBuildingBlock template;
                try
                {
                    template = Session.GetObject(path) as TemplateBuildingBlock;
                }
                catch (Exception ex)
                {
                    _logger.Warning("Error import of '" + path + "'. " + ex.ToString());
                    return(String.Empty);
                }

                if (template == null)
                {
                    _logger.Warning("Import of '" + path + "' not found.");
                    return(String.Empty);
                }

                _logger.Debug("Comaring import template " + template.Id + " to razor tbb ID " + templateID);
                // Get local copy of the imported template if possible.

                int publicationID = templateID.PublicationId;

                if (TcmUri.IsNullOrUriNull(templateID))
                {
                    // Is new item, so templateID is tcm:0-0-0.  We need to grab the pub id manually.
                    string[] webDav    = _webDavUrl.Split('/');
                    string   pubWebDav = "/webdav/" + webDav[2];

                    publicationID = Session.GetObject(pubWebDav).Id.ItemId;
                }

                if (template.Id.PublicationId != publicationID)
                {
                    // If import is from diff publication, try to grab local copy.
                    try
                    {
                        template = (TemplateBuildingBlock)Session.GetObject(TemplateUtilities.CreateTcmUriForPublication(publicationID, template.Id));
                    }
                    catch
                    {
                        _logger.Warning("Error trying to get local copy of template '" + template.Id + "' for Publication ID '" + publicationID + "'");
                    }
                }

                // Don't import itself
                if (template.Id.GetVersionlessUri().Equals(templateID.GetVersionlessUri()))
                {
                    return(String.Empty);
                }

                return(template.Content);
            }
            else
            {
                // If its a file path, get the contents of the file and return.
                return(File.ReadAllText(path));
            }
        }
Ejemplo n.º 3
0
 public void Warning(string message) => _logger.Warning(message);
        public static Dynamic.Component BuildComponent(TCM.Component tcmComponent, int currentLinkLevel, BuildManager manager)
        {
            log.Debug(string.Format("start BuildComponent with component {0} ({1}) and link level {2}", tcmComponent.Title, tcmComponent.Id, currentLinkLevel));
            Dynamic.Component c = new Dynamic.Component();
            c.Title        = tcmComponent.Title;
            c.Id           = tcmComponent.Id.ToString();
            c.RevisionDate = tcmComponent.RevisionDate;

            c.Version = tcmComponent.Version;
            c.Schema  = manager.BuildSchema(tcmComponent.Schema);

            c.ComponentType = (ComponentType)Enum.Parse(typeof(ComponentType), tcmComponent.ComponentType.ToString());

            if (tcmComponent.ComponentType.Equals(TCM.ComponentType.Multimedia))
            {
                Multimedia multimedia = new Multimedia();
                multimedia.MimeType = tcmComponent.BinaryContent.MultimediaType.MimeType;

                // PLEASE NOTE: this weird way to set the size of the multimedia is needed because of a difference between Tridion 2011 and 2013
                // The property in Tridion's BinaryContent class changed its name AND its type (int FileSize became long Size)
                // This way, we can use preprocessing to choose the right property
                // Thijs Borst and Quirijn Slings, 9 April 2015
#if Legacy
                PropertyInfo prop = tcmComponent.BinaryContent.GetType().GetProperty("FileSize", BindingFlags.Public | BindingFlags.Instance);
                multimedia.Size = Convert.ToInt64(prop.GetValue(tcmComponent.BinaryContent, null));
#else
                PropertyInfo prop = tcmComponent.BinaryContent.GetType().GetProperty("Size", BindingFlags.Public | BindingFlags.Instance);
                multimedia.Size = (long)prop.GetValue(tcmComponent.BinaryContent, null);
#endif
                multimedia.FileName = tcmComponent.BinaryContent.Filename;

                string extension = System.IO.Path.GetExtension(multimedia.FileName);
                if (string.IsNullOrEmpty(extension))
                {
                    multimedia.FileExtension = "";
                }
                else
                {
                    // remove leading dot from extension because microsoft returns this as ".gif"
                    multimedia.FileExtension = extension.Substring(1);
                }

                if (manager.BuildProperties.ResolveWidthAndHeight)
                {
                    try
                    {
                        MemoryStream memstream = new MemoryStream();
                        tcmComponent.BinaryContent.WriteToStream(memstream);
                        Image image = Image.FromStream(memstream);
                        memstream.Close();

                        multimedia.Width  = image.Size.Width;
                        multimedia.Height = image.Size.Height;
                    }
                    catch (Exception e)
                    {
                        log.Warning(string.Format("error retrieving width and height of image: is component with ID {0} really an image? Error message: {1}", c.Id, e.Message));
                        multimedia.Width  = 0;
                        multimedia.Height = 0;
                    }
                }
                else
                {
                    multimedia.Width  = 0;
                    multimedia.Height = 0;
                }
                c.Multimedia = multimedia;
                manager.PublishMultimediaComponent(c);
            }
            else
            {
                c.Multimedia = null;
            }
            c.Fields         = new Dynamic.FieldSet();
            c.MetadataFields = new Dynamic.FieldSet();
            if (currentLinkLevel > 0)
            {
                if (tcmComponent.Content != null)
                {
                    TCM.Fields.ItemFields tcmFields = new TCM.Fields.ItemFields(tcmComponent.Content, tcmComponent.Schema);
                    c.Fields = manager.BuildFields(tcmFields, currentLinkLevel);
                }

                if (tcmComponent.Metadata != null)
                {
                    TCM.Fields.ItemFields tcmMetadataFields = new TCM.Fields.ItemFields(tcmComponent.Metadata, tcmComponent.MetadataSchema);
                    c.MetadataFields = manager.BuildFields(tcmMetadataFields, currentLinkLevel);
                }
            }
            if (!manager.BuildProperties.OmitContextPublications)
            {
                c.Publication = manager.BuildPublication(tcmComponent.ContextRepository);
            }
            if (!manager.BuildProperties.OmitOwningPublications)
            {
                c.OwningPublication = manager.BuildPublication(tcmComponent.OwningRepository);
            }
            if (!manager.BuildProperties.OmitFolders)
            {
                TCM.Folder folder = (TCM.Folder)tcmComponent.OrganizationalItem;
                c.Folder = manager.BuildOrganizationalItem(folder);
            }
            if (!manager.BuildProperties.OmitCategories)
            {
                c.Categories = manager.BuildCategories(tcmComponent);
            }
            manager.AddXpathToFields(c.Fields, "tcm:Content/custom:" + tcmComponent.Schema.RootElementName);
            manager.AddXpathToFields(c.MetadataFields, "tcm:Metadata/custom:Metadata");
            return(c);
        }
Ejemplo n.º 5
0
        public static Dynamic.Component BuildComponent(TCM.Component tcmComponent, int currentLinkLevel, BuildManager manager)
        {
            log.Debug(string.Format("start BuildComponent with component {0} ({1}) and link level {2}", tcmComponent.Title, tcmComponent.Id, currentLinkLevel));
            Dynamic.Component c = new Dynamic.Component();
            c.Title        = tcmComponent.Title;
            c.Id           = tcmComponent.Id.ToString();
            c.RevisionDate = tcmComponent.RevisionDate;

            c.Version = tcmComponent.Version;
            c.Schema  = manager.BuildSchema(tcmComponent.Schema);

            c.ComponentType = (ComponentType)Enum.Parse(typeof(ComponentType), tcmComponent.ComponentType.ToString());

            if (tcmComponent.ComponentType.Equals(TCM.ComponentType.Multimedia))
            {
                Multimedia multimedia = new Multimedia();
                multimedia.MimeType = tcmComponent.BinaryContent.MultimediaType.MimeType;
                multimedia.Size     = tcmComponent.BinaryContent.Size;
                multimedia.FileName = tcmComponent.BinaryContent.Filename;

                string extension = System.IO.Path.GetExtension(multimedia.FileName);
                if (string.IsNullOrEmpty(extension))
                {
                    multimedia.FileExtension = "";
                }
                else
                {
                    // remove leading dot from extension because microsoft returns this as ".gif"
                    multimedia.FileExtension = extension.Substring(1);
                }

                if (manager.BuildProperties.ResolveWidthAndHeight)
                {
                    try
                    {
                        MemoryStream memstream = new MemoryStream();
                        tcmComponent.BinaryContent.WriteToStream(memstream);
                        Image image = Image.FromStream(memstream);
                        memstream.Close();

                        multimedia.Width  = image.Size.Width;
                        multimedia.Height = image.Size.Height;
                    }
                    catch (Exception e)
                    {
                        log.Warning(string.Format("error retrieving width and height of image: is component with ID {0} really an image? Error message: {1}", c.Id, e.Message));
                        multimedia.Width  = 0;
                        multimedia.Height = 0;
                    }
                }
                else
                {
                    multimedia.Width  = 0;
                    multimedia.Height = 0;
                }
                c.Multimedia     = multimedia;
                c.Multimedia.Url = manager.PublishMultimediaComponent(c);
            }
            else
            {
                c.Multimedia = null;
            }
            c.Fields         = new Dynamic.FieldSet();
            c.MetadataFields = new Dynamic.FieldSet();
            if (currentLinkLevel > 0)
            {
                if (tcmComponent.Content != null)
                {
                    TCM.Fields.ItemFields tcmFields = new TCM.Fields.ItemFields(tcmComponent.Content, tcmComponent.Schema);
                    c.Fields = manager.BuildFields(tcmFields, currentLinkLevel);
                }

                if (tcmComponent.Metadata != null)
                {
                    TCM.Fields.ItemFields tcmMetadataFields = new TCM.Fields.ItemFields(tcmComponent.Metadata, tcmComponent.MetadataSchema);
                    c.MetadataFields = manager.BuildFields(tcmMetadataFields, currentLinkLevel);
                }
            }
            if (!manager.BuildProperties.OmitContextPublications)
            {
                c.Publication = manager.BuildPublication(tcmComponent.ContextRepository);
            }
            if (!manager.BuildProperties.OmitOwningPublications)
            {
                c.OwningPublication = manager.BuildPublication(tcmComponent.OwningRepository);
            }
            if (!manager.BuildProperties.OmitFolders)
            {
                TCM.Folder folder = (TCM.Folder)tcmComponent.OrganizationalItem;
                c.Folder = manager.BuildOrganizationalItem(folder);
            }
            if (!manager.BuildProperties.OmitCategories)
            {
                c.Categories = manager.BuildCategories(tcmComponent);
            }
            manager.AddXpathToFields(c.Fields, "tcm:Content/custom:" + tcmComponent.Schema.RootElementName);
            manager.AddXpathToFields(c.MetadataFields, "tcm:Metadata/custom:Metadata");
            return(c);
        }
        /// <summary>
        /// Construct a filename for an item.
        /// </summary>
        /// <remarks>Based on the properties of the item. The filename property must be set, but
        /// there can be other aspects set on the item that are taken into account in the filename</remarks>
        /// <param name="item"></param>
        /// <returns></returns>
        public static string ConstructFileName(Item item)
        {
            IDictionary <string, string> properties = item.Properties;
            string fileName = properties[Item.ItemPropertyFileName];

            if (fileName == null)
            {
                log.Warning(String.Format("No filename set in property {0}", Item.ItemPropertyFileName));
                return(null);
            }

            // Handle prefix
            fileName = GetPropertyValue(item, Item.ItemPropertyFileNamePrefix) + fileName;

            // Handle subfolder (todo: fix this, ItemPropertyFileNameSubFolder does not exist!!
            //string subFolder = GetPropertyValue(item, Item.ItemPropertyFileNameSubFolder);
            //if (subFolder != "") {
            //    if (subFolder.StartsWith("/")) {
            //        // Strip of leading /
            //        subFolder = subFolder.Substring(1);
            //    }
            //    if (!subFolder.EndsWith(PathSeparator)) {
            //        // Ensure there is always a separator at the end
            //        subFolder += PathSeparator;
            //    }
            //    fileName = subFolder + fileName;
            //}

            // Handle extension
            int    extensionDotIndex   = fileName.LastIndexOf(".");
            string overriddenExtension = GetPropertyValue(item, Item.ItemPropertyFileNameExtension);

            if (overriddenExtension != "")
            {
                if (extensionDotIndex != -1)
                {
                    // replace extension, so strip current one first
                    fileName = fileName.Substring(0, extensionDotIndex);
                }
                // In all cases there will be an extension now
                extensionDotIndex = fileName.Length;

                fileName += "." + overriddenExtension;
            }

            // Handle filename suffix
            string suffix = GetPropertyValue(item, Item.ItemPropertyFileNameSuffix);

            if (suffix != "")
            {
                if (extensionDotIndex == -1)
                {
                    fileName += suffix;
                }
                else
                {
                    fileName = fileName.Substring(0, extensionDotIndex) + suffix + fileName.Substring(extensionDotIndex);
                }
            }

            return(fileName);
        }