SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IPagePublishJob.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Globalization;
4 using System.Linq;
5 using erminas.SmartAPI.CMS.ServerManagement;
6 using erminas.SmartAPI.Exceptions;
7 using erminas.SmartAPI.Utils;
8 
9 namespace erminas.SmartAPI.CMS.Project.Pages
10 {
11  internal class PagePublishJob : IPagePublishJob
12  {
13  internal PagePublishJob(IPage page)
14  {
15  Page = page;
16  LanguageVariants = new List<ILanguageVariant>();
17  ProjectVariants = new List<IProjectVariant>();
18  }
19 
20  public ISession Session { get { return Page.Session; } }
21  public IProject Project { get { return Page.Project; } }
22  public IPage Page { get; private set; }
23  public bool IsPublishingAllFollowingPages { get; set; }
24  public bool IsPublishingRelatedPages { get; set; }
25  public bool IsSendingEmailOnCompletion { get; set; }
26  public IUser EMailReceipient { get; set; }
27  public IApplicationServer Server { get; set; }
28  public DateTime PublishAccordingTo { get; set; }
29  public DateTime PublishOn { get; set; }
30  public IEnumerable<ILanguageVariant> LanguageVariants { get; set; }
31  public IEnumerable<IProjectVariant> ProjectVariants { get; set; }
32  public void RunAsync()
33  {
34  CheckValidity();
35 
36  const string PUBLISH = @"<PROJECT guid=""{0}"" sessionkey=""{10}""><PAGE guid=""{1}""><EXPORTJOB action=""save"" email=""{2}"" toppriority=""0"" generatenextpages=""{3}"" generaterelativepages=""{4}"" reddotserver=""{5}"" application="""" generatedate=""{6}"" startgenerationat=""{7}""><LANGUAGEVARIANTS action=""checkassigning"">{8}</LANGUAGEVARIANTS><PROJECTVARIANTS action=""checkassigning"">{9}</PROJECTVARIANTS></EXPORTJOB></PAGE></PROJECT>";
37 
38  var languageVariants = GetLanguageVariantsString();
39  var projectVariants = GetProjectVariantsString();
40 
41  var publishAccordingToString = ToRQLString(PublishAccordingTo);
42  var publishOnString = ToRQLString(PublishOn);
43 
44  var query = PUBLISH.RQLFormat(Project, Page, IsSendingEmailOnCompletion ? EMailReceipient : null,
45  IsPublishingAllFollowingPages, IsPublishingRelatedPages, Server, publishAccordingToString,
46  publishOnString, languageVariants, projectVariants, Session.SessionKey);
47 
48  Project.ExecuteRQL(query);
49  }
50 
51  private void CheckValidity()
52  {
53  if (LanguageVariants == null || !LanguageVariants.Any())
54  {
55  throw new SmartAPIException(Session.ServerLogin,
56  string.Format(
57  "Missing language variant assignment for the publication of page {0}", Page));
58  }
59 
60  if (ProjectVariants == null || !ProjectVariants.Any())
61  {
62  throw new SmartAPIException(Session.ServerLogin,
63  string.Format("Missing project variant assignment for the publication of page {0}",
64  Page));
65  }
66  }
67 
68  private static string ToRQLString(DateTime dt)
69  {
70  return dt != default(DateTime)
71  ? dt.ToString("M/d/yyyy", CultureInfo.InvariantCulture)
72  : RQL.SESSIONKEY_PLACEHOLDER;
73  }
74 
75  private string GetLanguageVariantsString()
76  {
77  const string SINGLE_LANGUAGE_VARIANT = @"<LANGUAGEVARIANT guid=""{0}"" checked=""{1}""/>";
78 
79  var checkedLanguageVariants = LanguageVariants.Aggregate("",
80  (s, variant) =>
81  s + SINGLE_LANGUAGE_VARIANT.RQLFormat(variant.Guid.ToRQLString(), true));
82  var uncheckedLanguageVariants = Project.LanguageVariants.Except(LanguageVariants)
83  .Aggregate("",
84  (s, variant) =>
85  s + SINGLE_LANGUAGE_VARIANT.RQLFormat(variant.Guid.ToRQLString(), false));
86 
87  var languageVariants = checkedLanguageVariants + uncheckedLanguageVariants;
88  return languageVariants;
89  }
90 
91  private string GetProjectVariantsString()
92  {
93  const string SINGLE_PROJECT_VARIANT = @"<PROJECTVARIANT guid=""{0}"" checked=""{1}""/>";
94 
95  var checkedProjectVariants = ProjectVariants.Aggregate("",
96  (s, variant) =>
97  s + SINGLE_PROJECT_VARIANT.RQLFormat(variant, true));
98 
99  var uncheckedProjectVariants = Project.ProjectVariants.Except(ProjectVariants)
100  .Aggregate("",
101  (s, variant) =>
102  s + SINGLE_PROJECT_VARIANT.RQLFormat(variant, false));
103  var projectVariants = checkedProjectVariants + uncheckedProjectVariants;
104  return projectVariants;
105  }
106  }
107 
108  public interface IPagePublishJob : IProjectObject
109  {
110  IPage Page { get; }
111 
112  bool IsPublishingAllFollowingPages { get; set; }
113  bool IsPublishingRelatedPages { get; set; }
114  bool IsSendingEmailOnCompletion { get; set; }
115 
116  IUser EMailReceipient { get; set; }
117  IApplicationServer Server { get; set; }
118 
119  DateTime PublishAccordingTo { get; set; }
120  DateTime PublishOn { get; set; }
121  IEnumerable<ILanguageVariant> LanguageVariants{ get; set; }
122  IEnumerable<IProjectVariant> ProjectVariants { get; set; }
123 
124  void RunAsync();
125  }
126 }