2010-04-28 18:21:32 -07:00
using System ;
using System.Linq ;
using System.Web ;
using Orchard.Comments.Models ;
using Orchard.ContentManagement ;
2010-06-04 13:37:34 -07:00
using Orchard.ContentManagement.MetaData ;
2010-04-28 18:21:32 -07:00
using Orchard.Core.Common.Models ;
2010-06-29 15:07:40 -07:00
using Orchard.Core.Common.Settings ;
2010-04-28 18:21:32 -07:00
using Orchard.Core.Navigation.Models ;
2010-07-13 02:52:02 -07:00
using Orchard.Core.Routable.Models ;
2010-04-28 18:21:32 -07:00
using Orchard.Core.Settings.Models ;
using Orchard.Data ;
2010-06-29 16:17:08 -07:00
using Orchard.Data.Migration.Interpreters ;
using Orchard.Data.Migration.Schema ;
2010-04-28 18:21:32 -07:00
using Orchard.Environment ;
using Orchard.Environment.Configuration ;
using Orchard.Environment.ShellBuilders ;
2010-06-11 17:28:46 -07:00
using Orchard.Environment.Descriptor ;
using Orchard.Environment.Descriptor.Models ;
2010-07-09 13:16:02 -07:00
using Orchard.Indexing ;
2010-04-28 18:21:32 -07:00
using Orchard.Localization ;
2010-06-01 17:09:26 -07:00
using Orchard.Localization.Services ;
2010-07-09 17:56:58 -07:00
using Orchard.Reports.Services ;
2010-04-28 18:21:32 -07:00
using Orchard.Security ;
using Orchard.Settings ;
using Orchard.Themes ;
2010-06-25 17:06:02 -07:00
using Orchard.Environment.State ;
2010-06-29 16:17:08 -07:00
using Orchard.Data.Migration ;
2010-04-28 18:21:32 -07:00
namespace Orchard.Setup.Services {
public class SetupService : ISetupService {
private readonly ShellSettings _shellSettings ;
private readonly IOrchardHost _orchardHost ;
private readonly IShellSettingsManager _shellSettingsManager ;
private readonly IShellContainerFactory _shellContainerFactory ;
private readonly ICompositionStrategy _compositionStrategy ;
2010-06-25 17:06:02 -07:00
private readonly IProcessingEngine _processingEngine ;
2010-04-28 18:21:32 -07:00
public SetupService (
ShellSettings shellSettings ,
IOrchardHost orchardHost ,
IShellSettingsManager shellSettingsManager ,
IShellContainerFactory shellContainerFactory ,
2010-06-25 17:06:02 -07:00
ICompositionStrategy compositionStrategy ,
2010-06-29 11:28:21 -07:00
IProcessingEngine processingEngine ) {
2010-04-28 18:21:32 -07:00
_shellSettings = shellSettings ;
_orchardHost = orchardHost ;
_shellSettingsManager = shellSettingsManager ;
_shellContainerFactory = shellContainerFactory ;
_compositionStrategy = compositionStrategy ;
2010-06-25 17:06:02 -07:00
_processingEngine = processingEngine ;
2010-04-28 18:21:32 -07:00
T = NullLocalizer . Instance ;
}
2010-06-02 13:54:50 -07:00
public Localizer T { get ; set ; }
2010-04-28 18:21:32 -07:00
2010-05-10 16:01:04 -07:00
public ShellSettings Prime ( ) {
return _shellSettings ;
}
2010-04-28 18:21:32 -07:00
public void Setup ( SetupContext context ) {
2010-05-13 19:07:53 -07:00
// The vanilla Orchard distibution has the following features enabled.
if ( context . EnabledFeatures = = null | | context . EnabledFeatures . Count ( ) = = 0 ) {
string [ ] hardcoded = {
"Orchard.Framework" ,
"Common" ,
2010-07-14 15:31:43 -07:00
"PublishLater" ,
2010-06-07 16:02:54 -07:00
"Contents" ,
2010-05-13 19:07:53 -07:00
"Dashboard" ,
2010-07-09 17:56:58 -07:00
"Reports" ,
2010-05-13 19:07:53 -07:00
"Feeds" ,
"HomePage" ,
"Navigation" ,
"Scheduling" ,
2010-06-02 15:56:54 -07:00
"Indexing" ,
2010-06-07 11:22:40 -07:00
"Localization" ,
2010-07-13 02:52:02 -07:00
"Routable" ,
2010-05-13 19:07:53 -07:00
"Settings" ,
"XmlRpc" ,
"Orchard.Users" ,
"Orchard.Roles" ,
"TinyMce" ,
"Orchard.Modules" ,
"Orchard.Themes" ,
"Orchard.Blogs" ,
"Orchard.Comments" ,
"Orchard.Tags" ,
"Orchard.Media" ,
"Futures.Widgets" } ;
context . EnabledFeatures = hardcoded ;
}
2010-07-08 15:18:13 -07:00
2010-05-10 16:01:04 -07:00
var shellSettings = new ShellSettings ( _shellSettings ) ;
2010-06-04 13:37:34 -07:00
2010-05-10 16:01:04 -07:00
if ( string . IsNullOrEmpty ( shellSettings . DataProvider ) ) {
shellSettings . DataProvider = context . DatabaseProvider ;
shellSettings . DataConnectionString = context . DatabaseConnectionString ;
shellSettings . DataTablePrefix = context . DatabaseTablePrefix ;
2010-05-13 19:07:53 -07:00
}
2010-04-28 18:21:32 -07:00
var shellDescriptor = new ShellDescriptor {
2010-05-28 13:03:57 -07:00
Features = context . EnabledFeatures . Select ( name = > new ShellFeature { Name = name } )
2010-04-28 18:21:32 -07:00
} ;
var shellToplogy = _compositionStrategy . Compose ( shellSettings , shellDescriptor ) ;
// initialize database explicitly, and store shell descriptor
var bootstrapLifetimeScope = _shellContainerFactory . CreateContainer ( shellSettings , shellToplogy ) ;
using ( var environment = new StandaloneEnvironment ( bootstrapLifetimeScope ) ) {
2010-06-29 11:28:21 -07:00
var schemaBuilder = new SchemaBuilder ( environment . Resolve < IDataMigrationInterpreter > ( ) ) ;
2010-07-09 17:56:58 -07:00
var reportsCoordinator = environment . Resolve < IReportsCoordinator > ( ) ;
reportsCoordinator . Register ( "Data Migration" , "Setup" , "Orchard installation" ) ;
2010-06-29 11:28:21 -07:00
schemaBuilder . CreateTable ( "Orchard_Framework_DataMigrationRecord" , table = > table
2010-07-08 15:18:13 -07:00
. Column < int > ( "Id" , column = > column . PrimaryKey ( ) . Identity ( ) )
2010-06-29 11:28:21 -07:00
. Column < string > ( "DataMigrationClass" )
2010-07-08 11:35:34 -07:00
. Column < int > ( "Version" ) ) ;
2010-06-29 11:28:21 -07:00
var dataMigrationManager = environment . Resolve < IDataMigrationManager > ( ) ;
2010-07-13 16:34:55 -07:00
foreach ( var feature in context . EnabledFeatures ) {
dataMigrationManager . Update ( feature ) ;
}
2010-04-28 18:21:32 -07:00
environment . Resolve < IShellDescriptorManager > ( ) . UpdateShellDescriptor (
0 ,
2010-05-28 13:03:57 -07:00
shellDescriptor . Features ,
2010-04-28 18:21:32 -07:00
shellDescriptor . Parameters ) ;
}
2010-06-25 17:06:02 -07:00
// in effect "pump messages" see PostMessage circa 1980
while ( _processingEngine . AreTasksPending ( ) )
_processingEngine . ExecuteNextTask ( ) ;
2010-04-28 18:21:32 -07:00
// creating a standalone environment.
// in theory this environment can be used to resolve any normal components by interface, and those
// components will exist entirely in isolation - no crossover between the safemode container currently in effect
// must mark state as Running - otherwise standalone enviro is created "for setup"
shellSettings . State = new TenantState ( "Running" ) ;
using ( var environment = _orchardHost . CreateStandaloneEnvironment ( shellSettings ) ) {
try {
// create superuser
var membershipService = environment . Resolve < IMembershipService > ( ) ;
var user =
membershipService . CreateUser ( new CreateUserParams ( context . AdminUsername , context . AdminPassword ,
String . Empty , String . Empty , String . Empty ,
true ) ) ;
// set site name and settings
var siteService = environment . Resolve < ISiteService > ( ) ;
var siteSettings = siteService . GetSiteSettings ( ) . As < SiteSettings > ( ) ;
siteSettings . Record . SiteSalt = Guid . NewGuid ( ) . ToString ( "N" ) ;
siteSettings . Record . SiteName = context . SiteName ;
siteSettings . Record . SuperUser = context . AdminUsername ;
siteSettings . Record . PageTitleSeparator = " - " ;
2010-06-01 15:00:28 -07:00
siteSettings . Record . SiteCulture = "en-US" ;
2010-04-28 18:21:32 -07:00
// set site theme
var themeService = environment . Resolve < IThemeService > ( ) ;
themeService . SetSiteTheme ( "Classic" ) ;
2010-06-01 15:00:28 -07:00
// add default culture
2010-06-01 13:26:16 -07:00
var cultureManager = environment . Resolve < ICultureManager > ( ) ;
cultureManager . AddCulture ( "en-US" ) ;
2010-07-07 11:15:48 -07:00
cultureManager . AddCulture ( "fr" ) ;
2010-06-01 13:26:16 -07:00
2010-04-28 18:21:32 -07:00
var contentManager = environment . Resolve < IContentManager > ( ) ;
2010-05-28 14:01:10 -07:00
// this needs to exit the standalone environment? rework this process entirely?
2010-04-28 18:21:32 -07:00
// simulate installation-time module activation events
2010-05-28 14:01:10 -07:00
//var hackInstallationGenerator = environment.Resolve<IHackInstallationGenerator>();
//hackInstallationGenerator.GenerateInstallEvents();
2010-04-28 18:21:32 -07:00
2010-06-08 16:25:56 -07:00
var contentDefinitionManager = environment . Resolve < IContentDefinitionManager > ( ) ;
2010-07-14 16:28:43 -07:00
contentDefinitionManager . AlterTypeDefinition ( "BlogPost" , cfg = > cfg
. DisplayedAs ( "Blog Post" )
. WithPart ( "HasComments" )
. WithPart ( "HasTags" )
. WithPart ( "Localized" )
. Indexed ( ) ) ;
contentDefinitionManager . AlterTypeDefinition ( "Page" , cfg = > cfg
. DisplayedAs ( "Page" )
. WithPart ( "CommonAspect" )
. WithPart ( "PublishLaterPart" )
. WithPart ( "IsRoutable" )
. WithPart ( "BodyAspect" )
. WithPart ( "HasComments" )
. WithPart ( "HasTags" )
. WithPart ( "Localized" )
. Indexed ( ) ) ;
contentDefinitionManager . AlterPartDefinition ( "BodyAspect" , cfg = > cfg
. WithSetting ( "BodyPartSettings.FlavorDefault" , BodyPartSettings . FlavorDefaultDefault ) ) ;
2010-06-08 16:25:56 -07:00
2010-04-28 18:21:32 -07:00
// create home page as a CMS page
2010-06-24 16:34:10 -07:00
var page = contentManager . Create ( "Page" , VersionOptions . Draft ) ;
2010-04-28 18:21:32 -07:00
page . As < BodyAspect > ( ) . Text = "<p>Welcome to Orchard!</p><p>Congratulations, you've successfully set-up your Orchard site.</p><p>This is the home page of your new site. We've taken the liberty to write here about a few things you could look at next in order to get familiar with the application. Once you feel confident you don't need this anymore, just click <a href=\"Admin/Pages/Edit/3\">Edit</a> to go into edit mode and replace this with whatever you want on your home page to make it your own.</p><p>One thing you could do (but you don't have to) is go into <a href=\"Admin/Settings\">Manage Settings</a> (follow the <a href=\"Admin\">Admin</a> link and then look for it under \"Settings\" in the menu on the left) and check that everything is configured the way you want.</p><p>You probably want to make the site your own. One of the ways you can do that is by clicking <a href=\"Admin/Themes\">Manage Themes</a> in the admin menu. A theme is a packaged look and feel that affects the whole site.</p><p>Next, you can start playing with the content types that we installed. For example, go ahead and click <a href=\"Admin/Pages/Create\">Add New Page</a> in the admin menu and create an \"about\" page. Then, add it to the navigation menu by going to <a href=\"Admin/Navigation\">Manage Menu</a>. You can also click <a href=\"Admin/Blogs/Create\">Add New Blog</a> and start posting by clicking \"Add New Post\".</p><p>Finally, Orchard has been designed to be extended. It comes with a few built-in modules such as pages and blogs or themes. You can install new themes by going to <a href=\"Admin/Themes\">Manage Themes</a> and clicking <a href=\"Admin/Themes/Install\">Install a new Theme</a>. Like for themes, modules are created by other users of Orchard just like you so if you feel up to it, please <a href=\"http://www.orchardproject.net/\">consider participating</a>.</p><p>--The Orchard Crew</p>" ;
2010-07-13 02:52:02 -07:00
page . As < IsRoutable > ( ) . Slug = "home" ;
2010-07-13 20:15:55 -07:00
page . As < IsRoutable > ( ) . Path = "home" ;
2010-07-13 02:52:02 -07:00
page . As < IsRoutable > ( ) . Title = T ( "Home" ) . ToString ( ) ;
2010-04-28 18:21:32 -07:00
page . As < CommonAspect > ( ) . Owner = user ;
2010-05-13 19:07:53 -07:00
if ( page . Has < HasComments > ( ) ) {
page . As < HasComments > ( ) . CommentsShown = false ;
}
2010-04-28 18:21:32 -07:00
contentManager . Publish ( page ) ;
2010-07-13 17:19:08 -07:00
siteSettings . Record . HomePage = "RoutableHomePageProvider;" + page . Id ;
2010-04-28 18:21:32 -07:00
// add a menu item for the shiny new home page
2010-06-24 16:34:10 -07:00
var menuItem = contentManager . Create ( "MenuItem" ) ;
2010-04-28 18:21:32 -07:00
menuItem . As < MenuPart > ( ) . MenuPosition = "1" ;
menuItem . As < MenuPart > ( ) . MenuText = T ( "Home" ) . ToString ( ) ;
menuItem . As < MenuPart > ( ) . OnMainMenu = true ;
menuItem . As < MenuItem > ( ) . Url = "" ;
//Temporary fix for running setup on command line
if ( HttpContext . Current ! = null ) {
var authenticationService = environment . Resolve < IAuthenticationService > ( ) ;
authenticationService . SignIn ( user , true ) ;
}
}
catch {
environment . Resolve < ITransactionManager > ( ) . Cancel ( ) ;
throw ;
}
}
_shellSettingsManager . SaveSettings ( shellSettings ) ;
}
}
}