PageRenderTime 34ms CodeModel.GetById 24ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Packaging/PackageRepository.cs

#
C# | 143 lines | 103 code | 15 blank | 25 comment | 23 complexity | 7bf4105636295b8a4b7c33e6b70e8f60 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Web.Caching;
  5using BlogEngine.Core.Json;
  6
  7namespace BlogEngine.Core.Packaging
  8{
  9    /// <summary>
 10    /// Package repository
 11    /// </summary>
 12    public class PackageRepository
 13    {
 14        /// <summary>
 15        /// 
 16        /// </summary>
 17        /// <param name="pkgType"></param>
 18        /// <param name="page"></param>
 19        /// <param name="sortOrder"></param>
 20        /// <param name="searchVal"></param>
 21        /// <returns></returns>
 22        public static List<JsonPackage> FromGallery(string pkgType, int page = 0, Gallery.OrderType sortOrder = Gallery.OrderType.Newest, string searchVal = "")
 23        {
 24            var packages = new List<JsonPackage>();
 25            var gallery = CachedPackages.Where(p => p.Location == "G" || p.Location == "I");
 26
 27            if (pkgType != "all")
 28                gallery = gallery.Where(p => p.PackageType == pkgType).ToList();
 29
 30            foreach (var pkg in gallery)
 31            {
 32                System.Diagnostics.Debug.WriteLine(string.Format("{0}|{1}|{2}|{3}", 
 33                    pkg.Id, pkg.OnlineVersion, pkg.PackageType, pkg.Location));
 34
 35                if (string.IsNullOrEmpty(searchVal))
 36                {
 37                    packages.Add(pkg);
 38                }
 39                else
 40                {
 41                    if (pkg.Title.IndexOf(searchVal, StringComparison.OrdinalIgnoreCase) != -1
 42                        || pkg.Description.IndexOf(searchVal, StringComparison.OrdinalIgnoreCase) != -1
 43                        ||
 44                        (!string.IsNullOrWhiteSpace(pkg.Tags) &&
 45                         pkg.Tags.IndexOf(searchVal, StringComparison.OrdinalIgnoreCase) != -1))
 46                    {
 47                        packages.Add(pkg);
 48                    }
 49                }
 50            }
 51
 52            Gallery.GalleryPager = new Pager(page, packages.Count, pkgType);
 53
 54            if (packages.Count > 0)
 55            {
 56                switch (sortOrder)
 57                {
 58                    case Gallery.OrderType.Downloads:
 59                        packages = packages.OrderByDescending(p => p.DownloadCount).ThenBy(p => p.Title).ToList();
 60                        break;
 61                    case Gallery.OrderType.Rating:
 62                        packages = packages.OrderByDescending(p => p.Rating).ThenBy(p => p.Title).ToList();
 63                        break;
 64                    case Gallery.OrderType.Newest:
 65                        packages = packages.OrderByDescending(p => Convert.ToDateTime(p.LastUpdated)).ThenBy(p => p.Title).ToList();
 66                        break;
 67                    case Gallery.OrderType.Alphanumeric:
 68                        packages = packages.OrderBy(p => p.Title).ToList();
 69                        break;
 70                }
 71            }
 72            return packages.Skip(page * Constants.PageSize).Take(Constants.PageSize).ToList();
 73        }
 74
 75        /// <summary>
 76        /// 
 77        /// </summary>
 78        /// <param name="pkgType"></param>
 79        /// <returns></returns>
 80        public static List<JsonPackage> LocalPackages(string pkgType)
 81        {
 82            var packages = new List<JsonPackage>();
 83
 84            foreach (var pkg in CachedPackages)
 85            {
 86                if(pkgType != "all")
 87                {
 88                    if (pkg.PackageType != pkgType) continue;
 89                }
 90
 91                if(pkg.Location == "G")
 92                    continue;
 93
 94                packages.Add(pkg);
 95            }
 96            return packages;
 97        }
 98
 99        /// <summary>
100        /// Package by ID
101        /// </summary>
102        /// <param name="pkgId"></param>
103        /// <returns></returns>
104        public static JsonPackage GetPackage(string pkgId)
105        {
106            return CachedPackages.FirstOrDefault(pkg => pkg.Id == pkgId);
107        }
108
109        /// <summary>
110        /// Local "packages" - list of extensions,
111        /// themes and widgets folders
112        /// </summary>
113        static IEnumerable<JsonPackage> CachedPackages
114        {
115            get
116            {
117                if (Blog.CurrentInstance.Cache[Constants.CacheKey] == null)
118                {
119                    Blog.CurrentInstance.Cache.Add(
120                        Constants.CacheKey,
121                        LoadPackages(),
122                        null,
123                        Cache.NoAbsoluteExpiration,
124                        new TimeSpan(0, 15, 0),
125                        CacheItemPriority.Low,
126                        null);
127                }
128                return (IEnumerable<JsonPackage>)Blog.CurrentInstance.Cache[Constants.CacheKey];
129            }
130        }
131
132        static List<JsonPackage> LoadPackages()
133        {
134            var packages = new List<JsonPackage>();
135
136            Gallery.Load(packages);
137            FileSystem.Load(packages);
138            Installer.MarkAsInstalled(packages);
139
140            return packages;
141        }
142    }
143}