From 302b3c3750fbc7ed2aa977043125ec558e94c8ba Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Tue, 28 May 2013 15:54:19 -1000 Subject: [PATCH] Fixed some build problems, replaces all calls to the legacy/obsolete data type factory to the DataTypesResolver and removes test for DataTypeFactoryTests. Updates data type editor to show legacy data types at the bottom and the new Property editors at the top. Updated the data type editor to use the new api so that we can test assigning Belle property editors to data types (pre-values have not been converted though). #U4-2289 --- src/Umbraco.Core/Models/DataTypeDefinition.cs | 2 +- .../Models/IDataTypeDefinition.cs | 2 +- src/Umbraco.Tests/DataTypeFactoryTests.cs | 109 ------------------ .../DataTypeDefinitionRepositoryTest.cs | 3 + src/Umbraco.Tests/Umbraco.Tests.csproj | 1 - .../ServerSidePropertyEditorsController.cs | 2 +- .../PropertyEditors/PostcodeValidator.cs | 11 +- .../ServerInfoPropertyEditor.cs | 1 + .../MyPackage/System/MyStartupHandler.cs | 9 +- .../MyPackage/Trees/LegacyTestTree.cs | 8 +- .../developer/DataTypes/editDatatype.aspx.cs | 109 +++++++++--------- .../umbraco/editMedia.aspx.cs | 2 +- src/umbraco.cms/businesslogic/Content.cs | 2 +- .../datatype/DataTypeDefinition.cs | 26 ++--- .../mediapicker/MediaPickerDataType.cs | 21 ++-- 15 files changed, 92 insertions(+), 216 deletions(-) delete mode 100644 src/Umbraco.Tests/DataTypeFactoryTests.cs diff --git a/src/Umbraco.Core/Models/DataTypeDefinition.cs b/src/Umbraco.Core/Models/DataTypeDefinition.cs index f7068a3b6e..b4b3f42422 100644 --- a/src/Umbraco.Core/Models/DataTypeDefinition.cs +++ b/src/Umbraco.Core/Models/DataTypeDefinition.cs @@ -170,7 +170,7 @@ namespace Umbraco.Core.Models public Guid ControlId { get { return _controlId; } - private set + set { SetPropertyValueAndDetectChanges(o => { diff --git a/src/Umbraco.Core/Models/IDataTypeDefinition.cs b/src/Umbraco.Core/Models/IDataTypeDefinition.cs index 5f0299e24b..4312a2b32d 100644 --- a/src/Umbraco.Core/Models/IDataTypeDefinition.cs +++ b/src/Umbraco.Core/Models/IDataTypeDefinition.cs @@ -9,7 +9,7 @@ namespace Umbraco.Core.Models /// /// Id of the DataType control /// - Guid ControlId { get; } + Guid ControlId { get; set; } /// /// Gets or Sets the DatabaseType for which the DataType's value is saved as diff --git a/src/Umbraco.Tests/DataTypeFactoryTests.cs b/src/Umbraco.Tests/DataTypeFactoryTests.cs deleted file mode 100644 index b8bfa3b183..0000000000 --- a/src/Umbraco.Tests/DataTypeFactoryTests.cs +++ /dev/null @@ -1,109 +0,0 @@ -using System; -using NUnit.Framework; -using Umbraco.Core; -using Umbraco.Core.ObjectResolution; -using Umbraco.Tests.TestHelpers; -using umbraco.cms.businesslogic.datatype; -using umbraco.interfaces; -using System.Linq; - -namespace Umbraco.Tests -{ - - [TestFixture] - public class DataTypeFactoryTests - { - [SetUp] - public void Initialize() - { - TestHelper.SetupLog4NetForTests(); - - //this ensures its reset - PluginManager.Current = new PluginManager(false); - - //for testing, we'll specify which assemblies are scanned for the PluginTypeResolver - PluginManager.Current.AssembliesToScan = new[] - { - this.GetType().Assembly - }; - - DataTypesResolver.Current = new DataTypesResolver( - () => PluginManager.Current.ResolveDataTypes()); - - Resolution.Freeze(); - } - - [TearDown] - public void TearDown() - { - DataTypesResolver.Reset(); - PluginManager.Current = null; - } - - [Test] - public void Get_All_Instances() - { - var factory = new umbraco.cms.businesslogic.datatype.controls.Factory(); - Assert.AreEqual(2, factory.GetAll().Count()); - } - - #region Classes for tests - public class DataType1 : AbstractDataEditor - { - public override Guid Id - { - get { return new Guid("FBAEA49B-F704-44FE-B725-6C8FE0767CF2"); } - } - - public override string DataTypeName - { - get { return "DataType1"; } - } - - public override IDataEditor DataEditor - { - get { throw new NotImplementedException(); } - } - - public override IDataPrevalue PrevalueEditor - { - get { throw new NotImplementedException(); } - } - - public override IData Data - { - get { throw new NotImplementedException(); } - } - } - - public class DataType2 : AbstractDataEditor - { - public override Guid Id - { - get { return new Guid("3F58099B-96AC-415E-B3F9-BA273F51E681"); } - } - - public override string DataTypeName - { - get { return "DataType2"; } - } - - public override IDataEditor DataEditor - { - get { throw new NotImplementedException(); } - } - - public override IDataPrevalue PrevalueEditor - { - get { throw new NotImplementedException(); } - } - - public override IData Data - { - get { throw new NotImplementedException(); } - } - } - #endregion - - } -} \ No newline at end of file diff --git a/src/Umbraco.Tests/Persistence/Repositories/DataTypeDefinitionRepositoryTest.cs b/src/Umbraco.Tests/Persistence/Repositories/DataTypeDefinitionRepositoryTest.cs index 0f19a5083f..85e9a62fe4 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/DataTypeDefinitionRepositoryTest.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/DataTypeDefinitionRepositoryTest.cs @@ -165,8 +165,10 @@ namespace Umbraco.Tests.Persistence.Repositories unitOfWork.Commit(); // Act + var newId = Guid.NewGuid(); var definition = repository.Get(dataTypeDefinition.Id); definition.Name = "AgeDataType Updated"; + definition.ControlId = newId; repository.AddOrUpdate(definition); unitOfWork.Commit(); @@ -175,6 +177,7 @@ namespace Umbraco.Tests.Persistence.Repositories // Assert Assert.That(definitionUpdated, Is.Not.Null); Assert.That(definitionUpdated.Name, Is.EqualTo("AgeDataType Updated")); + Assert.That(definitionUpdated.ControlId, Is.EqualTo(newId)); } [Test] diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index c4033e919d..e4c7d59117 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -296,7 +296,6 @@ - diff --git a/src/Umbraco.Web.UI/App_Plugins/MyPackage/Controllers/ServerSidePropertyEditorsController.cs b/src/Umbraco.Web.UI/App_Plugins/MyPackage/Controllers/ServerSidePropertyEditorsController.cs index c6afc1e5b6..5671a9a870 100644 --- a/src/Umbraco.Web.UI/App_Plugins/MyPackage/Controllers/ServerSidePropertyEditorsController.cs +++ b/src/Umbraco.Web.UI/App_Plugins/MyPackage/Controllers/ServerSidePropertyEditorsController.cs @@ -1,6 +1,6 @@ using System.Web.Mvc; -namespace Umbraco.Belle.App_Plugins.MyPackage.Controllers +namespace Umbraco.Web.UI.App_Plugins.MyPackage.Controllers { public class ServerSidePropertyEditorsController : Controller diff --git a/src/Umbraco.Web.UI/App_Plugins/MyPackage/PropertyEditors/PostcodeValidator.cs b/src/Umbraco.Web.UI/App_Plugins/MyPackage/PropertyEditors/PostcodeValidator.cs index cb6820d229..159acdce04 100644 --- a/src/Umbraco.Web.UI/App_Plugins/MyPackage/PropertyEditors/PostcodeValidator.cs +++ b/src/Umbraco.Web.UI/App_Plugins/MyPackage/PropertyEditors/PostcodeValidator.cs @@ -12,19 +12,20 @@ namespace Umbraco.Web.UI.App_Plugins.MyPackage.PropertyEditors /// internal class PostcodeValidator : ValidatorBase { - public override IEnumerable Validate(object value, string preValues, PropertyEditor editor) + + public override IEnumerable Validate(string value, string preValues, PropertyEditor editor) { - var stringVal = value.ToString(); + var stringVal = value; if (preValues.IsNullOrWhiteSpace()) yield break; var asJson = JObject.Parse(preValues); if (asJson["country"] == null) yield break; - + if (asJson["country"].ToString() == "Australia") { - if (!Regex.IsMatch(stringVal, "^\\d{4}$")) + if (Regex.IsMatch(stringVal, "^\\d{4}$") == false) { - yield return new ValidationResult("Australian postcodes must be a 4 digit number", + yield return new ValidationResult("Australian postcodes must be a 4 digit number", new[] { //we only store a single value for this editor so the 'member' or 'field' diff --git a/src/Umbraco.Web.UI/App_Plugins/MyPackage/PropertyEditors/ServerInfoPropertyEditor.cs b/src/Umbraco.Web.UI/App_Plugins/MyPackage/PropertyEditors/ServerInfoPropertyEditor.cs index 9b2565b6f2..2df6fa8698 100644 --- a/src/Umbraco.Web.UI/App_Plugins/MyPackage/PropertyEditors/ServerInfoPropertyEditor.cs +++ b/src/Umbraco.Web.UI/App_Plugins/MyPackage/PropertyEditors/ServerInfoPropertyEditor.cs @@ -2,6 +2,7 @@ using System.Web; using System.Web.Mvc; using System.Web.Routing; +using Umbraco.Core.PropertyEditors; namespace Umbraco.Web.UI.App_Plugins.MyPackage.PropertyEditors { diff --git a/src/Umbraco.Web.UI/App_Plugins/MyPackage/System/MyStartupHandler.cs b/src/Umbraco.Web.UI/App_Plugins/MyPackage/System/MyStartupHandler.cs index ef6b7f159f..d98707dedc 100644 --- a/src/Umbraco.Web.UI/App_Plugins/MyPackage/System/MyStartupHandler.cs +++ b/src/Umbraco.Web.UI/App_Plugins/MyPackage/System/MyStartupHandler.cs @@ -1,14 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Generic; using System.Web; using System.Web.Mvc; using System.Web.Routing; -using Umbraco.Belle.System; using Umbraco.Core; -using umbraco.businesslogic; +using Umbraco.Web.UI.JavaScript; -namespace Umbraco.Belle.App_Plugins.MyPackage.System +namespace Umbraco.Web.UI.App_Plugins.MyPackage.System { public class MyStartupHandler : ApplicationEventHandler { diff --git a/src/Umbraco.Web.UI/App_Plugins/MyPackage/Trees/LegacyTestTree.cs b/src/Umbraco.Web.UI/App_Plugins/MyPackage/Trees/LegacyTestTree.cs index e5d013d6a0..0cd5d43d8a 100644 --- a/src/Umbraco.Web.UI/App_Plugins/MyPackage/Trees/LegacyTestTree.cs +++ b/src/Umbraco.Web.UI/App_Plugins/MyPackage/Trees/LegacyTestTree.cs @@ -1,11 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Web; +using System.Text; using umbraco.cms.presentation.Trees; -namespace Umbraco.Belle.App_Plugins.MyPackage.Trees +namespace Umbraco.Web.UI.App_Plugins.MyPackage.Trees { public class LegacyTestTree : BaseTree { diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/developer/DataTypes/editDatatype.aspx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/developer/DataTypes/editDatatype.aspx.cs index 7e22296ed9..05a831cdf4 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/developer/DataTypes/editDatatype.aspx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/developer/DataTypes/editDatatype.aspx.cs @@ -1,9 +1,16 @@ using System; using System.Collections; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; using System.Web.UI; using System.Web.UI.WebControls; +using Umbraco.Core; +using Umbraco.Core.IO; +using Umbraco.Core.Models; +using Umbraco.Core.PropertyEditors; using umbraco.cms.presentation.Trees; -using umbraco.IO; +using umbraco.interfaces; namespace umbraco.cms.presentation.developer { @@ -15,68 +22,78 @@ namespace umbraco.cms.presentation.developer } protected ImageButton save; - private cms.businesslogic.datatype.DataTypeDefinition dt; - cms.businesslogic.datatype.controls.Factory f; + private int _id = 0; - private interfaces.IDataPrevalue _prevalue; + private IDataPrevalue _prevalue; + private IDataTypeDefinition _dataTypeDefinition; - protected void Page_Load(object sender, System.EventArgs e) + protected void Page_Load(object sender, EventArgs e) { pp_name.Text = ui.Text("name"); pp_renderControl.Text = ui.Text("renderControl"); pane_settings.Text = ui.Text("settings"); pp_guid.Text = ui.Text("guid"); - _id = int.Parse(Request.QueryString["id"]); - dt = cms.businesslogic.datatype.DataTypeDefinition.GetDataTypeDefinition(_id); - - f = new cms.businesslogic.datatype.controls.Factory(); - - if (!IsPostBack) + _dataTypeDefinition = ApplicationContext.Services.DataTypeService.GetDataTypeDefinitionById(_id); + + if (IsPostBack == false) { - txtName.Text = dt.Text; + txtName.Text = _dataTypeDefinition.Name; - SortedList datatypes = new SortedList(); + //get the legacy data types + var datatypes = DataTypesResolver.Current.DataTypes + .ToDictionary(df => df.Id, df => "(legacy) " + df.DataTypeName); - foreach (interfaces.IDataType df in f.GetAll()) - datatypes.Add(df.DataTypeName + "|" + Guid.NewGuid().ToString(), df.Id); + //get the new property editors + var propEditors = PropertyEditorResolver.Current.PropertyEditors + .ToDictionary(pe => pe.Id, pe => pe.Name); - IDictionaryEnumerator ide = datatypes.GetEnumerator(); - - string datatTypeId = dt.DataType != null ? dt.DataType.Id.ToString() : String.Empty; - while (ide.MoveNext()) + //join the lists + var combined = propEditors.Concat(datatypes); + + foreach (var item in combined) { - ListItem li = new ListItem(); - li.Text = ide.Key.ToString().Substring(0, ide.Key.ToString().IndexOf("|")); - li.Value = ide.Value.ToString(); + var li = new ListItem + { + + Text = item.Value, + Value = item.Key.ToString() + }; + + if (_dataTypeDefinition.ControlId != default(Guid) && li.Value == _dataTypeDefinition.ControlId.ToString()) + { + li.Selected = true; + } - if (!String.IsNullOrEmpty(datatTypeId) && li.Value.ToString() == datatTypeId) li.Selected = true; ddlRenderControl.Items.Add(li); - } + } ClientTools .SetActiveTreeType(TreeDefinitionCollection.Instance.FindTree().Tree.Alias) - .SyncTree("-1,init," + _id.ToString(), false); + .SyncTree("-1,init," + _id.ToString(CultureInfo.InvariantCulture), false); } - if (dt.DataType != null) - litGuid.Text = dt.DataType.Id.ToString(); - Panel1.Text = umbraco.ui.Text("edit") + " datatype: " + dt.Text; - insertPrevalueEditor(); + if (_dataTypeDefinition.ControlId != default(Guid)) + { + litGuid.Text = _dataTypeDefinition.ControlId.ToString(); + } + + Panel1.Text = ui.Text("edit") + " datatype: " + _dataTypeDefinition.Name; + InsertPrevalueEditor(); } - private void insertPrevalueEditor() + private void InsertPrevalueEditor() { try { if (ddlRenderControl.SelectedIndex >= 0) { - interfaces.IDataType o = f.DataType(new Guid(ddlRenderControl.SelectedValue)); + var o = DataTypesResolver.Current.GetById(new Guid(ddlRenderControl.SelectedValue)); - o.DataTypeDefinitionId = dt.Id; + o.DataTypeDefinitionId = _dataTypeDefinition.Id; _prevalue = o.PrevalueEditor; if (o.PrevalueEditor.Editor != null) @@ -97,44 +114,26 @@ namespace umbraco.cms.presentation.developer if (_prevalue != null) _prevalue.Save(); - dt.Text = txtName.Text; + _dataTypeDefinition.ControlId = new Guid(ddlRenderControl.SelectedValue); + _dataTypeDefinition.Name = txtName.Text; - dt.DataType = f.DataType(new Guid(ddlRenderControl.SelectedValue)); - dt.Save(); + ApplicationContext.Services.DataTypeService.Save(_dataTypeDefinition, UmbracoUser.Id); ClientTools.ShowSpeechBubble(speechBubbleIcon.save, ui.Text("speechBubbles", "dataTypeSaved", null), ""); - ClientTools.SyncTree(dt.Path, true); + ClientTools.SyncTree("-1,init," + _id.ToString(CultureInfo.InvariantCulture), true); } - - #region Web Form Designer generated code - - override protected void OnInit(EventArgs e) { save = Panel1.Menu.NewImageButton(); save.ID = "save"; - save.Click += new System.Web.UI.ImageClickEventHandler(save_click); + save.Click += save_click; save.ImageUrl = SystemDirectories.Umbraco + "/images/editor/save.gif"; Panel1.hasMenu = true; - // - // 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() - { - - } - #endregion } } diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/editMedia.aspx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/editMedia.aspx.cs index 38d5fc8146..0245097ce5 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/editMedia.aspx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/editMedia.aspx.cs @@ -128,7 +128,7 @@ namespace umbraco.cms.presentation private void UpdateMediaFileLinksLiteral() { - var uploadField = new Factory().GetNewObject(new Guid(Constants.PropertyEditors.UploadField)); + var uploadField = DataTypesResolver.Current.GetById(new Guid(Constants.PropertyEditors.UploadField)); // always clear, incase the upload file was removed this._mediaFileLinksLiteral.Text = string.Empty; diff --git a/src/umbraco.cms/businesslogic/Content.cs b/src/umbraco.cms/businesslogic/Content.cs index 786a7b2f6b..c87d1bb66f 100644 --- a/src/umbraco.cms/businesslogic/Content.cs +++ b/src/umbraco.cms/businesslogic/Content.cs @@ -568,7 +568,7 @@ namespace umbraco.cms.businesslogic // Remove all files var fs = FileSystemProviderManager.Current.GetFileSystemProvider(); - var uploadField = new Factory().GetNewObject(new Guid(Constants.PropertyEditors.UploadField)); + var uploadField = DataTypesResolver.Current.GetById(new Guid(Constants.PropertyEditors.UploadField)); foreach (Property p in GenericProperties) { diff --git a/src/umbraco.cms/businesslogic/datatype/DataTypeDefinition.cs b/src/umbraco.cms/businesslogic/datatype/DataTypeDefinition.cs index a0aff986f5..9bae792f5d 100644 --- a/src/umbraco.cms/businesslogic/datatype/DataTypeDefinition.cs +++ b/src/umbraco.cms/businesslogic/datatype/DataTypeDefinition.cs @@ -55,9 +55,8 @@ namespace umbraco.cms.businesslogic.datatype { if (_controlId == Guid.Empty) return null; - - controls.Factory factory = new controls.Factory(); - var dt = factory.DataType(_controlId); + + var dt = DataTypesResolver.Current.GetById(_controlId); if (dt != null) dt.DataTypeDefinitionId = Id; @@ -157,16 +156,12 @@ namespace umbraco.cms.businesslogic.datatype //Make sure that the dtd is not already present - if (!CMSNode.IsNode(new Guid(_def))) + if (IsNode(new Guid(_def)) == false) { - BusinessLogic.User u = BusinessLogic.User.GetCurrent(); + var u = BusinessLogic.User.GetCurrent() ?? BusinessLogic.User.GetUser(0); - if (u == null) - u = BusinessLogic.User.GetUser(0); - - var f = new controls.Factory(); - DataTypeDefinition dtd = MakeNew(u, _name, new Guid(_def)); - var dataType = f.DataType(new Guid(_id)); + var dtd = MakeNew(u, _name, new Guid(_def)); + var dataType = DataTypesResolver.Current.GetById(new Guid(_id)); if (dataType == null) throw new NullReferenceException("Could not resolve a data type with id " + _id); @@ -238,17 +233,16 @@ namespace umbraco.cms.businesslogic.datatype public static DataTypeDefinition MakeNew(BusinessLogic.User u, string Text, Guid UniqueId) { - int newId = CMSNode.MakeNew(-1, _objectType, u.Id, 1, Text, UniqueId).Id; - cms.businesslogic.datatype.controls.Factory f = new cms.businesslogic.datatype.controls.Factory(); + var newId = MakeNew(-1, _objectType, u.Id, 1, Text, UniqueId).Id; // 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; + var firstcontrolId = Guid.Empty; SqlHelper.ExecuteNonQuery("Insert into cmsDataType (nodeId, controlId, dbType) values (" + newId.ToString() + ",@controlId,'Ntext')", - SqlHelper.CreateParameter("@controlId", FirstcontrolId)); + SqlHelper.CreateParameter("@controlId", firstcontrolId)); - DataTypeDefinition dtd = new DataTypeDefinition(newId); + var dtd = new DataTypeDefinition(newId); dtd.OnNew(EventArgs.Empty); return dtd; diff --git a/src/umbraco.editorControls/mediapicker/MediaPickerDataType.cs b/src/umbraco.editorControls/mediapicker/MediaPickerDataType.cs index 6417b91fd0..03af12e828 100644 --- a/src/umbraco.editorControls/mediapicker/MediaPickerDataType.cs +++ b/src/umbraco.editorControls/mediapicker/MediaPickerDataType.cs @@ -3,12 +3,7 @@ using Umbraco.Core; namespace umbraco.editorControls.mediapicker { - //TODO: Properly rename this for a major release - public class MediaPickerDataType : MemberPickerDataType - { } - - [Obsolete("Renamed to MediaPickerDataType because.. that is what it was all along")] - public class MemberPickerDataType : cms.businesslogic.datatype.BaseDataType, interfaces.IDataType + public class MediaPickerDataType : cms.businesslogic.datatype.BaseDataType, interfaces.IDataType { private interfaces.IDataEditor _editor; private interfaces.IData _baseData; @@ -25,13 +20,13 @@ namespace umbraco.editorControls.mediapicker } - public override Guid Id - { - get - { - return new Guid(Constants.PropertyEditors.MediaPicker); - } - } + public override Guid Id + { + get + { + return new Guid(Constants.PropertyEditors.MediaPicker); + } + } public override string DataTypeName