PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Trunk/Content_7.0.6/Community/Library/Entities/Content/Taxonomy/TermController.cs

#
C# | 276 lines | 131 code | 41 blank | 104 comment | 6 complexity | 5f12639253eec837215822e1b6c7a8d6 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0
  1. #region Copyright
  2. //
  3. // DotNetNuke® - http://www.dotnetnuke.com
  4. // Copyright (c) 2002-2013
  5. // by DotNetNuke Corporation
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  8. // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
  9. // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  10. // to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all copies or substantial portions
  13. // of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  18. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. // DEALINGS IN THE SOFTWARE.
  20. #endregion
  21. #region Usings
  22. using System;
  23. using System.Collections.Generic;
  24. using System.Linq;
  25. using System.Web.Caching;
  26. using DotNetNuke.Common;
  27. using DotNetNuke.Common.Utilities;
  28. using DotNetNuke.Entities.Content.Common;
  29. using DotNetNuke.Entities.Content.Data;
  30. using DotNetNuke.Entities.Users;
  31. #endregion
  32. namespace DotNetNuke.Entities.Content.Taxonomy
  33. {
  34. /// <summary>
  35. /// The Main Business layer of Taxonomy.
  36. /// </summary>
  37. /// <example>
  38. /// <code lang="C#">
  39. /// internal static List&lt;Term&gt; GetTerms(this Vocabulary voc, int vocabularyId)
  40. /// {
  41. /// ITermController ctl = Util.GetTermController();
  42. /// return ctl.GetTermsByVocabulary(vocabularyId).ToList();
  43. /// }
  44. /// </code>
  45. /// </example>
  46. public class TermController : ITermController
  47. {
  48. private readonly IDataService _DataService;
  49. private const string _CacheKey = "Terms_{0}";
  50. private const CacheItemPriority _CachePriority = CacheItemPriority.Normal;
  51. private const int _CacheTimeOut = 20;
  52. #region Constructors
  53. public TermController() : this(Util.GetDataService())
  54. {
  55. }
  56. public TermController(IDataService dataService)
  57. {
  58. _DataService = dataService;
  59. }
  60. #endregion
  61. #region Private Methods
  62. private object GetTermsCallBack(CacheItemArgs cacheItemArgs)
  63. {
  64. var vocabularyId = (int) cacheItemArgs.ParamList[0];
  65. return CBO.FillQueryable<Term>(_DataService.GetTermsByVocabulary(vocabularyId)).ToList();
  66. }
  67. #endregion
  68. #region Public Methods
  69. /// <summary>
  70. /// Adds the term.
  71. /// </summary>
  72. /// <param name="term">The term.</param>
  73. /// <returns>term id.</returns>
  74. /// <exception cref="System.ArgumentNullException">term is null.</exception>
  75. /// <exception cref="System.ArgumentOutOfRangeException">term.VocabularyId is less than 0.</exception>
  76. /// <exception cref="System.ArgumentException">term.Name is empty.</exception>
  77. public int AddTerm(Term term)
  78. {
  79. //Argument Contract
  80. Requires.NotNull("term", term);
  81. Requires.PropertyNotNegative("term", "VocabularyId", term.VocabularyId);
  82. Requires.PropertyNotNullOrEmpty("term", "Name", term.Name);
  83. if ((term.IsHeirarchical))
  84. {
  85. term.TermId = _DataService.AddHeirarchicalTerm(term, UserController.GetCurrentUserInfo().UserID);
  86. }
  87. else
  88. {
  89. term.TermId = _DataService.AddSimpleTerm(term, UserController.GetCurrentUserInfo().UserID);
  90. }
  91. //Clear Cache
  92. DataCache.RemoveCache(string.Format(_CacheKey, term.VocabularyId));
  93. return term.TermId;
  94. }
  95. /// <summary>
  96. /// Adds the content of the term to.
  97. /// </summary>
  98. /// <param name="term">The term.</param>
  99. /// <param name="contentItem">The content item.</param>
  100. /// <exception cref="System.ArgumentNullException">term is null.</exception>
  101. /// <exception cref="System.ArgumentNullException">content item is null.</exception>
  102. public void AddTermToContent(Term term, ContentItem contentItem)
  103. {
  104. //Argument Contract
  105. Requires.NotNull("term", term);
  106. Requires.NotNull("contentItem", contentItem);
  107. _DataService.AddTermToContent(term, contentItem);
  108. }
  109. /// <summary>
  110. /// Deletes the term.
  111. /// </summary>
  112. /// <param name="term">The term.</param>
  113. /// <exception cref="System.ArgumentNullException">term is null.</exception>
  114. /// <exception cref="System.ArgumentOutOfRangeException">term.TermId is less than 0.</exception>
  115. public void DeleteTerm(Term term)
  116. {
  117. //Argument Contract
  118. Requires.NotNull("term", term);
  119. Requires.PropertyNotNegative("term", "TermId", term.TermId);
  120. if ((term.IsHeirarchical))
  121. {
  122. _DataService.DeleteHeirarchicalTerm(term);
  123. }
  124. else
  125. {
  126. _DataService.DeleteSimpleTerm(term);
  127. }
  128. //Clear Cache
  129. DataCache.RemoveCache(string.Format(_CacheKey, term.VocabularyId));
  130. }
  131. /// <summary>
  132. /// Gets the term.
  133. /// </summary>
  134. /// <param name="termId">The term id.</param>
  135. /// <returns>specific term.</returns>
  136. /// <exception cref="System.ArgumentOutOfRangeException">termId is less than 0.</exception>
  137. public Term GetTerm(int termId)
  138. {
  139. //Argument Contract
  140. Requires.NotNegative("termId", termId);
  141. return CBO.FillObject<Term>(_DataService.GetTerm(termId));
  142. }
  143. /// <summary>
  144. /// Retrieve usage data for the specified term ID.
  145. /// </summary>
  146. /// <param name="termId">Term ID in question</param>
  147. public TermUsage GetTermUsage(int termId)
  148. {
  149. Requires.NotNegative("termId", termId);
  150. return CBO.FillObject<TermUsage>(_DataService.GetTermUsage(termId));
  151. }
  152. /// <summary>
  153. /// Gets the content of the terms by content item id.
  154. /// </summary>
  155. /// <param name="contentItemId">The content item id.</param>
  156. /// <returns>term collection</returns>
  157. /// <exception cref="System.ArgumentOutOfRangeException">ContentItemId is less than 0.</exception>
  158. public IQueryable<Term> GetTermsByContent(int contentItemId)
  159. {
  160. //Argument Contract
  161. Requires.NotNegative("contentItemId", contentItemId);
  162. return CBO.FillQueryable<Term>(_DataService.GetTermsByContent(contentItemId));
  163. }
  164. /// <summary>
  165. /// Gets the terms by vocabulary id.
  166. /// </summary>
  167. /// <param name="vocabularyId">The vocabulary id.</param>
  168. /// <returns>term collection</returns>
  169. /// <exception cref="System.ArgumentOutOfRangeException">vocabularyId is less than 0.</exception>
  170. public IQueryable<Term> GetTermsByVocabulary(int vocabularyId)
  171. {
  172. //Argument Contract
  173. Requires.NotNegative("vocabularyId", vocabularyId);
  174. return CBO.GetCachedObject<List<Term>>(new CacheItemArgs(string.Format(_CacheKey, vocabularyId), _CacheTimeOut, _CachePriority, vocabularyId), GetTermsCallBack).AsQueryable();
  175. }
  176. /// <summary>
  177. /// Gets the terms by vocabulary name.
  178. /// </summary>
  179. /// <param name="vocabularyName">Name of the vocabulary.</param>
  180. /// <returns>term collection</returns>
  181. /// <exception cref="System.ArgumentException">vocabularyName is empty.</exception>
  182. public IQueryable<Term> GetTermsByVocabulary(string vocabularyName)
  183. {
  184. //Argument Contract
  185. Requires.NotNullOrEmpty("vocabularyName", vocabularyName);
  186. IVocabularyController vocabularyController = Util.GetVocabularyController();
  187. Vocabulary vocabulary = (vocabularyController.GetVocabularies()
  188. .Cast<Vocabulary>().Where(v => v.Name == vocabularyName))
  189. .SingleOrDefault();
  190. if (vocabulary == null)
  191. {
  192. throw new ArgumentException("Vocabulary does not exist.", "vocabularyName");
  193. }
  194. return GetTermsByVocabulary(vocabulary.VocabularyId);
  195. }
  196. /// <summary>
  197. /// Removes all terms from content item.
  198. /// </summary>
  199. /// <param name="contentItem">The content item.</param>
  200. /// <exception cref="System.ArgumentNullException">content item is null.</exception>
  201. public void RemoveTermsFromContent(ContentItem contentItem)
  202. {
  203. //Argument Contract
  204. Requires.NotNull("contentItem", contentItem);
  205. _DataService.RemoveTermsFromContent(contentItem);
  206. }
  207. /// <summary>
  208. /// Updates the term.
  209. /// </summary>
  210. /// <param name="term">The term.</param>
  211. /// <exception cref="System.ArgumentNullException">term is null.</exception>
  212. /// <exception cref="System.ArgumentOutOfRangeException">term.TermId is less than 0.</exception>
  213. /// <exception cref="System.ArgumentOutOfRangeException">term.VocabularyId is less than 0.</exception>
  214. /// <exception cref="System.ArgumentException">term.Name is empty.</exception>
  215. public void UpdateTerm(Term term)
  216. {
  217. //Argument Contract
  218. Requires.NotNull("term", term);
  219. Requires.PropertyNotNegative("term", "TermId", term.TermId);
  220. Requires.PropertyNotNegative("term", "Vocabulary.VocabularyId", term.VocabularyId);
  221. Requires.PropertyNotNullOrEmpty("term", "Name", term.Name);
  222. if ((term.IsHeirarchical))
  223. {
  224. _DataService.UpdateHeirarchicalTerm(term, UserController.GetCurrentUserInfo().UserID);
  225. }
  226. else
  227. {
  228. _DataService.UpdateSimpleTerm(term, UserController.GetCurrentUserInfo().UserID);
  229. }
  230. //Clear Cache
  231. DataCache.RemoveCache(string.Format(_CacheKey, term.VocabularyId));
  232. }
  233. #endregion
  234. }
  235. }