From 806c2a7ea4313b794474686c365e381e7ef7d505 Mon Sep 17 00:00:00 2001 From: Morten Christensen Date: Mon, 17 Dec 2012 15:07:08 -0100 Subject: [PATCH] Refactoring legacy Content, Property and propertytype though still a WIP. --- src/umbraco.cms/businesslogic/Content.cs | 29 ++++++++-------- .../businesslogic/Property/Property.cs | 34 +++++++++++++++---- .../propertytype/propertytype.cs | 23 +++++++++++-- src/umbraco.cms/businesslogic/web/Document.cs | 22 +++++++++++- 4 files changed, 84 insertions(+), 24 deletions(-) diff --git a/src/umbraco.cms/businesslogic/Content.cs b/src/umbraco.cms/businesslogic/Content.cs index fb393e115a..a083d4f1d7 100644 --- a/src/umbraco.cms/businesslogic/Content.cs +++ b/src/umbraco.cms/businesslogic/Content.cs @@ -91,18 +91,13 @@ namespace umbraco.cms.businesslogic [Obsolete("Deprecated, Use Umbraco.Core.Services.ContentService.GetByIdVersion() or Umbraco.Core.Services.MediaService.GetByIdVersion()", false)] public static Content GetContentFromVersion(Guid version) { - int id = - ApplicationContext.Current.DatabaseContext.Database.ExecuteScalar( - "Select ContentId from cmsContentVersion where versionId = @VersionId", new {VersionId = version}); - - var content = ApplicationContext.Current.Services.ContentService.GetByIdVersion(id, version); + var content = ApplicationContext.Current.Services.ContentService.GetByVersion(version); if (content != null) { return new Content(content); } - //TODO Change to use GetByIdVersion once that method has been implemented in the MediaService - var media = ApplicationContext.Current.Services.MediaService.GetById(id); + var media = ApplicationContext.Current.Services.MediaService.GetByVersion(version); return new Content(media); } @@ -285,7 +280,7 @@ namespace umbraco.cms.businesslogic /// /// Propertyalias (defined in the documenttype) /// The property with the given alias - public Property getProperty(string alias) + public virtual Property getProperty(string alias) { ContentType ct = this.ContentType; if (ct == null) @@ -301,7 +296,7 @@ namespace umbraco.cms.businesslogic /// /// PropertyType /// The property with the given propertytype - public Property getProperty(PropertyType pt) + public virtual Property getProperty(PropertyType pt) { EnsureProperties(); @@ -318,7 +313,7 @@ namespace umbraco.cms.businesslogic /// The PropertyType of the Property /// The version of the document on which the property should be add'ed /// The new Property - public Property addProperty(PropertyType pt, Guid versionId) + public virtual Property addProperty(PropertyType pt, Guid versionId) { ClearLoadedProperties(); @@ -486,6 +481,7 @@ namespace umbraco.cms.businesslogic if (_contentType == null) _contentType = ContentType.GetContentType(InitContentType); + _version = InitVersion; _versionDate = InitVersionDate; _contentTypeIcon = InitContentTypeIcon; @@ -500,12 +496,9 @@ namespace umbraco.cms.businesslogic SqlHelper.ExecuteNonQuery("insert into cmsContent (nodeId,ContentType) values (" + this.Id + "," + ct.Id + ")"); createNewVersion(DateTime.Now); } - - - + /// - /// Method for creating a new version of the data associated to the Content. - /// + /// Method for creating a new version of the data associated to the Content. /// /// The new version Id protected Guid createNewVersion(DateTime versionDate = default(DateTime)) @@ -657,6 +650,12 @@ namespace umbraco.cms.businesslogic { m_LoadedProperties = new Properties(); + if (_contentBase != null) + { + m_LoadedProperties.AddRange(_contentBase.Properties.Select(x => new Property(x))); + return; + } + if (this.ContentType == null) return; diff --git a/src/umbraco.cms/businesslogic/Property/Property.cs b/src/umbraco.cms/businesslogic/Property/Property.cs index 1d88e4f8ff..6a8f9f92fd 100644 --- a/src/umbraco.cms/businesslogic/Property/Property.cs +++ b/src/umbraco.cms/businesslogic/Property/Property.cs @@ -1,12 +1,12 @@ using System; -using System.Collections.Generic; -using System.Data; using System.Runtime.CompilerServices; using System.Xml; using Umbraco.Core; using Umbraco.Core.Persistence; using umbraco.DataLayer; using umbraco.BusinessLogic; +using umbraco.cms.businesslogic.datatype; +using umbraco.cms.businesslogic.propertytype; namespace umbraco.cms.businesslogic.property { @@ -16,7 +16,9 @@ namespace umbraco.cms.businesslogic.property /// public class Property { - private propertytype.PropertyType _pt; + private Umbraco.Core.Models.PropertyType _propertyType; + private Umbraco.Core.Models.Property _property; + private PropertyType _pt; private interfaces.IData _data; private int _id; @@ -42,12 +44,26 @@ namespace umbraco.cms.businesslogic.property public Property(int Id) { _id = Id; - _pt = umbraco.cms.businesslogic.propertytype.PropertyType.GetPropertyType( - SqlHelper.ExecuteScalar("select propertytypeid from cmsPropertyData where id = @id", SqlHelper.CreateParameter("@id", Id))); + _pt = PropertyType.GetPropertyType( + SqlHelper.ExecuteScalar("select propertytypeid from cmsPropertyData where id = @id", + SqlHelper.CreateParameter("@id", Id))); _data = _pt.DataTypeDefinition.DataType.Data; _data.PropertyId = Id; } + internal Property(Umbraco.Core.Models.Property property) + { + _id = property.Id; + _property = property; + _propertyType = property.PropertyType; + + var dataTypeDefinition = DataTypeDefinition.GetDataTypeDefinition(property.PropertyType.DataTypeId); + _data = dataTypeDefinition.DataType.Data; + + //Just to ensure that there is a PropertyType available + _pt = PropertyType.GetPropertyType(property.PropertyTypeId); + } + public Guid VersionId { get @@ -65,7 +81,13 @@ namespace umbraco.cms.businesslogic.property } public propertytype.PropertyType PropertyType { - get { return _pt; } + get + { + /*if (_propertyType != null) + return new PropertyType(_propertyType);*/ + + return _pt; + } } public object Value diff --git a/src/umbraco.cms/businesslogic/propertytype/propertytype.cs b/src/umbraco.cms/businesslogic/propertytype/propertytype.cs index 29d17b5759..9c6c61c710 100644 --- a/src/umbraco.cms/businesslogic/propertytype/propertytype.cs +++ b/src/umbraco.cms/businesslogic/propertytype/propertytype.cs @@ -22,8 +22,6 @@ namespace umbraco.cms.businesslogic.propertytype { #region Declarations - private static string _connstring = GlobalSettings.DbDSN; - private static readonly object propertyTypeCacheSyncLock = new object(); private static readonly string UmbracoPropertyTypeCacheKey = "UmbracoPropertyTypeCache"; private readonly int _contenttypeid; @@ -38,6 +36,8 @@ namespace umbraco.cms.businesslogic.propertytype private int _propertyTypeGroup; private string _validationRegExp = ""; + private Umbraco.Core.Models.PropertyType _propertyType; + #endregion protected static ISqlHelper SqlHelper @@ -73,6 +73,24 @@ namespace umbraco.cms.businesslogic.propertytype } } + internal PropertyType(Umbraco.Core.Models.PropertyType propertyType) + { + _propertyType = propertyType; + + //_contenttypeid + //_propertyTypeGroup + //_tabId + + _mandatory = propertyType.Mandatory; + _sortOrder = propertyType.SortOrder; + _alias = propertyType.Alias; + _name = propertyType.Name; + _validationRegExp = propertyType.ValidationRegExp; + _DataTypeId = propertyType.DataTypeId; + _description = propertyType.Description; + + } + #endregion #region Properties @@ -112,6 +130,7 @@ namespace umbraco.cms.businesslogic.propertytype InvalidateCache(); } } + public int PropertyTypeGroup { get { return _propertyTypeGroup; } diff --git a/src/umbraco.cms/businesslogic/web/Document.cs b/src/umbraco.cms/businesslogic/web/Document.cs index d528c44cf2..b6fce2f123 100644 --- a/src/umbraco.cms/businesslogic/web/Document.cs +++ b/src/umbraco.cms/businesslogic/web/Document.cs @@ -873,6 +873,12 @@ and node.nodeObjectType='C66BA18E-EAF3-4CFF-8A22-41B16D66A972'"); if (!e.Cancel) { + //NOTE This seems to currently override the data set in the content control + foreach (var property in GenericProperties) + { + //Content.SetValue(property.PropertyType.Alias, property.Value); + } + ApplicationContext.Current.Services.ContentService.Save(Content); base.Save(); @@ -1219,13 +1225,27 @@ and node.nodeObjectType='C66BA18E-EAF3-4CFF-8A22-41B16D66A972'"); #endregion + #region Public Methods - Overides Content for legacy api refactor + public override Property getProperty(string alias) + { + var prop = Content.Properties.FirstOrDefault(x => x.Alias == alias); + return new Property(prop); + } + + public override Property getProperty(propertytype.PropertyType pt) + { + var prop = Content.Properties.FirstOrDefault(x => x.Alias == pt.Alias); + return new Property(prop); + } + #endregion + #region Protected Methods [Obsolete("Deprecated", false)] protected override void setupNode() { var content = Version == Guid.Empty ? ApplicationContext.Current.Services.ContentService.GetById(Id) - : ApplicationContext.Current.Services.ContentService.GetByIdVersion(Id, Version); + : ApplicationContext.Current.Services.ContentService.GetByVersion(Version); if(content == null) throw new ArgumentException(string.Format("No Document exists with id '{0}'", Id));