17 using System.Collections.Generic;
20 using erminas.SmartAPI.Utils;
21 using erminas.SmartAPI.Utils.CachedCollections;
23 namespace erminas.SmartAPI.CMS.Project.Pages.Elements
31 internal class LinkConnections : RDList<IPage>, ILinkConnections
33 private readonly ILinkElement _element;
34 private bool _isNeedingRefresh;
36 protected internal LinkConnections(ILinkElement element,
Caching caching) : base(caching)
39 RetrieveFunc = GetConnectedPages;
54 public void Connect(IPage page)
59 public bool IsReference
61 get {
return _element.LinkType ==
LinkType.Reference; }
64 public bool IsReferencing
73 if (_isNeedingRefresh)
76 _isNeedingRefresh =
false;
78 return _element.LinkType;
82 public IProject Project
84 get {
return _element.Project; }
87 public ILinkTarget Reference
89 get {
throw new NotImplementedException(); }
90 set { SetReference(value); }
93 public void Remove(IPage page)
95 RemoveRange(
new[] {page});
98 public ISession Session
100 get {
return _element.Session; }
103 public virtual void Set(ILinkTarget target,
Linking linking)
105 if (linking ==
Linking.AsReference)
111 SaveConnection((IPage) target);
115 protected void RemoveRange(IEnumerable<IPage> pages)
117 const string DISCONNECT_PAGES =
@"<LINK action=""save"" guid=""{0}""><PAGES>{1}</PAGES></LINK>";
118 const string SINGLE_PAGE =
@"<PAGE deleted=""1"" guid=""{0}"" />";
120 var pagesStr = pages.Aggregate(
"", (x, page) => x +
string.Format(SINGLE_PAGE, (page.Guid).ToRQLString()));
121 Project.ExecuteRQL(DISCONNECT_PAGES.RQLFormat(_element, pagesStr));
125 protected void SaveConnection(IPage page)
127 const string CONNECT_PREPARE =
128 @"<LINK action=""save"" reddotcacheguid="""" guid=""{0}"" value=""" + RQL.SESSIONKEY_PLACEHOLDER +
131 Project.ExecuteRQL(CONNECT_PREPARE.RQLFormat(_element));
133 const string CONNECT =
134 @"<LINKSFROM action=""save"" pageid="""" pageguid=""{0}"" reddotcacheguid=""""><LINK guid=""{1}""/></LINKSFROM>";
136 Project.ExecuteRQL(CONNECT.RQLFormat(page, _element));
141 _isNeedingRefresh =
true;
145 private List<IPage> GetConnectedPages()
147 const string LIST_LINKED_PAGES =
@"<LINK guid=""{0}""><PAGES action=""list"" /></LINK>";
148 var xmlDoc = Project.ExecuteRQL(LIST_LINKED_PAGES.RQLFormat(_element));
149 return (from XmlElement curPage in xmlDoc.GetElementsByTagName(
"PAGE")
152 new Page(Project, curPage.GetGuid(), _element.LanguageVariant)
154 Id = curPage.GetIntAttributeValue(
"id").GetValueOrDefault(),
155 InitialHeadlineValue = curPage.GetAttributeValue(
"headline")
157 select page).ToList();
160 private void ReferenceElement(ILinkElement element)
162 const string LINK_TO_ELEMENT =
163 @"<PAGE><LINK action=""assign"" guid=""{0}""><LINK guid=""{1}"" /></LINK></PAGE>";
165 Project.ExecuteRQL(LINK_TO_ELEMENT.RQLFormat(_element, element));
170 _isNeedingRefresh =
true;
174 private void ReferencePage(IPage target)
176 const string LINK_TO_PAGE =
177 @"<PAGE><LINK action=""reference"" guid=""{0}""><PAGE guid=""{1}"" /></LINK></PAGE>";
179 Project.ExecuteRQL(LINK_TO_PAGE.RQLFormat(_element, target));
184 _isNeedingRefresh =
true;
188 private void SetReference(ILinkTarget target)
198 ReferencePage((IPage) target);
202 ReferenceElement((ILinkElement) target);
207 private void UnlinkReference()
209 const string UNLINK_ELEMENT =
210 @"<LINK guid=""{0}""><LINK action=""unlink"" reddotcacheguid=""""/><URL action=""unlink""/></LINK>";
212 Project.ExecuteRQL(UNLINK_ELEMENT.RQLFormat(_element));
215 _isNeedingRefresh =
true;
222 void Connect(IPage page);
224 bool IsReferencing {
get; }
227 void Remove(IPage page);