SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
ReferencePreassignment.cs
Go to the documentation of this file.
1 ï»¿using System;
2 using erminas.SmartAPI.CMS.Project.Pages;
3 using erminas.SmartAPI.Exceptions;
4 using erminas.SmartAPI.Utils;
5 
6 namespace erminas.SmartAPI.CMS.Project.ContentClasses.Elements
7 {
8  internal class ReferencePreassignment
9  {
10  private readonly IReferencePreassignable _parent;
11  private bool _isInitialized;
12  private IReferencePreassignTarget _preassignTarget;
13 
14  public ReferencePreassignment(IReferencePreassignable parent)
15  {
16  _parent = parent;
17  }
18 
19  public IReferencePreassignTarget Target
20  {
21  get
22  {
23  if (_isInitialized)
24  {
25  return _preassignTarget;
26  }
27 
28  var parent = (ContentClassElement) _parent;
29  parent.EnsureInitialization();
30  _isInitialized = true;
31 
32  var referenceGuidStr = parent.XmlReadWriteWrapper.GetAttributeValue("eltrefelementguid");
33  Guid referenceGuid;
34  if (Guid.TryParse(referenceGuidStr, out referenceGuid))
35  {
36  var type = parent.XmlReadWriteWrapper.GetAttributeValue("eltrefelementtype");
37  if (type == "PAG")
38  {
39  IPage targetPage;
40  _preassignTarget = parent.Project.Pages.TryGetByGuid(
41  referenceGuid,
42  parent.Project.LanguageVariants.Current,
43  out targetPage)
44  ? targetPage
45  : null;
46  return _preassignTarget;
47  }
48 
49  IContentClassElement ccOut;
50  if (parent.ContentClass.Elements.TryGetByGuid(referenceGuid, out ccOut))
51  {
52  _preassignTarget = (IReferencePreassignTarget) ccOut;
53  return _preassignTarget;
54  }
55  }
56  _preassignTarget = null;
57 
58  return _preassignTarget;
59  }
60  set
61  {
62  RemovePreassignedTarget();
63  if (value != null)
64  {
65  SetPreassignedTarget(value);
66  _preassignTarget = value;
67  }
68  }
69  }
70 
71  private void SetPreassignedTarget(IReferencePreassignTarget value)
72  {
73  const string SET_TARGET =
74  @"<CLIPBOARD action=""ReferenceToPage"" guid=""{0}"" type=""project.4145"" descent=""unknown"" addition="""">
75 <ENTRY guid=""{1}"" type=""page"" descent=""unknown"" />
76 </CLIPBOARD>";
77  var result = _parent.Project.ExecuteRQL(SET_TARGET.RQLFormat(_parent, value));
78  if (!result.InnerText.Contains("ok"))
79  {
80  throw new SmartAPIException(
81  _parent.Project.Session.ServerLogin,
82  string.Format("Could not set reference on {0} to {1}", _parent, value));
83  }
84  }
85 
86  private void RemovePreassignedTarget()
87  {
88  if (Target == null)
89  {
90  return;
91  }
92 
93  const string REMOVE_TARGET =
94  @"<TEMPLATE><ELEMENT action=""unlink"" guid=""{0}""><ELEMENT guid=""{1}""/></ELEMENT></TEMPLATE>";
95  _parent.Project.ExecuteRQL(REMOVE_TARGET.RQLFormat(_parent, Target));
96  _isInitialized = false;
97  ((ContentClassElement) _parent).IsInitialized = false;
98  }
99 
100  public void InvalidateCache()
101  {
102  _isInitialized = false;
103  }
104  }
105 }