/src/Libraries/Nop.Services/Installation/SqlFileInstallationService.cs
C# | 316 lines | 253 code | 57 blank | 6 comment | 20 complexity | 3372736d1bffccbc35861ef8c3f96605 MD5 | raw file
Possible License(s): LGPL-2.1
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Xml;
- using Nop.Core;
- using Nop.Core.Configuration;
- using Nop.Core.Data;
- using Nop.Core.Domain;
- using Nop.Core.Domain.Blogs;
- using Nop.Core.Domain.Catalog;
- using Nop.Core.Domain.Cms;
- using Nop.Core.Domain.Common;
- using Nop.Core.Domain.Customers;
- using Nop.Core.Domain.Directory;
- using Nop.Core.Domain.Discounts;
- using Nop.Core.Domain.Forums;
- using Nop.Core.Domain.Localization;
- using Nop.Core.Domain.Logging;
- using Nop.Core.Domain.Media;
- using Nop.Core.Domain.Messages;
- using Nop.Core.Domain.News;
- using Nop.Core.Domain.Orders;
- using Nop.Core.Domain.Payments;
- using Nop.Core.Domain.Polls;
- using Nop.Core.Domain.Security;
- using Nop.Core.Domain.Shipping;
- using Nop.Core.Domain.Tasks;
- using Nop.Core.Domain.Tax;
- using Nop.Core.Domain.Topics;
- using Nop.Core.Infrastructure;
- using Nop.Core.IO;
- using Nop.Data;
- using Nop.Services.Common;
- using Nop.Services.Customers;
- using Nop.Services.Helpers;
- using Nop.Services.Media;
- using Nop.Services.Localization;
- using Nop.Services.Seo;
- using Nop.Core.Domain.Seo;
-
- namespace Nop.Services.Installation
- {
- public partial class SqlFileInstallationService : IInstallationService
- {
- #region Fields
-
- private readonly IRepository<Language> _languageRepository;
- private readonly IRepository<Customer> _customerRepository;
- private readonly IDataProvider _dataProvider;
- private readonly IDbContext _dbContext;
- private readonly IWebHelper _webHelper;
-
- #endregion
-
- #region Ctor
-
- public SqlFileInstallationService(IRepository<Language> languageRepository,
- IRepository<Customer> customerRepository,
- IDataProvider dataProvider,
- IDbContext dbContext,
- IWebHelper webHelper)
- {
- this._languageRepository = languageRepository;
- this._customerRepository = customerRepository;
- this._dataProvider = dataProvider;
- this._dbContext = dbContext;
- this._webHelper = webHelper;
- }
-
- #endregion
-
- #region Classes
-
- private class LocaleStringResourceParent : LocaleStringResource
- {
- public LocaleStringResourceParent(XmlNode localStringResource, string nameSpace = "")
- {
- Namespace = nameSpace;
- var resNameAttribute = localStringResource.Attributes["Name"];
- var resValueNode = localStringResource.SelectSingleNode("Value");
-
- if (resNameAttribute == null)
- {
- throw new NopException("All language resources must have an attribute Name=\"Value\".");
- }
- var resName = resNameAttribute.Value.Trim();
- if (string.IsNullOrEmpty(resName))
- {
- throw new NopException("All languages resource attributes 'Name' must have a value.'");
- }
- ResourceName = resName;
-
- if (resValueNode == null || string.IsNullOrEmpty(resValueNode.InnerText.Trim()))
- {
- IsPersistable = false;
- }
- else
- {
- IsPersistable = true;
- ResourceValue = resValueNode.InnerText.Trim();
- }
-
- foreach (XmlNode childResource in localStringResource.SelectNodes("Children/LocaleResource"))
- {
- ChildLocaleStringResources.Add(new LocaleStringResourceParent(childResource, NameWithNamespace));
- }
- }
- public string Namespace { get; set; }
- public IList<LocaleStringResourceParent> ChildLocaleStringResources = new List<LocaleStringResourceParent>();
-
- public bool IsPersistable { get; set; }
-
- public string NameWithNamespace
- {
- get
- {
- var newNamespace = Namespace;
- if (!string.IsNullOrEmpty(newNamespace))
- {
- newNamespace += ".";
- }
- return newNamespace + ResourceName;
- }
- }
- }
-
- private class ComparisonComparer<T> : IComparer<T>, IComparer
- {
- private readonly Comparison<T> _comparison;
-
- public ComparisonComparer(Comparison<T> comparison)
- {
- _comparison = comparison;
- }
-
- public int Compare(T x, T y)
- {
- return _comparison(x, y);
- }
-
- public int Compare(object o1, object o2)
- {
- return _comparison((T)o1, (T)o2);
- }
- }
-
- #endregion
-
- #region Utilities
-
- private void RecursivelyWriteResource(LocaleStringResourceParent resource, XmlWriter writer)
- {
- //The value isn't actually used, but the name is used to create a namespace.
- if (resource.IsPersistable)
- {
- writer.WriteStartElement("LocaleResource", "");
-
- writer.WriteStartAttribute("Name", "");
- writer.WriteString(resource.NameWithNamespace);
- writer.WriteEndAttribute();
-
- writer.WriteStartElement("Value", "");
- writer.WriteString(resource.ResourceValue);
- writer.WriteEndElement();
-
- writer.WriteEndElement();
- }
-
- foreach (var child in resource.ChildLocaleStringResources)
- {
- RecursivelyWriteResource(child, writer);
- }
-
- }
-
- private void RecursivelySortChildrenResource(LocaleStringResourceParent resource)
- {
- ArrayList.Adapter((IList)resource.ChildLocaleStringResources).Sort(new ComparisonComparer<LocaleStringResourceParent>((x1, x2) => x1.ResourceName.CompareTo(x2.ResourceName)));
-
- foreach (var child in resource.ChildLocaleStringResources)
- {
- RecursivelySortChildrenResource(child);
- }
-
- }
-
- protected virtual void InstallLocaleResources()
- {
- //'English' language
- var language = _languageRepository.Table.Where(l => l.Name == "English").Single();
-
- //save resoureces
- foreach (var filePath in System.IO.Directory.EnumerateFiles(_webHelper.MapPath("~/App_Data/Localization/"), "*.nopres.xml", SearchOption.TopDirectoryOnly))
- {
- #region Parse resource files (with <Children> elements)
- //read and parse original file with resources (with <Children> elements)
-
- var originalXmlDocument = new XmlDocument();
- originalXmlDocument.Load(filePath);
-
- var resources = new List<LocaleStringResourceParent>();
-
- foreach (XmlNode resNode in originalXmlDocument.SelectNodes(@"//Language/LocaleResource"))
- resources.Add(new LocaleStringResourceParent(resNode));
-
- resources.Sort((x1, x2) => x1.ResourceName.CompareTo(x2.ResourceName));
-
- foreach (var resource in resources)
- RecursivelySortChildrenResource(resource);
-
- var sb = new StringBuilder();
- var writer = XmlWriter.Create(sb);
- writer.WriteStartDocument();
- writer.WriteStartElement("Language", "");
-
- writer.WriteStartAttribute("Name", "");
- writer.WriteString(originalXmlDocument.SelectSingleNode(@"//Language").Attributes["Name"].InnerText.Trim());
- writer.WriteEndAttribute();
-
- foreach (var resource in resources)
- RecursivelyWriteResource(resource, writer);
-
- writer.WriteEndElement();
- writer.WriteEndDocument();
- writer.Flush();
-
- var parsedXml = sb.ToString();
- #endregion
-
- //now we have a parsed XML file (the same structure as exported language packs)
- //let's save resources
- var localizationService = EngineContext.Current.Resolve<ILocalizationService>();
- localizationService.ImportResourcesFromXml(language, parsedXml);
- }
-
- }
-
- protected virtual void UpdateDefaultCustomer(string defaultUserEmail, string defaultUserPassword)
- {
- var adminUser = _customerRepository.Table.Single(x => !x.IsSystemAccount);
- if (adminUser == null)
- throw new Exception("Admin user cannot be loaded");
-
- adminUser.CustomerGuid = Guid.NewGuid();
- adminUser.Email = defaultUserEmail;
- adminUser.Username = defaultUserEmail;
- _customerRepository.Update(adminUser);
-
- var customerRegistrationService = EngineContext.Current.Resolve<ICustomerRegistrationService>();
- customerRegistrationService.ChangePassword(new ChangePasswordRequest(defaultUserEmail, false,
- PasswordFormat.Hashed, defaultUserPassword));
- }
-
- protected virtual void ExecuteSqlFile(string path)
- {
- var statements = new List<string>();
-
- using (var stream = File.OpenRead(path))
- using (var reader = new StreamReader(stream))
- {
- string statement = "";
- while ((statement = ReadNextStatementFromStream(reader)) != null)
- statements.Add(statement);
- }
-
- foreach (string stmt in statements)
- _dbContext.ExecuteSqlCommand(stmt);
- }
-
- protected virtual string ReadNextStatementFromStream(StreamReader reader)
- {
- var sb = new StringBuilder();
- string lineOfText = "";
- while (true)
- {
- lineOfText = reader.ReadLine();
- if (lineOfText == null)
- {
- if (sb.Length > 0)
- return sb.ToString();
- else
- return null;
- }
-
- if (lineOfText.TrimEnd().ToUpper() == "GO")
- break;
-
- sb.Append(lineOfText + Environment.NewLine);
- }
-
- return sb.ToString();
- }
-
- #endregion
-
- #region Methods
-
- public virtual void InstallData(string defaultUserEmail,
- string defaultUserPassword, bool installSampleData = true)
- {
- ExecuteSqlFile(_webHelper.MapPath("~/App_Data/Install/create_required_data.sql"));
- InstallLocaleResources();
- UpdateDefaultCustomer(defaultUserEmail, defaultUserPassword);
-
- if (installSampleData)
- {
- ExecuteSqlFile(_webHelper.MapPath("~/App_Data/Install/create_sample_data.sql"));
- }
- }
-
- #endregion
- }
- }