/*
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.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Configuration;
using Everest.Library;
using Everest.Library.Data;
using Everest.Library.ExtensionMethod;
using Everest.CmsServices.Models;
using Everest.CmsServices.Providers;
using System.Reflection;
namespace Everest.CmsServices.Services{
/// <summary>
/// application initializer
/// </summary>
public class ApplicationInitializer
{
#region IApplicationInitializer Members
volatile static bool initialized = false;
volatile static object initializeLockHelper = new object();
/// <summary>
/// Initializes the global applciation.
/// </summary>
/// <param name="applicationName">Name of the application.</param>
public virtual void InitializeGlobal(string applicationName)
{
lock (initializeLockHelper)
{
if (initialized == false)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
IContentProvider contentProvider = UnityManager.Resolve<IContentProvider>();
//contentProvider.SystemManager.EnsureDatabaseInitialized(dataContext);
CheckDbVersion(dataContext);
CreateAdministrator();
#region Init Folders
var app = (from a in dataContext.aspnet_Applications
where a.LoweredApplicationName == applicationName.ToLower()
select a).FirstOrDefault();
if (app != null && !app.Initialized)
{
ClearApplicationFolder(applicationName, dataContext);
Cms_Folder rootFolder = new Cms_Folder();
rootFolder.Parent = null;
rootFolder.FolderName = "Everest";
rootFolder.aspnet_Applications = app;
#region System Node
Cms_Folder system = new Cms_Folder();
system.FolderName = "System";
system.FolderType = (int)FolderType.System;
system.Order = 1;
system.IconCls = FolderType.System.ToString();
system.aspnet_Applications = app;
rootFolder.Children.Add(system);
//Cms_Folder settings = new Cms_Folder();
//settings.FolderName = "Setting";
//settings.FormType = "form";
//settings.FolderType = (int)FolderType.Setting;
//settings.IconCls = FolderType.Setting.ToString();
//settings.SchemaName = FolderType.Setting.ToString();
//settings.Order = 1;
//settings.aspnet_Applications = app;
//system.Cms_Children.Add(settings);
Cms_Folder schedule = new Cms_Folder();
schedule.FolderName = "Schedule";
schedule.FolderType = (int)FolderType.Schedule;
schedule.IconCls = FolderType.Schedule.ToString();
schedule.SchemaName = FolderType.Schedule.ToString();
schedule.Order = 1;
schedule.aspnet_Applications = app;
system.Children.Add(schedule);
Cms_Folder workflow = new Cms_Folder();
workflow.FolderName = "Workflow";
workflow.FolderType = (int)FolderType.Workflow;
workflow.IconCls = FolderType.Workflow.ToString();
workflow.SchemaName = FolderType.Workflow.ToString();
workflow.Order = 2;
workflow.CanInherit = true;
workflow.aspnet_Applications = app;
system.Children.Add(workflow);
#region UserGroup
Cms_Folder userGroup = new Cms_Folder();
userGroup.FolderName = "User Group";
userGroup.FolderType = (int)FolderType.UserGroup;
userGroup.IconCls = FolderType.UserGroup.ToString();
userGroup.Order = 3;
userGroup.aspnet_Applications = app;
system.Children.Add(userGroup);
Cms_Folder users = new Cms_Folder();
users.FolderName = "Users";
users.FolderType = (int)FolderType.Users;
users.SchemaName = FolderType.Users.ToString();
users.IconCls = FolderType.Users.ToString();
users.Order = 1;
users.aspnet_Applications = app;
userGroup.Children.Add(users);
Cms_Folder roles = new Cms_Folder();
roles.FolderName = "Roles";
roles.FolderType = (int)FolderType.Roles;
roles.IconCls = FolderType.Roles.ToString();
roles.SchemaName = FolderType.Roles.ToString();
roles.Order = 2;
roles.aspnet_Applications = app;
userGroup.Children.Add(roles);
Cms_Folder userProfile = new Cms_Folder();
userProfile.FolderName = "UserProfile";
userProfile.FolderType = (int)FolderType.UserProfile;
userProfile.IconCls = FolderType.UserProfile.ToString();
userProfile.SchemaName = FolderType.UserProfile.ToString();
userProfile.Order = 3;
userProfile.FormType = "form";
userProfile.aspnet_Applications = app;
userGroup.Children.Add(userProfile);
#endregion
#region Schema Node
Cms_Folder schema = AddSchemaFolder(4, app);
system.Children.Add(schema);
#endregion
Cms_Folder webevent = new Cms_Folder();
webevent.FolderName = "WebEvent";
webevent.FolderType = (int)FolderType.WebEvent;
webevent.IconCls = FolderType.WebEvent.ToString();
webevent.SchemaName = FolderType.WebEvent.ToString();
webevent.Order = 9;
webevent.CanInherit = false;
webevent.aspnet_Applications = app;
system.Children.Add(webevent);
Cms_Folder sitePackage = new Cms_Folder();
sitePackage.FolderName = "SitePackage";
sitePackage.FolderType = (int)FolderType.SitePackage;
sitePackage.IconCls = FolderType.SitePackage.ToString();
sitePackage.SchemaName = FolderType.SitePackage.ToString();
sitePackage.Order = 10;
sitePackage.CanInherit = false;
sitePackage.aspnet_Applications = app;
system.Children.Add(sitePackage);
Cms_Folder templatePackage = new Cms_Folder();
templatePackage.FolderName = "TemplatePackage";
templatePackage.FolderType = (int)FolderType.TemplatePackage;
templatePackage.IconCls = FolderType.TemplatePackage.ToString();
templatePackage.SchemaName = FolderType.TemplatePackage.ToString();
templatePackage.Order = 11;
templatePackage.CanInherit = false;
templatePackage.aspnet_Applications = app;
system.Children.Add(templatePackage);
Cms_Folder searchSetting = new Cms_Folder();
searchSetting.FolderName = "SearchSetting";
searchSetting.FolderType = (int)FolderType.SearchSetting;
searchSetting.IconCls = FolderType.SearchSetting.ToString();
searchSetting.SchemaName = FolderType.SearchSetting.ToString();
searchSetting.Order = 12;
searchSetting.CanInherit = false;
searchSetting.aspnet_Applications = app;
system.Children.Add(searchSetting);
Cms_Folder koobooStore = new Cms_Folder();
koobooStore.FolderName = "KoobooStore";
koobooStore.FolderType = (int)FolderType.KoobooStore;
koobooStore.IconCls = FolderType.KoobooStore.ToString();
koobooStore.SchemaName = FolderType.KoobooStore.ToString();
koobooStore.Order = 15;
koobooStore.CanInherit = false;
koobooStore.aspnet_Applications = app;
system.Children.Add(koobooStore);
Cms_Folder checkUpdates = new Cms_Folder();
checkUpdates.FolderName = "CheckUpdates";
checkUpdates.FolderType = (int)FolderType.CheckUpdates;
checkUpdates.IconCls = FolderType.CheckUpdates.ToString();
checkUpdates.SchemaName = FolderType.CheckUpdates.ToString();
checkUpdates.Order = 20;
checkUpdates.CanInherit = false;
checkUpdates.aspnet_Applications = app;
system.Children.Add(checkUpdates);
#endregion
#region Template Node
Cms_Folder template = AddTemplateFolder(3, app);
rootFolder.Children.Add(template);
#endregion
#region Plugin Node
Cms_Folder plugin = AddPluginFolder(4, app);
rootFolder.Children.Add(plugin);
#endregion
#region Content Node
Cms_Folder textContentRoot = new Cms_Folder();
textContentRoot.FolderName = FolderType.Content.ToString();
textContentRoot.FolderType = (int)FolderType.Content;
textContentRoot.Order = 6;
textContentRoot.IconCls = FolderType.Content.ToString();
textContentRoot.CanInherit = true;
textContentRoot.FolderPath = GetFolderRelativePath(applicationName, FolderType.Content.ToString());
textContentRoot.aspnet_Applications = app;
rootFolder.Children.Add(textContentRoot);
#endregion
#region Applications Node
Cms_Folder applications = new Cms_Folder();
applications.FolderName = "Applications";
applications.FolderType = (int)FolderType.Applications;
applications.IconCls = FolderType.Applications.ToString();
//applications.SchemaName = FolderType.Applications.ToString();
applications.Order = 8;
applications.aspnet_Applications = app;
rootFolder.Children.Add(applications);
#endregion
dataContext.AddToCms_Folder(rootFolder);
app.Initialized = true;
dataContext.SaveChanges();
}
#endregion
initialized = true;
}
}
}
private void CheckDbVersion(IEverestCmsDataContext dataContext)
{
var assemblyVersion = CmsGlobal.KoobooVersion;
var dbKoobooVersion = dataContext.aspnet_SchemaVersions.Where(sv => sv.Feature == "kooboo").FirstOrDefault();
if (dbKoobooVersion == null)
{
throw new Exception(Resources.KoobooVersionInSchema);
}
if (!dbKoobooVersion.CompatibleSchemaVersion.Equals(assemblyVersion, StringComparison.InvariantCultureIgnoreCase))
{
throw new Exception(string.Format(Resources.KoobooVersionIsNotMatch, dbKoobooVersion.CompatibleSchemaVersion, assemblyVersion));
}
}
private void CreateAdministrator()
{
string administratorName = CmsGlobal.AdministratorUserName;
string administratorPassword = ConfigurationManager.AppSettings["administratorPassword"];
if (!StringExtensions.IsNullOrEmptyTrim(administratorName) && !StringExtensions.IsNullOrEmptyTrim(administratorPassword))
{
MembershipUser membershipUser = Membership.GetUser(administratorName);
if (membershipUser == null)
{
string administratorEmail = CmsGlobal.AdministratorEmail;
Membership.CreateUser(administratorName, administratorPassword, administratorEmail);
}
}
}
private void ClearApplicationFolder(string applicationName, IEverestCmsDataContext dataContext)
{
var folders = (from f in dataContext.Cms_Folder
where f.aspnet_Applications.ApplicationName == applicationName
select f).ToArray();
foreach (var item in folders)
{
dataContext.DeleteObject(item);
}
dataContext.SaveChanges();
}
private static Cms_Folder AddSchemaFolder(int order, aspnet_Applications app)
{
Cms_Folder schema = new Cms_Folder();
schema.FolderName = "Schema";
schema.FolderType = (int)FolderType.Schema;
schema.IconCls = FolderType.Schema.ToString();
schema.Order = order;
schema.CanInherit = true;
schema.aspnet_Applications = app;
Cms_Folder formSchema = new Cms_Folder();
formSchema.FolderName = "Text";
formSchema.FolderType = (int)FolderType.TextSchema;
formSchema.SchemaName = FolderType.TextSchema.ToString();
formSchema.IconCls = FolderType.TextSchema.ToString();
formSchema.Order = 1;
formSchema.CanInherit = true;
formSchema.aspnet_Applications = app;
schema.Children.Add(formSchema);
Cms_Folder fileSchema = new Cms_Folder();
fileSchema.FolderName = "Binary";
fileSchema.FolderType = (int)FolderType.BinarySchema;
fileSchema.SchemaName = FolderType.BinarySchema.ToString();
fileSchema.IconCls = FolderType.BinarySchema.ToString();
fileSchema.Order = 2;
fileSchema.CanInherit = true;
fileSchema.aspnet_Applications = app;
schema.Children.Add(fileSchema);
Cms_Folder validateGroup = new Cms_Folder();
validateGroup.FolderName = "ValidatorGroup";
validateGroup.FolderType = (int)FolderType.ValidatorGroup;
validateGroup.SchemaName = FolderType.ValidatorGroup.ToString();
validateGroup.IconCls = FolderType.ValidatorGroup.ToString();
validateGroup.Order = 3;
validateGroup.CanInherit = true;
validateGroup.aspnet_Applications = app;
schema.Children.Add(validateGroup);
Cms_Folder validator = new Cms_Folder();
validator.FolderName = "Validator";
validator.FolderType = (int)FolderType.Validator;
validator.IconCls = FolderType.Validator.ToString();
validator.SchemaName = FolderType.Validator.ToString();
validator.Order = 4;
validator.CanInherit = true;
validator.aspnet_Applications = app;
schema.Children.Add(validator);
return schema;
}
private static Cms_Folder AddTemplateFolder(int order, aspnet_Applications app)
{
Cms_Folder template = new Cms_Folder();
template.FolderName = "Template";
template.FolderType = (int)FolderType.Template;
template.IconCls = FolderType.Template.ToString();
template.Order = order;
template.CanInherit = true;
template.aspnet_Applications = app;
Cms_Folder contentTemplate = new Cms_Folder();
contentTemplate.FolderName = "ContentTemplate";
contentTemplate.FolderType = (int)FolderType.ContentTemplate;
contentTemplate.SchemaName = FolderType.ContentTemplate.ToString();
contentTemplate.IconCls = FolderType.ContentTemplate.ToString();
contentTemplate.Order = 1;
contentTemplate.CanInherit = true;
contentTemplate.aspnet_Applications = app;
template.Children.Add(contentTemplate);
Cms_Folder pageTemplate = new Cms_Folder();
pageTemplate.FolderName = "LayoutTemplate";
pageTemplate.FolderType = (int)FolderType.LayoutTemplate;
pageTemplate.SchemaName = FolderType.LayoutTemplate.ToString();
pageTemplate.IconCls = FolderType.LayoutTemplate.ToString();
pageTemplate.Order = 2;
pageTemplate.CanInherit = true;
pageTemplate.aspnet_Applications = app;
template.Children.Add(pageTemplate);
Cms_Folder textResource = new Cms_Folder();
textResource.FolderName = "TextResource";
textResource.FolderType = (int)FolderType.TextResource;
textResource.SchemaName = FolderType.TextResource.ToString();
textResource.IconCls = FolderType.TextResource.ToString();
textResource.Order = 3;
textResource.CanInherit = true;
textResource.aspnet_Applications = app;
template.Children.Add(textResource);
Cms_Folder binaryResource = new Cms_Folder();
binaryResource.FolderName = "BinaryResource";
binaryResource.FolderType = (int)FolderType.BinaryResource;
binaryResource.SchemaName = FolderType.BinaryResource.ToString();
binaryResource.IconCls = FolderType.BinaryResource.ToString();
binaryResource.FolderPath = TemplateFileManager.GetRelativeTemplateFolderPath(app.ApplicationName, FolderType.BinaryResource);
binaryResource.Order = 4;
binaryResource.CanInherit = true;
binaryResource.aspnet_Applications = app;
template.Children.Add(binaryResource);
//Cms_Folder customerForm = new Cms_Folder();
//customerForm.FolderName = FolderType.WebForm.ToString();
//customerForm.FolderType = (int)FolderType.WebForm;
//customerForm.SchemaName = FolderType.WebForm.ToString();
//customerForm.IconCls = FolderType.WebForm.ToString();
//customerForm.Order = 5;
//customerForm.CanInherit = true;
//customerForm.aspnet_Applications = app;
//template.Children.Add(customerForm);
return template;
}
private static Cms_Folder AddPluginFolder(int order, aspnet_Applications app)
{
Cms_Folder plugin = new Cms_Folder();
plugin.FolderName = "Extension";
plugin.FolderType = (int)FolderType.Extension;
plugin.IconCls = FolderType.Extension.ToString();
plugin.Order = order;
plugin.CanInherit = true;
plugin.aspnet_Applications = app;
Cms_Folder pagePlugin = new Cms_Folder();
pagePlugin.FolderName = "PagePlugin";
pagePlugin.FolderType = (int)FolderType.PagePlugin;
pagePlugin.IconCls = FolderType.PagePlugin.ToString();
pagePlugin.SchemaName = FolderType.PagePlugin.ToString();
pagePlugin.FormType = "grid";
pagePlugin.Order = 1;
pagePlugin.CanInherit = true;
pagePlugin.aspnet_Applications = app;
plugin.Children.Add(pagePlugin);
Cms_Folder module = new Cms_Folder();
module.FolderName = "Module";
module.FolderType = (int)FolderType.Module;
module.IconCls = FolderType.Module.ToString();
module.SchemaName = FolderType.Module.ToString();
module.FormType = "grid";
module.Order = 2;
module.CanInherit = true;
module.aspnet_Applications = app;
module.ApplicationLevel = app.ApplicationLevel;
plugin.Children.Add(module);
Cms_Folder staticCode = new Cms_Folder();
staticCode.FolderName = FolderType.StaticCode.ToString();
staticCode.FolderType = (int)FolderType.StaticCode;
staticCode.IconCls = FolderType.StaticCode.ToString();
staticCode.Order = 5;
staticCode.SchemaName = FolderType.StaticCode.ToString();
staticCode.FormType = "grid";
staticCode.CanInherit = true;
staticCode.aspnet_Applications = app;
plugin.Children.Add(staticCode);
return plugin;
}
/// <summary>
/// Initializes the CMS application.
/// </summary>
/// <param name="applicationName">Name of the application.</param>
public virtual void InitializeCmsApplication(string applicationName)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
var app = (from a in dataContext.aspnet_Applications
where a.LoweredApplicationName == applicationName.ToLower()
select a).FirstOrDefault();
if (app != null && !app.Initialized)
{
ClearApplicationFolder(applicationName, dataContext);
string defaultApp = Membership.ApplicationName;
var appRoot = (from f in dataContext.Cms_Folder
where f.FolderType == (int)FolderType.Applications
select f).First();
#region Application Node
Cms_Folder applicationFolder = new Cms_Folder();
applicationFolder.FolderName = applicationName;
applicationFolder.FolderType = (int)FolderType.Application;
applicationFolder.IconCls = FolderType.Application.ToString();
applicationFolder.Order = appRoot.Children.Count() + 1;
applicationFolder.CanInherit = true;
applicationFolder.Base = dataContext.QueryFolders(app.BaseApplication, FolderType.Application).FirstOrDefault();
applicationFolder.aspnet_Applications = app;
appRoot.Children.Add(applicationFolder);
#endregion
#region SiteManagerNode
Cms_Folder siteManagerNode = new Cms_Folder();
siteManagerNode.FolderName = "SiteManager";
siteManagerNode.FolderType = (int)FolderType.SiteManager;
siteManagerNode.IconCls = FolderType.SiteManager.ToString();
siteManagerNode.Order = 1;
siteManagerNode.aspnet_Applications = app;
applicationFolder.Children.Add(siteManagerNode);
Cms_Folder setting = new Cms_Folder();
setting.FolderName = FolderType.SiteSetting.ToString();
setting.FolderType = (int)FolderType.SiteSetting;
setting.IconCls = FolderType.SiteSetting.ToString();
setting.SchemaName = FolderType.SiteSetting.ToString();
setting.FormType = "form";
setting.Order = 0;
setting.aspnet_Applications = app;
siteManagerNode.Children.Add(setting);
Cms_Folder schedule = new Cms_Folder();
schedule.FolderName = "Schedule";
schedule.FolderType = (int)FolderType.Schedule;
schedule.IconCls = FolderType.Schedule.ToString();
schedule.SchemaName = FolderType.Schedule.ToString();
schedule.Order = 1;
schedule.aspnet_Applications = app;
siteManagerNode.Children.Add(schedule);
Cms_Folder workflow = new Cms_Folder();
workflow.FolderName = "Workflow";
workflow.FolderType = (int)FolderType.Workflow;
workflow.IconCls = FolderType.Workflow.ToString();
workflow.SchemaName = FolderType.Workflow.ToString();
workflow.Order = 3;
workflow.CanInherit = true;
workflow.aspnet_Applications = app;
siteManagerNode.Children.Add(workflow);
Cms_Folder searchSetting = new Cms_Folder();
searchSetting.FolderName = "SearchSetting";
searchSetting.FolderType = (int)FolderType.SearchSetting;
searchSetting.IconCls = FolderType.SearchSetting.ToString();
searchSetting.SchemaName = FolderType.SearchSetting.ToString();
searchSetting.Order = 5;
searchSetting.CanInherit = false;
searchSetting.aspnet_Applications = app;
siteManagerNode.Children.Add(searchSetting);
Cms_Folder rss = new Cms_Folder();
rss.FolderName = "Rss";
rss.FolderType = (int)FolderType.Rss;
rss.IconCls = FolderType.Rss.ToString();
rss.SchemaName = FolderType.Rss.ToString();
rss.Order = 10;
rss.CanInherit = false;
rss.aspnet_Applications = app;
siteManagerNode.Children.Add(rss);
#region Schema Node
Cms_Folder schema = AddSchemaFolder(9, app);
schema.CanInherit = true;
// schema.Base = dataContext.QueryFolders(app.BaseApplication, FolderType.Schema).First();
siteManagerNode.Children.Add(schema);
#endregion
#endregion
#region Template Node
Cms_Folder template = AddTemplateFolder(4, app);
template.CanInherit = true;
applicationFolder.Children.Add(template);
#endregion
#region Plugin Node
Cms_Folder plugin = AddPluginFolder(5, app);
plugin.CanInherit = true;
applicationFolder.Children.Add(plugin);
#endregion
#region Content Node
Cms_Folder textContentRoot = new Cms_Folder();
textContentRoot.FolderName = FolderType.Content.ToString();
textContentRoot.FolderType = (int)FolderType.Content;
textContentRoot.Order = 6;
textContentRoot.IconCls = FolderType.Content.ToString();
textContentRoot.CanInherit = true;
textContentRoot.FolderPath = GetFolderRelativePath(applicationName, FolderType.Content.ToString());
textContentRoot.aspnet_Applications = app;
applicationFolder.Children.Add(textContentRoot);
#endregion
#region Page Node
Cms_Folder page = new Cms_Folder();
page.FolderName = "Page";
page.FolderType = (int)FolderType.Page;
page.Order = 8;
page.CanInherit = true;
page.SchemaName = FolderType.Page.ToString();
page.IconCls = FolderType.Page.ToString();
page.aspnet_Applications = app;
applicationFolder.Children.Add(page);
#endregion
app.Initialized = true;
dataContext.SaveChanges();
#region Save Folder Properties
NameValueCollection propertyValues = new NameValueCollection();
propertyValues["BaseApplication"] = app.BaseApplication;
var folderService = UnityManager.Resolve<FolderService>();
//folderService.SaveProperties(applicationFolder, propertyValues);
#endregion
CopySiteTemplate(app.ApplicationName);
}
}
private string GetFolderRelativePath(string application, string folderName)
{
//string filePath = Path.Combine("ContentFiles", application);
return Path.Combine(application, folderName);
}
private void CopySiteTemplate(string application)
{
if (StringExtensions.IsNullOrEmptyTrim(CmsGlobal.SiteTemplatePath))
{
return;
}
string appTemplatePath = TemplateFileManager.GetAbsoluteTemplatePath(application);
if (!Directory.Exists(appTemplatePath))
{
Directory.CreateDirectory(appTemplatePath);
}
FileExtensions.CopyDirectory(CmsGlobal.SiteTemplatePath, appTemplatePath);
}
#endregion
#region IApplicationInitializer Members
public virtual void RemoveCmsApplication(string applicationName)
{
var dataContext = EverestCmsEntities.GetDataContext();
if (dataContext.QueryApplicationsByBase(applicationName).Exists())
{
throw new Exception(Resources.AppliationInherited);
}
UnityManager.Resolve<IContentProvider>().SystemManager.DeleteApplication(dataContext, applicationName);
try
{
Everest.Library.Providers.Caching.CacheManager.RemoveAllGroups();
TemplateFileManager.DeleteTemplateDirectory(applicationName);
ContentFileManager.DeleteContentFiles(applicationName);
}
catch (Exception e)
{
Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(e);
}
}
#endregion
}
}
|