19 using erminas.SmartAPI.CMS.ServerManagement;
20 using erminas.SmartAPI.Exceptions;
21 using erminas.SmartAPI.Utils;
23 namespace erminas.SmartAPI.CMS.Project.ContentClasses
29 new string Name {
get;
set; }
33 void AssignToProjectVariant(
IProjectVariant variant,
bool doNotPublish,
bool doNotUseTidy);
37 bool ContainsAreaMarksInPage {
get;
set; }
50 DateTime CreationDate {
get; }
60 string Data {
get;
set; }
71 string Description {
get;
set; }
73 string FileExtension {
get;
set; }
75 bool HasContainerPageReference {
get;
set; }
76 bool IsLocked {
get; }
81 bool IsStylesheetIncludedInHeader {
get;
set; }
86 DateTime LastChangeDate {
get; }
104 internal class TemplateVariant : PartialRedDotProjectObject, ITemplateVariant
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;
120 public TemplateVariant(IContentClass contentClass, Guid guid) : base(contentClass.Project, guid)
122 ContentClass = contentClass;
125 internal TemplateVariant(IContentClass contentClass, XmlElement xmlElement)
126 : base(contentClass.Project, xmlElement)
128 ContentClass = contentClass;
130 if (IsOnlyPartiallyInitialized(xmlElement))
132 IsInitialized =
false;
139 public void AssignToProjectVariant(IProjectVariant variant,
bool doNotPublish,
bool doNotUseTidy)
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>";
146 ContentClass.Project.ExecuteRQL(
string.Format(ASSIGN_PROJECT_VARIANT, ContentClass.Guid.ToRQLString(),
147 Guid.ToRQLString(), variant.Guid.ToRQLString(),
148 doNotPublish.ToRQLString(), doNotUseTidy.ToRQLString()));
150 ContentClass.ProjectVariantAssignments.InvalidateCache();
155 public bool ContainsAreaMarksInPage
157 get {
return !LazyLoad(ref _noStartEndMarkers); }
158 set { _noStartEndMarkers = !value; }
161 public IContentClass ContentClass {
get;
private set; }
167 public void CopyToContentClass(IContentClass target)
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>";
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(),
179 if (xmlDoc.DocumentElement.InnerText.Trim().Length == 0)
183 string errorMsg =
string.Format(
"Error during addition of template variant '{0}' to content class '{1}'.",
186 XmlNodeList errorElements = xmlDoc.GetElementsByTagName(
"ERROR");
187 if (errorElements.Count > 0)
190 errorMsg +
string.Format(
" Reason: {0}.", errorElements[0].FirstChild.Value));
198 public DateTime CreationDate
200 get {
return LazyLoad(ref _creationDate); }
206 public IUser CreationUser
208 get {
return LazyLoad(ref _createUser); }
216 get {
return LazyLoad(ref _data); }
219 const string SAVE_DATA =
220 @"<TEMPLATE action=""save"" guid=""{0}""><TEMPLATEVARIANT guid=""{1}"">{2}</TEMPLATEVARIANT></TEMPLATE>";
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()))
228 new Exception(
"Could not save templatevariant '" + Name +
"' for content class '" +
230 e.Data.Add(
"query_result", result);
238 const string DELETE_TEMPLATE =
239 @"<TEMPLATE><TEMPLATEVARIANTS><TEMPLATEVARIANT action=""delete"" guid=""{0}""/></TEMPLATEVARIANTS></TEMPLATE>";
242 Project.ExecuteRQL(DELETE_TEMPLATE.RQLFormat(
this),
RqlType.SessionKeyInProject);
247 "Could not delete template variant {0} from content class {1}: {2}",
248 this, ContentClass, e.
Message), e);
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>";
258 SAVE_DATA.SecureRQLFormat(
262 FileExtension ?? RQL.SESSIONKEY_PLACEHOLDER,
263 IsStylesheetIncludedInHeader,
265 HasContainerPageReference,
275 public string Description
277 get {
return LazyLoad(ref _description); }
278 set { _description = value; }
281 public new string Name {
get {
return base.Name; }
set { base.Name = value; } }
283 public string FileExtension
285 get {
return LazyLoad(ref _fileExtension); }
286 set { _fileExtension = value; }
289 public IRedDotObject Handle
294 public bool HasContainerPageReference
296 get {
return LazyLoad(ref _hasContainerPageReference); }
297 set { _hasContainerPageReference = value; }
302 get {
return LazyLoad(ref _isLocked); }
308 public bool IsStylesheetIncludedInHeader
310 get {
return LazyLoad(ref _isStylesheetIncluded); }
311 set { _isStylesheetIncluded = value; }
317 public DateTime LastChangeDate
319 get {
return LazyLoad(ref _changeDate); }
325 public IUser LastChangeUser
327 get {
return LazyLoad(ref _changeUser); }
332 get {
return LazyLoad(ref _pdfOrientation); }
333 set { _pdfOrientation = value; }
341 get {
return LazyLoad(ref _status); }
344 protected override void LoadWholeObject()
349 protected override XmlElement RetrieveWholeObject()
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()));
355 return (XmlElement) xmlDoc.GetElementsByTagName(
"TEMPLATEVARIANT")[0];
358 private static bool IsOnlyPartiallyInitialized(XmlElement xmlElement)
360 return xmlElement.GetAttributeNode(
"pdforientation") == null;
363 private void LoadXml()
365 if (!String.IsNullOrEmpty(_xmlElement.InnerText))
367 _data = _xmlElement.InnerText;
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",
374 new User(ContentClass.Project.Session, Guid.Parse(x))
376 Name = _xmlElement.GetAttributeValue(
"createusername")
378 InitIfPresent(ref _changeUser,
"changeduserguid",
380 new User(ContentClass.Project.Session, Guid.Parse(x))
382 Name = _xmlElement.GetAttributeValue(
"changedusername")
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")))
396 _status = BoolConvert(_xmlElement.GetAttributeValue(
"waitforrelease"))