示例#1
0
		/// <summary>
		/// Saves the current edit model.
		/// </summary>
		/// <returns>Whether the entity was updated or not</returns>
		public bool Save() {
			using (var db = new DataContext()) {
				InternalId = (!String.IsNullOrEmpty(InternalId) ? InternalId.Replace(" ", "") : Name.Replace(" ", "")).ToUpper() ;

				var site = db.SiteTrees.Where(s => s.Id == Id).SingleOrDefault() ;
				if (site == null) {
					// Create new dedicated namespace
					var name = new Namespace() {
						Id = Guid.NewGuid(),
						Name = "Site namespace",
						InternalId = InternalId,
						Description = "Namespace for the site " + InternalId,
					} ;
					db.Namespaces.Add(name) ;

					// Create site
					site = new SiteTree() ;
					site.Id = Id ;
					site.NamespaceId = NamespaceId = name.Id ;
					db.SiteTrees.Add(site) ;

				}

				// If we've changed namespace, update all related permalinks.
				if (site.NamespaceId != NamespaceId)
					ChangeNamespace(db, Id, NamespaceId) ;

				// Update the site tree
				site.NamespaceId = NamespaceId ;
				site.InternalId = InternalId ;
				site.Name = Name ;
				site.HostNames = HostNames ;
				site.Description = Description ;

				// Update the site template
				var template = db.PageTemplates.Include(pt => pt.RegionTemplates).Where(pt => pt.Id == Id && pt.IsSiteTemplate).SingleOrDefault() ;
				if (template == null) {
					template = new PageTemplate() ;
					db.PageTemplates.Add(template) ;
					
					template.Id = Id ;
					template.Name = Id.ToString() ;
					template.IsSiteTemplate = true ;
				}
				template.Preview = Template.Preview ;
				template.Properties = Template.Properties ;

				// Update the regions
				var currentRegions = new List<Guid>() ;
				foreach (var reg in Regions) {
					var region = template.RegionTemplates.Where(r => r.Id == reg.Id).SingleOrDefault() ;
					if (region == null) {
						region = new RegionTemplate() ;
						db.RegionTemplates.Add(region) ;
						template.RegionTemplates.Add(region) ;

						region.Id = Guid.NewGuid() ;
						region.TemplateId = template.Id ;
						region.Type = reg.Type ;
					}
					region.Name = reg.Name ;
					region.InternalId = reg.InternalId ;
					region.Seqno = reg.Seqno ;
					region.Description = reg.Description ;
					currentRegions.Add(region.Id) ;
				}
				// Delete removed regions
				foreach (var reg in template.RegionTemplates.Where(r => !currentRegions.Contains(r.Id)).ToList()) {
					db.RegionTemplates.Remove(reg) ;
				}

				// Check that we have a site page, if not, create it
				var page = db.Pages.Where(p => p.SiteTreeId == site.Id && p.TemplateId == site.Id).SingleOrDefault() ;
				if (page == null) {
					// Create page
					page = new Page() {
						Id = Guid.NewGuid(),
						SiteTreeId = site.Id,
						TemplateId = site.Id,
						ParentId = site.Id,
						Title = site.Id.ToString(),
						PermalinkId = Guid.NewGuid()
					} ;

					// Create published version
					var published = page.Clone() ;
					published.IsDraft = false ;

					// Create permalink
					var permalink = new Permalink() {
						Id = page.PermalinkId,
						NamespaceId = site.NamespaceId,
						Name = site.Id.ToString(),
						Type = "SITE"
					} ;

					// Attach to context
					page.Attach(db, EntityState.Added) ;
					published.Attach(db, EntityState.Added) ;
					permalink.Attach(db, EntityState.Added) ;
				}
				var ret = db.SaveChanges() > 0 ;

				Id = site.Id ;

				// Refresh host name configuration
				if (ret)
					WebPages.WebPiranha.RegisterDefaultHostNames() ;
				return ret ;
			}
		}
示例#2
0
        /// <summary>
        /// Saves the current edit model.
        /// </summary>
        /// <returns>Whether the entity was updated or not</returns>
        public bool Save()
        {
            using (var db = new DataContext()) {
                InternalId = (!String.IsNullOrEmpty(InternalId) ? InternalId.Replace(" ", "") : Name.Replace(" ", "")).ToUpper();

                var site = db.SiteTrees.Where(s => s.Id == Id).SingleOrDefault();
                if (site == null)
                {
                    // Create new dedicated namespace
                    var name = new Namespace()
                    {
                        Id          = Guid.NewGuid(),
                        Name        = "Site namespace",
                        InternalId  = InternalId,
                        Description = "Namespace for the site " + InternalId,
                    };
                    db.Namespaces.Add(name);

                    // Create site
                    site             = new SiteTree();
                    site.Id          = Id;
                    site.NamespaceId = NamespaceId = name.Id;
                    db.SiteTrees.Add(site);
                }

                // If we've changed namespace, update all related permalinks.
                if (site.NamespaceId != NamespaceId)
                {
                    ChangeNamespace(db, Id, NamespaceId);
                }

                // Update the site tree
                site.NamespaceId = NamespaceId;
                site.InternalId  = InternalId;
                site.Name        = Name;
                site.HostNames   = HostNames;
                site.Description = Description;

                // Update the site template
                var template = db.PageTemplates.Include(pt => pt.RegionTemplates).Where(pt => pt.Id == Id && pt.IsSiteTemplate).SingleOrDefault();
                if (template == null)
                {
                    template = new PageTemplate();
                    db.PageTemplates.Add(template);

                    template.Id             = Id;
                    template.Name           = Id.ToString();
                    template.IsSiteTemplate = true;
                }
                template.Preview    = Template.Preview;
                template.Properties = Template.Properties;

                // Update the regions
                var currentRegions = new List <Guid>();
                foreach (var reg in Regions)
                {
                    var region = template.RegionTemplates.Where(r => r.Id == reg.Id).SingleOrDefault();
                    if (region == null)
                    {
                        region = new RegionTemplate();
                        db.RegionTemplates.Add(region);
                        template.RegionTemplates.Add(region);

                        region.Id         = Guid.NewGuid();
                        region.TemplateId = template.Id;
                        region.Type       = reg.Type;
                    }
                    region.Name        = reg.Name;
                    region.InternalId  = reg.InternalId;
                    region.Seqno       = reg.Seqno;
                    region.Description = reg.Description;
                    currentRegions.Add(region.Id);
                }
                // Delete removed regions
                foreach (var reg in template.RegionTemplates.Where(r => !currentRegions.Contains(r.Id)).ToList())
                {
                    db.RegionTemplates.Remove(reg);
                }

                // Check that we have a site page, if not, create it
                var page = db.Pages.Where(p => p.SiteTreeId == site.Id && p.TemplateId == site.Id).SingleOrDefault();
                if (page == null)
                {
                    // Create page
                    page = new Page()
                    {
                        Id          = Guid.NewGuid(),
                        SiteTreeId  = site.Id,
                        TemplateId  = site.Id,
                        ParentId    = site.Id,
                        Title       = site.Id.ToString(),
                        PermalinkId = Guid.NewGuid()
                    };

                    // Create published version
                    var published = page.Clone();
                    published.IsDraft = false;

                    // Create permalink
                    var permalink = new Permalink()
                    {
                        Id          = page.PermalinkId,
                        NamespaceId = site.NamespaceId,
                        Name        = site.Id.ToString(),
                        Type        = "SITE"
                    };

                    // Attach to context
                    page.Attach(db, EntityState.Added);
                    published.Attach(db, EntityState.Added);
                    permalink.Attach(db, EntityState.Added);
                }
                var ret = db.SaveChanges() > 0;

                Id = site.Id;

                // Refresh host name configuration
                if (ret)
                {
                    WebPages.WebPiranha.RegisterDefaultHostNames();
                }
                return(ret);
            }
        }