PageRenderTime 808ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/web/studio/ASC.Web.Studio/Products/Projects/Core/Dao/CommentDao.cs

https://gitlab.com/rekby-archive/onlyoffice-CommunityServer
C# | 181 lines | 144 code | 22 blank | 15 comment | 5 complexity | f2e40fffb4c922e094ea2bcf0fac60ab MD5 | raw file
  1. /*
  2. *
  3. * (c) Copyright Ascensio System Limited 2010-2021
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. *
  15. */
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using ASC.Common.Data.Sql.Expressions;
  20. using ASC.Common.Logging;
  21. using ASC.Core.Tenants;
  22. using ASC.Projects.Core.DataInterfaces;
  23. using ASC.Projects.Core.Domain;
  24. using Microsoft.Security.Application;
  25. namespace ASC.Projects.Data.DAO
  26. {
  27. internal class CommentDao : BaseDao, ICommentDao
  28. {
  29. private static readonly string ColumnId = "id";
  30. private static readonly string ColumnTargetUniqId = "target_uniq_id";
  31. private static readonly string ColumnContent = "content";
  32. private static readonly string ColumnInactive = "inactive";
  33. private static readonly string ColumnCreateBy = "create_by";
  34. private static readonly string ColumnCreateOn = "create_on";
  35. private static readonly string ColumnParentId = "parent_id";
  36. private static readonly string ColumnCommentId = "comment_id";
  37. public CommentDao(int tenantID)
  38. : base(tenantID)
  39. {
  40. }
  41. public List<Comment> GetAll(DomainObject<int> target)
  42. {
  43. return Db
  44. .ExecuteList(
  45. Query("projects_comments")
  46. .Select(ColumnId,
  47. ColumnTargetUniqId,
  48. ColumnContent,
  49. ColumnInactive,
  50. ColumnCreateBy,
  51. ColumnCreateOn,
  52. ColumnParentId,
  53. ColumnCommentId)
  54. .Where("target_uniq_id", target.UniqID))
  55. .ConvertAll(ToComment)
  56. .OrderBy(c => c.CreateOn)
  57. .ToList();
  58. }
  59. public Comment GetById(Guid id)
  60. {
  61. return Db.ExecuteList(Query(CommentsTable).Select(ColumnId,
  62. ColumnTargetUniqId,
  63. ColumnContent,
  64. ColumnInactive,
  65. ColumnCreateBy,
  66. ColumnCreateOn,
  67. ColumnParentId,
  68. ColumnCommentId).Where("id", id.ToString()))
  69. .ConvertAll(ToComment)
  70. .SingleOrDefault();
  71. }
  72. public List<int> Count(List<ProjectEntity> targets)
  73. {
  74. var pairs = Db.ExecuteList(
  75. Query(CommentsTable)
  76. .Select("target_uniq_id", "count(*)")
  77. .Where(Exp.In("target_uniq_id", targets.ConvertAll(target => target.UniqID)))
  78. .Where("inactive", false)
  79. .GroupBy(1)
  80. ).ConvertAll(r => new object[] { Convert.ToString(r[0]), Convert.ToInt32(r[1]) });
  81. return targets.ConvertAll(
  82. target =>
  83. {
  84. var pair = pairs.Find(p => String.Equals(Convert.ToString(p[0]), target.UniqID));
  85. return pair == null ? 0 : Convert.ToInt32(pair[1]);
  86. });
  87. }
  88. public List<Comment> GetComments(Exp where)
  89. {
  90. return Db.ExecuteList(
  91. Query(CommentsTable)
  92. .Select(ColumnId,
  93. ColumnTargetUniqId,
  94. ColumnContent,
  95. ColumnInactive,
  96. ColumnCreateBy,
  97. ColumnCreateOn,
  98. ColumnParentId,
  99. ColumnCommentId)
  100. .Where(where)
  101. .Where("inactive", false)
  102. .OrderBy("create_on", false))
  103. .ConvertAll(ToComment);
  104. }
  105. public int Count(DomainObject<Int32> target)
  106. {
  107. return Db.ExecuteScalar<int>(
  108. Query(CommentsTable)
  109. .SelectCount()
  110. .Where("target_uniq_id", target.UniqID)
  111. .Where("inactive", false));
  112. }
  113. public Comment Save(Comment comment)
  114. {
  115. if (comment.OldGuidId == default(Guid)) comment.OldGuidId = Guid.NewGuid();
  116. if (!string.IsNullOrWhiteSpace(comment.Content) && comment.Content.Contains("<w:WordDocument>"))
  117. {
  118. try
  119. {
  120. comment.Content = Sanitizer.GetSafeHtmlFragment(comment.Content);
  121. }
  122. catch (Exception err)
  123. {
  124. LogManager.GetLogger("ASC").Error(err);
  125. }
  126. }
  127. var insert = Insert(CommentsTable)
  128. .InColumnValue(ColumnCommentId, comment.ID)
  129. .InColumnValue(ColumnId, comment.OldGuidId)
  130. .InColumnValue(ColumnTargetUniqId, comment.TargetUniqID)
  131. .InColumnValue(ColumnContent, comment.Content)
  132. .InColumnValue(ColumnInactive, comment.Inactive)
  133. .InColumnValue(ColumnCreateBy, comment.CreateBy.ToString())
  134. .InColumnValue(ColumnCreateOn, TenantUtil.DateTimeToUtc(comment.CreateOn))
  135. .InColumnValue(ColumnParentId, comment.Parent.ToString())
  136. .Identity(1, 0, true);
  137. comment.ID = Db.ExecuteScalar<int>(insert);
  138. return comment;
  139. }
  140. public void Delete(Guid id)
  141. {
  142. Db.ExecuteNonQuery(Delete(CommentsTable).Where("id", id.ToString()));
  143. }
  144. private static Comment ToComment(object[] r)
  145. {
  146. return new Comment
  147. {
  148. OldGuidId = ToGuid(r[0]),
  149. TargetUniqID = (string)r[1],
  150. Content = (string)r[2],
  151. Inactive = Convert.ToBoolean(r[3]),
  152. CreateBy = ToGuid(r[4]),
  153. CreateOn = TenantUtil.DateTimeFromUtc(Convert.ToDateTime(r[5])),
  154. Parent = ToGuid(r[6]),
  155. ID = Convert.ToInt32(r[7])
  156. };
  157. }
  158. }
  159. }