/// <summary>Create missing mappings for a page.</summary>
        /// <return>The number of inserted mappings.</return>
        public int ProcessPage(Page page, Lang[] langs)
        {
            // don't process ignored pages
            if (page.IsIgnored) return 0;

            var maps = page.Mappings;
            var now = DateTime.UtcNow;

            var count = 0;

            // Creating the missing mappings
            foreach(var lang in langs.Where(x => !maps.Any(y => y.Code == x.Code)))
            {
                var mapping = new Mapping
                {
                    Code = lang.Code,
                    Created = now,
                    Page = page
                };
                page.Mappings.Add(mapping);
                Mappings.Create(mapping);

                count++;
            }

            return count;
        }
        /// <summary>Create missing mappings for a page.</summary>
        /// <return>The number of inserted mappings.</return>
        public int ProcessPage(Page page)
        {
            // TODO: performance is really poor, to be redesigned (see also below)
            var langs = Langs.List().ToArray();

            return ProcessPage(page, langs);
        }
        public void Create(Page page)
        {
            // HACK: should be using database-side value generation?
            page.Created = DateTime.UtcNow;
            page.LastUpdated = page.Created;

            using (var trans = Session.BeginTransaction())
            {
                Session.Save(page);
                trans.Commit();
            }
        }
        public void EditFull(long id, Page page)
        {
            using(var trans = Session.BeginTransaction())
            {
                var dbPage = Session.Get<Page>(id);

                dbPage.LastUpdated = page.LastUpdated;
                dbPage.IsIgnored = page.IsIgnored;
                dbPage.Url = page.Url;

                Session.Update(dbPage);
                trans.Commit();
            }
        }
        public void Edit(long id, Page page)
        {
            using (var trans = Session.BeginTransaction())
            {
                var dbPage = Session.Get<Page>(id);

                // don't change 'LastUpdated' (on purpose)
                dbPage.IsIgnored = page.IsIgnored;
                dbPage.Url = page.Url;

                Session.Update(dbPage);
                trans.Commit();
            }
        }
        /// <summary>Create missing mappings.</summary>
        /// <return>The number of inserted mappings.</return>
        public void ProcessPage(Page page)
        {
            var langs = Langs.List().ToArray();

            var maps = page.Mappings;
            var now = DateTime.UtcNow;

            foreach (var lang in langs.Where(x => !maps.Any(y => y.Code == x.Code)))
            {
                var mapping = new Mapping
                  	{
                  		Code = lang.Code,
                  		Created = now,
                  		Page = page
                  	};
                page.Mappings.Add(mapping);

                Mappings.Create(mapping);
            }
        }
        /// <summary>Retrieves all the udaptes and
        /// inserts the new pages.</summary>
        /// <returns>Number of updated pages.</returns>
        public int ProcessFeed(long feedId)
        {
            var feed = Feeds.Edit(feedId);

            var doc = new XPathDocument(feed.Url);

            // HACK: parsing should be made more fault tolerant

            // TODO: using 2 iterators is NOT the way to go.

            var nav = doc.CreateNavigator();
            var iter1 = nav.Select("/rss/channel/item/link");
            var iter2 = nav.Select("/rss/channel/item/pubDate");

            var feedItems = new List<FeedItem>();
            while (iter1.MoveNext())
            {
                iter2.MoveNext();

                feedItems.Add(new FeedItem
                    {
                        Link = iter1.Current.Value,
                        PubDate = DateTime.Parse(iter2.Current.Value)
                    });
            };

            foreach(var item in feedItems)
            {
                var page = Pages.Find(item.Link);

                if (null == page)
                {
                    var now = DateTime.UtcNow;
                    page = new Page
                           	{
                           		Created = now,
                           		LastUpdated = item.PubDate,
                           		Url = item.Link
                           	};

                    Pages.Create(page);
                }
                else
                {
                    page.LastUpdated = item.PubDate;
                    Pages.EditFull(page.Id, page);
                }
            }

            return feedItems.Count();
        }
 public ActionResult Edit(long id, Page page)
 {
     _pages.Edit(id, page);
     return RedirectToAction("Index");
 }