public ActionResult Create(string name, string lang, string title = "", string desc = "", string solution = "", string theme = "") { var app = App.Get(); if (app.Webs[name] != null) { throw new HttpException("There is a web named \"" + name + "\"."); } try { WebDecorator web = null; if (!string.IsNullOrEmpty(solution)) { web = app.Solutions.Install(solution, name, User.Identity.Name, title, desc, theme, lang); } else { web = app.CreateWeb(name, title, desc, lang, theme); if (!string.IsNullOrEmpty(theme)) { web.Theme = theme; } var defaultPage = web.CreatePage("Default"); } string webJson = JsonConvert.SerializeObject(web.ToObject()); return(Content(webJson, "application/json", Encoding.UTF8)); } catch (Exception e) { //var msg = e.Message; Exception innerExpt = e.InnerException; var errors = new StringBuilder(); errors.AppendLine(e.Message); if (app.Webs[name] != null) { App.Get().DataContext.Delete <Web>(w => w.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); App.Get().DataContext.SaveChanges(); } while (innerExpt != null) { //msg = innerExpt.Message; errors.AppendLine(innerExpt.Message); innerExpt = innerExpt.InnerException; } throw new HttpException(errors.ToString()); } }
/// <summary> /// Initializes a new instance of CategoryCollection class with datacontext, web object and parent category object. /// </summary> /// <param name="context">The data context object.</param> /// <param name="parentWeb">The web contains categories.</param> /// <param name="parent">The parent category.</param> public CategoryCollection(IDataContext context, WebDecorator parentWeb, CategoryDecorator parent = null) { this.DataContext = context; this.Parent = parent; this.ParentWeb = parentWeb; }
public WebDecorator Install(string name, string webName, string owner, string title, string desc = "", string theme = "default", string lang = "") { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } if (string.IsNullOrEmpty(webName)) { throw new ArgumentNullException("webName"); } if (DataContext == null) { throw new ArgumentNullException("DataContext could not be null"); } var defaultLocale = App.Settings.DefaultLocale; var pkg = Packages[name]; if (pkg.GetParams().Count == 0) { pkg.AddParam("app_path", httpContext.Request.ApplicationPath); pkg.AddParam("web_name", webName); pkg.AddParam("web_title", webName); } if (pkg == null) { throw new Exception("\"" + name + "\" solution package not found"); } var supports = pkg.GetSupportLanguages(); var installLocale = string.IsNullOrEmpty(lang) ? defaultLocale.ToLower() : lang.ToLower(); var tmpl = pkg.Locale(installLocale); var web = this.DataContext.Find <Web>(w => w.Name.Equals(webName, StringComparison.OrdinalIgnoreCase)); var solutionName = name + "." + installLocale; #region deserialize the web element and create new instance if (web == null) { web = new Web() { Created = DateTime.UtcNow, MostOnlined = DateTime.UtcNow, MostOnlineUserCount = 1, IsEnabled = true }; //web.Popuplate(tmpl); web.Name = webName; web.InstalledSolutions = solutionName; if (!string.IsNullOrEmpty(title)) { web.Title = title; } if (!string.IsNullOrEmpty(desc)) { web.Description = desc; } if (tmpl.LogoImage != null) { web.LogoImageUrl = tmpl.LogoImage.Ref; } if (tmpl.ShortcutIcon != null) { web.ShortcutIconUrl = tmpl.ShortcutIcon.Ref; } if (!string.IsNullOrEmpty(tmpl.Theme)) { web.Theme = tmpl.Theme; } if (string.IsNullOrEmpty(web.Theme)) { web.Theme = theme; } web.Owner = owner; if (!string.IsNullOrEmpty(lang)) { web.DefaultLocale = lang; } else { web.DefaultLocale = App.Settings.DefaultLocale; } //else //{ // if (string.IsNullOrEmpty(web.DefaultLocale) && !string.IsNullOrEmpty(tmpl.DefaultLanguage)) // { // web.DefaultLocale = tmpl.DefaultLanguage; // } // else // web.DefaultLocale = defaultLocale; //} DataContext.Add(web); } else { if (!web.Owner.Equals(owner)) { throw new UnauthorizedAccessException(string.Format("You are not the owner of the {0}.", web.Name)); } var solutionArgs = web.InstalledSolutions.Split(','); if (solutionArgs.Contains(solutionName)) { throw new Exception("The solution had already installed."); } var wrapper = new WebDecorator(web, DataContext); //wrapper.SwitchLocale(lang); if (!string.IsNullOrEmpty(title)) { web.Title = title; } if (!string.IsNullOrEmpty(desc)) { web.Description = desc; } if (tmpl.LogoImage != null) { web.LogoImageUrl = tmpl.LogoImage.Ref; } if (tmpl.ShortcutIcon != null) { web.ShortcutIconUrl = tmpl.ShortcutIcon.Ref; } web.InstalledSolutions += "," + solutionName; } DataContext.SaveChanges(); #endregion foreach (var wpTmpl in tmpl.Pages) { try { wpTmpl.Install(DataContext, web); } catch (Exception e) { throw new Exception("There is an error occur in creating the \"" + wpTmpl.Title.Text + "\" page", e); } } #region create categories var catsHolder = new Dictionary <int, Category>(); if (tmpl.Categories != null && tmpl.Categories.Categories != null) { foreach (var cat in tmpl.Categories.Categories) { var c = new Category() { //ID = cat.ID, // ParentID = cat.ParentID, Name = cat.Name, WebID = web.Id, Locale = web.DefaultLocale }; if (cat.ID > 0) { c.ID = cat.ID; } DataContext.Add(c); DataContext.SaveChanges(); catsHolder.Add(cat.ID > 0 ? cat.ID : c.ID, c); } var waitForFixed = tmpl.Categories.Categories.Where(c => c.ParentID > 0); foreach (var w in waitForFixed) { if (catsHolder.ContainsKey(w.ID)) { catsHolder[w.ID].ParentID = catsHolder[w.ParentID].ID; } } DataContext.SaveChanges(); } #endregion #region create list if (tmpl.ListRefs != null && tmpl.ListRefs.Count > 0) { var wrapperWeb = App.Get().Wrap(web); var listTmpls = pkg.LoadContentLists(installLocale); foreach (var listTmpl in listTmpls) { try { var list = wrapperWeb.CreateList(listTmpl); //if (list.Items != null && list.TotalItems > 0) //{ // foreach (var item in list.Items) // { // var catArgs = string.IsNullOrEmpty(item.Categories) ? null : item.Categories.Split(','); // if (catArgs != null) // { // var fixedIDs = new List<int>(); // for (int i = 0; i < catArgs.Length; i++) // { // var catID = 0; // if (int.TryParse(catArgs[i], out catID)) // { // if (catsHolder.ContainsKey(catID)) // fixedIDs.Add(catsHolder[catID].ID); // } // } // if (fixedIDs.Count > 0) // item.Categories = string.Join(",", fixedIDs.ToArray()); // } // } //} DataContext.SaveChanges(); list.ClearCache(); } catch (Exception e) { throw new Exception("There is an error occur in creating the \"" + tmpl.Title.Text + "\" list", e); } } } #endregion #region Copy resources var ndRoot = "~/app_data/files/" + (web.Name.Equals("home") ? "public" : "personal/" + web.Name) + "/"; var storagePath = pkg.InstalledPath + "\\files"; if (System.IO.Directory.Exists(storagePath)) { var dirs = System.IO.Directory.GetDirectories(storagePath); foreach (var dir in dirs) { //Copy all files to web storage var srcInfo = new DirectoryInfo(dir); var dest = httpContext.Server.MapPath(ndRoot + srcInfo.Name); DNA.Utility.FileUtility.CopyDirectory(dir, dest); } } #endregion return(new WebDecorator(web, DataContext)); }