SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IPageSearch.cs
Go to the documentation of this file.
1 ï»¿// SmartAPI - .Net programmatic access to RedDot servers
2 //
3 // Copyright (C) 2013 erminas GbR
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free Software Foundation,
7 // either version 3 of the License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 // See the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program.
14 // If not, see <http://www.gnu.org/licenses/>.
15 
16 using System;
17 using System.Collections.Generic;
18 using System.Globalization;
19 using System.Linq;
20 using System.Xml;
21 using erminas.SmartAPI.CMS.Project.ContentClasses;
22 using erminas.SmartAPI.CMS.Project.Workflows;
23 using erminas.SmartAPI.CMS.ServerManagement;
24 using erminas.SmartAPI.Utils;
25 
26 namespace erminas.SmartAPI.CMS.Project.Pages
27 {
28  public interface IPageSearch : IProjectObject
29  {
30  string Category { get; set; }
31  IContentClass ContentClass { get; set; }
32  DateTime CreatedFrom { get; set; }
33  DateTime CreatedTo { get; set; }
34  IEnumerable<IPage> Execute();
35  string Headline { get; set; }
36  bool IsMatchingHeadlineExactly { get; set; }
37  bool IsMatchingKeywordExactly { get; set; }
38  string Keyword { get; set; }
39  int MaxRecords { get; set; }
40  int PageIdFrom { get; set; }
41  int PageIdTo { get; set; }
42  PageType PageType { get; set; }
43  void SetDefaults();
44  string Text { get; set; }
45  bool TextExact { get; set; }
46  }
47 
48  internal class PageSearch : IPageSearch
49  {
50  private const int DEFAULT_MAX_RECORDS = 20000;
51  private readonly IProject _project;
52 
53  internal PageSearch(IProject project)
54  {
55  _project = project;
56  SetDefaults();
57  }
58 
59  public string Category { get; set; }
60  public IContentClass ContentClass { get; set; }
61  public DateTime CreatedFrom { get; set; }
62 
63  public DateTime CreatedTo { get; set; }
64 
65  public IEnumerable<IPage> Execute()
66  {
67  var rqlXml = new XmlDocument();
68  XmlElement pageElement = rqlXml.CreateElement("PAGE");
69  pageElement.SetAttribute("action", "search");
70  pageElement.SetAttribute("flags", ((int) PageType).ToString(CultureInfo.InvariantCulture));
71  pageElement.SetAttribute("maxrecords", MaxRecords.ToString(CultureInfo.InvariantCulture));
72  if (Headline != null)
73  {
74  pageElement.SetAttribute("headline", Headline);
75  pageElement.SetAttribute("headlinelike", IsMatchingHeadlineExactly ? "0" : "-1");
76  }
77  if (Category != null)
78  {
79  pageElement.SetAttribute("section", Category);
80  }
81  if (Keyword != null)
82  {
83  pageElement.SetAttribute("keyword", Keyword);
84  pageElement.SetAttribute("keywordlike", IsMatchingKeywordExactly ? "0" : "-1");
85  }
86  if (Text != null)
87  {
88  pageElement.SetAttribute("searchtext", Text);
89  }
90  if (PageIdFrom != -1)
91  {
92  pageElement.SetAttribute("pageidfrom", PageIdFrom.ToString(CultureInfo.InvariantCulture));
93  }
94  if (PageIdTo != -1)
95  {
96  pageElement.SetAttribute("pageidto", PageIdTo.ToString(CultureInfo.InvariantCulture));
97  }
98  if (CreatedTo != DateTime.MinValue)
99  {
100  pageElement.SetAttribute("createdateto", (CreatedTo.ToOADate()).ToString(CultureInfo.InvariantCulture));
101  }
102  if (CreatedFrom != DateTime.MinValue)
103  {
104  pageElement.SetAttribute("createdatefrom",
105  (CreatedFrom.ToOADate()).ToString(CultureInfo.InvariantCulture));
106  }
107  if (ContentClass != null)
108  {
109  pageElement.SetAttribute("templateguid", ContentClass.Guid.ToRQLString());
110  }
111 
112  XmlDocument xmlDoc = _project.ExecuteRQL(pageElement.OuterXml);
113 
114  return (from XmlElement curNode in xmlDoc.GetElementsByTagName("PAGE")
115  let nodeWithLanguageVariantId = AddLanguageVariantId(curNode)
116  select new Page(_project, nodeWithLanguageVariantId)).ToList();
117  }
118 
119  public string Headline { get; set; }
120 
121  public bool IsMatchingHeadlineExactly { get; set; }
122 
123  public bool IsMatchingKeywordExactly { get; set; }
124  public string Keyword { get; set; }
125  public int MaxRecords { get; set; }
126 
127  public int PageIdFrom { get; set; }
128 
129  public int PageIdTo { get; set; }
130  public PageType PageType { get; set; }
131 
132  public IProject Project
133  {
134  get { return _project; }
135  }
136 
137  public ISession Session
138  {
139  get { return _project.Session; }
140  }
141 
142  public void SetDefaults()
143  {
144  PageType = PageType.All;
145  Text = null;
146  TextExact = true;
147  Category = null;
148  Keyword = null;
149  IsMatchingKeywordExactly = true;
150  Headline = null;
151  IsMatchingHeadlineExactly = true;
152  MaxRecords = DEFAULT_MAX_RECORDS;
153  PageIdFrom = -1;
154  PageIdTo = -1;
155  CreatedFrom = DateTime.MinValue;
156  CreatedTo = DateTime.MinValue;
157  ContentClass = null;
158  }
159 
160  public string Text { get; set; }
161 
162  public bool TextExact { get; set; }
163 
164  private XmlElement AddLanguageVariantId(XmlElement curNode)
165  {
166  curNode.SetAttributeValue("languagevariantid", _project.LanguageVariants.Current.Abbreviation);
167 
168  return curNode;
169  }
170  }
171 
172  public class ResultGroup
173  {
174  public readonly string Group;
175  public readonly IEnumerable<Result> Results;
176 
177  public ResultGroup(string @group, IEnumerable<Result> results)
178  {
179  Group = @group;
180  Results = results;
181  }
182  }
183 
184  public class Result
185  {
186  public readonly IContentClass ContentClass;
187  public readonly DateTime CreationDate;
188  public readonly DateTime DateOfLastChange;
189  public readonly IUser LastEditor;
190  public readonly IUser OriginalAuthor;
191  public readonly IPage Page;
192 
193  public Result(IPage page, DateTime creationDate, IUser originalAuthor, DateTime dateOfLastChange,
194  IUser lastEditor, IContentClass contentClass)
195  {
196  Page = page;
197  CreationDate = creationDate;
198  OriginalAuthor = originalAuthor;
199  DateOfLastChange = dateOfLastChange;
200  LastEditor = lastEditor;
201  ContentClass = contentClass;
202  }
203 
204  public DateTime ReleaseDate { get; set; }
205  public WorkflowInfo WorkflowInfo { get; set; }
206  }
207 
208  public class WorkflowInfo
209  {
210  #region Rejection enum
211 
212  public enum Rejection
213  {
214  NoReleaseReactionDefined = 0,
215  ToAuthor = 4,
216  ToPreviousReleaseLevel = 8,
217  ToAnEligibleLevel = 16,
218  SelectionOfReleaseLevelByRejectingUser = 32
219  }
220 
221  #endregion
222 
223  #region RejectionSkippableType enum
224 
226  {
227  NotApplicable = -1,
228  RejectionCannotBeSkipped = 0,
229  RejectionCanBeSkipped = 1
230  }
231 
232  #endregion
233 
234  #region ReleaseType enum
235 
236  public enum ReleaseType
237  {
238  NoWorkflowReactionSet = 0,
239  PageWaitingForReleaseOrPageWasRejected = 1155,
240  WebComplianceManagerRejectedAutomatically = 1156
241  }
242 
243  #endregion
244 
245  public readonly int EscalationTimeoutInHours;
246  public readonly bool IsEscalationProcedureSet;
247  public readonly bool? IsRejectionSkippable;
248  public readonly IEnumerable<INote> Notes;
250  public readonly Rejection RejectionType;
251  public readonly string ReleaseName;
252  public readonly IEnumerable<ReleaseInfo> Releases;
253  public readonly IWorkflow Workflow;
255 
256  public WorkflowInfo(IWorkflow workflow, IEnumerable<ReleaseInfo> releases, string releaseName,
257  Rejection rejectionType, ReleaseType workflowReactionTypeResponsibleForRejection,
258  RejectionSkippableType rejectionSkippability, int escalationTimeoutInHours,
259  IEnumerable<INote> notes)
260  {
261  Releases = releases;
262  ReleaseName = releaseName;
263  RejectionType = rejectionType;
264  WorkflowReactionTypeResponsibleForRejection = workflowReactionTypeResponsibleForRejection;
265  RejectionSkippability = rejectionSkippability;
266  EscalationTimeoutInHours = escalationTimeoutInHours;
267  Notes = notes;
268  Workflow = workflow;
269  IsEscalationProcedureSet = EscalationTimeoutInHours > 0;
270  switch (rejectionSkippability)
271  {
272  case RejectionSkippableType.NotApplicable:
273  IsRejectionSkippable = null;
274  break;
275  case RejectionSkippableType.RejectionCannotBeSkipped:
276  IsRejectionSkippable = false;
277  break;
278  case RejectionSkippableType.RejectionCanBeSkipped:
279  IsRejectionSkippable = true;
280  break;
281  default:
282  throw new ArgumentException("Unknown rejection skippability type");
283  }
284  }
285  }
286 
287  public class ReleaseInfo
288  {
289  public readonly int AssentCount;
290  public readonly int RequiredAssentCount;
291  public readonly IEnumerable<IIUserInfo> Users;
292 
293  public ReleaseInfo(int assentCount, int requiredAssentCount, IEnumerable<IIUserInfo> users)
294  {
295  AssentCount = assentCount;
296  RequiredAssentCount = requiredAssentCount;
297  Users = users;
298  }
299 
300  public interface IIUserInfo
301  {
302  bool HasUserReleasedPage { get; }
303  DateTime PageReleaseDate { get; }
304  IUser User { get; }
305  }
306 
307  internal class UserInfo : IIUserInfo
308  {
309  public UserInfo(IProject project, XmlElement user)
310  {
311  User = new User(project.Session, user.GetGuid()) {Name = user.GetName()};
312  HasUserReleasedPage = user.GetIntAttributeValue("released").GetValueOrDefault() == 1;
313  PageReleaseDate = user.GetOADate().GetValueOrDefault();
314  }
315 
316  public bool HasUserReleasedPage { get; private set; }
317  public DateTime PageReleaseDate { get; private set; }
318  public IUser User { get; private set; }
319  }
320  }
321 }