⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 searchdatastore.cs

📁 SharpNuke源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// GetSetting gets a Search Setting from the Portal Modules Settings table (or
		/// from the Host Settings)
		/// </summary>
		/// <returns></returns>
		/// <remarks>
		/// </remarks>
		/// <history>
		/// 	[cnurse]	11/16/2004	created
		/// </history>
		/// -----------------------------------------------------------------------------
		private string GetSetting(string name)
		{
			string settingValue = "";
			
			//Try Portal setting first
			if (settings[name] != null)
			{
				settingValue = System.Convert.ToString(settings[name]);
			}
			else
			{
				//Get Default setting
				if (defaultSettings[name] != null)
				{
					settingValue = System.Convert.ToString(defaultSettings[name]);
				}
			}
			return settingValue;
		}
		
		#endregion
		
		#region "Public Methods"
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// GetSearchItems gets a collection of Search Items for a Module/Tab/Portal
		/// </summary>
		/// <remarks>
		/// </remarks>
		/// <param name="PortalID">A Id of the Portal</param>
		/// <param name="TabID">A Id of the Tab</param>
		/// <param name="ModuleID">A Id of the Module</param>
		/// <history>
		///		[cnurse]	11/15/2004	documented
		/// </history>
		/// -----------------------------------------------------------------------------
		public override SearchResultsInfoCollection GetSearchItems(int portalID, int tabID, int moduleID)
		{
			return new SearchResultsInfoCollection(CBO.FillCollection(DataProvider.Instance().GetSearchItems(portalID, tabID, moduleID), typeof(SearchResultsInfo)));
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// GetSearchResults gets the search results for a passed in criteria string
		/// </summary>
		/// <remarks>
		/// </remarks>
		/// <param name="PortalID">A Id of the Portal</param>
		/// <param name="Criteria">The criteria string</param>
		/// <history>
		///		[cnurse]	11/15/2004	documented
		/// </history>
		/// -----------------------------------------------------------------------------
		public override SearchResultsInfoCollection GetSearchResults(int portalID, string criteria)
		{
			//We will assume that the content is in the locale of the Portal
			PortalController portalController = new PortalController();
			PortalInfo portalInfo = portalController.GetPortal(portalID);
			string locale = portalInfo.DefaultLanguage;
			Hashtable commonWords = GetCommonWords(locale);
			
			// clean criteria
			criteria = criteria.ToLower();
			
			// split search criteria into words
			SearchCriteriaCollection searchWords = new SearchCriteriaCollection(criteria);
			
			Hashtable searchResults = new Hashtable();
			// iterate through search criteria words
			foreach (SearchCriteria criterion in searchWords)
			{
				if (!commonWords.ContainsKey(criterion.Criteria))
				{
					SearchResultsInfoCollection resultsCollection = SearchDataStoreController.GetSearchResults(portalID, criterion.Criteria);
					if (!criterion.MustExclude)
					{
						// Add all these to the results
						foreach (SearchResultsInfo result in resultsCollection)
						{
							if (searchResults.ContainsKey(result.SearchItemID))
							{
								((SearchResultsInfo) searchResults[result.SearchItemID]).Relevance += result.Relevance;
							}
							else
							{
								searchResults.Add(result.SearchItemID, result);
							}
						}
					}
					if (criterion.MustInclude)
					{
						// We need to remove items which do not include this term
						Hashtable mandatoryResults = new Hashtable();
						foreach (SearchResultsInfo result in resultsCollection)
						{
							mandatoryResults.Add(result.SearchItemID, 0);
						}
						foreach (SearchResultsInfo result in searchResults.Values)
						{
							if (!mandatoryResults.ContainsKey(result.SearchItemID))
							{
								result.Delete = true;
							}
						}
					}
					if (criterion.MustExclude)
					{
						// We need to remove items which do include this term
						Hashtable excludedResults = new Hashtable();
						foreach (SearchResultsInfo result in resultsCollection)
						{
							excludedResults.Add(result.SearchItemID, 0);
						}
						foreach (SearchResultsInfo result in searchResults.Values)
						{
							if (excludedResults.ContainsKey(result.SearchItemID))
							{
								result.Delete = true;
							}
						}
					}
				}
			}
			
			//Only include results we have permission to see
			SearchResultsInfoCollection results = new SearchResultsInfoCollection();
			foreach (SearchResultsInfo searchResult in searchResults.Values)
			{
				
				//Check If authorised to View Tab
				TabController tabController = new TabController();
				TabInfo tabInfo = tabController.GetTab(searchResult.TabId);
				if (PortalSecurity.IsInRoles(tabInfo.AuthorizedRoles))
				{
					//Now check if authorized to view module
					ModuleController moduleController = new ModuleController();
					ModuleInfo moduleInfo = moduleController.GetModule(searchResult.ModuleId, searchResult.TabId);
					if (PortalSecurity.IsInRoles(moduleInfo.AuthorizedViewRoles) && !moduleInfo.IsDeleted)
					{
						//If authorised add result to collection
						results.Add(searchResult);
					}
				}
			}
			
			//Return Search Results Collection
			return results;
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// StoreSearchItems adds the Search Item to the Data Store
		/// </summary>
		/// <remarks>
		/// </remarks>
		/// <param name="SearchItems">A Collection of SearchItems</param>
		/// <history>
		///		[cnurse]	11/15/2004	documented
		/// </history>
		/// -----------------------------------------------------------------------------
		public override void StoreSearchItems (SearchItemInfoCollection searchItems)
		{
			//Get the default Search Settings
			defaultSettings = Common.Globals.HostSettings;
			
			//For now as we don't support Localized content - set the locale to the default locale. This
			//is to avoid the error in GetDefaultLanguageByModule which artificially limits the number
			//of modules that can be indexed.  This will need to be addressed when we support localized content.
			Hashtable modules = new Hashtable();
			for (int i = 0; i < searchItems.Count; i++)
			{
				if (! modules.ContainsKey(searchItems[i].ModuleId.ToString()))
				{
					modules.Add(searchItems[i].ModuleId.ToString(), "en-US");
				}
			}
			
			SearchItemInfo searchItem;
			SearchItemInfoCollection indexedItems;
			SearchItemInfoCollection moduleItems;
			int indexID;
			int moduleId;
			string language;
			bool itemFound;
			
			//Process the SearchItems by Module to reduce Database hits
			IDictionaryEnumerator moduleEnumerator = modules.GetEnumerator();
			while (moduleEnumerator.MoveNext())
			{
				moduleId = System.Convert.ToInt32(moduleEnumerator.Key);
				language = System.Convert.ToString(moduleEnumerator.Value);
				
				//Get the Indexed Items that are in the Database for this Module
				indexedItems = GetSearchItems(moduleId);
				//Get the Module's SearchItems to compare
				moduleItems = searchItems.ModuleItems(moduleId);
				
				//As we will be potentially removing items from the collection iterate backwards
				for (int i = moduleItems.Count - 1; i > 0; i--)
				{
					searchItem = moduleItems[i];
					itemFound = false;
					
					//Iterate through Indexed Items
					foreach (SearchItemInfo indexedItem in indexedItems)
					{
						//Compare the SearchKeys
						if (searchItem.SearchKey == indexedItem.SearchKey)
						{
							//Item exists so compare Dates to see if modified
							if (indexedItem.PubDate < searchItem.PubDate)
							{
								try
								{
									//Content modified so update SearchItem and delete item's Words Collection
									searchItem.SearchItemId = indexedItem.SearchItemId;
									SearchDataStoreController.UpdateSearchItem(searchItem);
									SearchDataStoreController.DeleteSearchItemWords(searchItem.SearchItemId);
									
									// re-index the content
									AddIndexWords(searchItem.SearchItemId, searchItem, language);
								}
								catch (Exception exception)
								{
									//Log Exception
									Exceptions.Exceptions.LogException(exception);
								}
							}
							
							//Remove Items from both collections
							indexedItems.Remove(indexedItem);
							moduleItems.Remove(searchItem);
							
							//Exit the Iteration as Match found
							itemFound = true;
							break;
						}
					}
					
					if (! itemFound)
					{
						try
						{
							//Item doesn't exist so Add to Index
							indexID = SearchDataStoreController.AddSearchItem(searchItem);
							// index the content
							AddIndexWords(indexID, searchItem, language);
						}
						catch (Exception exception)
						{
							//Log Exception
							Exceptions.Exceptions.LogException(exception);
						}
					}
				}
				
				//As we removed the IndexedItems as we matched them the remaining items are deleted Items
				//ie they have been indexed but are no longer present
				Hashtable hashTable = new Hashtable();
				foreach (SearchItemInfo indexedItem in indexedItems)
				{
					try
					{
						//dedupe
						if (!hashTable.Contains(indexedItem.SearchItemId))
						{
							SearchDataStoreController.DeleteSearchItem(indexedItem.SearchItemId);
							hashTable.Add(indexedItem.SearchItemId, 0);
						}
					}
					catch (Exception exception)
					{
						//Log Exception
						Exceptions.Exceptions.LogException(exception);
					}
				}
			}
		}
		
		#endregion
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -