Adds MediaTypeRepository implementation U4-958

This commit is contained in:
Morten@Thinkpad-X220
2012-10-09 10:26:55 -02:00
parent 9ca4fbff9d
commit 87ff6d03e8
5 changed files with 175 additions and 7 deletions

View File

@@ -394,6 +394,9 @@ namespace Umbraco.Core.Models
{
base.AddingEntity();
Key = Guid.NewGuid();
if (ParentId == 0)
_parentId = -1;
}
/// <summary>

View File

@@ -247,5 +247,27 @@ namespace Umbraco.Core.Models
{
get { return PropertyGroups.SelectMany(x => x.PropertyTypes); }
}
/// <summary>
/// Method to call when Entity is being saved
/// </summary>
/// <remarks>Created date is set and a Unique key is assigned</remarks>
internal override void AddingEntity()
{
base.AddingEntity();
Key = Guid.NewGuid();
if (ParentId == 0)
_parentId = -1;
}
/// <summary>
/// Method to call when Entity is being updated
/// </summary>
/// <remarks>Modified Date is set and a new Version guid is set</remarks>
internal override void UpdatingEntity()
{
base.UpdatingEntity();
}
}
}

View File

@@ -7,7 +7,7 @@ namespace Umbraco.Core.Persistence.Factories
{
internal class PropertyGroupFactory : IEntityFactory<IEnumerable<PropertyGroup>, IEnumerable<TabDto>>
{
private int _id;
private readonly int _id;
public PropertyGroupFactory(int id)
{

View File

@@ -181,7 +181,7 @@ namespace Umbraco.Core.Persistence.Repositories
var contentTypeDto = dto.ContentTypeDto;
Database.Insert(contentTypeDto);
//TODO Insert new DocumentType entries - NOTE only seems relevant as long as Templates resides in the DB
//TODO Insert new DocumentType entries - NOTE only seems relevant as long as Templates resides in the DB?
//TODO Insert allowed Templates and DocumentTypes
//TODO Insert ContentType composition in new table

View File

@@ -1,8 +1,14 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Core.Models.EntityBase;
using Umbraco.Core.Models.Rdbms;
using Umbraco.Core.Persistence.Caching;
using Umbraco.Core.Persistence.Factories;
using Umbraco.Core.Persistence.Querying;
using Umbraco.Core.Persistence.Relators;
using Umbraco.Core.Persistence.UnitOfWork;
namespace Umbraco.Core.Persistence.Repositories
@@ -24,17 +30,65 @@ namespace Umbraco.Core.Persistence.Repositories
protected override IMediaType PerformGet(int id)
{
throw new NotImplementedException();
var contentTypeSql = GetBaseQuery(false);
contentTypeSql.Append(GetBaseWhereClause(id));
var dto = Database.Query<ContentTypeDto, NodeDto>(contentTypeSql).FirstOrDefault();
if (dto == null)
return null;
var propertySql = new Sql();
propertySql.Select("*");
propertySql.From("cmsTab");
propertySql.RightJoin("cmsPropertyType ON [cmsTab].[id] = [cmsPropertyType].[tabId]");
propertySql.InnerJoin("cmsDataType ON [cmsPropertyType].[dataTypeId] = [cmsDataType].[nodeId]");
propertySql.Where("[cmsPropertyType].[contentTypeId] = @Id", new { Id = id });
var tabDtos = Database.Fetch<TabDto, PropertyTypeDto, DataTypeDto, TabDto>(new TabPropertyTypeRelator().Map, propertySql);
var factory = new MediaTypeFactory(NodeObjectTypeId);
var contentType = factory.BuildEntity(dto);
var propertyFactory = new PropertyGroupFactory(id);
var propertyGroups = propertyFactory.BuildEntity(tabDtos);
contentType.PropertyGroups = new PropertyGroupCollection(propertyGroups);
((MediaType)contentType).ResetDirtyProperties();
return contentType;
}
protected override IEnumerable<IMediaType> PerformGetAll(params int[] ids)
{
throw new NotImplementedException();
if (ids.Any())
{
foreach (var id in ids)
{
yield return Get(id);
}
}
else
{
var nodeDtos = Database.Fetch<NodeDto>("WHERE nodeObjectType = @NodeObjectType", new { NodeObjectType = NodeObjectTypeId });
foreach (var nodeDto in nodeDtos)
{
yield return Get(nodeDto.NodeId);
}
}
}
protected override IEnumerable<IMediaType> PerformGetByQuery(IQuery<IMediaType> query)
{
throw new NotImplementedException();
var sqlClause = GetBaseQuery(false);
var translator = new SqlTranslator<IMediaType>(sqlClause, query);
var sql = translator.Translate();
var documentTypeDtos = Database.Fetch<ContentTypeDto, NodeDto>(sql);
foreach (var dto in documentTypeDtos)
{
yield return Get(dto.NodeId);
}
}
#endregion
@@ -85,12 +139,101 @@ namespace Umbraco.Core.Persistence.Repositories
protected override void PersistNewItem(IMediaType entity)
{
throw new NotImplementedException();
((MediaType)entity).AddingEntity();
var factory = new MediaTypeFactory(NodeObjectTypeId);
var dto = factory.BuildDto(entity);
//Logic for setting Path, Level and SortOrder
var parent = Database.First<NodeDto>("WHERE id = @ParentId", new { ParentId = entity.ParentId });
int level = parent.Level + 1;
int sortOrder =
Database.ExecuteScalar<int>("SELECT COUNT(*) FROM umbracoNode WHERE parentID = @ParentId AND nodeObjectType = @NodeObjectType",
new { ParentId = entity.ParentId, NodeObjectType = NodeObjectTypeId });
//Create the (base) node data - umbracoNode
var nodeDto = dto.NodeDto;
nodeDto.Path = parent.Path;
nodeDto.Level = short.Parse(level.ToString(CultureInfo.InvariantCulture));
nodeDto.SortOrder = sortOrder;
var o = Database.IsNew(nodeDto) ? Convert.ToInt32(Database.Insert(nodeDto)) : Database.Update(nodeDto);
//Update with new correct path
nodeDto.Path = string.Concat(parent.Path, ",", nodeDto.NodeId);
Database.Update(nodeDto);
//Update entity with correct values
entity.Id = nodeDto.NodeId; //Set Id on entity to ensure an Id is set
entity.Path = nodeDto.Path;
entity.SortOrder = sortOrder;
entity.Level = level;
//Insert new ContentType entry
Database.Insert(dto);
}
protected override void PersistUpdatedItem(IMediaType entity)
{
throw new NotImplementedException();
//Updates Modified date
((MediaType)entity).UpdatingEntity();
var propertyFactory = new PropertyGroupFactory(entity.Id);
var factory = new MediaTypeFactory(NodeObjectTypeId);
var dto = factory.BuildDto(entity);
var nodeDto = dto.NodeDto;
var o = Database.Update(nodeDto);
//Look up ContentType entry to get PrimaryKey for updating the DTO
var dtoPk = Database.First<ContentTypeDto>("WHERE nodeId = @Id", new { Id = entity.Id });
dto.PrimaryKey = dtoPk.PrimaryKey;
Database.Update(dto);
//Check Dirty properties for Tabs/Groups and PropertyTypes - insert and delete accordingly
if (((ICanBeDirty)entity).IsPropertyDirty("PropertyGroups") || entity.PropertyGroups.Any(x => x.IsDirty()))
{
//Delete PropertyTypes by excepting entries from db with entries from collections
var dbPropertyTypes = Database.Fetch<PropertyTypeDto>("WHERE contentTypeId = @Id", new { Id = entity.Id }).Select(x => x.Alias);
var entityPropertyTypes = entity.PropertyTypes.Select(x => x.Alias);
var aliases = dbPropertyTypes.Except(entityPropertyTypes);
foreach (var alias in aliases)
{
Database.Delete<PropertyTypeDto>("WHERE contentTypeId = @Id AND Alias = @Alias", new { Id = entity.Id, Alias = alias });
}
//Delete Tabs/Groups by excepting entries from db with entries from collections
var dbPropertyGroups = Database.Fetch<TabDto>("WHERE contenttypeNodeId = @Id", new { Id = entity.Id }).Select(x => x.Text);
var entityPropertyGroups = entity.PropertyGroups.Select(x => x.Name);
var tabs = dbPropertyGroups.Except(entityPropertyGroups);
foreach (var tabName in tabs)
{
Database.Delete<TabDto>("WHERE contenttypeNodeId = @Id AND text = @Name", new { Id = entity.Id, Name = tabName });
}
//Run through all groups and types to insert or update entries
foreach (var propertyGroup in entity.PropertyGroups)
{
var tabDto = propertyFactory.BuildTabDto(propertyGroup);
int groupPrimaryKey = propertyGroup.HasIdentity
? Database.Update(tabDto)
: Convert.ToInt32(Database.Insert(tabDto));
if (!propertyGroup.HasIdentity)
propertyGroup.Id = groupPrimaryKey;//Set Id on new PropertyGroup
//This should indicate that neither group nor property types has been touched, but this implies a deeper 'Dirty'-lookup
//if(!propertyGroup.IsDirty()) continue;
foreach (var propertyType in propertyGroup.PropertyTypes)
{
var propertyTypeDto = propertyFactory.BuildPropertyTypeDto(propertyGroup.Id, propertyType);
int typePrimaryKey = propertyType.HasIdentity
? Database.Update(propertyTypeDto)
: Convert.ToInt32(Database.Insert(propertyTypeDto));
if (!propertyType.HasIdentity)
propertyType.Id = typePrimaryKey;//Set Id on new PropertyType
}
}
}
((MediaType)entity).ResetDirtyProperties();
}
#endregion