示例#1
0
        public GCAccountSettings GetAccountSettings()
        {
            var accountSettingItem = ContextDatabase.GetItem(Constants.AccountItemId, ContextLanguage);

            if (accountSettingItem != null)
            {
                var tenantName  = string.Empty;
                var tenantMatch = System.Text.RegularExpressions.Regex.Match(accountSettingItem["Platform Url"], @"^(http(s)?:\/\/)?(?<tenant>.*)\.gathercontent\.com(\/)?$");
                if (tenantMatch.Groups["tenant"] != null)
                {
                    tenantName = tenantMatch.Groups["tenant"].Value.ToLower();
                }
                return(new GCAccountSettings
                {
                    TenantName = tenantName,
                    ApiUrl = "https://api.gathercontent.com/",                     //hardcoded value requested by client
                    Username = accountSettingItem["Account Owner Email Address"],
                    ApiKey = accountSettingItem["API Key"],
                    TemplateFolderId = accountSettingItem["Templates Root Folder"],
                    DateFormat = accountSettingItem["Date Format"],
                    DateTimeParseFormat = accountSettingItem["DateTime Field Format"],
                    GatherContentUrl = accountSettingItem["Platform Url"],
                    DropTreeHomeNode = accountSettingItem["DropTree Home Node"],
                    AccountItemId = accountSettingItem.ID.ToString()
                });
            }

            return(null);
        }
        public void CleanUp(string targetPath)
        {
            var mediaPath = ContextDatabase.GetItem(targetPath);

            if (mediaPath != null)
            {
                var allDescendants = mediaPath.Axes.GetDescendants();
                if (allDescendants != null && allDescendants.Any())
                {
                    foreach (var item in allDescendants)
                    {
                        Globals.LinkDatabase.UpdateItemVersionReferences(item);
                        var  itemLisnks    = Globals.LinkDatabase.GetReferrers(item);
                        bool anyLinkedItem = itemLisnks.Any(l => l.TargetItemID.ToString().ToLower() == item.ID.ToString().ToLower());
                        if (!anyLinkedItem)
                        {
                            using (new SecurityDisabler())
                            {
                                item.Delete();
                            }
                        }
                    }
                }
            }
        }
示例#3
0
        /// <summary>
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="cmsItem"></param>
        /// <returns></returns>
        public string GetItemId(string parentId, CmsItem cmsItem)
        {
            if (parentId == null)
            {
                return null;
            }

            using (new SecurityDisabler())
            {
                using (new LanguageSwitcher(cmsItem.Language))
                {
                    var validName = cmsItem.Title.ProposeValidItemName();
                    var parent = ContextDatabase.GetItem(new ID(parentId));
                    if (parent != null)
                    {
                        var items = parent.Axes.SelectItems(string.Format("./*[@@name='{0}']", validName));
                        if (items != null && items.Any())
                        {
                            return items.First().ID.ToString();
                        }
                    }
                }
            }

            return null;
        }
示例#4
0
 /// <summary>
 /// </summary>
 /// <param name="parentId"></param>
 /// <param name="cmsItem"></param>
 /// <returns></returns>
 public string CreateNotMappedItem(string parentId, CmsItem cmsItem)
 {
     if (parentId != null)
     {
         using (new SecurityDisabler())
         {
             using (new LanguageSwitcher(cmsItem.Language))
             {
                 var templateItem = ContextDatabase.GetItem("/sitecore/templates/GatherContent/Not Mapped Item");
                 if (templateItem != null)
                 {
                     var template = ContextDatabase.GetTemplate(new ID(templateItem.ID.Guid));
                     var validName = cmsItem.Title.ProposeValidItemName();
                     var parent = ContextDatabase.GetItem(new ID(parentId));
                     if (parent != null)
                     {
                         try
                         {
                             var createdItem = parent.Add(validName, template);
                             return createdItem.ID.ToString();
                         }
                         catch (Exception)
                         {
                             throw new Exception(string.Format("Item cannot be created: parent='{0}l cmsItem.Title='{1}'", parent.Name, cmsItem.Title));
                         }
                     }
                 }
             }
         }
     }
     return null;
 }
示例#5
0
        /// <summary>
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="cmsItem"></param>
        /// <param name="mappingId"></param>
        /// <param name="gcPath"></param>
        public string AddNewVersion(string parentId, CmsItem cmsItem, string mappingId, string gcPath)
        {
            if (parentId == null)
            {
                return null;
            }

            using (new SecurityDisabler())
            {
                using (new LanguageSwitcher(cmsItem.Language))
                {
                    var validName = cmsItem.Title.ProposeValidItemName();
                    var parent = ContextDatabase.GetItem(new ID(parentId));
                    if (parent == null)
                    {
                        return null;
                    }

                    var items = parent.Axes.SelectItems(string.Format("./*[@@name='{0}']", validName));
                    if (items == null)
                    {
                        return null;
                    }

                    items = items.Where(item => item["GCPath"] == gcPath).ToArray();

                    foreach (var item in items)
                    {
                        var newVersion = item.Versions.AddVersion();

                        try
                        {
                            EnsureMetaTemplateInherited(newVersion.Template);
                            var idField = cmsItem.Fields.FirstOrDefault(f => f.TemplateField.FieldName == GcContentId);
                            if (idField != null)
                            {
                                newVersion.Editing.BeginEdit();

                                newVersion.Fields[GcContentId].Value = idField.Value.ToString();
                                var isoDate = DateUtil.ToIsoDate(DateTime.UtcNow);
                                newVersion.Fields[LastSyncDate].Value = isoDate;
                                newVersion.Fields[MappingId].Value = mappingId;
                                newVersion.Fields[GcPath].Value = gcPath;

                                newVersion.Editing.EndEdit();
                            }
                            return newVersion.ID.ToString();
                        }
                        catch (Exception ex)
                        {
                            Log.Error("cannot update mapped item.", ex, this);
                            throw new Exception(string.Format("Your template({0}) is not inherited from the GC Linked Item.", newVersion.TemplateName));
                        }
                    }
                }
            }
            return null;
        }
示例#6
0
        protected override IEnumerable <Item> GetJobItems(ID rootFolderId)
        {
            var folder = ContextDatabase?.GetItem(rootFolderId);

            if (folder == null)
            {
                return(Enumerable.Empty <Item>());
            }

            return(folder.Axes.GetDescendants().Where(i =>
                                                      TemplateManager.IsFieldPartOfTemplate(SitecronConstants.SiteCronFieldIds.CronExpression, i)));
        }
示例#7
0
        public void ExpandLinksInText(string cmsRootId, bool includeDescendants)
        {
            Item root = ContextDatabase.GetItem(ID.Parse(cmsRootId));

            if (root == null)
            {
                return;
            }

            var baseUrl = _accountsRepository.GetAccountSettings().GatherContentUrl;
            string pattern = StringUtil.EnsurePostfix('/', baseUrl.Replace("/", "\\/")) + "item\\/";
            Regex rgx = new Regex(pattern + "(\\d+)");

            ExpandLinksInTextRecursive(root, rgx, pattern, includeDescendants);
        }
示例#8
0
        public string AddNewVersion(CmsItem cmsItem)
        {
            if (cmsItem == null)
            {
                return null;
            }

            using (new SecurityDisabler())
            {
                using (new LanguageSwitcher(cmsItem.Language))
                {
                    var item = ContextDatabase.GetItem(new ID(cmsItem.Id));
                    var newVersion = item?.Versions.AddVersion();
                    return newVersion?.ID.ToString();
                }
            }
        }
        private List <Item> GetSolrJobs()
        {
            List <Item> siteCronItems = new List <Item>();

            try
            {
                var folder = ContextDatabase?.GetItem(SitecronConstants.ItemIds.RootFolderID);
                if (folder == null)
                {
                    return(siteCronItems);
                }
                List <ID> validTemplates          = new List <ID>();
                string[]  configSiteCronTemplates = Settings.GetSetting(SitecronConstants.SettingsNames.SiteCronValidTemplates, "").Trim().Replace(" ", "").Split(new char[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);

                foreach (string templateId in configSiteCronTemplates)
                {
                    ID testId;
                    if (ID.TryParse(templateId, out testId))
                    {
                        validTemplates.Add(testId);
                    }
                }

                if (validTemplates.Any())
                {
                    var index = ContentSearchManager.GetIndex(Settings.GetSetting(SitecronConstants.SettingsNames.SiteCronGetItemsIndex, "sitecore_master_index").Trim());
                    if (index != null)
                    {
                        using (var context = index.CreateSearchContext())
                        {
                            var query = PredicateBuilder.True <SearchResultItem>();
                            query = query.And(i => i.Paths.Contains(folder.ID));
                            query = query.And(i => validTemplates.Contains(i.TemplateId));

                            var results = context.GetQueryable <SearchResultItem>().Where(query).GetResults();
                            siteCronItems.AddRange(results.Select(i => i.Document.GetItem()));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("SiteCron IndexJobProvider ERROR: " + ex.Message, ex, this);
            }
            return(siteCronItems);
        }
示例#10
0
        /// <summary>
        /// </summary>
        /// <param name="itemId"></param>
        /// <param name="language"></param>
        /// <param name="readAllFields"></param>
        /// <returns></returns>
        public CmsItem GetItem(string itemId, string language, bool readAllFields = false)
        {
            var item = ContextDatabase.GetItem(itemId);

            if (item != null)
            {
                var cmsItem = new CmsItem
                {
                    Id       = item.ID.ToString(),
                    Title    = item.Name,
                    Language = item.Language.ToString(),
                };

                cmsItem.Fields.Add(new CmsField
                {
                    TemplateField = new CmsTemplateField {
                        FieldName = "GC Content Id"
                    },
                    Value = item[GcContentId]
                });

                cmsItem.Fields.Add(new CmsField
                {
                    TemplateField = new CmsTemplateField {
                        FieldName = "Last Sync Date"
                    },
                    Value = DateUtil.IsoDateToDateTime(item[LastSyncDate])
                });

                cmsItem.Fields.Add(new CmsField
                {
                    TemplateField = new CmsTemplateField {
                        FieldName = "Template"
                    },
                    Value = item.TemplateName
                });


                if (readAllFields)
                {
                    //cmsItem.Fields
                }
                return(cmsItem);
            }
            return(null);
        }
 /// <summary>
 /// </summary>
 /// <param name="parentId"></param>
 /// <param name="cmsItem"></param>
 /// <param name="mappingId"></param>
 /// <param name="gcPath"></param>
 public string CreateMappedItem(string parentId, CmsItem cmsItem, string mappingId, string gcPath)
 {
     if (parentId != null)
     {
         using (new SecurityDisabler())
         {
             using (new LanguageSwitcher(cmsItem.Language))
             {
                 using (new EnforceVersionPresenceDisabler())
                 {
                     var template  = ContextDatabase.GetTemplate(new ID(cmsItem.Template.TemplateId));
                     var validName = ItemUtil.ProposeValidItemName(cmsItem.Title);
                     var parent    = ContextDatabase.GetItem(new ID(parentId));
                     if (parent != null)
                     {
                         var createdItem = parent.Add(validName, template);
                         Log.Error("createdItem is null:" + (createdItem == null), this);
                         try
                         {
                             EnsureMetaTemplateInherited(createdItem.Template);
                             var idField = cmsItem.Fields.FirstOrDefault(f => f.TemplateField.FieldName == GcContentId);
                             if (idField != null)
                             {
                                 createdItem.Editing.BeginEdit();
                                 createdItem.Fields[GcContentId].Value = idField.Value.ToString();
                                 var isoDate = DateUtil.ToIsoDate(DateTime.UtcNow);
                                 createdItem.Fields[LastSyncDate].Value = isoDate;
                                 createdItem.Fields[MappingId].Value    = mappingId;
                                 createdItem.Fields[GcPath].Value       = gcPath;
                                 createdItem.Editing.EndEdit();
                             }
                             return(createdItem.ID.ToString());
                         }
                         catch (Exception ex)
                         {
                             Log.Error("cannot create mapped item.", ex, this);
                             throw new Exception(string.Format("Your template({0}) is not inherited from the GC Linked Item.", createdItem.TemplateName));
                         }
                     }
                 }
             }
         }
     }
     return(null);
 }
        public GCAccountSettings GetAccountSettings()
        {
            var accountSettingItem = ContextDatabase.GetItem(Constants.AccountItemId, ContextLanguage);

            if (accountSettingItem != null)
            {
                return(new GCAccountSettings
                {
                    ApiUrl = "https://api.gathercontent.com/", //hardcoded value requested by client
                    Username = accountSettingItem["Account Owner Email Address"],
                    ApiKey = accountSettingItem["API Key"],
                    TemplateFolderId = accountSettingItem["Templates Root Folder"],
                    DateFormat = accountSettingItem["Date Format"],
                    GatherContentUrl = accountSettingItem["Platform Url"],
                    DropTreeHomeNode = accountSettingItem["DropTree Home Node"],
                    AccountItemId = accountSettingItem.ID.ToString()
                });
            }

            return(null);
        }
示例#13
0
 /// <summary>
 /// </summary>
 /// <param name="parentId"></param>
 /// <param name="cmsItem"></param>
 /// <returns></returns>
 public bool IfNotMappedItemExists(string parentId, CmsItem cmsItem)
 {
     if (parentId != null)
     {
         using (new SecurityDisabler())
         {
             using (new LanguageSwitcher(cmsItem.Language))
             {
                 var validName = cmsItem.Title.ProposeValidItemName();
                 var parent = ContextDatabase.GetItem(new ID(parentId));
                 if (parent != null)
                 {
                     var items = parent.Axes.SelectItems(string.Format("./*[@@name='{0}']", validName));
                     if (items != null && items.Any())
                     {
                         return true;
                     }
                 }
             }
         }
     }
     return false;
 }
示例#14
0
 /// <summary>
 /// </summary>
 /// <param name="parentId"></param>
 /// <param name="cmsItem"></param>
 /// <param name="mappingId"></param>
 /// <param name="gcPath"></param>
 /// <returns></returns>
 public bool IfMappedItemExists(string parentId, CmsItem cmsItem, string mappingId, string gcPath)
 {
     if (parentId != null)
     {
         using (new SecurityDisabler())
         {
             using (new LanguageSwitcher(cmsItem.Language))
             {
                 var validName = ItemUtil.ProposeValidItemName(cmsItem.Title);
                 var parent    = ContextDatabase.GetItem(new ID(parentId));
                 if (parent != null)
                 {
                     var items = parent.Axes.SelectItems(string.Format("./*[@@name='{0}']", validName));
                     if (items != null && items.Any(item => item["GCPAth"] == gcPath))
                     {
                         return(true);
                     }
                 }
             }
         }
     }
     return(false);
 }
示例#15
0
        protected override IEnumerable <Item> GetJobItems(ID rootFolderId)
        {
            List <Item> siteCronItems = new List <Item>();

            var folder = ContextDatabase?.GetItem(rootFolderId);

            if (folder == null)
            {
                return(Enumerable.Empty <Item>());
            }

            var autoFolderItems = folder.Axes.GetDescendants().Where(i =>
                                                                     TemplateManager.IsFieldPartOfTemplate(SitecronConstants.SiteCronFieldIds.CronExpression, i));

            if (autoFolderItems != null && autoFolderItems.Any())
            {
                siteCronItems.AddRange(autoFolderItems);
            }

            var solrItems = GetSolrJobs();

            if (!solrItems.Any())
            {
                //ERROR: There is no method 'GetResults' on type 'Sitecore.ContentSearch.Linq.QueryableExtensions'
                Log.Warn("SiteCron IndexJobProvider got no results. Trying again assuming its Solr Initialization issue.", this);
                //might be caused due to solr not initializing
                System.Threading.Thread.Sleep(3000);
                solrItems = GetSolrJobs();
            }

            if (solrItems != null && solrItems.Any())
            {
                siteCronItems.AddRange(solrItems.Where(i => !siteCronItems.Contains(i)));
            }

            return(siteCronItems);
        }