ModuleManager.cs :  » Content-Management-Systems-CMS » Kooboo » Everest » CmsServices » Extension » Module » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » Kooboo 
Kooboo » Everest » CmsServices » Extension » Module » ModuleManager.cs
/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.

This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;

using Everest.CmsServices.Models;

using Ionic.Zip;
using Everest.Library.ExtensionMethod;
namespace Everest.CmsServices.Extension.Module{
    public static class ModuleManager
    {
        public const string ModuleDirName = "Module";
        /// <summary>
        /// Uploads the module.
        /// </summary>
        /// <param name="moduleName">Name of the module.</param>
        /// <param name="zipStream">The zip stream.</param>
        /// <returns></returns>
        public static string UploadModule(string moduleName, Stream zipStream, bool upgrade)
        {
            var modules = GetModules(moduleName);
            if (modules.Where(m => m.ModuleName.Equals(moduleName, StringComparison.InvariantCultureIgnoreCase)).Count() != 0 && upgrade == false)
            {
                return string.Format(Resources.ModuleAlreadyExists, moduleName);
            }

            string moduleDirectory = ModuleManager.GetModuleDirectory(moduleName);

            using (ZipFile zipFile = ZipFile.Read(zipStream))
            {
                //determine if the module is valid.
                var moduleConfigEntry = zipFile[ModuleInfo.Const_ModuleConfigFile];
                var routesConfigEntry = zipFile[ModuleInfo.Const_ModuleRouteTableFile];
                if (moduleConfigEntry == null || routesConfigEntry == null)
                {
                    return string.Format(Resources.ModuleInvalid, moduleName);
                }

                zipFile.ExtractAll(moduleDirectory, ExtractExistingFileAction.OverwriteSilently);
            }

            //delete some inefficacy files
            DirectoryInfo directoryInfo = new DirectoryInfo(moduleDirectory);
            FileInfo[] webConfigFile = directoryInfo.GetFiles("web.config", SearchOption.TopDirectoryOnly);
            foreach (var item in webConfigFile)
            {
                File.Delete(item.FullName);
            }

            return string.Empty;
        }

        /// <summary>
        /// Gets the module.
        /// </summary>
        /// <param name="moduleApplication">The module application.</param>
        /// <param name="moduleName">Name of the module.</param>
        /// <param name="runtimeApplication">The runtime application.</param>
        /// <returns></returns>
        public static ModuleInfo GetModule(string moduleName, string runtimeApplication)
        {
            return ModuleInfo.GetModule(moduleName, runtimeApplication, true);
        }

        /// <summary>
        /// Gets the modules by application.
        /// </summary>
        /// <param name="runtimeApplication">The runtime application.</param>
        /// <returns></returns>
        public static IEnumerable<ModuleInfo> GetModules(string runtimeApplication)
        {
            IList<ModuleInfo> modules = new List<ModuleInfo>();
            var directoryInfo = new DirectoryInfo(GetModuleBaseDirectory());
            if (directoryInfo.Exists)
            {
                var moduleDirectories = directoryInfo.GetDirectories().Where(di => (di.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden);
                foreach (var dir in moduleDirectories)
                {
                    var moduleInfo = ModuleInfo.GetModule(dir.Name, runtimeApplication, true);
                    if (moduleInfo.Verify())
                    {
                        var applications = moduleInfo.GetAllowedApplications();
                        if (runtimeApplication == CmsGlobal.RootApplicationName || applications.Contains(CmsGlobal.RootApplicationName, StringComparer.InvariantCultureIgnoreCase))
                        {
                            modules.Add(moduleInfo);
                        }
                        else
                        {
                            if (applications.Contains(runtimeApplication, StringComparer.InvariantCultureIgnoreCase))
                            {
                                modules.Add(moduleInfo);
                            }
                        }
                    }
                }
            }
            return modules;
        }

        /// <summary>
        /// Deletes the module.
        /// </summary>
        /// <param name="moduleName">Name of the module.</param>
        public static void DeleteModule(string moduleName)
        {
            var dataContext = EverestCmsEntities.GetDataContext();
            ModuleInfo.DeleteModuleInApplications(dataContext, moduleName);
            ModuleInfo.DeletePermissions(dataContext, string.Empty, moduleName);
            var directoryInfo = new DirectoryInfo(GetModuleDirectory(moduleName));
            DeleteDirectory(directoryInfo);
        }
        private static void DeleteDirectory(DirectoryInfo directory)
        {
            if (directory.Exists)
            {
                // contain files
                var files = directory.GetFiles();
                foreach (var file in files)
                {
                    if (file.Exists)
                    {
                        file.IsReadOnly = false;
                        file.Delete();
                    }
                }
                // contain directories
                var dirs = directory.GetDirectories();
                foreach (var dir in dirs)
                {
                    DeleteDirectory(dir);
                }
                // self
                directory.Delete(true);
            }
        }

        /// <summary>
        /// Installs the specified application.
        /// </summary>
        /// <param name="application">The application.</param>
        /// <param name="moduleName">Name of the module.</param>
        public static string Install(string moduleName, string userName)
        {
            ModuleInstaller installer = new ModuleInstaller();
            return installer.Install(ModuleInfo.GetModule(moduleName, CmsGlobal.RootApplicationName, true), userName);
        }
        /// <summary>
        /// Unstalls the specified application.
        /// </summary>
        /// <param name="application">The application.</param>
        /// <param name="moduleName">Name of the module.</param>
        public static string Uninstall(string moduleName)
        {
            ModuleUninstall uninstall = new ModuleUninstall();
            return uninstall.Uninstall(ModuleInfo.GetModule(moduleName, CmsGlobal.RootApplicationName, true));
        }
        /// <summary>
        /// Gets the module base directory.
        /// </summary>
        /// <param name="moduleName">Name of the module.</param>
        /// <returns></returns>
        public static string GetModuleDirectory(string moduleName)
        {
            return Path.Combine(GetModuleBaseDirectory(), moduleName);
        }
        /// <summary>
        /// Gets the application modules base directory.
        /// </summary>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static string GetModuleBaseDirectory()
        {
            return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ModuleDirName);
        }
    }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.