/// <summary> /// Gets the field. /// </summary> /// <param name="field">The field.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.Object.</returns> public override object GetField(Sitecore.Data.Fields.Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { var data = field.GetBlobStream(); MemoryStream stream = null; if (data.CanRead) { stream = new MemoryStream(); byte[] buffer = new byte[2048]; int bytesRead; while ((bytesRead = data.Read(buffer, 0, buffer.Length)) > 0) { stream.Write(buffer, 0, bytesRead); } data.Close(); stream.Seek(0, SeekOrigin.Begin); } return stream; }
/// <summary> /// Sets the field. /// </summary> /// <param name="field">The field.</param> /// <param name="value">The value.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <exception cref="Glass.Mapper.MapperException">No item with ID {0}. Can not update File Item field.Formatted(newId)</exception> public override void SetField(Field field, object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { File file = value as File; var item = field.Item; FileField fileField = new FileField(field); if (file == null) { fileField.Clear(); return; } if (fileField.MediaID.Guid != file.Id) { if (file.Id == Guid.Empty) { ItemLink link = new ItemLink(item.Database.Name, item.ID, fileField.InnerField.ID, fileField.MediaItem.Database.Name, fileField.MediaID, fileField.MediaItem.Paths.FullPath); fileField.RemoveLink(link); } else { ID newId = new ID(file.Id); Item target = item.Database.GetItem(newId); if (target != null) { fileField.MediaID = newId; ItemLink link = new ItemLink(item.Database.Name, item.ID, fileField.InnerField.ID, target.Database.Name, target.ID, target.Paths.FullPath); fileField.UpdateLink(link); } else throw new MapperException("No item with ID {0}. Can not update File Item field".Formatted(newId)); } } }
/// <summary> /// Gets the field. /// </summary> /// <param name="field">The field.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.Object.</returns> public override object GetField(Sitecore.Data.Fields.Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { if (field == null) return string.Empty; if (config.Setting == SitecoreFieldSettings.RichTextRaw) { return field.Value; } Guid fieldGuid = field.ID.Guid; // shortest route - we know whether or not its rich text if (isRichTextDictionary.ContainsKey(fieldGuid)) { return GetResult(field, isRichTextDictionary[fieldGuid]); } // we don't know - it might still be rich text bool isRichText = field.TypeKey == _richTextKey; isRichTextDictionary.TryAdd(fieldGuid, isRichText); // now we know it isn't rich text - return the raw result. return GetResult(field, isRichText); }
/// <summary> /// Gets the field. /// </summary> /// <param name="field">The field.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.Object.</returns> public override object GetField(Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { Image img = new Image(); ImageField scImg = new ImageField(field); int height = 0; int.TryParse(scImg.Height, out height); int width = 0; int.TryParse(scImg.Width, out width); int hSpace = 0; int.TryParse(scImg.HSpace, out hSpace); int vSpace = 0; int.TryParse(scImg.VSpace, out vSpace); img.Alt = scImg.Alt; img.Border = scImg.Border; img.Class = scImg.Class; img.Height = height; img.HSpace = hSpace; img.MediaId = scImg.MediaID.Guid; if (scImg.MediaItem != null) img.Src = MediaManager.GetMediaUrl(scImg.MediaItem); img.VSpace = vSpace; img.Width = width; return img; }
public void GetField_ForceRenderFieldPipeline_StringIsReturned() { //Assign var fieldValue = "<p>hello world</p>"; var expected = "<p>hello world</p>"; var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldStringMapper/GetField"); var field = item.Fields[FieldName]; var mapper = new SitecoreFieldStringMapper(); var config = new SitecoreFieldConfiguration(); config.Setting = SitecoreFieldSettings.ForceRenderField; using (new ItemEditing(item, true)) { field.Value = fieldValue; } Sitecore.Context.Site = Sitecore.Configuration.Factory.GetSite("website"); Sitecore.Context.Site.SetDisplayMode(DisplayMode.Preview, DisplayModeDuration.Remember); //Act var result = mapper.GetField(field, config, null) as string; //Assert Sitecore.Context.Site = null; Assert.AreEqual(expected, result); }
/// <summary> /// Sets the field value. /// </summary> /// <param name="value">The value.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.String.</returns> /// <exception cref="System.NullReferenceException">Could not find item to save value {0}.Formatted(Configuration)</exception> public override string SetFieldValue(object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { if (context == null) throw new ArgumentNullException("context", "The context was incorrectly set"); if(context.Service == null) throw new NullReferenceException("The context's service property was null"); if (context.Service.GlassContext == null) throw new NullReferenceException("The service glass context is null"); if (context.Service.Database == null) throw new NullReferenceException("The database is not set for the service"); if (value == null) return string.Empty; var type = value.GetType(); var typeConfig = context.Service.GlassContext.GetTypeConfiguration<SitecoreTypeConfiguration>(value); if(typeConfig == null) throw new NullReferenceException("The type {0} has not been loaded into context {1}".Formatted(type.FullName, context.Service.GlassContext.Name)); var item = typeConfig.ResolveItem(value, context.Service.Database); if(item == null) throw new NullReferenceException("Could not find item to save value {0}".Formatted(Configuration)); return item.ID.ToString(); }
public void GetField_RichText_StringIsReturnedWithScapedUrl() { //Assign var fieldValue = "<p>Test with <a href=\"~/link.aspx?_id=BFD7975DF42F41E19DDA9A38E971555F&_z=z\">link</a></p>"; var expected = "<p>Test with <a href=\"/en/Tests/DataMappers/SitecoreFieldStringMapper/GetField.aspx\">link</a></p>"; var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldStringMapper/GetFieldRichText"); var field = item.Fields[FieldName]; var mapper = new SitecoreFieldStringMapper(); var config = new SitecoreFieldConfiguration(); Sitecore.Context.Site = Sitecore.Configuration.Factory.GetSite("website"); Sitecore.Context.Site.SetDisplayMode(DisplayMode.Preview, DisplayModeDuration.Remember); using (new ItemEditing(item, true)) { field.Value = fieldValue; } //Act var result = mapper.GetField(field, config, null) as string; Sitecore.Context.Site = null; //Assert Assert.AreEqual(expected, result); }
public void GetField_FieldContainsId_ReturnsConcreteType() { //Assign var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldTypeMapper/GetField"); var targetId = Guid.Parse("{BB01B0A5-A3F0-410E-8A6D-07FF3A1E78C3}"); var mapper = new SitecoreFieldTypeMapper(); var field = item.Fields[FieldName]; var config = new SitecoreFieldConfiguration(); config.PropertyInfo = typeof (StubContaining).GetProperty("PropertyTrue"); var context = Context.Create(Utilities.CreateStandardResolver()); context.Load(new SitecoreAttributeConfigurationLoader("Glass.Mapper.Sc.Integration")); var service = new SitecoreService(Database, context); var scContext = new SitecoreDataMappingContext(null, item, service); using (new ItemEditing(item, true)) { field.Value = targetId.ToString(); } //Act var result = mapper.GetField(field, config, scContext) as Stub; //Assert Assert.AreEqual(targetId, result.Id); }
/// <summary> /// Gets the field value. /// </summary> /// <param name="fieldValue">The field value.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.Object.</returns> public override object GetFieldValue(string fieldValue, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { Type type = config.PropertyInfo.PropertyType; //Get generic type Type pType = Glass.Mapper.Utilities.GetGenericArgument(type); //The enumerator only works with piped lists IEnumerable<string> parts = fieldValue.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); //replace any pipe encoding with an actual pipe parts = parts.Select(x => x.Replace(Global.PipeEncoding, "|")).ToArray(); IEnumerable<object> items = parts.Select(x => Mapper.GetFieldValue(x, Mapper.Configuration as SitecoreFieldConfiguration, context)).ToArray(); var list = Utilities.CreateGenericType(typeof (List<>), new Type[] {pType}) as IList; foreach (var item in items) { if(item != null) list.Add(item); } return list; }
/// <summary> /// Gets the field value. /// </summary> /// <param name="fieldValue">The field value.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.Object.</returns> public override object GetFieldValue(string fieldValue, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { if (fieldValue.IsNullOrEmpty()) return Guid.Empty; return Guid.Parse(fieldValue); }
/// <summary> /// Sets the field value. /// </summary> /// <param name="value">The value.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.String.</returns> /// <exception cref="Glass.Mapper.MapperException">The value is not of type System.Guid</exception> public override string SetFieldValue(object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { if (value is Guid) { return ((Guid)value).ToString("B").ToUpper(); } else throw new MapperException("The value is not of type System.Guid"); }
/// <summary> /// Sets the field value. /// </summary> /// <param name="value">The value.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.String.</returns> /// <exception cref="Glass.Mapper.MapperException">The value is not of type System.Guid</exception> public override string SetFieldValue(object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { if (value is ID) { return ((ID)value).ToString(); } else throw new MapperException("The value is not of type Sitecore.Data.ID"); }
/// <summary> /// Gets the field value. /// </summary> /// <param name="fieldValue">The field value.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.Object.</returns> /// <exception cref="Glass.Mapper.MapperException">Could not convert value to double</exception> public override object GetFieldValue(string fieldValue, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { if (fieldValue.IsNullOrEmpty()) return 0; int dValue = 0; if (int.TryParse(fieldValue, NumberStyles.Any, CultureInfo.InvariantCulture, out dValue)) return dValue; else throw new MapperException("Could not convert value to Integer"); }
public override object GetFieldValue(string fieldValue, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { var settings = context.Service.GetItem<TwitterSettings>(TweetCommand.SettingsPath); TwitterService twitterService = new TwitterService(settings.ConsumerKey, settings.ConsumerSecret); twitterService.AuthenticateWith(settings.AccessToken, settings.AccessTokenSecret); return twitterService.ListTweetsOnUserTimeline(new ListTweetsOnUserTimelineOptions() { ScreenName = fieldValue }); }
/// <summary> /// Sets the field. /// </summary> /// <param name="field">The field.</param> /// <param name="value">The value.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> public override void SetField(Sitecore.Data.Fields.Field field, object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { if (value == null) { return; } field.SetBlobStream(value as Stream); }
/// <summary> /// Gets the field. /// </summary> /// <param name="field">The field.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.Object.</returns> public override object GetField(Sitecore.Data.Fields.Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { if (string.IsNullOrWhiteSpace(field.Value)) { return null; } return base.GetField(field, config, context); }
/// <summary> /// Gets the field. /// </summary> /// <param name="field">The field.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.Object.</returns> public override object GetField(Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { Image img = new Image(); ImageField scImg = new ImageField(field); MapToImage(img, scImg); return img; }
/// <summary> /// Sets the field value. /// </summary> /// <param name="value">The value.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.String.</returns> /// <exception cref="System.NotSupportedException">The value is not of type System.Decimal</exception> public override string SetFieldValue(object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { if (value is decimal) { return value.ToString(); } else throw new NotSupportedException("The value is not of type System.Decimal"); }
/// <summary> /// Sets the field value. /// </summary> /// <param name="value">The value.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.String.</returns> public override string SetFieldValue( object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { if (value is bool) { bool actual = (bool)value; return actual ? "1" : "0"; } else throw new NotSupportedException("The value is not of type System.Boolean"); }
/// <summary> /// Sets the field value. /// </summary> /// <param name="value">The value.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.String.</returns> /// <exception cref="System.NotSupportedException">The value is not of type System.Double</exception> public override string SetFieldValue(object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { double? doubleValue = value as double?; if (doubleValue.HasValue) { return doubleValue.Value.ToString(CultureInfo.InvariantCulture); } throw new NotSupportedException("The value is not of type System.Double"); }
/// <summary> /// Sets the field. /// </summary> /// <param name="field">The field.</param> /// <param name="value">The value.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> public override void SetField(Sitecore.Data.Fields.Field field, object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { if (value == null) { field.Value = null; return; } base.SetField(field, value, config, context); }
/// <summary> /// Gets the field. /// </summary> /// <param name="field">The field.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.Object.</returns> public override object GetField(Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { FileField fileField = new FileField(field); File file = new File(); if (fileField.MediaItem != null) file.Src = MediaManager.GetMediaUrl(fileField.MediaItem); file.Id = fileField.MediaID.Guid; return file; }
/// <summary> /// Sets the field value. /// </summary> /// <param name="value">The value.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.String.</returns> public override string SetFieldValue(object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { NameValueCollection collection = value as NameValueCollection; if (collection != null) { return Utilities.ConstructQueryString(collection); } else return string.Empty; }
public void CanHandle_ConfigIsNotNodeAndClassMapped_ReturnsFalse() { //Assign var config = new SitecoreFieldConfiguration(); var mapper = new SitecoreNodeMapper(); //Act var result = mapper.CanHandle(config, null); //Assert Assert.IsFalse(result); }
/// <summary> /// Gets the field. /// </summary> /// <param name="field">The field.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.Object.</returns> public override object GetField(Sitecore.Data.Fields.Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { Type ruleFactory = typeof(RuleFactory); var method = ruleFactory.GetMethod("GetRules", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static, null, new Type[] { typeof(global::Sitecore.Data.Fields.Field) }, null); Type[] genericArgs = Configuration.PropertyInfo.PropertyType.GetGenericArguments(); method = method.MakeGenericMethod(genericArgs); object rules = method.Invoke(null, new object[] { field }); return rules; }
public void CanHandle_PropertyIsArray_ReturnsFalse() { //Assign var config = new SitecoreFieldConfiguration(); config.PropertyInfo = typeof(StubClass).GetProperty("Array"); var mapper = new SitecoreFieldIEnumerableMapper(); //Act var result = mapper.CanHandle(config, null); //Assert Assert.IsFalse(result); }
/// <summary> /// Sets the field. /// </summary> /// <param name="field">The field.</param> /// <param name="value">The value.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <exception cref="System.NotSupportedException">It is not possible to save data from a rich text field when the data isn't raw. /// + Set the SitecoreFieldAttribute setting property to SitecoreFieldSettings.RichTextRaw for property {0} on type {1}.Formatted(config.PropertyInfo.Name, config.PropertyInfo.ReflectedType.FullName)</exception> public override void SetField(Sitecore.Data.Fields.Field field, object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { if (field == null) { return; } if (field.Type.StartsWith("Rich Text") && config.Setting != SitecoreFieldSettings.RichTextRaw) { throw new NotSupportedException("It is not possible to save data from a rich text field when the data isn't raw." + "Set the SitecoreFieldAttribute setting property to SitecoreFieldSettings.RichTextRaw for property {0} on type {1}".Formatted(config.PropertyInfo.Name, config.PropertyInfo.ReflectedType.FullName)); } field.Value = value != null ? value.ToString() : null; }
/// <summary> /// Gets the field. /// </summary> /// <param name="field">The field.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.Object.</returns> public override object GetField(Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { if (field.Value.IsNullOrEmpty()) { return null; } Image img = new Image(); ImageField scImg = new ImageField(field); MapToImage(img, scImg); return img; }
public void CanHandle_TypeIsHandledWithConfig_ReturnsTrue() { //Assign var config = new SitecoreFieldConfiguration(); var type1 = typeof (string); var mapper = new StubMapper(type1); config.PropertyInfo = typeof (Stub).GetProperty("Property"); //Act var result = mapper.CanHandle(config, null); //Assert Assert.IsTrue(result); }
public void CanHandle_TypeHasNotBeenLoadedByGlass_ReturnsTrueOnDemand() { //Assign var mapper = new SitecoreFieldTypeMapper(); var config = new SitecoreFieldConfiguration(); config.PropertyInfo = typeof(StubContaining).GetProperty("PropertyFalse"); var context = Context.Create(Utilities.CreateStandardResolver()); context.Load(new SitecoreAttributeConfigurationLoader("Glass.Mapper.Sc.Integration")); //Act var result = mapper.CanHandle(config, context); //Assert Assert.IsTrue(result); }
/// <summary> /// Called to map each property automatically /// </summary> /// <param name="property"></param> /// <returns></returns> protected override AbstractPropertyConfiguration AutoMapProperty(System.Reflection.PropertyInfo property) { string name = property.Name; SitecoreInfoType infoType; if (name.ToLowerInvariant() == "id") { SitecoreIdConfiguration idConfig = new SitecoreIdConfiguration(); idConfig.PropertyInfo = property; return(idConfig); } if (name.ToLowerInvariant() == "parent") { SitecoreParentConfiguration parentConfig = new SitecoreParentConfiguration(); parentConfig.PropertyInfo = property; return(parentConfig); } if (name.ToLowerInvariant() == "children") { SitecoreChildrenConfiguration childrenConfig = new SitecoreChildrenConfiguration(); childrenConfig.PropertyInfo = property; return(childrenConfig); } if (Enum.TryParse(name, true, out infoType)) { SitecoreInfoConfiguration infoConfig = new SitecoreInfoConfiguration(); infoConfig.PropertyInfo = property; infoConfig.Type = infoType; return(infoConfig); } SitecoreFieldConfiguration fieldConfig = new SitecoreFieldConfiguration(); fieldConfig.FieldName = name; fieldConfig.PropertyInfo = property; return(fieldConfig); }