private static string GetContainerTcmId(MappingInfo mapping, TridionRole tridionRole, ProjectFileInfo file)
 {
     ProjectFolderInfo folder = file.GetTopFolder();
     return GetContainerTcmId(mapping, tridionRole, folder, file.Path);
 }
        public static void ProcessFile(MappingInfo mapping, ProjectFileInfo file)
        {
            if (file == null)
                return;

            ShowMessage(file.Path + "...");

            if (!file.IsChecked() && !String.IsNullOrEmpty(file.TcmId))
                return;

            if (file.Checked != true)
                return;

            ProjectFolderRole role = file.ProjectFolderRole();
            bool syncTemplate = file.IsSyncTemplate();
            string templateFormat = file.TemplateFormat();
            List<string> schemaNames = file.SchemaNames;

            string titleItem = String.IsNullOrEmpty(file.Title) ? Path.GetFileNameWithoutExtension(file.FullPath) : file.Title;

            string tcmItemContainer = String.Empty;
            string tcmItem = String.Empty;

            string titleTemplate = String.Empty;
            string tcmTemplateContainer = String.Empty;

            TridionRole tridionRole = TridionRole.Other;

            if (role == ProjectFolderRole.PageLayout)
            {
                tridionRole = TridionRole.PageLayoutContainer;

                tcmItemContainer = GetContainerTcmId(mapping, TridionRole.PageLayoutContainer, file);
                tcmItem = String.IsNullOrEmpty(file.TcmId) ? GetItemTcmId(mapping, tcmItemContainer, titleItem) : file.TcmId;

                titleTemplate = String.IsNullOrEmpty(file.TemplateTitle) ? Path.GetFileNameWithoutExtension(file.FullPath) : file.TemplateTitle;
                tcmTemplateContainer = GetContainerTcmId(mapping, TridionRole.PageTemplateContainer, file.Path);
            }

            if (role == ProjectFolderRole.ComponentLayout)
            {
                titleItem = String.IsNullOrEmpty(file.Title) ? Path.GetFileNameWithoutExtension(file.FullPath) : file.Title;

                tridionRole = TridionRole.ComponentLayoutContainer;

                tcmItemContainer = GetContainerTcmId(mapping, TridionRole.ComponentLayoutContainer, file);
                tcmItem = String.IsNullOrEmpty(file.TcmId) ? GetItemTcmId(mapping, tcmItemContainer, titleItem) : file.TcmId;

                titleTemplate = String.IsNullOrEmpty(file.TemplateTitle) ? Path.GetFileNameWithoutExtension(file.FullPath) : file.TemplateTitle;
                tcmTemplateContainer = GetContainerTcmId(mapping, TridionRole.ComponentTemplateContainer, file.Path);
            }

            if (role == ProjectFolderRole.Binary)
            {
                tridionRole = TridionRole.MultimediaComponentContainer;

                tcmItemContainer = GetContainerTcmId(mapping, TridionRole.MultimediaComponentContainer, file);

                tcmItem = file.TcmId;
                if (String.IsNullOrEmpty(tcmItem))
                    tcmItem = GetItemTcmId(mapping, tcmItemContainer, Path.GetFileName(file.Path));
                if (String.IsNullOrEmpty(tcmItem))
                    tcmItem = GetItemTcmId(mapping, tcmItemContainer, Path.GetFileNameWithoutExtension(file.Path));
            }

            if (tridionRole == TridionRole.Other)
            {
                WriteErrorLog(String.Format("Role for item \"{0}\" is not detected", titleItem));
                ShowMessage(String.Empty);
                return;
            }

            if (String.IsNullOrEmpty(tcmItemContainer))
            {
                WriteErrorLog(String.Format("Could not find Tridion mapping for role {0}", tridionRole));
                ShowMessage(String.Empty);
                return;
            }

            //save (back) to file object
            file.TcmId = tcmItem;
            file.Title = titleItem;
            file.TemplateTitle = titleTemplate;

            //create sub-folder if file is not direct descedent
            if (file.Parent.FullPath.Trim('\\') != file.GetTopFolder().FullPath.Trim('\\'))
            {
                //get low-level folder
                tcmItemContainer = CreateFolderChain(mapping, file.Parent.FullPath.Trim('\\').Replace(file.GetTopFolder().FullPath.Trim('\\'), "").Trim('\\'), tcmItemContainer);
            }

            if (role == ProjectFolderRole.Binary)
            {
                SyncMultimedia(mapping, file, tcmItemContainer, mapping.TimeZoneId);
            }
            else
            {
                SyncRazorLayoutTbb(mapping, tcmItemContainer, file, role, mapping.TimeZoneId);
            }

            if (syncTemplate)
            {
                tcmItem = file.TcmId;

                //take template name from gialog
                if (!String.IsNullOrEmpty(file.TemplateTitle))
                    titleTemplate = file.TemplateTitle;

                if (String.IsNullOrEmpty(titleTemplate))
                    titleTemplate = titleItem;

                if(!String.IsNullOrEmpty(tcmItem) && !String.IsNullOrEmpty(tcmTemplateContainer) && !ExistsItem(mapping, tcmTemplateContainer, titleTemplate))
                {
                    if (role == ProjectFolderRole.PageLayout)
                    {
                        string stackTraceMessage;
                        bool res = SavePageTemplate(mapping, titleTemplate, String.Format(templateFormat, tcmItem, titleItem), tcmTemplateContainer, "cshtml", out stackTraceMessage);
                        if (!res)
                        {
                            WriteErrorLog(titleTemplate + " - Saving failed", stackTraceMessage);
                        }
                    }
                    if (role == ProjectFolderRole.ComponentLayout)
                    {
                        string stackTraceMessage;
                        bool res = SaveComponentTemplate(mapping, titleTemplate, String.Format(templateFormat, tcmItem, titleItem), tcmTemplateContainer, "HTML Fragment", false, out stackTraceMessage, schemaNames == null ? null : schemaNames.ToArray());
                        if (!res)
                        {
                            WriteErrorLog(titleTemplate + " - Saving failed", stackTraceMessage);
                        }
                    }
                }
            }

            ShowMessage(String.Empty);
        }