/// <summary>
 /// Order by priority of condition
 /// </summary>
 public static int ProcessingPriorityOrder(Mapping m)
 {
     if (string.IsNullOrEmpty(m.DestinationUrl)) return 0;
     if (string.IsNullOrEmpty(m.Version)) return 1;
     if (m.Page.LastUpdated > m.LastUpdated) return 2;
     return 3;
 }
        /// <summary>Create all missing mappings.</summary>
        /// <returns>The number of new mappings.</returns>
        public int ProcessAll()
        {
            var count = 0;

            // TODO: performance is really poor, to be redesigned
            var langs = Langs.List().ToArray();

            var now = DateTime.UtcNow;

            foreach(var page in Pages.List())
            {
                var mapLangs = new List<string>();

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

                    count++;
                }
            }

            return count;
        }
        public void Create(Mapping mapping)
        {
            mapping.Created = DateTime.UtcNow;
            mapping.LastUpdated = new DateTime(2001,1,1);

            using (var trans = Session.BeginTransaction())
            {
                Session.Save(mapping);
                trans.Commit();
            }
        }
        public Mapping Edit(long id, Mapping mapping)
        {
            using (var trans = Session.BeginTransaction())
            {
                var dbMapping = Session.Get<Mapping>(id);
                dbMapping.DestinationUrl = mapping.DestinationUrl;

                Session.Update(dbMapping);
                trans.Commit();

                return dbMapping;
            }
        }
        /// <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);
            }
        }
 public void Update(Mapping mapping)
 {
     using (var trans = Session.BeginTransaction())
     {
         Session.Update(mapping);
         trans.Commit();
     }
 }
        /// <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;
        }
 public ActionResult Edit(long id, Mapping mapping)
 {
     var dbMapping = Mappings.Edit(id, mapping);
     return RedirectToAction("List", new {id = dbMapping.Code});
 }