diff --git a/src/Umbraco.Core/Persistence/Repositories/DataTypeDefinitionRepository.cs b/src/Umbraco.Core/Persistence/Repositories/DataTypeDefinitionRepository.cs index 7cd7069751..d8bfe62deb 100644 --- a/src/Umbraco.Core/Persistence/Repositories/DataTypeDefinitionRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/DataTypeDefinitionRepository.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Data; using System.Globalization; using System.Linq; using Umbraco.Core.Models; @@ -8,6 +9,7 @@ using Umbraco.Core.Models.Rdbms; using Umbraco.Core.Persistence.Caching; using Umbraco.Core.Persistence.Factories; using Umbraco.Core.Persistence.Querying; +using Umbraco.Core.Persistence.SqlSyntax; using Umbraco.Core.Persistence.UnitOfWork; namespace Umbraco.Core.Persistence.Repositories @@ -119,6 +121,15 @@ namespace Umbraco.Core.Persistence.Repositories { ((DataTypeDefinition)entity).AddingEntity(); + //Cannot add a duplicate data type + var exists = Database.ExecuteScalar(@"SELECT COUNT(*) FROM cmsDataType +INNER JOIN umbracoNode ON cmsDataType.nodeId = umbracoNode.id +WHERE umbracoNode." + SqlSyntaxContext.SqlSyntaxProvider.GetQuotedColumnName("text") + "= @name", new {name = entity.Name}); + if (exists > 0) + { + throw new DuplicateNameException("A data type with the name " + entity.Name + " already exists"); + } + var factory = new DataTypeDefinitionFactory(NodeObjectTypeId); var dto = factory.BuildDto(entity); diff --git a/src/Umbraco.Tests/Persistence/Repositories/DataTypeDefinitionRepositoryTest.cs b/src/Umbraco.Tests/Persistence/Repositories/DataTypeDefinitionRepositoryTest.cs index 3825677fa8..5a3470d457 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/DataTypeDefinitionRepositoryTest.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/DataTypeDefinitionRepositoryTest.cs @@ -1,4 +1,5 @@ using System; +using System.Data; using System.Linq; using NUnit.Framework; using Umbraco.Core; @@ -28,6 +29,57 @@ namespace Umbraco.Tests.Persistence.Repositories return dataTypeDefinitionRepository; } + [Test] + public void Can_Create() + { + var provider = new PetaPocoUnitOfWorkProvider(); + var unitOfWork = provider.GetUnitOfWork(); + int id; + using (var repository = CreateRepository(unitOfWork)) + { + var dataTypeDefinition = new DataTypeDefinition(-1, new Guid(Constants.PropertyEditors.RadioButtonList)) {Name = "test"}; + + repository.AddOrUpdate(dataTypeDefinition); + + unitOfWork.Commit(); + id = dataTypeDefinition.Id; + Assert.That(id, Is.GreaterThan(0)); + } + using (var repository = CreateRepository(unitOfWork)) + { + // Act + var dataTypeDefinition = repository.Get(id); + + // Assert + Assert.That(dataTypeDefinition, Is.Not.Null); + Assert.That(dataTypeDefinition.HasIdentity, Is.True); + } + } + + [Test] + public void Cannot_Create_Duplicate_Name() + { + var provider = new PetaPocoUnitOfWorkProvider(); + var unitOfWork = provider.GetUnitOfWork(); + int id; + using (var repository = CreateRepository(unitOfWork)) + { + var dataTypeDefinition = new DataTypeDefinition(-1, new Guid(Constants.PropertyEditors.RadioButtonList)) { Name = "test" }; + repository.AddOrUpdate(dataTypeDefinition); + unitOfWork.Commit(); + id = dataTypeDefinition.Id; + Assert.That(id, Is.GreaterThan(0)); + } + using (var repository = CreateRepository(unitOfWork)) + { + var dataTypeDefinition = new DataTypeDefinition(-1, new Guid(Constants.PropertyEditors.RadioButtonList)) { Name = "test" }; + repository.AddOrUpdate(dataTypeDefinition); + + Assert.Throws(unitOfWork.Commit); + + } + } + [Test] public void Can_Instantiate_Repository_From_Resolver() { diff --git a/src/Umbraco.Web.UI/umbraco/create/simple.ascx b/src/Umbraco.Web.UI/umbraco/create/simple.ascx index a885ca9008..16ec4cdaad 100644 --- a/src/Umbraco.Web.UI/umbraco/create/simple.ascx +++ b/src/Umbraco.Web.UI/umbraco/create/simple.ascx @@ -1,11 +1,12 @@ -<%@ Control Language="c#" AutoEventWireup="True" Codebehind="simple.ascx.cs" Inherits="umbraco.cms.presentation.create.controls.simple" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %> +<%@ Control Language="c#" AutoEventWireup="True" Inherits="umbraco.cms.presentation.create.controls.simple" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<%=umbraco.ui.Text("name")%>:*
+
diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 11d70c41b8..b7baea2f1f 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -424,6 +424,9 @@ ASPXCodeBehind + + ASPXCodeBehind + ASPXCodeBehind @@ -1176,13 +1179,6 @@ script.ascx - - simple.ascx - ASPXCodeBehind - - - simple.ascx - Code @@ -1930,7 +1926,6 @@ ASPXCodeBehind - ASPXCodeBehind diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/create/simple.ascx b/src/Umbraco.Web/umbraco.presentation/umbraco/create/simple.ascx deleted file mode 100644 index a885ca9008..0000000000 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/create/simple.ascx +++ /dev/null @@ -1,15 +0,0 @@ -<%@ Control Language="c#" AutoEventWireup="True" Codebehind="simple.ascx.cs" Inherits="umbraco.cms.presentation.create.controls.simple" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %> - -
<%=umbraco.ui.Text("name")%>:*
- - - -
- -
- -   <%= umbraco.ui.Text("or") %>   - <%=umbraco.ui.Text("cancel")%> -
diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/create/simple.ascx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/create/simple.ascx.cs index db219d6a05..974dbaabbe 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/create/simple.ascx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/create/simple.ascx.cs @@ -1,65 +1,90 @@ -using System; +using System; +using System.Web.UI.WebControls; using umbraco.BasePages; +using Umbraco.Web; namespace umbraco.cms.presentation.create.controls { /// - /// Summary description for simple. - /// - public partial class simple : System.Web.UI.UserControl - { + /// Summary description for simple. + /// + public partial class simple : System.Web.UI.UserControl + { - protected void Page_Load(object sender, System.EventArgs e) - { - sbmt.Text = ui.Text("create"); - // Put user code to initialize the page here - } + protected void Page_Load(object sender, EventArgs e) + { + sbmt.Text = ui.Text("create"); + // Put user code to initialize the page here + } - #region Web Form Designer generated code - override protected void OnInit(EventArgs e) - { - // - // CODEGEN: This call is required by the ASP.NET Web Form Designer. - // - InitializeComponent(); - base.OnInit(e); - } - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { + protected void sbmt_Click(object sender, EventArgs e) + { + if (Page.IsValid) + { + int nodeId; + if (int.TryParse(Request.QueryString["nodeId"], out nodeId) == false) + nodeId = -1; - } - #endregion + if (Request.GetItemAsString("nodeId") != "init") + nodeId = int.Parse(Request.GetItemAsString("nodeId")); - protected void sbmt_Click(object sender, System.EventArgs e) - { - if (Page.IsValid) - { - int nodeId; - if (int.TryParse(Request.QueryString["nodeId"], out nodeId) == false) - nodeId = -1; + try + { + var returnUrl = umbraco.presentation.create.dialogHandler_temp.Create( + Request.GetItemAsString("nodeType"), + nodeId, + rename.Text.Trim()); - if (umbraco.helper.Request("nodeId") != "init") - nodeId = int.Parse(umbraco.helper.Request("nodeId")); - - string returnUrl = umbraco.presentation.create.dialogHandler_temp.Create( - umbraco.helper.Request("nodeType"), - nodeId, - rename.Text.Trim()); - - - BasePage.Current.ClientTools - .ChangeContentFrameUrl(returnUrl) - .ChildNodeCreated() - .CloseModalWindow(); - - + BasePage.Current.ClientTools + .ChangeContentFrameUrl(returnUrl) + .ChildNodeCreated() + .CloseModalWindow(); + } + catch (Exception ex) + { + CustomValidation.ErrorMessage = ex.Message; + CustomValidation.IsValid = false; + } } - - } - } + + } + + protected CustomValidator CustomValidation; + + /// + /// RequiredFieldValidator1 control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1; + + /// + /// rename control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.TextBox rename; + + /// + /// Textbox1 control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.TextBox Textbox1; + + /// + /// sbmt control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Button sbmt; + } } diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/create/simple.ascx.designer.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/create/simple.ascx.designer.cs deleted file mode 100644 index aa789441c0..0000000000 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/create/simple.ascx.designer.cs +++ /dev/null @@ -1,52 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:2.0.50727.4200 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace umbraco.cms.presentation.create.controls { - - - public partial class simple { - - /// - /// RequiredFieldValidator1 control. - /// - /// - /// Auto-generated field. - /// To modify move field declaration from designer file to code-behind file. - /// - protected global::System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1; - - /// - /// rename control. - /// - /// - /// Auto-generated field. - /// To modify move field declaration from designer file to code-behind file. - /// - protected global::System.Web.UI.WebControls.TextBox rename; - - /// - /// Textbox1 control. - /// - /// - /// Auto-generated field. - /// To modify move field declaration from designer file to code-behind file. - /// - protected global::System.Web.UI.WebControls.TextBox Textbox1; - - /// - /// sbmt control. - /// - /// - /// Auto-generated field. - /// To modify move field declaration from designer file to code-behind file. - /// - protected global::System.Web.UI.WebControls.Button sbmt; - } -} diff --git a/src/umbraco.cms/businesslogic/datatype/DataTypeDefinition.cs b/src/umbraco.cms/businesslogic/datatype/DataTypeDefinition.cs index a0aff986f5..4466d2c621 100644 --- a/src/umbraco.cms/businesslogic/datatype/DataTypeDefinition.cs +++ b/src/umbraco.cms/businesslogic/datatype/DataTypeDefinition.cs @@ -1,7 +1,9 @@ using System; using System.Collections; +using System.Data; using System.Linq; using Umbraco.Core.Cache; +using Umbraco.Core.Persistence.SqlSyntax; using umbraco.DataLayer; using System.Xml; using umbraco.cms.businesslogic.media; @@ -237,10 +239,17 @@ namespace umbraco.cms.businesslogic.datatype /// public static DataTypeDefinition MakeNew(BusinessLogic.User u, string Text, Guid UniqueId) { + //Cannot add a duplicate data type + var exists = Database.ExecuteScalar(@"SELECT COUNT(*) FROM cmsDataType +INNER JOIN umbracoNode ON cmsDataType.nodeId = umbracoNode.id +WHERE umbracoNode." + SqlSyntaxContext.SqlSyntaxProvider.GetQuotedColumnName("text") + "= @name", new { name = Text }); + if (exists > 0) + { + throw new DuplicateNameException("A data type with the name " + Text + " already exists"); + } int newId = CMSNode.MakeNew(-1, _objectType, u.Id, 1, Text, UniqueId).Id; - cms.businesslogic.datatype.controls.Factory f = new cms.businesslogic.datatype.controls.Factory(); - + // initial control id changed to empty to ensure that it'll always work no matter if 3rd party configurators fail // ref: http://umbraco.codeplex.com/workitem/29788 Guid FirstcontrolId = Guid.Empty;