/// <summary> /// Maps data from the .Net property value to the CMS value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>The value to write</returns> /// <exception cref="System.NotSupportedException"> /// Can't set DisplayName. Value is not of type System.String /// or /// Can't set Name. Value is not of type System.String /// or /// You can not save SitecoreInfo {0}.Formatted(scConfig.Type) /// </exception> /// <exception cref="Glass.Mapper.MapperException">You can not set an empty or null Item name</exception> public override void MapToCms(AbstractDataMappingContext mappingContext) { var context = mappingContext as SitecoreDataMappingContext; var item = context.Item; var value = context.PropertyValue; var scConfig = Configuration as SitecoreInfoConfiguration; switch (scConfig.Type) { case SitecoreInfoType.DisplayName: if (value is string || value == null) item[Global.Fields.DisplayName] = (value ?? string.Empty).ToString(); else throw new NotSupportedException("Can't set DisplayName. Value is not of type System.String"); break; case SitecoreInfoType.Name: if (value is string || value == null) { //if the name is null or empty nothing should happen if ((value ?? string.Empty).ToString().IsNullOrEmpty()) throw new MapperException("You can not set an empty or null Item name"); if (item.Name != value.ToString()) { item.Name = value.ToString(); } } else throw new NotSupportedException("Can't set Name. Value is not of type System.String"); break; default: throw new NotSupportedException("You can not save SitecoreInfo {0}".Formatted(scConfig.Type)); } }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scConfig = Configuration as SitecoreNodeConfiguration; var scContext = mappingContext as SitecoreDataMappingContext; var item = scContext.Item; Item targetItem = null; if (scConfig.Id.IsNotNullOrEmpty()) { var guid = Guid.Empty; if (Guid.TryParse(scConfig.Id, out guid) && guid != Guid.Empty) { targetItem = item.Database.GetItem(new ID(guid), item.Language); } } else if (!scConfig.Path.IsNullOrEmpty()) { targetItem = item.Database.GetItem(scConfig.Path, item.Language); } if (targetItem == null || targetItem.Versions.Count == 0) { return null; } else { return scContext.Service.CreateType(scConfig.PropertyInfo.PropertyType, targetItem, scConfig.IsLazy, scConfig.InferType, null); } }
/// <summary> /// Maps data from the .Net property value to the CMS value /// </summary> /// <param name="mappingContext"></param> /// <exception cref="MapperException">You can not set an empty or null Item name</exception> /// <exception cref="System.NotSupportedException"> /// Can't set Name. Value is not of type System.String /// or /// You can not save UmbracoInfo {0}.Formatted(config.Type) /// </exception> public override void MapToCms(AbstractDataMappingContext mappingContext) { var context = mappingContext as UmbracoDataMappingContext; var content = context.Content; var value = context.PropertyValue; var config = Configuration as UmbracoInfoConfiguration; switch (config.Type) { case UmbracoInfoType.Name: if (value is string || value == null) { //if the name is null or empty nothing should happen if ((value ?? string.Empty).ToString().IsNullOrEmpty()) throw new MapperException("You can not set an empty or null Item name"); if (content.Name != value.ToString()) { content.Name = value.ToString(); context.Service.ContentService.Save(content); } } else throw new NotSupportedException("Can't set Name. Value is not of type System.String"); break; default: throw new NotSupportedException("You can not save UmbracoInfo {0}".Formatted(config.Type)); } }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scContext = mappingContext as SitecoreDataMappingContext; var scConfig = Configuration as SitecoreChildrenConfiguration; Func<IEnumerable<Item>> getItems = () => ItemManager.GetChildren(scContext.Item, SecurityCheck.Enable, ChildListOptions.None); if (_activator == null) { _activator = Mapper.Utilities.GetActivator( typeof (LazyItemEnumerable<>), new[] {GenericType}, getItems, scConfig.IsLazy, scConfig.InferType, scContext.Service); } return _activator(getItems, scConfig.IsLazy, scConfig.InferType, scContext.Service); }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext"></param> /// <returns></returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var umbContext = mappingContext as UmbracoDataMappingContext; var umbConfig = Configuration as UmbracoChildrenConfiguration; Type genericType = Utilities.GetGenericArgument(Configuration.PropertyInfo.PropertyType); Func<IEnumerable<IContent>> getItems = null; if (umbContext.PublishedOnly) { getItems = () => umbContext.Service.ContentService.GetChildren(umbContext.Content.Id) .Select(c => umbContext.Service.ContentService.GetPublishedVersion(c.Id)); } else { getItems = () => umbContext.Service.ContentService.GetChildren(umbContext.Content.Id); } return Utilities.CreateGenericType( typeof(LazyContentEnumerable<>), new[] {genericType}, getItems, umbConfig.IsLazy, umbConfig.InferType, umbContext.Service ); }
/// <summary> /// Maps data from the .Net property value to the CMS value /// </summary> /// <param name="mappingContext"></param> /// <exception cref="MapperException">You can not set an empty or null Item name</exception> /// <exception cref="System.NotSupportedException"> /// Can't set Name. Value is not of type System.String /// or /// You can not save UmbracoInfo {0}.Formatted(config.Type) /// </exception> public override void MapToCms(AbstractDataMappingContext mappingContext) { var context = mappingContext as UmbracoDataMappingContext; var content = context.Content; var value = context.PropertyValue; var config = Configuration as UmbracoInfoConfiguration; switch (config.Type) { case UmbracoInfoType.Name: if (value is string || value == null) { //if the name is null or empty nothing should happen if ((value ?? string.Empty).ToString().IsNullOrEmpty()) { throw new MapperException("You can not set an empty or null Item name"); } if (content.Name != value.ToString()) { content.Name = value.ToString(); context.Service.ContentService.Save(content); } } else { throw new NotSupportedException("Can't set Name. Value is not of type System.String"); } break; default: throw new NotSupportedException("You can not save UmbracoInfo {0}".Formatted(config.Type)); } }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> /// <exception cref="System.NotSupportedException">The type {0} on {0}.{1} is not supported by SitecoreIdMapper.Formatted /// (scConfig.PropertyInfo.ReflectedType.FullName, /// scConfig.PropertyInfo.Name)</exception> public override object MapToProperty(AbstractDataMappingContext mappingContext) { SitecoreDataMappingContext context = (SitecoreDataMappingContext)mappingContext; var item = context.Item; return(_getValue(item)); }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scConfig = Configuration as SitecoreNodeConfiguration; var scContext = mappingContext as SitecoreDataMappingContext; var item = scContext.Item; Item targetItem = null; if (scConfig.Id.HasValue()) { var guid = Guid.Empty; if (Guid.TryParse(scConfig.Id, out guid) && guid != Guid.Empty) { targetItem = item.Database.GetItem(new ID(guid), item.Language); } } else if (!scConfig.Path.IsNullOrEmpty()) { targetItem = item.Database.GetItem(scConfig.Path, item.Language); } var options = new GetItemByItemOptions(); options.Copy(mappingContext.Options); options.Item = targetItem; scConfig.GetPropertyOptions(options); return(scContext.Service.GetItem(options)); }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext"></param> /// <returns></returns> /// <exception cref="MapperException">UmbracoInfoType {0} not supported.Formatted(config.Type)</exception> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var context = mappingContext as UmbracoDataMappingContext; var content = context.Content; var config = Configuration as UmbracoInfoConfiguration; switch (config.Type) { case UmbracoInfoType.Name: return content.Name; case UmbracoInfoType.Path: return content.Path; case UmbracoInfoType.ContentTypeAlias: return content.ContentType.Alias; case UmbracoInfoType.ContentTypeName: return content.ContentType.Name; //case UmbracoInfoType.Url: // return content.Name.FormatUrl().ToLower(); case UmbracoInfoType.CreateDate: return content.CreateDate; case UmbracoInfoType.UpdateDate: return content.UpdateDate; case UmbracoInfoType.Version: return content.Version; case UmbracoInfoType.Creator: var user = new User(content.CreatorId); return user.LoginName; default: throw new MapperException("UmbracoInfoType {0} not supported".Formatted(config.Type)); } }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext"></param> /// <returns></returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var umbContext = mappingContext as UmbracoDataMappingContext; var umbConfig = Configuration as UmbracoChildrenConfiguration; Type genericType = Utilities.GetGenericArgument(Configuration.PropertyInfo.PropertyType); Func <IEnumerable <IContent> > getItems = null; if (umbContext.PublishedOnly) { getItems = () => umbContext.Service.ContentService.GetChildren(umbContext.Content.Id) .Select(c => umbContext.Service.ContentService.GetPublishedVersion(c.Id)); } else { getItems = () => umbContext.Service.ContentService.GetChildren(umbContext.Content.Id); } return(Utilities.CreateGenericType( typeof(LazyContentEnumerable <>), new[] { genericType }, getItems, umbConfig.IsLazy, umbConfig.InferType, umbContext.Service )); }
/// <summary> /// Maps the properties to object. /// </summary> /// <param name="obj">The obj.</param> /// <param name="service">The service.</param> /// <param name="context">The context.</param> public void MapPropertiesToObject(object obj, IAbstractService service, AbstractTypeCreationContext context) { try { //create properties AbstractDataMappingContext dataMappingContext = service.CreateDataMappingContext(context, obj); foreach (var prop in Properties) { try { prop.Mapper.MapCmsToProperty(dataMappingContext); } catch (Exception e) { throw new MapperException( "Failed to map property {0} on {1}".Formatted(prop.PropertyInfo.Name, prop.PropertyInfo.DeclaringType.FullName), e); } } } catch (Exception ex) { throw new MapperException( "Failed to map properties on {0}.".Formatted(context.DataSummary()), ex); } }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scConfig = Configuration as SitecoreNodeConfiguration; var scContext = mappingContext as SitecoreDataMappingContext; var item = scContext.Item; Item targetItem = null; if (scConfig.Id.IsNotNullOrEmpty()) { var guid = Guid.Empty; if (Guid.TryParse(scConfig.Id, out guid) && guid != Guid.Empty) { targetItem = item.Database.GetItem(new ID(guid), item.Language); } } else if (!scConfig.Path.IsNullOrEmpty()) { targetItem = item.Database.GetItem(scConfig.Path, item.Language); } if (targetItem == null || targetItem.Versions.Count == 0) { return(null); } else { return(scContext.Service.CreateType(scConfig.PropertyInfo.PropertyType, targetItem, scConfig.IsLazy, scConfig.InferType, null)); } }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scConfig = Configuration as SitecoreLinkedConfiguration; var scContext = mappingContext as SitecoreDataMappingContext; var item = scContext.Item; var linkDb = global::Sitecore.Globals.LinkDatabase; var options = new GetItemsByFuncOptions(); options.Copy(mappingContext.Options); scConfig.GetPropertyOptions(options); options.ItemsFunc = new Func <Database, IEnumerable <Item> >(database => { //ME - i am not sure this is correct but there is an odd behaviour of references // languges come back as invariant, going with default language in this scenario var references = new Func <IEnumerable <Item> >(() => { var itemLinks = linkDb.GetReferences(item); return(itemLinks.Select(x => x.GetTargetItem())); }); IEnumerable <Item> items; switch (scConfig.Option) { case SitecoreLinkedOptions.All: var itemLinks1 = references(); var itemLinks2 = linkDb.GetReferrers(item); items = itemLinks1.Union(itemLinks2.Select(x => x.GetSourceItem())); break; case SitecoreLinkedOptions.References: items = references(); break; case SitecoreLinkedOptions.Referrers: var itemLinks4 = linkDb.GetReferrers(item); items = itemLinks4.Select(x => x.GetSourceItem()); break; default: items = new List <Item>(); break; } return(items); }); var result = scContext.Service.GetItems(options); return(result); }
public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scContext = mappingContext as SitecoreDataMappingContext; if (scContext != null) { return scContext.Item; } return null; }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> /// <exception cref="Glass.Mapper.MapperException">SitecoreInfoType {0} not supported.Formatted(scConfig.Type)</exception> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var context = mappingContext as SitecoreDataMappingContext; var item = context.Item; var scConfig = Configuration as SitecoreInfoConfiguration; //TODO: move this to the config? var urlOptions = Utilities.CreateUrlOptions(scConfig.UrlOptions); switch (scConfig.Type) { case SitecoreInfoType.ContentPath: return(item.Paths.ContentPath); case SitecoreInfoType.DisplayName: return(item.DisplayName); case SitecoreInfoType.FullPath: return(item.Paths.FullPath); case SitecoreInfoType.Name: return(item.Name); case SitecoreInfoType.Key: return(item.Key); case SitecoreInfoType.MediaUrl: var media = new global::Sitecore.Data.Items.MediaItem(item); return(global::Sitecore.Resources.Media.MediaManager.GetMediaUrl(media)); case SitecoreInfoType.Path: return(item.Paths.Path); case SitecoreInfoType.TemplateId: if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof(Sitecore.Data.ID)) { return(item.TemplateID); } return(item.TemplateID.Guid); case SitecoreInfoType.TemplateName: return(item.TemplateName); case SitecoreInfoType.Url: return(LinkManager.GetItemUrl(item, urlOptions)); case SitecoreInfoType.Version: return(item.Version.Number); case SitecoreInfoType.Language: return(item.Language); default: throw new MapperException("SitecoreInfoType {0} not supported".Formatted(scConfig.Type)); } }
/// <summary> /// Maps data from the .Net property value to the CMS value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>The value to write</returns> public override void MapToCms(AbstractDataMappingContext mappingContext) { var scConfig = Configuration as SitecoreFieldConfiguration; var scContext = mappingContext as SitecoreDataMappingContext; var field = Utilities.GetField(scContext.Item, scConfig.FieldId, scConfig.FieldName); object value = Configuration.PropertyInfo.GetValue(mappingContext.Object, null); SetField(field, value, scConfig, scContext); }
/// <summary> /// Maps the properties to object. /// </summary> /// <param name="obj">The obj.</param> /// <param name="service">The service.</param> /// <param name="context">The context.</param> public void MapPropertiesToObject(object obj, IAbstractService service, AbstractTypeCreationContext context) { //create properties AbstractDataMappingContext dataMappingContext = service.CreateDataMappingContext(context, obj); foreach (var prop in Properties) { prop.Mapper.MapCmsToProperty(dataMappingContext); } }
/// <summary> /// Maps data from the .Net property value to the CMS value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>The value to write</returns> public override void MapToCms(AbstractDataMappingContext mappingContext) { var scConfig = Configuration as SitecoreFieldConfiguration; var scContext = mappingContext as SitecoreDataMappingContext ; var field = Utilities.GetField(scContext.Item, scConfig.FieldId, scConfig.FieldName); object value = Configuration.PropertyInfo.GetValue(mappingContext.Object, null); SetField(field, value, scConfig, scContext); }
public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scContext = mappingContext as SitecoreDataMappingContext; var scConfig = Configuration as SitecoreChildrenConfiguration; if (scContext != null && scConfig != null) { return new ChildrenCast(scContext.Service, scContext.Item, scConfig.IsLazy, scConfig.InferType); } return null; }
public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scContext = mappingContext as SitecoreDataMappingContext; if (scContext != null) { return(scContext.Item); } return(null); }
/// <summary> /// Maps data from the .Net property value to the CMS value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>The value to write</returns> /// <exception cref="System.NotSupportedException"> /// Can't set DisplayName. Value is not of type System.String /// or /// Can't set Name. Value is not of type System.String /// or /// You can not save SitecoreInfo {0}.Formatted(scConfig.Type) /// </exception> /// <exception cref="Glass.Mapper.MapperException">You can not set an empty or null Item name</exception> public override void MapToCms(AbstractDataMappingContext mappingContext) { var context = mappingContext as SitecoreDataMappingContext; if (context == null) { throw new NullReferenceException("Mapping context has not been set."); } var item = context.Item; var value = context.PropertyValue; var scConfig = Configuration as SitecoreInfoConfiguration; if (scConfig == null) { throw new NullReferenceException("Configuration is not set"); } switch (scConfig.Type) { case SitecoreInfoType.DisplayName: if (value is string || value == null) { item[Global.Fields.DisplayName] = (value ?? string.Empty).ToString(); } else { throw new NotSupportedException("Can't set DisplayName. Value is not of type System.String"); } break; case SitecoreInfoType.Name: if (value is string || value == null) { //if the name is null or empty nothing should happen if (value == null || value.ToString().IsNullOrEmpty()) { throw new MapperException("You can not set an empty or null Item name"); } if (item.Name != value.ToString()) { item.Name = value.ToString(); } } else { throw new NotSupportedException("Can't set Name. Value is not of type System.String"); } break; default: throw new NotSupportedException("You can not save SitecoreInfo {0}".Formatted(scConfig.Type)); } }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scContext = mappingContext as SitecoreDataMappingContext; var scConfig = Configuration as SitecoreParentConfiguration; return(scContext.Service.CreateType( scConfig.PropertyInfo.PropertyType, scContext.Item.Parent, scConfig.IsLazy, scConfig.InferType)); }
public override void MapPropertyToCms(AbstractDataMappingContext mappingContext) { var scConfig = Configuration as SitecoreFieldConfiguration; if ((scConfig.Setting & SitecoreFieldSettings.PageEditorOnly) == SitecoreFieldSettings.PageEditorOnly) { return; } base.MapPropertyToCms(mappingContext); }
public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scContext = mappingContext as SitecoreDataMappingContext; var scConfig = Configuration as SitecoreChildrenConfiguration; if (scContext != null && scConfig != null) { return(new ChildrenCast(scContext.Service, scContext.Item, scConfig.IsLazy, scConfig.InferType)); } return(null); }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext"></param> /// <returns></returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var umbContext = mappingContext as UmbracoDataMappingContext; var umbConfig = Configuration as UmbracoParentConfiguration; return umbContext.Service.CreateType( umbConfig.PropertyInfo.PropertyType, umbContext.Service.ContentService.GetById(umbContext.Content.ParentId), umbConfig.IsLazy, umbConfig.InferType); }
public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scContext = mappingContext as SitecoreDataMappingContext; var scConfig = Configuration as SitecoreSelfConfiguration; if (scContext != null && scConfig != null) { return(scContext.Service.CreateType(scConfig.PropertyInfo.PropertyType, scContext.Item, scConfig.IsLazy, scConfig.InferType, null)); } return(null); }
public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scContext = mappingContext as SitecoreDataMappingContext; var scConfig = Configuration as SitecoreSelfConfiguration; if (scContext != null && scConfig != null) { return scContext.Service.CreateType(scConfig.PropertyInfo.PropertyType, scContext.Item, scConfig.IsLazy, scConfig.InferType, null); } return null; }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext"></param> /// <returns></returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var umbContext = mappingContext as UmbracoDataMappingContext; var umbConfig = Configuration as UmbracoParentConfiguration; return(umbContext.Service.CreateType( umbConfig.PropertyInfo.PropertyType, umbContext.Service.ContentService.GetById(umbContext.Content.ParentId), umbConfig.IsLazy, umbConfig.InferType)); }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scContext = mappingContext as SitecoreDataMappingContext; var scConfig = Configuration as SitecoreParentConfiguration; return scContext.Service.CreateType( scConfig.PropertyInfo.PropertyType, scContext.Item.Parent, scConfig.IsLazy, scConfig.InferType); }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scConfig = Configuration as SitecoreFieldConfiguration; var scContext = mappingContext as SitecoreDataMappingContext; var field = Utilities.GetField(scContext.Item, scConfig.FieldId, scConfig.FieldName); if (field == null) return null; return GetField(field, scConfig, scContext); }
/// <summary> /// Executes the specified args. /// </summary> /// <param name="args">The args.</param> /// <exception cref="Glass.Mapper.Pipelines.PipelineException">No config set, can not save object</exception> public void Execute(ObjectSavingArgs args) { var savingContext = args.SavingContext; AbstractDataMappingContext dataMappingContext = args.Service.CreateDataMappingContext(savingContext); if (savingContext.Config == null) { throw new PipelineException("No config set, can not save object", args); } savingContext.Config.Properties.ForEach(x => x.Mapper.MapPropertyToCms(dataMappingContext)); }
public LazyObjectInterceptor(ObjectConstructionArgs args, LazyLoadingHelper lazyLoadingHelper) { _args = args; Values = new ConcurrentDictionary <string, object>(); _mappingContext = _args.AbstractTypeCreationContext.CreateDataMappingContext(null); //if lazy loading diabled load all values now if (!lazyLoadingHelper.IsEnabled(args.Options)) { LoadAllValues(); } }
/// <summary> /// Maps data from the .Net property value to the CMS value /// </summary> /// <param name="mappingContext"></param> public override void MapToCms(AbstractDataMappingContext mappingContext) { var config = Configuration as UmbracoPropertyConfiguration; var context = mappingContext as UmbracoDataMappingContext; if (context.Content.Properties.Contains(config.PropertyAlias)) { var property = context.Content.Properties[config.PropertyAlias]; object value = Configuration.PropertyInfo.GetValue(mappingContext.Object, null); SetProperty(property, value, config, context); } }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext"></param> /// <returns></returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var config = Configuration as UmbracoPropertyConfiguration; var context = mappingContext as UmbracoDataMappingContext; if (context.Content.Properties.Select(p => p.Alias.ToLowerInvariant()).Contains(config.PropertyAlias.ToLowerInvariant())) { var property = context.Content.Properties.FirstOrDefault(p => p.Alias.ToLowerInvariant() == config.PropertyAlias.ToLowerInvariant()); return(GetProperty(property, config, context)); } return(null); }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> /// <exception cref="Glass.Mapper.MapperException">SitecoreInfoType {0} not supported.Formatted(scConfig.Type)</exception> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var context = mappingContext as SitecoreDataMappingContext; if (context == null) { throw new NullReferenceException("Mapping Context has not been set."); } var item = context.Item; return(_getValue(item)); }
/// <summary> /// Initializes a new instance of the <see cref="InterfacePropertyInterceptor"/> class. /// </summary> /// <param name="args">The args.</param> public InterfacePropertyInterceptor(ObjectConstructionArgs args) { _args = args; _fullName = _args.Configuration.Type.FullName; Values = new ConcurrentDictionary<string, object>(); _mappingContext = _args.Service.CreateDataMappingContext(_args.AbstractTypeCreationContext, null); //if lazy loading diabled load all values now if (!args.AbstractTypeCreationContext.IsLazy) { LoadAllValues(); } }
/// <summary> /// Initializes a new instance of the <see cref="InterfacePropertyInterceptor"/> class. /// </summary> /// <param name="args">The args.</param> public InterfacePropertyInterceptor(ObjectConstructionArgs args) { _args = args; _fullName = _args.Configuration.Type.FullName; Values = new ConcurrentDictionary <string, object>(); _mappingContext = _args.Service.CreateDataMappingContext(_args.AbstractTypeCreationContext, null); //if lazy loading diabled load all values now if (!args.AbstractTypeCreationContext.IsLazy) { LoadAllValues(); } }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scConfig = Configuration as SitecoreFieldConfiguration; var scContext = mappingContext as SitecoreDataMappingContext; var field = Utilities.GetField(scContext.Item, scConfig.FieldId, scConfig.FieldName); if (field == null) { return(DefaultValue); } return(GetField(field, scConfig, scContext)); }
public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scContext = mappingContext as SitecoreDataMappingContext; var scConfig = Configuration as SitecoreChildrenConfiguration; if (scContext != null && scConfig != null) { var options = new GetItemOptions(); options.Copy(mappingContext.Options); scConfig.GetPropertyOptions(options); return(new ChildrenCast(scContext.Service, scContext.Item, options, _lazyLoadingHelper)); } return(null); }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scContext = mappingContext as SitecoreDataMappingContext; var scConfig = Configuration as SitecoreChildrenConfiguration; Func <Database, IEnumerable <Item> > getItems = (database) => ItemManager.GetChildren(scContext.Item, SecurityCheck.Enable, ChildListOptions.None); var options = new GetItemsByFuncOptions(getItems); options.Copy(mappingContext.Options); scConfig.GetPropertyOptions(options); return(scContext.Service.GetItems(options)); }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext"></param> /// <returns></returns> /// <exception cref="System.NotSupportedException">The type {0} on {0}.{1} is not supported by UmbracoIdMapper.Formatted /// (umbConfig.PropertyInfo.ReflectedType.FullName, /// umbConfig.PropertyInfo.Name)</exception> public override object MapToProperty(AbstractDataMappingContext mappingContext) { UmbracoDataMappingContext context = mappingContext as UmbracoDataMappingContext; var node = context.Content; var umbConfig = Configuration as UmbracoIdConfiguration; if (umbConfig.PropertyInfo.PropertyType == typeof(int)) return node.Id; if (umbConfig.PropertyInfo.PropertyType == typeof(Guid)) return node.Key; throw new NotSupportedException("The type {0} on {0}.{1} is not supported by UmbracoIdMapper".Formatted (umbConfig.PropertyInfo.ReflectedType.FullName, umbConfig.PropertyInfo.Name)); }
public void Intercept(IInvocation invocation) { if (IndexFields.Any(x => x == invocation.Method.Name)) { invocation.Proceed(); } if (!invocation.Method.IsSpecialName || !invocation.Method.Name.StartsWith("get_") && !invocation.Method.Name.StartsWith("set_")) { return; } string str = invocation.Method.Name.Substring(0, 4); string name = invocation.Method.Name.Substring(4); if (!_isLoaded) { SitecoreTypeCreationContext typeCreationContext = _args.AbstractTypeCreationContext as SitecoreTypeCreationContext; typeCreationContext.Item = typeCreationContext.SitecoreService.Database.GetItem(Id); SitecoreTypeConfiguration typeConfiguration = TypeConfiguration; AbstractDataMappingContext dataMappingContext = _args.Service.CreateDataMappingContext(_args.AbstractTypeCreationContext, null); //todo filter fieldnames from FieldConfigs! foreach (AbstractPropertyConfiguration propertyConfiguration in typeConfiguration.Properties.Where(x => IndexFields.All(y => y != x.PropertyInfo.Name))) { object obj = propertyConfiguration.Mapper.MapToProperty(dataMappingContext); _values[propertyConfiguration.PropertyInfo.Name] = obj; } _isLoaded = true; } if (str == "get_") { if (_values.ContainsKey(name)) { object obj = _values[name]; invocation.ReturnValue = obj; } } else if (str == "set_") { _values[name] = invocation.Arguments[0]; } else { throw new MapperException(Glass.Mapper.ExtensionMethods.Formatted("Method with name {0}{1} on type {2} not supported.", (object)str, (object)name, (object)this._args.Configuration.Type.FullName)); } }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scConfig = Configuration as SitecoreQueryConfiguration; var scContext = mappingContext as SitecoreDataMappingContext; string query = ParseQuery(scConfig.Query, scContext.Item); if (scConfig.PropertyInfo.PropertyType.IsGenericType) { var options = new GetItemsByQueryOptions(); options.Copy(mappingContext.Options); options.Query = Sc.Query.New(query); options.Language = scContext.Item.Language; scConfig.GetPropertyOptions(options); if (scConfig.IsRelative) { options.RelativeItem = scContext.Item; } var result = scContext.Service.GetItems(options); return(result); } else { var options = new GetItemByQueryOptions(); options.Copy(mappingContext.Options); options.Query = Sc.Query.New(query); options.Language = scContext.Item.Language; scConfig.GetPropertyOptions(options); if (scConfig.IsRelative) { options.RelativeItem = scContext.Item; } return(scContext.Service.GetItem(options)); } }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scConfig = Configuration as SitecoreLinkedConfiguration; var scContext = mappingContext as SitecoreDataMappingContext; Type genericType = Utilities.GetGenericArgument(scConfig.PropertyInfo.PropertyType); var item = scContext.Item; //ME - i am not sure this is correct but there is an odd behaviour of references // languges come back as invariant, going with default language in this scenario var references = new Func <IEnumerable <Item> >(() => { var itemLinks = global::Sitecore.Configuration.Factory.GetLinkDatabase().GetReferences(item); var items = itemLinks.Select(x => x.GetTargetItem()); return(Utilities.GetLanguageItems(items, LanguageManager.DefaultLanguage)); }); var getItems = new Func <IEnumerable <Item> >(() => { switch (scConfig.Option) { case SitecoreLinkedOptions.All: var itemLinks1 = references(); var itemLinks2 = global::Sitecore.Configuration.Factory.GetLinkDatabase().GetReferrers(item); return(itemLinks1.Union(itemLinks2.Select(x => x.GetSourceItem()))); break; case SitecoreLinkedOptions.References: return(references()); break; case SitecoreLinkedOptions.Referrers: var itemLinks4 = global::Sitecore.Configuration.Factory.GetLinkDatabase().GetReferrers(item); return(itemLinks4.Select(x => x.GetSourceItem())); break; default: return(new List <Item>()); } }); return(scContext.Service.CreateTypes(genericType, getItems, scConfig.IsLazy, scConfig.InferType)); }
public override object MapToProperty(AbstractDataMappingContext mappingContext) { var config = Configuration as UmbracoDelegateConfiguration; var context = mappingContext as UmbracoDataMappingContext; if (config == null) { throw new ArgumentException("A delegate property configuration was expected"); } if (context == null) { throw new ArgumentException("A sitecore data mapping context was expected"); } return config.MapToPropertyAction == null ? null : config.MapToPropertyAction(context); }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scContext = mappingContext as SitecoreDataMappingContext; var scConfig = Configuration as SitecoreChildrenConfiguration; Type genericType = Utilities.GetGenericArgument(Configuration.PropertyInfo.PropertyType); Func <IEnumerable <Item> > getItems = () => scContext.Item.Children; return(Utilities.CreateGenericType( typeof(LazyItemEnumerable <>), new[] { genericType }, getItems, scConfig.IsLazy, scConfig.InferType, scContext.Service )); }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scContext = mappingContext as SitecoreDataMappingContext; var scConfig = Configuration as SitecoreChildrenConfiguration; Type genericType = Utilities.GetGenericArgument(Configuration.PropertyInfo.PropertyType); Func<IEnumerable<Item>> getItems = () => scContext.Item.Children; return Utilities.CreateGenericType( typeof (LazyItemEnumerable<>), new[] {genericType}, getItems, scConfig.IsLazy, scConfig.InferType, scContext.Service ); }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> /// <exception cref="System.NotSupportedException">The type {0} on {0}.{1} is not supported by SitecoreIdMapper.Formatted /// (scConfig.PropertyInfo.ReflectedType.FullName, /// scConfig.PropertyInfo.Name)</exception> public override object MapToProperty(AbstractDataMappingContext mappingContext) { SitecoreDataMappingContext context = (SitecoreDataMappingContext)mappingContext; var item = context.Item; var scConfig = Configuration; if (scConfig.PropertyInfo.PropertyType == typeof(Guid)) return item.ID.Guid; else if (scConfig.PropertyInfo.PropertyType == typeof(ID)) return item.ID; else { throw new NotSupportedException("The type {0} on {0}.{1} is not supported by SitecoreIdMapper".Formatted (scConfig.PropertyInfo.ReflectedType.FullName, scConfig.PropertyInfo.Name)); } }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scConfig = Configuration as SitecoreLinkedConfiguration; var scContext = mappingContext as SitecoreDataMappingContext; Type genericType = Mapper.Utilities.GetGenericArgument(scConfig.PropertyInfo.PropertyType); var item = scContext.Item; var linkDb = global::Sitecore.Globals.LinkDatabase; //ME - i am not sure this is correct but there is an odd behaviour of references // languges come back as invariant, going with default language in this scenario var references = new Func<IEnumerable<Item>>(() =>{ var itemLinks = linkDb.GetReferences(item); var items = itemLinks.Select(x => x.GetTargetItem()); return Utilities.GetLanguageItems(items, LanguageManager.DefaultLanguage, scContext.Service.ItemVersionHandler); }); var getItems = new Func<IEnumerable<Item>>(() => { switch (scConfig.Option) { case SitecoreLinkedOptions.All: var itemLinks1 = references(); var itemLinks2 = linkDb.GetReferrers(item); return itemLinks1.Union(itemLinks2.Select(x => x.GetSourceItem())); case SitecoreLinkedOptions.References: return references(); case SitecoreLinkedOptions.Referrers: var itemLinks4 = linkDb.GetReferrers(item); return itemLinks4.Select(x => x.GetSourceItem()); default: return new List<Item>(); } }); return scContext.Service.CreateTypes(genericType, getItems, scConfig.IsLazy, scConfig.InferType); }
public override void MapToCms(AbstractDataMappingContext mappingContext) { var config = Configuration as SitecoreDelegateConfiguration; var context = mappingContext as SitecoreDataMappingContext; if (config == null) { throw new ArgumentException("A delegate property configuration was expected"); } if (context == null) { throw new ArgumentException("A sitecore data mapping context was expected"); } if (config.MapToCmsAction == null) { return; } config.MapToCmsAction(context); }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> /// <exception cref="Glass.Mapper.MapperException">SitecoreInfoType {0} not supported.Formatted(scConfig.Type)</exception> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var context = mappingContext as SitecoreDataMappingContext; if (context == null) { throw new NullReferenceException("Mapping Context has not been set."); } var item = context.Item; var scConfig = Configuration as SitecoreInfoConfiguration; if (scConfig == null) { throw new NullReferenceException("Configuration has not been set."); } //TODO: move this to the config? var urlOptions = Utilities.CreateUrlOptions(scConfig.UrlOptions); switch (scConfig.Type) { case SitecoreInfoType.ContentPath: return item.Paths.ContentPath; case SitecoreInfoType.DisplayName: return item[Global.Fields.DisplayName]; case SitecoreInfoType.FullPath: return item.Paths.FullPath; case SitecoreInfoType.Name: return item.Name; case SitecoreInfoType.Key: return item.Key; case SitecoreInfoType.MediaUrl: var media = new MediaItem(item); return MediaManager.GetMediaUrl(media); case SitecoreInfoType.Path: return item.Paths.Path; case SitecoreInfoType.TemplateId: if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof (ID)) return item.TemplateID; return item.TemplateID.Guid; case SitecoreInfoType.TemplateName: return item.TemplateName; case SitecoreInfoType.Url: urlOptions.Language = null; return LinkManager.GetItemUrl(item, urlOptions); case SitecoreInfoType.Version: if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof (string)) { return item.Version.Number.ToString(); } return item.Version.Number; case SitecoreInfoType.Language: if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof (string)) { return item.Language.Name; } return item.Language; case SitecoreInfoType.BaseTemplateIds: Template template = TemplateManager.GetTemplate(item.TemplateID, item.Database); if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof (IEnumerable<ID>)) return template.GetBaseTemplates().Select(x => x.ID); return template.GetBaseTemplates().Select(x => x.ID.Guid); case SitecoreInfoType.ItemUri: return new ItemUri(item.ID, item.Language, item.Version, item.Database); #if SC81 case SitecoreInfoType.OriginalLanguage: return item.OriginalLanguage; case SitecoreInfoType.OriginatorId: return item.OriginatorId; #endif default: throw new MapperException("SitecoreInfoType {0} not supported".Formatted(scConfig.Type)); } }
public override void MapToCms(AbstractDataMappingContext mappingContext) { }
public override object MapToProperty(AbstractDataMappingContext mappingContext) { return Value; }
public override object MapToProperty(AbstractDataMappingContext mappingContext) { throw new NotImplementedException(); }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scConfig = Configuration as SitecoreQueryConfiguration; var scContext = mappingContext as SitecoreDataMappingContext; string query = ParseQuery(scConfig.Query, scContext.Item); if (scConfig.PropertyInfo.PropertyType.IsGenericType) { Type outerType = Utilities.GetGenericOuter(scConfig.PropertyInfo.PropertyType); if (typeof(IEnumerable<>) == outerType) { Type genericType = Utilities.GetGenericArgument(scConfig.PropertyInfo.PropertyType); Func<IEnumerable<Item>> getItems = null; if (scConfig.IsRelative) { getItems = new Func<IEnumerable<Item>>(() => { try { return Utilities.GetLanguageItems(scContext.Item.Axes.SelectItems(query), scContext.Item.Language); } catch (Exception ex) { throw new MapperException("Failed to perform query {0}".Formatted(query), ex); } }); } else { getItems = new Func<IEnumerable<Item>>(() => { if (scConfig.UseQueryContext) { Query conQuery = new Query(query); QueryContext queryContext = new QueryContext(scContext.Item.Database.DataManager); object obj = conQuery.Execute(queryContext); QueryContext[] contextArray = obj as QueryContext[]; QueryContext context = obj as QueryContext; if (contextArray == null) contextArray = new QueryContext[] { context }; return Utilities.GetLanguageItems(contextArray.Select(x => scContext.Item.Database.GetItem(x.ID)), scContext.Item.Language); } else return Utilities.GetLanguageItems(scContext.Item.Database.SelectItems(query), scContext.Item.Language); }); } var result = Utilities.CreateGenericType(typeof (LazyItemEnumerable<>), new []{genericType}, getItems, scConfig.IsLazy, scConfig.InferType, scContext.Service); return result; //return scContext.Service.CreateTypes(scConfig.IsLazy, scConfig.InferType, genericType, getItems); } else throw new NotSupportedException("Generic type not supported {0}. Must be IEnumerable<>.".Formatted(outerType.FullName)); } else { Item result = null; if (scConfig.IsRelative) { result = Utilities.GetLanguageItem(scContext.Item.Axes.SelectSingleItem(query), scContext.Item.Language); } else { result = Utilities.GetLanguageItem(scContext.Item.Database.SelectSingleItem(query), scContext.Item.Language); } return scContext.Service.CreateType(scConfig.PropertyInfo.PropertyType, result, scConfig.IsLazy, scConfig.InferType); } }
/// <summary> /// Maps data from the .Net property value to the CMS value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>The value to write</returns> /// <exception cref="System.NotImplementedException"></exception> public override void MapToCms(AbstractDataMappingContext mappingContext) { throw new NotImplementedException(); }
public override void MapToCms(AbstractDataMappingContext mappingContext) { MapCalled = true; }
/// <summary> /// Maps data from the CMS value to the .Net property value /// </summary> /// <param name="mappingContext">The mapping context.</param> /// <returns>System.Object.</returns> /// <exception cref="Glass.Mapper.MapperException">SitecoreInfoType {0} not supported.Formatted(scConfig.Type)</exception> public override object MapToProperty(AbstractDataMappingContext mappingContext) { var context = mappingContext as SitecoreDataMappingContext; var item = context.Item; var scConfig = Configuration as SitecoreInfoConfiguration; //TODO: move this to the config? var urlOptions = Utilities.CreateUrlOptions(scConfig.UrlOptions); switch (scConfig.Type) { case SitecoreInfoType.ContentPath: return item.Paths.ContentPath; case SitecoreInfoType.DisplayName: return item.DisplayName; case SitecoreInfoType.FullPath: return item.Paths.FullPath; case SitecoreInfoType.Name: return item.Name; case SitecoreInfoType.Key: return item.Key; case SitecoreInfoType.MediaUrl: var media = new global::Sitecore.Data.Items.MediaItem(item); return global::Sitecore.Resources.Media.MediaManager.GetMediaUrl(media); case SitecoreInfoType.Path: return item.Paths.Path; case SitecoreInfoType.TemplateId: if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof(Sitecore.Data.ID)) return item.TemplateID; return item.TemplateID.Guid; case SitecoreInfoType.TemplateName: return item.TemplateName; case SitecoreInfoType.Url: return LinkManager.GetItemUrl(item, urlOptions); case SitecoreInfoType.Version: return item.Version.Number; case SitecoreInfoType.Language: return item.Language; default: throw new MapperException("SitecoreInfoType {0} not supported".Formatted(scConfig.Type)); } }
public override object MapToProperty(AbstractDataMappingContext mappingContext) { this.MappingContext = mappingContext; return "Hello world"; }