/// <summary>
        /// Updates the resource in the database
        /// </summary>
        /// <param name="id"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="languageID"></param>
        /// <param name="route"></param>
        /// <param name="type"></param>
        public static void UpdateResourceString(int id, string key, string value, int languageID, string route, ResourceType type)
        {
            var repo = new ResourceRepository();

            var resource = repo.GetResourceByID(id);
            if (resource.Key != key)
            {
                RouteResourceProviderFactory.RemoveKey(route, key);
            }
            if (resource.LanguageID == languageID)
            {
                repo.UpdateResource(id, key, value, languageID, route, type);
            }
            else
            {
                var lanResource = repo.GetResource(key, route, languageID);
                if (lanResource != null)
                {
                    repo.UpdateResource(lanResource.ID, key, value, languageID, route, type);
                }
                else
                {
                    repo.AddResource(key, value, languageID, route, type);
                }
            }
        }
 /// <summary>
 /// Deletes the resource from the database
 /// </summary>
 /// <param name="id"></param>
 public static void DeleteResourceString(int id)
 {
     var repo = new ResourceRepository();
     var resource = repo.GetResourceByID(id);
     RouteResourceProviderFactory.RemoveKey(resource.Route, resource.Key);
     repo.DeleteResource(id);
 }
 public virtual JsonResult Get(int id)
 {
     var repo = new ResourceRepository();
     var resource = repo.GetResourceByID(id) ?? new Resource { Key = "Missing resource!!!", ResourceType = ResourceType.Text, Route = Resource.ROUTE_SEPARATOR, Value = "" };
     var model = new RouteResourceModel()
         {
             ResourceID = resource.ID,
             ResourceKey = resource.Key,
             ResourceRoute = resource.Route,
             ResourceType = (int) resource.ResourceType,
             Value = resource.Value
         };
     return Json(model, JsonRequestBehavior.AllowGet);
 }
 public virtual JsonResult Get(int id)
 {
     var repo = new ResourceRepository();
     var resource = repo.GetResourceByID(id) ??
                    new Resource { Key = "Missing resource!!!", ResourceType = ResourceType.Text, Route = Resource.ROUTE_SEPARATOR, Value = "" };
     return Json(resource, JsonRequestBehavior.AllowGet);
 }
        public virtual ActionResult Find(int id = 0, string route = Resource.ROUTE_SEPARATOR, int languageID = 0, string saveError = "")
        {
            var repo = new ResourceRepository();
            var pages = repo.GetRoutes();
            var languages = repo.GetLanguages();
            var factory = new RouteResourceProviderFactory();
            var provider = (RouteResourceProvider)factory.CreateGlobalResourceProvider(route);
            Resource resource = null;
            Language selectedLanguage;
            if (id > 0)
            {
                resource = repo.GetResourceByID(id);
                selectedLanguage = resource.Language;
            }
            else
            {
                selectedLanguage =
                    languages.Where(l => l.Value.Default == true || l.Value.ID == languageID).OrderBy(l => l.Value.Default).
                    First().Value;
            }

            var model = new RouteResourceModel
            {
                Routes = new SelectList(pages, Resource.ROUTE_SEPARATOR),
                Route = pages.FirstOrDefault(p => p.ToLowerInvariant() == provider.ResourceRoute) ?? provider.ResourceRoute,
                Languages =
                    new SelectList(languages.Values, "ID", "EnglishName",
                                   selectedLanguage.ID),
                LanguageID = selectedLanguage.ID
            };

            var resources = provider.ResourceReader;
            var names = (from DictionaryEntry key in resources
                         select repo.GetResource(key.Key.ToString(), route, selectedLanguage.ID) ??
                                (provider.FindMissingResource(key.Key.ToString(), selectedLanguage.Name) ??
                                new Resource
                                {
                                    Key = key.Key.ToString(),
                                    ResourceType = ResourceType.Text,
                                    Route = Resource.ROUTE_SEPARATOR,
                                    Value = "Missing resource!!!"
                                })).OrderBy(n => n.Key).ToList();

            model.Resources = names;
            if (resource != null)
            {
                model.ResourceID = resource.ID;
                model.ResourceKey = resource.Key;
                model.ResourceRoute = resource.Route;
                model.Value = resource.Value;
                model.ResourceType = (int)resource.ResourceType;
                model.SaveError = saveError;
            }
            else if (names.Count > 0)
            {
                model.ResourceID = names[0].ID;
                model.ResourceKey = names[0].Key;
                model.ResourceRoute = names[0].Route;
                model.Value = names[0].Value;
                model.ResourceType = (int)names[0].ResourceType;
            }

            return View("Index", model);
        }
        public virtual ActionResult Save(RouteResourceEditModel model)
        {
            var value = string.Empty;
            var type = (ResourceType)model.ResourceType;
            if (type == ResourceType.Text)
            {
                value = model.TextEditor;
            }
            if (type == ResourceType.Html)
            {
                value = model.CKEditor;
            }

            try
            {
                if (model.ResourceID == 0)
                {
                    RouteResourceService.AddResourceString(model.ResourceKey, value, model.LanguageID,
                                                           model.ResourceRoute, type);
                }
                else
                {
                    RouteResourceService.UpdateResourceString(model.ResourceID, model.ResourceKey, value,
                                                              model.LanguageID,
                                                              model.ResourceRoute, type);
                }
            }
            catch (DbUpdateException ex)
            {
                Exception trueException = ex;
                while (trueException.InnerException != null)
                {
                    trueException = trueException.InnerException;
                }

                return RedirectToAction(Actions.Find(model.ResourceID, model.ResourceRoute, model.LanguageID, trueException.Message));
            }
            catch (Exception ex)
            {
                Exception trueException = ex;
                while (trueException.InnerException != null)
                {
                    trueException = trueException.InnerException;
                }

                return RedirectToAction(Actions.Find(model.ResourceID, model.ResourceRoute, model.LanguageID, trueException.Message));
            }
            var repo = new ResourceRepository();
            var resource = repo.GetResource(model.ResourceKey, model.ResourceRoute, model.LanguageID);

            if (model.ClearCache) RouteResourceProviderFactory.ClearCache();

            return RedirectToAction(Actions.Find(resource.ID, model.ResourceRoute, model.LanguageID));
        }
 static RouteResourceProviderFactory()
 {
     var repo = new ResourceRepository();
     _routes = repo.GetRoutes().ConvertAll(r => r.ToLowerInvariant());
 }
 /// <summary>
 /// static constructor, retreive static resources Languages and DefaultLanguage
 /// </summary>
 static RouteResourceProvider()
 {
     var repo = new ResourceRepository();
     DefaultLanguage = repo.GetDefaultLanguage();
     Languages = repo.GetLanguages();
 }
        /// <summary>
        /// Manages caching of the Resource Sets. Once loaded the values are loaded from the 
        /// cache only.
        /// </summary>
        /// <param name="language"></param>
        /// <returns></returns>
        private Dictionary<string, object> GetResourceCache(string language)
        {
            if (_resourceCache == null)
            {
                _resourceCache = new Dictionary<string, Dictionary<string, object>>();
            }

            lock (_syncLock)
            {
                if (_resourceCache.ContainsKey(language))
                {
                    return _resourceCache[language];
                }

                // *** Use ResourceRepository to retrieve the resource keys from the database
                var data = new ResourceRepository();

                var resourceList = data.GetResources(language, _resourceRoute);
                var resources = resourceList.ToDictionary(resource => resource.Key, ConvertDbResource);
                _resourceCache[language] = resources;
                foreach (var resource in resources)
                {
                    if (_usedResourceKeys.ContainsKey(resource.Key))
                    {
                        _usedResourceKeys[resource.Key] = true;
                    }
                    else
                    {
                        _usedResourceKeys.Add(resource.Key, true);
                    }
                }
                return resources;
            }
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="resourceKey"></param>
 /// <param name="language"></param>
 /// <returns></returns>
 public Resource FindMissingResource(string resourceKey, string language)
 {
     var repo = new ResourceRepository();
     var route = _resourceRoute;
     var resource = repo.GetResource(resourceKey, route, language);
     while (route != Resource.ROUTE_SEPARATOR && resource == null)
     {
         route = Resource.GetUpperRoute(route);
         resource = repo.GetResource(resourceKey, route, language);
     }
     if (resource == null && language != DefaultLanguage.Name)
     {
         return FindMissingResource(resourceKey, DefaultLanguage.Name);
     }
     return resource;
 }
 /// <summary>
 /// Add the resource to the database
 /// </summary>
 /// <param name="code"></param>
 /// <param name="value"></param>
 /// <param name="language"></param>
 /// <param name="route"></param>
 /// <param name="type"></param>
 public static void AddResourceString(string code, string value, int language, string route, ResourceType type)
 {
     var repo = new ResourceRepository();
     repo.AddResource(code, value, language, route, type);
 }