Inheritance: ContentBlockBase
Beispiel #1
0
        internal static SxcInstance GetSxcOfApiRequest(HttpRequestMessage request)
        {
            string cbidHeader = "ContentBlockId";
            var moduleInfo = request.FindModuleInfo();

            // get url parameters and provide override values to ensure all configuration is
            // preserved in AJAX calls
            List<KeyValuePair<string, string>> urlParams = null;
            var requestParams = request.GetQueryNameValuePairs();
            var origParams = requestParams.Where(p => p.Key == "originalparameters").ToList();
            if (origParams.Any())
            {
                var paramSet = origParams.First().Value;

                // Workaround for deserializing KeyValuePair -it requires lowercase properties(case sensitive), which seems to be a bug in some Newtonsoft.Json versions: http://stackoverflow.com/questions/11266695/json-net-case-insensitive-property-deserialization
                var items = Json.Deserialize<List<UpperCaseStringKeyValuePair>>(paramSet);
                urlParams = items.Select(a => new KeyValuePair<string, string>(a.Key, a.Value)).ToList();

                //urlParams = requestParams
                //    .Where(keyValuePair => keyValuePair.Key.IndexOf("orig", StringComparison.Ordinal) == 0)
                //    .Select(pair => new KeyValuePair<string, string>(pair.Key.Substring(4), pair.Value))
                //    .ToList();
            }
            // first, check the special overrides
            //var origparams = requestParams.Select(np => np.Key == "urlparameters").ToList();
            //if (origparams.Any())
            //{
            //    var paramSet = origparams.First();
            //}

            // then add remaining params

            IContentBlock contentBlock = new ModuleContentBlock(moduleInfo, urlParams);

            // check if we need an inner block
            if (request.Headers.Contains(cbidHeader)) {
                var cbidh = request.Headers.GetValues(cbidHeader).FirstOrDefault();
                int cbid;
                Int32.TryParse(cbidh, out cbid);
                if (cbid < 0)   // negative id, so it's an inner block
                    contentBlock = new EntityContentBlock(contentBlock, cbid);
            }

            return contentBlock.SxcInstance;
        }
Beispiel #2
0
 public static ISxcInstance SxcInstanceForModule(ModuleInfo moduleInfo)
 {
     ModuleContentBlock mcb = new ModuleContentBlock(moduleInfo);
     return mcb.SxcInstance;
 }
Beispiel #3
0
        /// <summary>
        /// Get search info for each dnn module containing 2sxc data
        /// </summary>
        /// <param name="moduleInfo"></param>
        /// <param name="beginDate"></param>
        /// <returns></returns>
        public IList<SearchDocument> GetModifiedSearchDocuments(ModuleInfo moduleInfo, DateTime beginDate)
        {
            var searchDocuments = new List<SearchDocument>();

            var isContentModule = moduleInfo.DesktopModule.ModuleName == "2sxc";

            // New Context because PortalSettings.Current is null
            var zoneId = ZoneHelpers.GetZoneID(moduleInfo.OwnerPortalID);

            if (!zoneId.HasValue)
                return searchDocuments;

            int? appId = AppHelpers.GetDefaultAppId(zoneId.Value);

            if (!isContentModule)
            {
                appId = AppHelpers.GetAppIdFromModule(moduleInfo, zoneId.Value);
                if (!appId.HasValue)
                    return searchDocuments;
            }

            // new 2016-03-27
            var mcb = new ModuleContentBlock(moduleInfo);
            var sexy = mcb.SxcInstance;

            // old 2016-03-27
            var language = moduleInfo.CultureCode;
            var contentGroup = sexy.App.ContentGroupManager./*AppContentGroups.*/GetContentGroupForModule(moduleInfo.ModuleID);
            var template = contentGroup.Template;

            // This list will hold all EAV entities to be indexed
            // before 2016-02-27 2dm: var dataSource = sexy.GetViewDataSource(moduleInfo.ModuleID, false, template);
            var dataSource = sexy.Data;// ViewDataSource.ForModule(moduleInfo.ModuleID, false, template, sexy);

            if (template == null)
                return searchDocuments;

            var engine = EngineFactory.CreateEngine(template);
            engine.Init(template, sexy.App, moduleInfo, dataSource, InstancePurposes.IndexingForSearch, sexy);

            // see if data customization inside the cshtml works
            try
            {
                engine.CustomizeData();
            }
            catch (Exception e) // Catch errors here, because of references to Request etc.
            {
                Exceptions.LogException(new SearchIndexException(moduleInfo, e));
            }

            var searchInfoDictionary = new Dictionary<string, List<ISearchInfo>>();

            // Get DNN SearchDocuments from 2Sexy SearchInfos
            foreach (var stream in dataSource.Out.Where(p => p.Key != Constants.PresentationKey && p.Key != "ListPresentation" ))
            {

                var entities = stream.Value.List.Select(p => p.Value);
                var searchInfoList = searchInfoDictionary[stream.Key] = new List<ISearchInfo>();

                searchInfoList.AddRange(entities.Select(entity =>
                {
                    var searchInfo = new SearchInfo
                    {
                        Entity = entity,
                        Url = "",
                        Description = "",
                        Body = GetJoinedAttributes(entity, language),
                        Title = entity.Title != null && entity.Title[language] != null ? entity.Title[language].ToString() : "(no title)",
                        ModifiedTimeUtc = (entity.Modified == DateTime.MinValue ? DateTime.Now.Date.AddHours(DateTime.Now.Hour) : entity.Modified).ToUniversalTime(),
                        UniqueKey = "2sxc-" + moduleInfo.ModuleID + "-" + (entity.EntityGuid != new Guid() ? entity.EntityGuid.ToString() : (stream.Key + "-" + entity.EntityId)),
                        IsActive = true,
                        TabId = moduleInfo.TabID,
                        PortalId = moduleInfo.PortalID
                    };

                    // Take the newest value (from ContentGroupItem and Entity)
                    if (entity is IHasEditingData)
                    {
                        var contentGroupItemModifiedUtc = ((IHasEditingData) entity).ContentGroupItemModified.ToUniversalTime();
                        searchInfo.ModifiedTimeUtc = searchInfo.ModifiedTimeUtc > contentGroupItemModifiedUtc
                            ? searchInfo.ModifiedTimeUtc
                            : contentGroupItemModifiedUtc;
                    }

                    return searchInfo;
                }));
            }

            // check if the cshtml has search customizations
            try
            {
                engine.CustomizeSearch(searchInfoDictionary, moduleInfo, beginDate);
            }
            catch (Exception e)
            {
                Exceptions.LogException(new SearchIndexException(moduleInfo, e));
            }

            // reduce load by only keeping recently modified ites
            foreach (var searchInfoList in searchInfoDictionary)
            {
                // Filter by Date - take only SearchDocuments that changed since beginDate
                var searchDocumentsToAdd = searchInfoList.Value.Where(p => p.ModifiedTimeUtc >= beginDate.ToUniversalTime()).Select(p => (SearchDocument) p);

                searchDocuments.AddRange(searchDocumentsToAdd);
            }

            return searchDocuments;
        }