public override IMedia GetByVersion(Guid versionId) { var sql = GetBaseQuery(false); sql.Where("cmsContentVersion.VersionId = @VersionId", new { VersionId = versionId }); sql.OrderByDescending <ContentVersionDto>(x => x.VersionDate); var dto = Database.Fetch <ContentVersionDto, ContentDto, NodeDto>(sql).FirstOrDefault(); if (dto == null) { return(null); } var mediaType = _mediaTypeRepository.Get(dto.ContentDto.ContentTypeId); var factory = new MediaFactory(mediaType, NodeObjectTypeId, dto.NodeId); var media = factory.BuildEntity(dto); var properties = GetPropertyCollection(sql, new[] { new DocumentDefinition(dto.NodeId, dto.VersionId, media.UpdateDate, media.CreateDate, mediaType) }); media.Properties = properties[dto.NodeId]; //on initial construction we don't want to have dirty properties tracked // http://issues.umbraco.org/issue/U4-1946 ((Entity)media).ResetDirtyProperties(false); return(media); }
protected override IMedia PerformGet(int id) { var sql = GetBaseQuery(false); sql.Where(GetBaseWhereClause(), new { Id = id }); sql.OrderByDescending <ContentVersionDto>(x => x.VersionDate); var dto = Database.Fetch <ContentVersionDto, ContentDto, NodeDto>(sql).FirstOrDefault(); if (dto == null) { return(null); } var mediaType = _mediaTypeRepository.Get(dto.ContentDto.ContentTypeId); var factory = new MediaFactory(mediaType, NodeObjectTypeId, id); var media = factory.BuildEntity(dto); media.Properties = GetPropertyCollection(id, dto.VersionId, mediaType, media.CreateDate, media.UpdateDate); //on initial construction we don't want to have dirty properties tracked // http://issues.umbraco.org/issue/U4-1946 ((Entity)media).ResetDirtyProperties(false); return(media); }
private IEnumerable <IMedia> MapDtosToContent(List <ContentDto> dtos, bool withCache = false) { var temps = new List <TempContent <Core.Models.Media> >(); var contentTypes = new Dictionary <int, IMediaType?>(); var content = new Core.Models.Media[dtos.Count]; for (var i = 0; i < dtos.Count; i++) { ContentDto dto = dtos[i]; if (withCache) { // if the cache contains the (proper version of the) item, use it IMedia?cached = IsolatedCache.GetCacheItem <IMedia>(RepositoryCacheKeys.GetKey <IMedia, int>(dto.NodeId)); if (cached != null && cached.VersionId == dto.ContentVersionDto.Id) { content[i] = (Core.Models.Media)cached; continue; } } // else, need to build it // get the content type - the repository is full cache *but* still deep-clones // whatever comes out of it, so use our own local index here to avoid this var contentTypeId = dto.ContentTypeId; if (contentTypes.TryGetValue(contentTypeId, out IMediaType? contentType) == false) { contentTypes[contentTypeId] = contentType = _mediaTypeRepository.Get(contentTypeId); } Core.Models.Media c = content[i] = ContentBaseFactory.BuildEntity(dto, contentType); // need properties var versionId = dto.ContentVersionDto.Id; temps.Add(new TempContent <Core.Models.Media>(dto.NodeId, versionId, 0, contentType, c)); } // load all properties for all documents from database in 1 query - indexed by version id IDictionary <int, PropertyCollection> properties = GetPropertyCollections(temps); // assign properties foreach (TempContent <Core.Models.Media> temp in temps) { if (temp.Content is not null) { temp.Content.Properties = properties[temp.VersionId]; // reset dirty initial properties (U4-1946) temp.Content.ResetDirtyProperties(false); } } return(content); }
protected override IMedia PerformGet(int id) { var sql = GetBaseQuery(false); sql.Where(GetBaseWhereClause(), new { Id = id }); sql.OrderByDescending <ContentVersionDto>(x => x.VersionDate); var dto = Database.Fetch <ContentVersionDto, ContentDto, NodeDto>(sql).FirstOrDefault(); if (dto == null) { return(null); } var mediaType = _mediaTypeRepository.Get(dto.ContentDto.ContentTypeId); var factory = new MediaFactory(mediaType, NodeObjectTypeId, id); var media = factory.BuildEntity(dto); media.Properties = GetPropertyCollection(id, dto.VersionId, mediaType, media.CreateDate, media.UpdateDate); ((ICanBeDirty)media).ResetDirtyProperties(); return(media); }
/// <summary> /// This is the underlying method that processes most queries for this repository /// </summary> /// <param name="sqlFull"> /// The full SQL to select all media data /// </param> /// <param name="pagingSqlQuery"> /// The Id SQL to just return all media ids - used to process the properties for the media item /// </param> /// <param name="withCache"></param> /// <returns></returns> private IEnumerable <IMedia> ProcessQuery(Sql sqlFull, PagingSqlQuery pagingSqlQuery, bool withCache = false) { // fetch returns a list so it's ok to iterate it in this method var dtos = Database.Fetch <ContentVersionDto, ContentDto, NodeDto>(sqlFull); //This is a tuple list identifying if the content item came from the cache or not var content = new List <Tuple <IMedia, bool> >(); var defs = new DocumentDefinitionCollection(); //track the looked up content types, even though the content types are cached // they still need to be deep cloned out of the cache and we don't want to add // the overhead of deep cloning them on every item in this loop var contentTypes = new Dictionary <int, IMediaType>(); foreach (var dto in dtos) { // if the cache contains the item, use it if (withCache) { var cached = IsolatedCache.GetCacheItem <IMedia>(GetCacheIdKey <IMedia>(dto.NodeId)); //only use this cached version if the dto returned is the same version - this is just a safety check, media doesn't //store different versions, but just in case someone corrupts some data we'll double check to be sure. if (cached != null && cached.Version == dto.VersionId) { content.Add(new Tuple <IMedia, bool>(cached, true)); continue; } } // else, need to fetch from the database // content type repository is full-cache so OK to get each one independently IMediaType contentType; if (contentTypes.ContainsKey(dto.ContentDto.ContentTypeId)) { contentType = contentTypes[dto.ContentDto.ContentTypeId]; } else { contentType = _mediaTypeRepository.Get(dto.ContentDto.ContentTypeId); contentTypes[dto.ContentDto.ContentTypeId] = contentType; } // track the definition and if it's successfully added or updated then processed if (defs.AddOrUpdate(new DocumentDefinition(dto, contentType))) { content.Add(new Tuple <IMedia, bool>(MediaFactory.BuildEntity(dto, contentType), false)); } } // load all properties for all documents from database in 1 query var propertyData = GetPropertyCollection(pagingSqlQuery, defs); // assign property data foreach (var contentItem in content) { var cc = contentItem.Item1; var fromCache = contentItem.Item2; //if this has come from cache, we do not need to build up it's structure if (fromCache) { continue; } cc.Properties = propertyData[cc.Version]; //on initial construction we don't want to have dirty properties tracked // http://issues.umbraco.org/issue/U4-1946 cc.ResetDirtyProperties(false); } return(content.Select(x => x.Item1).ToArray()); }