public RuntimeContentModel ParseContent(RuntimeContentModel model)
        {
            var newContent = model.Content.ToDictionary(entry => entry.Key, entry => entry.Value);

            foreach (var property in model.Content)
            {
                if (property.Value is string && ((string)property.Value).Contains("{localLink:"))
                {
                    var localLinkRegex = new Regex(@"\/\{localLink\:(.*?)\}", RegexOptions.Compiled | RegexOptions.Multiline);
                    var value = (string) property.Value;

                    foreach (Match match in localLinkRegex.Matches(value))
                    {
                        var replacement = match.Value;
                        var docId = match.Groups[1].Value;

                        value = value.Replace(replacement, UrlForLocalLink(docId));
                    }

                    newContent[property.Key] = value;
                }
            }
            model.Content = newContent;
            return model;
        }
        private Document GetLuceneDocument(RuntimeContentModel content)
        {
            var d = new Document();

            d.Add(new Field("Url", content.Url, Field.Store.YES, Field.Index.NOT_ANALYZED));
            d.Add(new Field("Name", content.Name, Field.Store.YES, Field.Index.ANALYZED));
            
            d.Add(new Field("CreateDate", content.CreateDate.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            d.Add(new Field("UpdateDate", content.UpdateDate.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            
            d.Add(new Field("Type", content.Type, Field.Store.YES, Field.Index.NOT_ANALYZED));
            d.Add(new Field("CreatorName", content.CreatorName, Field.Store.YES, Field.Index.NOT_ANALYZED));
            d.Add(new Field("WriterName", content.WriterName, Field.Store.YES, Field.Index.NOT_ANALYZED));

            d.Add(new Field("RelativeUrl", content.RelativeUrl, Field.Store.YES, Field.Index.NOT_ANALYZED));
            d.Add(new Field("Template", content.Template, Field.Store.YES, Field.Index.NOT_ANALYZED));
            d.Add(new Field("SortOrder", content.SortOrder.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            d.Add(new Field("Level", content.Level.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));

            foreach (var property in content.Content)
            {
                var value = property.Value.ToString();
                value = StripHtml(value);

                d.Add(new Field(property.Key, value, Field.Store.YES, Field.Index.ANALYZED));
            }

            return d;
        }
        public override IEnumerable<RuntimeContentModel> Descendants(RuntimeContentModel model,
            IDictionary<string, string> filters)
        {
            var items = SearchService.Search(filters);
            var content = new List<RuntimeContentModel>();

            foreach (var url in items)
                content.Add(GetCachedContent(url));

            return content;
        }
        private RuntimeContentModel Build404Model(Uri url)
        {
            var homeUrl = url.Scheme + "://" + url.Host + ":" + url.Port + "/";
            var content = RuntimeContext.Instance.ContentService.GetContent(homeUrl);

            if (content == null)
            {
                content = new RuntimeContentModel();
                content.Name = "404 Not Found";
            }

            return content;
        }
        public RuntimeContentModel ParseContent(RuntimeContentModel model)
        {

            var newContent = model.Content.ToDictionary(entry => entry.Key, entry => entry.Value);
            foreach (var property in model.Content)
            {
                if (property.Value != null)
                    continue;

                newContent.Remove(property.Key);
                newContent.Add(property.Key, string.Empty);
            }
            model.Content = newContent;
            return model;
        }
        public void AddContent(RuntimeContentModel model)
        {
            var targetPath = PathMapper.PathForUrl(model.Url, true);
            var serialisedContent = JsonConvert.SerializeObject(model, Formatting.Indented);

            lock (_lock)
            {
                File.WriteAllText(targetPath, serialisedContent);

                if (!Urls.Contains(model.Url))
                    Urls.Add(model.Url);

            }
            FlushUrls();

            SearchService.Index(model);
      
        }
        public RuntimeContentModel ParseContent(RuntimeContentModel model)
        {
            var newContent = model.Content.ToDictionary(entry => entry.Key, entry => entry.Value);

            foreach (var property in model.Content)
            {
                if (property.Key != "umbracoNaviHide") continue;

                var v = property.Value;
                newContent.Remove(property.Key);

                var newValue = v.ToString() != "0";
                newContent.Add("HideInNavigation", newValue);
            }

            model.Content = newContent;
            return model;
        }
 protected string HomeUrl(RuntimeContentModel model)
 {
     var a = Urls.Where(x => model.Url.StartsWith(x)).OrderBy(x => x.Length);
     return a.First();
 }
 public void DeployContent(RuntimeContentModel model, DeploymentAction action)
 {
     
 }
 public override IEnumerable<RuntimeContentModel> Descendants(RuntimeContentModel model)
 {
     return FromUrls(DescendantsUrls(model)).Where(x => x != null); 
 }
 public override IEnumerable<RuntimeContentModel> Children(RuntimeContentModel model)
 {
     return FromUrls(ChildrenUrls(model)).Where(x => x != null); 
 }
 public override IEnumerable<RuntimeContentModel> TopNavigation(RuntimeContentModel model)
 {
     return FromUrls(TopNavigationUrls(model)).Where(x => x!= null);
 }
 public override RuntimeContentModel Home(RuntimeContentModel model)
 {
     return (GetCachedContent(HomeUrl(model)));
 }
 protected IEnumerable<string> ChildrenUrls(RuntimeContentModel model)
 {
     return Urls.Where(x => x.StartsWith(model.Url) && x != model.Url && x.Split('/').Length == model.Url.Split('/').Length + 1);
 }
 public virtual RuntimeContentModel Home(RuntimeContentModel model)
 {
     return GetContent(HomeUrl(model));
 }
 public void Index(RuntimeContentModel model)
 {
     if(Logger.IsDebugEnabled)
         Logger.Debug(JsonConvert.SerializeObject(model, Formatting.Indented));
 }
        public RuntimeContentModel CreateContent(string url, IDictionary<string, object> properties)
        {
            url = ProcessUrlAliases(url);

            var content = new RuntimeContentModel();

            content.Url = url;
            
            content.CreateDate = DateTime.Now;
            content.UpdateDate = DateTime.Now;
            
            content.CreatorName = "User Generated";
            content.WriterName = "User Generated";

            content.RelativeUrl = new Uri(url).AbsolutePath;

            content.Type = "UserGeneratedContent";
            content.Template = string.Empty;

            content.Content = properties;

            return content;
        }
        public virtual IEnumerable<RuntimeContentModel> Descendants(RuntimeContentModel model, IDictionary<string, string> filters)
        {
            var descendants = Descendants(model);
            var filteredDescendants = new List<RuntimeContentModel>();

            foreach (var descendant in descendants)
            {
                var include = true;

                foreach (var filter in filters)
                {
                    var key = filter.Key;
                    var value = filter.Value;

                    if (
                        (descendant.Content.ContainsKey(key) && descendant.Content[key] != value)
                        ||
                        (HasProperty(descendant, key) && GetPropertyValue(descendant, key) != value)
                        )
                    {
                        include = false;
                    }
                }

                if (include)
                {
                    filteredDescendants.Add(descendant);
                }
            }
            return filteredDescendants;
        }
 protected IEnumerable<string> DescendantsUrls(RuntimeContentModel model)
 {
     return Urls.Where(x => x.StartsWith(model.Url) && x != model.Url);
 }
        // For debugging!

        //public void FlushToDisc()
        //{
        //    var dir = new DirectoryInfo(@"C:\temp\index");
        //    foreach (var file in dir.GetFiles())
        //    {
        //        file.Delete();
        //    }

        //    var targetDir = FSDirectory.Open(dir);

        //    Directory.Copy(_directory, targetDir, false);
        //}

        public void Index(RuntimeContentModel model)
        {
            var doc = GetLuceneDocument(model);

            lock (_lock)
            {

                try
                {
                    _writer.DeleteDocuments(new Term("Url", doc.Get("Url")));
                    _writer.Commit();

                    _writer.AddDocument(doc);
                    _writer.Commit();

                    if (Logger.IsDebugEnabled)
                    {
                        Logger.Debug("Indexing: " + model.Name + " for search.");
                        // FlushToDisc();
                    }
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex);
                }
                //finally
                //{
                //    // _writer.Optimize();
                //}
            }
        }
        void PlaceInCache(string url, RuntimeContentModel content)
        {
            if (content == null)
                return;

            if(Logger.IsDebugEnabled)
                Logger.Debug("Caching: " + url);

            content.CacheTime = DateTime.Now;
            _customCache.Set(url, content, _policy);
        }
 public ActionResult TopNavigation(RuntimeContentModel viewModel)
 {
     return PartialView("~/Views/Partial/TopNavigation.cshtml", viewModel);
 }
 protected IEnumerable<string> TopNavigationUrls(RuntimeContentModel model)
 {
     return Urls.Where(x => x.Split('/').Length == 5 && x.StartsWith(model.Home().Url));
 }