示例#1
0
        public SettingsRepository(IRequestContext requestContext)
        {
            var context = requestContext.SitecoreService;

            try {
                var options = new GetItemByPathOptions {
                    Path = Settings.GetSetting("SitecoreUrlShorter.Feature.Core.Settings")
                };

                _settings = context.GetItem <IUrlShorteningServiceSettings>(options);
            } catch {
                // ignored
            }
        }
        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);
            }
        }
示例#4
0
        /// <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));
            }
        }
示例#5
0
 protected AbstractGetItemByPathBuilder(GetItemByPathOptions options)
     : base(options)
 {
     _options = options;
 }
        public ActionResult Deals()
        {
            // product path
            string dealPath = "/sitecore/content/home/deals/london";
            // product Id
            string dealID = "{0CD5C7B6-D994-49BF-BB62-AC303D99A5D3}";
            // product name
            string dealName = "sydney";
            // query by product name
            string dealQuery = $"/sitecore/content/home/deals//*[contains(@@name,'{dealName.ToLower()}')]";
            // query type 1 to get multiple products
            string dealsPathType1 = "/sitecore/content/home/deals/*";
            // query type 2 to get multiple products
            string dealsPathType2 = "/sitecore/content/home/deals//*";

            BaseDeal target = _sitecoreService.GetItem <BaseDeal>(dealPath, x => x.LazyDisabled());

            #region Single Item

            #region  Get Item by Id
            //Example 1
            var options = new GetItemByIdOptions(new Guid(dealID))
            {
                InferType = true,
                Lazy      = Glass.Mapper.LazyLoading.Disabled
            };
            var target2 = _sitecoreService.GetItem <BaseDeal>(options);
            #endregion

            #region  Get Item by Path
            // Example 2
            var options1 = new GetItemByPathOptions(dealPath)
            {
                InferType = true,
                Lazy      = Glass.Mapper.LazyLoading.Disabled
            };
            var target3 = _sitecoreService.GetItem <BaseDeal>(options1);
            #endregion

            #region  Get Item by Query
            // Example 3
            var options4 = new GetItemByQueryOptions(new Query(dealQuery))
            {
                InferType = true,
                Lazy      = Glass.Mapper.LazyLoading.Disabled
            };
            var target4 = _sitecoreService.GetItem <BaseDeal>(options4);
            #endregion

            #endregion

            #region Multiple Items

            #region  Get Item by Query
            //Example 1
            var options10 = new GetItemsByQueryOptions(new Query(dealsPathType1))
            {
                InferType = true,
                Lazy      = Glass.Mapper.LazyLoading.Disabled
            };
            var target10 = _sitecoreService.GetItems <BaseDeal>(options10);

            //Example 2
            var options11 = new GetItemsByQueryOptions(new Query(dealsPathType2))
            {
                InferType = true,
                Lazy      = Glass.Mapper.LazyLoading.Disabled
            };
            var target11 = _sitecoreService.GetItems <BaseDeal>(options11);

            #endregion

            #endregion

            return(View("~/Areas/basic/Views/Home/Deals.cshtml", target11));
        }