/// <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) { var item = context.Item; if (fieldValue.IsNullOrEmpty()) { return(null); } Guid id = Guid.Empty; Item target; if (Guid.TryParse(fieldValue, out id)) { target = item.Database.GetItem(new ID(id), item.Language); } else { target = item.Database.GetItem(fieldValue, item.Language); } if (target == null) { return(null); } var getOptions = new GetItemByItemOptions(); getOptions.Copy(context.Options); getOptions.Item = target; config.GetPropertyOptions(getOptions); return(context.Service.GetItem(getOptions)); }
/// <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)); }
public void Excute_TemplateWithEnAndDeStandardValues_ItemOnlyInEn_DoesntReturnDe() { //Arrange using (Db database = new Db { new DbTemplate(new ID(TemplateInferredTypeTaskFixture.StubInferred.TemplateId)) { new Sitecore.FakeDb.DbField("Title") { { "en", "Hello!" }, { "de-de", "Hej!" } } }, new Sitecore.FakeDb.DbItem("Target", ID.NewID, new ID(TemplateInferredTypeTaskFixture.StubInferred.TemplateId)) }) { var path = "/sitecore/content/Target"; var itemEn = database.GetItem(path, "en"); var itemDe = database.GetItem(path, "de-de"); // itemEn.Template.CreateStandardValues(); var nextTaskEn = new NextTask(); var taskEn = new ItemVersionCountByRevisionTask(); taskEn.SetNext(x => nextTaskEn.Execute(x)); var contextEn = new SitecoreTypeCreationContext(); contextEn.Item = itemEn; var optionsEn = new GetItemByItemOptions(itemEn); contextEn.Options = optionsEn; var argsEn = new ObjectConstructionArgs(null, contextEn, null, null); //Act EN taskEn.Execute(argsEn); //Assert EN Assert.IsTrue(nextTaskEn.WasCalled); var nextTaskDe = new NextTask(); var taskDe = new ItemVersionCountByRevisionTask(); taskDe.SetNext(x => nextTaskDe.Execute(x)); var contextDe = new SitecoreTypeCreationContext(); var optionsDe = new GetItemByItemOptions(itemDe); contextDe.Item = itemDe; contextDe.Options = optionsDe; var argsDe = new ObjectConstructionArgs(null, contextDe, null, null); //Act De taskDe.Execute(argsDe); //Assert De Assert.IsFalse(nextTaskDe.WasCalled); } }
/// <summary> /// Raises the <see cref="E:System.Web.UI.Control.Load" /> event. /// </summary> /// <param name="e">The <see cref="T:System.EventArgs" /> object that contains the event data.</param> protected override void OnLoad(EventArgs e) { var options = new GetItemByItemOptions() { Item = LayoutItem }; Model = WebContext.SitecoreService.GetItem <T>(options); base.OnLoad(e); }
/// <summary> /// Gets the model. /// </summary> protected virtual void GetModel() { var options = new GetItemByItemOptions() { Item = LayoutItem, InferType = InferType, Lazy = Lazy }; Model = WebContext.SitecoreService.GetItem <T>(options); }
public override object MapToProperty(AbstractDataMappingContext mappingContext) { var scContext = mappingContext as SitecoreDataMappingContext; var scConfig = Configuration as SitecoreSelfConfiguration; if (scContext != null && scConfig != null) { var options = new GetItemByItemOptions(); options.Copy(mappingContext.Options); options.Item = scContext.Item; scConfig.GetPropertyOptions(options); return(scContext.Service.GetItem(options)); } return(null); }
protected virtual object GetDataSourceItem(GetModelArgs args, Type modelType) { IMvcContext mvcContext = new MvcContext(new SitecoreService(Sitecore.Context.Database)); Rendering renderingItem = args.Rendering; if (renderingItem.DataSource.HasValue()) { var getOptions = new GetItemByPathOptions(); getOptions.Type = modelType; getOptions.Path = renderingItem.DataSource; return(mvcContext.SitecoreService.GetItem(getOptions)); } if (renderingItem.RenderingItem.DataSource.HasValue()) { var getOptions = new GetItemByPathOptions(); getOptions.Type = modelType; getOptions.Path = renderingItem.RenderingItem.DataSource; return(mvcContext.SitecoreService.GetItem(getOptions)); } if (renderingItem.Item != null) { /** * Issues #82: * Check Item before defaulting to the current item. */ var getOptions = new GetItemByItemOptions(); getOptions.Type = modelType; getOptions.Item = renderingItem.Item; return(mvcContext.SitecoreService.GetItem(getOptions)); } else { var getOptions = new GetItemByItemOptions(); getOptions.Type = modelType; getOptions.Item = mvcContext.ContextItem; return(mvcContext.SitecoreService.GetItem(getOptions)); } }
public override void Process(GetModelArgs args) { var key = "GetModelFromView"; var watch = Stopwatch.StartNew(); try { if (!IsValidForProcessing(args)) { return; } string path = GetViewPath(args); if (string.IsNullOrWhiteSpace(path)) { return; } string cacheKey = modelCacheManager.GetKey(path); Type modelType = modelCacheManager.Get(cacheKey); if (modelType == typeof(NullModel)) { // The model has been attempted before and is not useful return; } // The model type hasn't been found before or has been cleared. if (modelType == null) { modelType = GetModel(args, path); modelCacheManager.Add(cacheKey, modelType); if (modelType == typeof(NullModel) || typeof(RenderingModel).IsAssignableFrom(modelType)) { // This is not the type we are looking for return; } } IMvcContext mvcContext = new MvcContext(new SitecoreService(Sitecore.Context.Database)); Rendering renderingItem = args.Rendering; object model = null; if (renderingItem.DataSource.HasValue()) { var getOptions = new GetItemByPathOptions(); getOptions.Type = modelType; getOptions.Path = renderingItem.DataSource; model = mvcContext.SitecoreService.GetItem(getOptions); } else if (renderingItem.RenderingItem.DataSource.HasValue()) { var getOptions = new GetItemByPathOptions(); getOptions.Type = modelType; getOptions.Path = renderingItem.RenderingItem.DataSource; model = mvcContext.SitecoreService.GetItem(getOptions); } else if (renderingItem.Item != null) { /** * Issues #82: * Check Item before defaulting to the current item. */ var getOptions = new GetItemByItemOptions(); getOptions.Type = modelType; getOptions.Item = renderingItem.Item; model = mvcContext.SitecoreService.GetItem(getOptions); } else { var getOptions = new GetItemByItemOptions(); getOptions.Type = modelType; getOptions.Item = mvcContext.ContextItem; model = mvcContext.SitecoreService.GetItem(getOptions); } args.Result = model; args.AbortPipeline(); } finally { Sitecore.Diagnostics.Log.Debug("GetModelFromView {0} {1}".Formatted(watch.ElapsedMilliseconds, args.Rendering.RenderingItem.ID), this); } }
/// <summary> /// Gets the object. /// </summary> /// <param name="model">The model.</param> /// <param name="db">The db.</param> /// <returns></returns> /// <exception cref="Glass.Mapper.MapperException">Failed to find context {0}.Formatted(ContextName)</exception> public virtual object GetObject(string model, Database db, Rendering renderingItem) { if (model.IsNullOrEmpty()) { return(null); } //must be a path to a Model item if (model.StartsWith("/sitecore")) { var target = db.GetItem(model); if (target == null) { return(null); } string newModel = target[ModelTypeField]; return(GetObject(newModel, db, renderingItem)); } //if guid must be that to Model item Guid targetId; if (Guid.TryParse(model, out targetId)) { var target = db.GetItem(new ID(targetId)); if (target == null) { return(null); } string newModel = target[ModelTypeField]; return(GetObject(newModel, db, renderingItem)); } var type = Type.GetType(model, false); if (type == null || renderingModelType.IsAssignableFrom(type)) { return(null); } IMvcContext mvcContext = new MvcContext(new SitecoreService(Sitecore.Context.Database)); //this is really aggressive if (!mvcContext.SitecoreService.GlassContext.TypeConfigurations.ContainsKey(type)) { //if the config is null then it is probably an ondemand mapping so we have to load the ondemand part IConfigurationLoader loader = new OnDemandLoader <SitecoreTypeConfiguration>(type); mvcContext.SitecoreService.GlassContext.Load(loader); } if (renderingItem.DataSource.HasValue()) { var getOptions = new GetItemByPathOptions() { Path = renderingItem.DataSource, Type = type }; return(mvcContext.SitecoreService.GetItem(getOptions)); } if (renderingItem.RenderingItem.DataSource.HasValue()) { var getOptions = new GetItemByPathOptions() { Path = renderingItem.RenderingItem.DataSource, Type = type }; return(mvcContext.SitecoreService.GetItem(getOptions)); } /** * Issues #82: * Check Item before defaulting to the current item. */ if (renderingItem.Item != null) { var getOptions = new GetItemByItemOptions() { Item = renderingItem.Item, Type = type }; return(mvcContext.SitecoreService.GetItem(getOptions)); } else { //TODO? shoudl we use the GetCurrentitem var getOptions = new GetItemByItemOptions() { Item = mvcContext.ContextItem, Type = type }; return(mvcContext.SitecoreService.GetItem(getOptions)); } }
protected AbstractGetItemByItemBuilder(GetItemByItemOptions options) : base(options) { _options = options; }