Remove PropertyGroup.ParentId, sanitize Property groups/types

This commit is contained in:
Stephan
2015-11-17 12:30:37 +01:00
parent aaffa01f36
commit eb5589e064
39 changed files with 777 additions and 919 deletions

View File

@@ -636,6 +636,8 @@ namespace umbraco.cms.businesslogic
{
if (m_masterContentTypes == null)
{
var ct = ApplicationContext.Current.Services.ContentTypeService.GetContentType(Id);
m_masterContentTypes = ct.CompositionPropertyGroups.Select(x => x.Id).ToList();
m_masterContentTypes = ContentTypeItem == null
? new List<int>()
: ContentTypeItem.CompositionIds().ToList();
@@ -1008,7 +1010,7 @@ namespace umbraco.cms.businesslogic
public int AddVirtualTab(string Caption)
{
// The method is synchronized
PropertyTypeGroup ptg = new PropertyTypeGroup(0, Id, Caption);
PropertyTypeGroup ptg = new PropertyTypeGroup(Id, Caption);
ptg.Save();
// Remove from cache
@@ -1304,24 +1306,16 @@ namespace umbraco.cms.businesslogic
[Obsolete("Use PropertyTypeGroup methods instead", false)]
private void InitializeVirtualTabs()
{
// While we are initialising, we should not use the class-scoped list, as it may be used by other threads
var temporaryList = new List<TabI>();
foreach (PropertyTypeGroup ptg in PropertyTypeGroups.Where(x => x.ParentId == 0 && x.ContentTypeId == this.Id))
temporaryList.Add(new Tab(ptg.Id, ptg.Name, ptg.SortOrder, this));
// somewhat fixing... this whole class should be removed anyways
var ct = ContentTypeItem ?? ApplicationContext.Current.Services.ContentTypeService.GetContentType(Id);
// Master Content Type
if (MasterContentTypes.Count > 0)
{
foreach (var mct in MasterContentTypes)
temporaryList.AddRange(GetContentType(mct).getVirtualTabs.ToList());
}
// sort all tabs
temporaryList.Sort((a, b) => a.SortOrder.CompareTo(b.SortOrder));
// now that we aren't going to modify the list, we can set it to the class-scoped variable.
_virtualTabs = temporaryList.DistinctBy(x => x.Id).ToList();
var tmp1 = ct.PropertyGroups
.Select(x => (TabI) new Tab(x.Id, x.Name, x.SortOrder, this))
.Union(ct.ContentTypeComposition.SelectMany(x => GetContentType(x.Id).getVirtualTabs))
.OrderBy(x => x.SortOrder)
.DistinctBy(x => x.Id)
.ToList();
_virtualTabs = tmp1;
}
private static void PopulateMasterContentTypes(PropertyType pt, int docTypeId)
@@ -1486,22 +1480,13 @@ namespace umbraco.cms.businesslogic
// regardless of the PropertyTypes belonging to the current ContentType.
public List<PropertyType> GetAllPropertyTypes()
{
var db = ApplicationContext.Current.DatabaseContext.Database;
var propertyTypeDtos = db.Fetch<PropertyTypeDto>("WHERE propertyTypeGroupId = @Id", new { Id = _id });
var tmp = propertyTypeDtos
.Select(propertyTypeDto => PropertyType.GetPropertyType(propertyTypeDto.Id))
.ToList();
var propertyTypeGroupDtos = db.Fetch<PropertyTypeGroupDto>("WHERE parentGroupId = @Id", new { Id = _id });
foreach (var propertyTypeGroupDto in propertyTypeGroupDtos)
{
var inheritedPropertyTypeDtos = db.Fetch<PropertyTypeDto>("WHERE propertyTypeGroupId = @Id", new { Id = propertyTypeGroupDto.Id });
tmp.AddRange(inheritedPropertyTypeDtos
.Select(propertyTypeDto => PropertyType.GetPropertyType(propertyTypeDto.Id))
.ToList());
}
return tmp;
// somewhat fixing... this whole class should be removed anyways
var ct = _contenttype.ContentTypeItem ?? ApplicationContext.Current.Services.ContentTypeService.GetContentType(_contenttype.Id);
return ct.CompositionPropertyTypes
.OrderBy(x => x.SortOrder)
.Select(x => x.Id)
.Select(PropertyType.GetPropertyType)
.ToList();
}
/// <summary>

View File

@@ -9,7 +9,6 @@ namespace umbraco.cms.businesslogic.propertytype
public class PropertyTypeGroup
{
public int Id { get; set; }
public int ParentId { get; set; }
public int ContentTypeId { get; set; }
public string Name { get; set; }
public int SortOrder { get; set; }
@@ -18,17 +17,15 @@ namespace umbraco.cms.businesslogic.propertytype
{
}
public PropertyTypeGroup(int parentId, int contentTypeId, string name, int sortOrder)
public PropertyTypeGroup(int contentTypeId, string name, int sortOrder)
{
ParentId = parentId;
ContentTypeId = contentTypeId;
Name = name;
SortOrder = sortOrder;
}
public PropertyTypeGroup(int parentId, int contentTypeId, string name)
public PropertyTypeGroup(int contentTypeId, string name)
{
ParentId = parentId;
ContentTypeId = contentTypeId;
Name = name;
SortOrder = -1; // we set this to -1 so in the save method we can get the current highest sortorder in case it's not sat after init (ie. if you want to force a sortOrder)
@@ -44,19 +41,25 @@ namespace umbraco.cms.businesslogic.propertytype
return PropertyType.GetPropertyTypesByGroup(Id);
}
//TODO: Verify support for master doctypes / mixins!
// note: this is used to delete all groups that inherit from a group, when the group is deleted,
// see the Delete method in this class - but it is all done in an obsolete way which does not
// take compositions in account + we delete the group but do not re-allocate the properties, etc.
// ALL THIS should be either removed, or refactored to use the new APIs - so... returning nothing
// from now on, which is just another way of being broken.
public IEnumerable<PropertyTypeGroup> GetPropertyTypeGroups()
{
var ptgs = new List<PropertyTypeGroup>();
using (var dr = SqlHelper.ExecuteReader(@"SELECT id FROM cmsPropertyTypeGroup WHERE parentGroupId = @id", SqlHelper.CreateParameter("@id", Id)))
{
while (dr.Read())
{
ptgs.Add(GetPropertyTypeGroup(dr.GetInt("id")));
}
}
return Enumerable.Empty<PropertyTypeGroup>();
return ptgs;
//var ptgs = new List<PropertyTypeGroup>();
//using (var dr = SqlHelper.ExecuteReader(@"SELECT id FROM cmsPropertyTypeGroup WHERE parentGroupId = @id", SqlHelper.CreateParameter("@id", Id)))
//{
// while (dr.Read())
// {
// ptgs.Add(GetPropertyTypeGroup(dr.GetInt("id")));
// }
//}
//return ptgs;
}
public void Save()
@@ -67,7 +70,6 @@ namespace umbraco.cms.businesslogic.propertytype
@"UPDATE
cmsPropertyTypeGroup
SET
parentGroupId = @parentGroupId,
contenttypeNodeId = @contentTypeId,
sortOrder = @sortOrder,
text = @name
@@ -75,7 +77,6 @@ namespace umbraco.cms.businesslogic.propertytype
id = @id
",
SqlHelper.CreateParameter("@id", Id),
SqlHelper.CreateParameter("@parentGroupId", ConvertParentId(ParentId)),
SqlHelper.CreateParameter("@contentTypeId", ContentTypeId),
SqlHelper.CreateParameter("@sortOrder", SortOrder),
SqlHelper.CreateParameter("@name", Name)
@@ -84,18 +85,17 @@ namespace umbraco.cms.businesslogic.propertytype
else
{
if (SortOrder == -1)
SortOrder = SqlHelper.ExecuteScalar<int>("select count(*) from cmsPropertyTypeGroup where COALESCE(parentGroupId, 0) = 0 and contenttypeNodeId = @nodeId",
SortOrder = SqlHelper.ExecuteScalar<int>("select count(*) from cmsPropertyTypeGroup where contenttypeNodeId = @nodeId",
SqlHelper.CreateParameter("@nodeId", ContentTypeId)) + 1;
SqlHelper.ExecuteNonQuery(
@"
INSERT INTO
cmsPropertyTypeGroup
(parentGroupId, contenttypeNodeId, sortOrder, text)
(contenttypeNodeId, sortOrder, text)
VALUES
(@parentGroupId, @contentTypeId, @sortOrder, @name)
(@contentTypeId, @sortOrder, @name)
",
SqlHelper.CreateParameter("@parentGroupId", ConvertParentId(ParentId)),
SqlHelper.CreateParameter("@contentTypeId", ContentTypeId),
SqlHelper.CreateParameter("@sortOrder", SortOrder),
SqlHelper.CreateParameter("@name", Name)
@@ -122,12 +122,10 @@ namespace umbraco.cms.businesslogic.propertytype
internal void Load()
{
using (var dr = SqlHelper.ExecuteReader(@" SELECT parentGroupId, contenttypeNodeId, sortOrder, text FROM cmsPropertyTypeGroup WHERE id = @id", SqlHelper.CreateParameter("@id", Id)))
using (var dr = SqlHelper.ExecuteReader(@" SELECT contenttypeNodeId, sortOrder, text FROM cmsPropertyTypeGroup WHERE id = @id", SqlHelper.CreateParameter("@id", Id)))
{
if (dr.Read())
{
// if no parent, the value should just be null. The GetInt helper method returns -1 if value is null so we need to check
ParentId = dr.GetInt("parentGroupId") != -1 ? dr.GetInt("parentGroupId") : 0;
SortOrder = dr.GetInt("sortOrder");
ContentTypeId = dr.GetInt("contenttypeNodeId");
Name = dr.GetString("text");
@@ -156,14 +154,6 @@ namespace umbraco.cms.businesslogic.propertytype
return ptgs;
}
private object ConvertParentId(int parentId)
{
if (parentId == 0)
return DBNull.Value;
return parentId;
}
/// <summary>
/// Gets the SQL helper.
/// </summary>