/*
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.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Everest.Library;
using Everest.CmsServices.Models;
using Everest.Library.ExtensionMethod;
using Everest.Library.Data.Rule;
using Microsoft.Data.Extensions;
using Everest.CmsServices.Services;
namespace Everest.CmsServices.Extension.Module{
public class ModuleUninstall
{
public ModuleUninstall()
{
}
public string Uninstall(ModuleInfo moduleInfo)
{
StringBuilder messageBuilder = new StringBuilder();
if (moduleInfo.Configuration.Installed || moduleInfo.HostInCms == false)
{
string application = CmsGlobal.RootApplicationName;
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
messageBuilder.AppendHtmlLine(ExecuteSql(moduleInfo));
messageBuilder.AppendHtmlLine(DeleteFolders(dataContext, application, moduleInfo));
messageBuilder.AppendHtmlLine(DeleteSchemas(dataContext, moduleInfo));
if (moduleInfo.HostInCms)
{
messageBuilder.AppendHtmlLine(DeleteAssemblies(moduleInfo));
}
//set installed
moduleInfo.Configuration.Installed = false;
moduleInfo.SaveConfiguration(moduleInfo.Configuration);
}
else
{
messageBuilder.AppendFormat(Resources.UnstallModule_ModuleNotInstalled, moduleInfo.ModuleName);
}
return messageBuilder.ToString();
}
private string DeleteAssemblies(ModuleInfo moduleInfo)
{
string binPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Bin");
StringBuilder message = new StringBuilder();
//StringBuilder bathBuilder = new StringBuilder();
if (moduleInfo.Configuration.Assemblies != null)
{
foreach (var item in moduleInfo.Configuration.Assemblies)
{
string fileName = Path.GetFileName(item.Path);
// string sourceFilePath = Path.Combine(moduleInfo.ModuleBaseDirectory, item.Path);
string targetFilePath = Path.Combine(binPath, fileName);
try
{
// bathBuilder.AppendHtmlLine(string.Format("copy {0} {1}", sourceFilePath, targetFilePath));
File.Delete(targetFilePath);
}
catch (Exception e)
{
message.AppendHtmlLine(string.Format(Resources.UnstallModule_DeleteAssemblyFailed, targetFilePath, e.Message));
Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(e);
}
}
}
return message.ToString();
}
private string ExecuteSql(ModuleInfo moduleInfo)
{
ModuleSqlFileRunner runner = new ModuleSqlFileRunner();
StringBuilder errorMessage = new StringBuilder();
if (moduleInfo.Configuration.UnstallSqlFiles != null)
{
foreach (var item in moduleInfo.Configuration.UnstallSqlFiles)
{
string sourceFilePath = Path.Combine(moduleInfo.ModuleBaseDirectory, item.Path);
try
{
runner.Run(sourceFilePath);
}
catch (Exception e)
{
errorMessage.AppendHtmlLine(string.Format(Resources.InstallModule_RunSqlFileFailed, sourceFilePath, e.Message));
Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(e);
}
}
}
return errorMessage.ToString();
}
private string DeleteSchemas(IEverestCmsDataContext dataContext, ModuleInfo moduleInfo)
{
StringBuilder errorMessage = new StringBuilder();
var schemaService = UnityManager.Resolve<SchemaService>();
if (moduleInfo.Configuration.Schemas != null)
{
foreach (var item in moduleInfo.Configuration.Schemas.Reverse())
{
//
dataContext.ObjectContext.AcceptAllChanges();
string schemaFile = Path.Combine(moduleInfo.ModuleBaseDirectory, item.Path);
try
{
Cms_Schema schema = ObjectExtensions.DeserializeFromXmlFile<Cms_Schema>(schemaFile);
schemaService.DeleteSchema(schema.UUID);
}
catch (RuleViolationException ruleViolationEx)
{
StringBuilder violationMessage = new StringBuilder();
foreach (var issue in ruleViolationEx.Issues)
{
violationMessage.AppendHtmlLine(issue.ErrorMessage);
}
errorMessage.AppendHtmlLine(string.Format(Resources.UnstallModule_DeleteSchemaFailed, schemaFile, violationMessage.ToString()));
Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(ruleViolationEx);
}
catch (Exception e)
{
errorMessage.AppendHtmlLine(string.Format(Resources.UnstallModule_DeleteSchemaFailed, schemaFile, e.Message));
Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(e);
}
}
}
return errorMessage.ToString();
}
private string DeleteFolders(IEverestCmsDataContext dataContext, string application, ModuleInfo moduleInfo)
{
StringBuilder errorMessage = new StringBuilder();
var folderService = UnityManager.Resolve<FolderService>();
if (moduleInfo.Configuration.Folders != null)
{
foreach (var item in moduleInfo.Configuration.Folders)
{
//
dataContext.ObjectContext.AcceptAllChanges();
try
{
var folder = dataContext.QueryFolder(application, item.FolderName).FirstOrDefault();
if (folder != null)
{
folderService.DeleteFolder(folder.UUID);
}
}
catch (RuleViolationException ruleViolationEx)
{
StringBuilder violationMessage = new StringBuilder();
foreach (var issue in ruleViolationEx.Issues)
{
violationMessage.AppendHtmlLine(issue.ErrorMessage);
}
errorMessage.AppendHtmlLine(string.Format(Resources.UnstallModule_DeleteFolderFailed, item.FolderName, violationMessage.ToString()));
Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(ruleViolationEx);
}
catch (Exception e)
{
errorMessage.AppendHtmlLine(string.Format(Resources.UnstallModule_DeleteFolderFailed, item.FolderName, e.Message));
Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(e);
}
}
}
return errorMessage.ToString();
}
}
}
|