SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
ITemplateVariant.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.Web;
18 using System.Xml;
19 using erminas.SmartAPI.CMS.ServerManagement;
20 using erminas.SmartAPI.Exceptions;
21 using erminas.SmartAPI.Utils;
22 
23 namespace erminas.SmartAPI.CMS.Project.ContentClasses
24 {
25  //TODO templatevariant auf attributes umstellen
27  {
28 
29  new string Name { get; set; }
33  void AssignToProjectVariant(IProjectVariant variant, bool doNotPublish, bool doNotUseTidy);
34 
37  bool ContainsAreaMarksInPage { get; set; }
38 
39  IContentClass ContentClass { get; }
40 
45  void CopyToContentClass(IContentClass target);
46 
50  DateTime CreationDate { get; }
51 
55  IUser CreationUser { get; }
56 
60  string Data { get; set; }
61 
66  void Commit();
67 
71  string Description { get; set; }
72 
73  string FileExtension { get; set; }
74  IRedDotObject Handle { get; }
75  bool HasContainerPageReference { get; set; }
76  bool IsLocked { get; }
77 
81  bool IsStylesheetIncludedInHeader { get; set; }
82 
86  DateTime LastChangeDate { get; }
87 
91  IUser LastChangeUser { get; }
92 
94 
98  TemplateVariantState ReleaseStatus { get; }
99  }
100 
104  internal class TemplateVariant : PartialRedDotProjectObject, ITemplateVariant
105  {
106  private DateTime _changeDate;
107  private User _changeUser;
108  private User _createUser;
109  private DateTime _creationDate;
110  private string _data;
111  private string _description;
112  private string _fileExtension;
113  private bool _hasContainerPageReference;
114  private bool _isLocked;
115  private bool _isStylesheetIncluded;
116  private bool _noStartEndMarkers;
117  private PdfOrientation _pdfOrientation;
118  private TemplateVariantState _status;
119 
120  public TemplateVariant(IContentClass contentClass, Guid guid) : base(contentClass.Project, guid)
121  {
122  ContentClass = contentClass;
123  }
124 
125  internal TemplateVariant(IContentClass contentClass, XmlElement xmlElement)
126  : base(contentClass.Project, xmlElement)
127  {
128  ContentClass = contentClass;
129  LoadXml();
130  if (IsOnlyPartiallyInitialized(xmlElement))
131  {
132  IsInitialized = false;
133  }
134  }
135 
139  public void AssignToProjectVariant(IProjectVariant variant, bool doNotPublish, bool doNotUseTidy)
140  {
141  const string ASSIGN_PROJECT_VARIANT =
142  @"<TEMPLATE guid=""{0}""><TEMPLATEVARIANTS> <TEMPLATEVARIANT guid=""{1}"">
143  <PROJECTVARIANTS action=""assign""><PROJECTVARIANT donotgenerate=""{3}"" donotusetidy=""{4}"" guid=""{2}"" />
144  </PROJECTVARIANTS></TEMPLATEVARIANT></TEMPLATEVARIANTS></TEMPLATE>";
145 
146  ContentClass.Project.ExecuteRQL(string.Format(ASSIGN_PROJECT_VARIANT, ContentClass.Guid.ToRQLString(),
147  Guid.ToRQLString(), variant.Guid.ToRQLString(),
148  doNotPublish.ToRQLString(), doNotUseTidy.ToRQLString()));
149 
150  ContentClass.ProjectVariantAssignments.InvalidateCache();
151  }
152 
155  public bool ContainsAreaMarksInPage
156  {
157  get { return !LazyLoad(ref _noStartEndMarkers); }
158  set { _noStartEndMarkers = !value; }
159  }
160 
161  public IContentClass ContentClass { get; private set; }
162 
167  public void CopyToContentClass(IContentClass target)
168  {
169  const string ADD_TEMPLATE_VARIANT = @"<TEMPLATE action=""assign"" guid=""{0}"">
170  <TEMPLATEVARIANTS action=""addnew"">
171  <TEMPLATEVARIANT name=""{1}"" description=""{2}"" code=""{3}"" fileextension=""{4}"" insertstylesheetinpage=""{5}"" nostartendmarkers=""{6}"" containerpagereference=""{7}"" pdforientation=""{8}"">{3}</TEMPLATEVARIANT></TEMPLATEVARIANTS></TEMPLATE>";
172  XmlDocument xmlDoc =
173  target.Project.ExecuteRQL(
174  string.Format(ADD_TEMPLATE_VARIANT, target.Guid.ToRQLString(), HttpUtility.HtmlEncode(Name),
175  HttpUtility.HtmlEncode(Description), HttpUtility.HtmlEncode(Data),
176  HttpUtility.HtmlEncode(FileExtension), IsStylesheetIncludedInHeader.ToRQLString(),
177  ContainsAreaMarksInPage.ToRQLString(), HasContainerPageReference.ToRQLString(),
178  PdfOrientation), RqlType.SessionKeyInProject);
179  if (xmlDoc.DocumentElement.InnerText.Trim().Length == 0)
180  {
181  return;
182  }
183  string errorMsg = string.Format("Error during addition of template variant '{0}' to content class '{1}'.",
184  Name, target.Name);
185  //sometimes it's <IODATA><ERROR>Reason</ERROR></IODATA> and sometimes just <IODATA>ERROR</IODATA>
186  XmlNodeList errorElements = xmlDoc.GetElementsByTagName("ERROR");
187  if (errorElements.Count > 0)
188  {
189  throw new SmartAPIException(Session.ServerLogin,
190  errorMsg + string.Format(" Reason: {0}.", errorElements[0].FirstChild.Value));
191  }
192  throw new SmartAPIException(Session.ServerLogin, errorMsg);
193  }
194 
198  public DateTime CreationDate
199  {
200  get { return LazyLoad(ref _creationDate); }
201  }
202 
206  public IUser CreationUser
207  {
208  get { return LazyLoad(ref _createUser); }
209  }
210 
214  public string Data
215  {
216  get { return LazyLoad(ref _data); }
217  set
218  {
219  const string SAVE_DATA =
220  @"<TEMPLATE action=""save"" guid=""{0}""><TEMPLATEVARIANT guid=""{1}"">{2}</TEMPLATEVARIANT></TEMPLATE>";
221  XmlDocument result =
222  ContentClass.Project.ExecuteRQL(
223  String.Format(SAVE_DATA, ContentClass.Guid.ToRQLString(), Guid.ToRQLString(),
224  HttpUtility.HtmlEncode(value)), RqlType.SessionKeyInProject);
225  if (!result.DocumentElement.InnerText.Contains(ContentClass.Guid.ToRQLString()))
226  {
227  var e =
228  new Exception("Could not save templatevariant '" + Name + "' for content class '" +
229  ContentClass.Name);
230  e.Data.Add("query_result", result);
231  }
232  _data = value;
233  }
234  }
235 
236  public void Delete()
237  {
238  const string DELETE_TEMPLATE =
239  @"<TEMPLATE><TEMPLATEVARIANTS><TEMPLATEVARIANT action=""delete"" guid=""{0}""/></TEMPLATEVARIANTS></TEMPLATE>";
240  try
241  {
242  Project.ExecuteRQL(DELETE_TEMPLATE.RQLFormat(this), RqlType.SessionKeyInProject);
243  } catch (RQLException e)
244  {
245  throw new SmartAPIException(Session.ServerLogin,
246  string.Format(
247  "Could not delete template variant {0} from content class {1}: {2}",
248  this, ContentClass, e.Message), e);
249  }
250  }
251 
252  public void Commit()
253  {
254  const string SAVE_DATA =
255  @"<TEMPLATE action=""save"" guid=""{0}""><TEMPLATEVARIANT guid=""{1}"" name=""{2}"" fileextension=""{3}"" insertstylesheetinpage=""{4}"" nostartendmarkers=""{5}"" containerpagereference=""{6}"" pdforientation=""{7}""/></TEMPLATE>";
256 
257  Project.ExecuteRQL(
258  SAVE_DATA.SecureRQLFormat(
259  ContentClass,
260  this,
261  Name,
262  FileExtension ?? RQL.SESSIONKEY_PLACEHOLDER,
263  IsStylesheetIncludedInHeader,
264  _noStartEndMarkers,
265  HasContainerPageReference,
266  PdfOrientation.ToRQLString()));
267 
268 
269  Refresh();
270  }
271 
275  public string Description
276  {
277  get { return LazyLoad(ref _description); }
278  set { _description = value; }
279  }
280 
281  public new string Name { get { return base.Name; } set { base.Name = value; } }
282 
283  public string FileExtension
284  {
285  get { return LazyLoad(ref _fileExtension); }
286  set { _fileExtension = value; }
287  }
288 
289  public IRedDotObject Handle
290  {
291  get { return new RedDotObjectHandle(Guid, Name); }
292  }
293 
294  public bool HasContainerPageReference
295  {
296  get { return LazyLoad(ref _hasContainerPageReference); }
297  set { _hasContainerPageReference = value; }
298  }
299 
300  public bool IsLocked
301  {
302  get { return LazyLoad(ref _isLocked); }
303  }
304 
308  public bool IsStylesheetIncludedInHeader
309  {
310  get { return LazyLoad(ref _isStylesheetIncluded); }
311  set { _isStylesheetIncluded = value; }
312  }
313 
317  public DateTime LastChangeDate
318  {
319  get { return LazyLoad(ref _changeDate); }
320  }
321 
325  public IUser LastChangeUser
326  {
327  get { return LazyLoad(ref _changeUser); }
328  }
329 
331  {
332  get { return LazyLoad(ref _pdfOrientation); }
333  set { _pdfOrientation = value; }
334  }
335 
339  public TemplateVariantState ReleaseStatus
340  {
341  get { return LazyLoad(ref _status); }
342  }
343 
344  protected override void LoadWholeObject()
345  {
346  LoadXml();
347  }
348 
349  protected override XmlElement RetrieveWholeObject()
350  {
351  const string LOAD_TEMPLATEVARIANT =
352  @"<TEMPLATE><TEMPLATEVARIANT action=""load"" readonly=""1"" guid=""{0}"" /></TEMPLATE>";
353  XmlDocument xmlDoc = ContentClass.Project.ExecuteRQL(string.Format(LOAD_TEMPLATEVARIANT, Guid.ToRQLString()));
354 
355  return (XmlElement) xmlDoc.GetElementsByTagName("TEMPLATEVARIANT")[0];
356  }
357 
358  private static bool IsOnlyPartiallyInitialized(XmlElement xmlElement)
359  {
360  return xmlElement.GetAttributeNode("pdforientation") == null;
361  }
362 
363  private void LoadXml()
364  {
365  if (!String.IsNullOrEmpty(_xmlElement.InnerText))
366  {
367  _data = _xmlElement.InnerText;
368  }
369  InitIfPresent(ref _creationDate, "createdate", XmlUtil.ToOADate);
370  InitIfPresent(ref _changeDate, "changeddate", XmlUtil.ToOADate);
371  InitIfPresent(ref _description, "description", x => x);
372  InitIfPresent(ref _createUser, "createuserguid",
373  x =>
374  new User(ContentClass.Project.Session, Guid.Parse(x))
375  {
376  Name = _xmlElement.GetAttributeValue("createusername")
377  });
378  InitIfPresent(ref _changeUser, "changeduserguid",
379  x =>
380  new User(ContentClass.Project.Session, Guid.Parse(x))
381  {
382  Name = _xmlElement.GetAttributeValue("changedusername")
383  });
384  InitIfPresent(ref _fileExtension, "fileextension", x => x);
385  InitIfPresent(ref _pdfOrientation, "pdforientation", PdfOrientationUtils.ToPdfOrientation);
386  InitIfPresent(ref _isStylesheetIncluded, "insertstylesheetinpage", BoolConvert);
387  InitIfPresent(ref _noStartEndMarkers, "nostartendmarkers", BoolConvert);
388  InitIfPresent(ref _isLocked, "lock", BoolConvert);
389  InitIfPresent(ref _hasContainerPageReference, "containerpagereference", BoolConvert);
390  if (BoolConvert(_xmlElement.GetAttributeValue("draft")))
391  {
392  _status = TemplateVariantState.Draft;
393  }
394  else
395  {
396  _status = BoolConvert(_xmlElement.GetAttributeValue("waitforrelease"))
397  ? TemplateVariantState.WaitsForRelease
398  : TemplateVariantState.Released;
399  }
400  }
401  }
402 
407  {
408  Draft,
409  WaitsForRelease,
410  Released
411  }
412 }