Example #1
0
        public static CategoriesJson Categories(string accountNameKey, List <CategoryModel> categoriesIn)
        {
            var categoriesObjectOut = new CategoriesJson();

            categoriesObjectOut.categories = new List <CategorizationListItemJson>();

            foreach (CategoryModel categoryModel in categoriesIn)
            {
                var categoryListItem = new CategorizationListItemJson
                {
                    //id = categoryModel.CategoryID.ToString(),
                    name               = categoryModel.CategoryName,
                    nameKey            = categoryModel.CategoryNameKey,
                    fullyQualifiedName = categoryModel.FullyQualifiedName
                };


                //Get listing image records for each object from Table Storage
                categoryListItem.images = Dynamics.Images.BuildDynamicImagesListForJson(accountNameKey, "category", categoryModel.CategoryID.ToString(), true);


                categoriesObjectOut.categories.Add(categoryListItem);
            }



            categoriesObjectOut.count = categoriesObjectOut.categories.Count;

            return(categoriesObjectOut);
        }
        public JsonNetResult Categories(bool includeHidden = false)
        {
            ExecutionType executionType = ExecutionType.local;
            Stopwatch     stopWatch     = new Stopwatch();

            stopWatch.Start();

            //Get the subdomain (if exists) for the api call
            string accountNameKey = Common.GetSubDomain(Request.Url);

            if (String.IsNullOrEmpty(accountNameKey))
            {
                return(new JsonNetResult {
                    Data = "Not found"
                });                                              //return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            List <CategoryModel> categories           = null;
            CategoriesJson       categoriesObjectJson = null;

            string localCacheKey = accountNameKey + ":categories:list:public";

            if (includeHidden == true)
            {
                localCacheKey = accountNameKey + ":categories:list:private";
            }

            #region (Plan A) Get json from local cache

            try
            {
                categoriesObjectJson = (CategoriesJson)HttpRuntime.Cache[localCacheKey];
            }
            catch (Exception e)
            {
                var error = e.Message;
                //TODO: Log: error message for local cache call
            }

            #endregion

            if (categoriesObjectJson == null)
            {
                #region (Plan B) Get Public json from second layer of Redis Cache

                IDatabase cache = CoreServices.RedisConnectionMultiplexers.RedisMultiplexer.GetDatabase();

                string pathAndQuery = Common.GetApiPathAndQuery(Request.Url);

                string hashApiKey   = accountNameKey + ":apicache";
                string hashApiField = pathAndQuery;

                try
                {
                    var redisApiValue = cache.HashGet(hashApiKey, hashApiField);

                    if (redisApiValue.HasValue)
                    {
                        categoriesObjectJson = JsonConvert.DeserializeObject <CategoriesJson>(redisApiValue);
                        executionType        = ExecutionType.redis_secondary;
                    }
                }
                catch
                {
                }

                #endregion

                if (categoriesObjectJson == null)
                {
                    #region (Plan C) Get category data from Redis Cache and rebuild

                    try
                    {
                        //IDatabase cache = CoreServices.RedisConnectionMultiplexers.RedisMultiplexer.GetDatabase();

                        string hashMainKey   = accountNameKey + ":categories";
                        string hashMainField = "list:public";

                        if (includeHidden == true)
                        {
                            hashMainField = "list:private";
                        }

                        try
                        {
                            var redisValue = cache.HashGet(hashMainKey, hashMainField);

                            if (redisValue.HasValue)
                            {
                                categories    = JsonConvert.DeserializeObject <List <ApplicationCategorizationService.CategoryModel> >(redisValue);
                                executionType = ExecutionType.redis_main;
                            }
                        }
                        catch
                        {
                        }
                    }
                    catch (Exception e)
                    {
                        var error = e.Message;
                        //TODO: Log: error message for Redis call
                    }

                    #endregion

                    if (categories == null)
                    {
                        #region (Plan D) Get data from WCF

                        var applicationCategorizationServiceClient = new ApplicationCategorizationService.ApplicationCategorizationServiceClient();

                        try
                        {
                            applicationCategorizationServiceClient.Open();

                            categories = applicationCategorizationServiceClient.GetCategories(accountNameKey, includeHidden, Common.SharedClientKey).ToList();

                            executionType = ExecutionType.wcf;

                            WCFManager.CloseConnection(applicationCategorizationServiceClient);
                        }
                        catch (Exception e)
                        {
                            #region Manage Exception

                            string exceptionMessage = e.Message.ToString();

                            var    currentMethod       = System.Reflection.MethodBase.GetCurrentMethod();
                            string currentMethodString = currentMethod.DeclaringType.FullName + "." + currentMethod.Name;

                            // Abort the connection & manage the exception
                            WCFManager.CloseConnection(applicationCategorizationServiceClient, exceptionMessage, currentMethodString);

                            #endregion
                        }

                        #endregion
                    }
                }

                #region Transform into json object, add images & cache locally or locally and radisAPI layer

                if (categoriesObjectJson != null)
                {
                    //Just cache locally (we got json from the api redis layer)
                    HttpRuntime.Cache.Insert(localCacheKey, categoriesObjectJson, null, DateTime.Now.AddMinutes(Common.CategorizationCacheTimeInMinutes), TimeSpan.Zero);
                }
                else if (categories != null)
                {
                    //Transform categories into JSON and cache BOTH locally AND into redis
                    categoriesObjectJson = Transforms.Json.CategorizationTransforms.Categories(accountNameKey, categories);
                    HttpRuntime.Cache.Insert(localCacheKey, categoriesObjectJson, null, DateTime.Now.AddMinutes(Common.CategorizationCacheTimeInMinutes), TimeSpan.Zero);

                    try
                    {
                        cache.HashSet(hashApiKey, hashApiField, JsonConvert.SerializeObject(categoriesObjectJson), When.Always, CommandFlags.FireAndForget);
                    }
                    catch
                    {
                    }
                }

                #endregion
            }

            //Add execution data
            stopWatch.Stop();
            categoriesObjectJson.executionType = executionType.ToString();
            categoriesObjectJson.executionTime = stopWatch.Elapsed.TotalMilliseconds + "ms";

            JsonNetResult jsonNetResult = new JsonNetResult();
            jsonNetResult.Formatting = Newtonsoft.Json.Formatting.Indented;
            jsonNetResult.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local; //<-- Convert UTC times to LocalTime
            jsonNetResult.Data = categoriesObjectJson;

            return(jsonNetResult);
        }