17 using System.Collections.Generic;
21 using erminas.SmartAPI.CMS.Project.ContentClasses;
22 using erminas.SmartAPI.Exceptions;
23 using erminas.SmartAPI.Utils;
24 using erminas.SmartAPI.Utils.CachedCollections;
26 namespace erminas.SmartAPI.CMS.Project.Pages
40 IndexedRDList<int, IPage>
this[
string language] {
get; }
48 IndexedRDList<int, IPage> OfCurrentLanguage {
get; }
103 IEnumerable<IPage> Search(Action<IPageSearch> configurator = null);
124 List<ResultGroup> SearchExtended(Action<IExtendedPageSearch> configurator = null);
129 private readonly Dictionary<string, IndexedRDList<int, IPage>> _pagesByLanguage =
130 new Dictionary<string, IndexedRDList<int, IPage>>();
138 _startPage =
new StartPagesValue(project);
143 get {
return _startPage; }
148 return new Page(_project, pageGuid, languageVariant).
Refreshed();
155 page = GetByGuid(pageGuid, languageVariant);
173 XmlDocument xmlDoc = _project.ExecuteRQL(PageCreationString(cc, headline));
174 return CreatePageFromCreationReply(xmlDoc);
186 const string CREATE_AND_LINK_PAGE =
@"<LINK action=""assign"" guid=""{0}"">{1}</LINK>";
188 _project.ExecuteRQL(
string.Format(CREATE_AND_LINK_PAGE, linkGuid.ToRQLString(), PageCreationString(cc, headline)));
189 return CreatePageFromCreationReply(xmlDoc);
198 return new ExtendedPageSearch(_project);
207 return new PageSearch(_project);
213 public IndexedRDList<int, IPage>
this[
string language]
215 get {
return GetPagesForLanguageVariant(language); }
221 public IndexedRDList<int, IPage> OfCurrentLanguage
223 get {
return GetPagesForLanguageVariant(_project.LanguageVariants.Current.Abbreviation); }
236 public IEnumerable<IPage> Search(Action<IPageSearch> configurator = null)
238 var search =
new PageSearch(_project);
239 if (configurator != null)
241 configurator(search);
244 return search.Execute();
266 public List<ResultGroup> SearchExtended(Action<IExtendedPageSearch> configurator = null)
268 var search =
new ExtendedPageSearch(_project);
269 if (configurator != null)
271 configurator(search);
274 return search.Execute();
277 private IPage CreatePageFromCreationReply(XmlDocument xmlDoc)
281 var pageItem = (XmlElement) xmlDoc.GetElementsByTagName(
"PAGE")[0];
282 return new Page(_project, pageItem);
286 throw new SmartAPIException(_project.Session.ServerLogin,
"Could not create page", e);
290 private List<IPage> GetPages()
292 const string LIST_PAGES =
@"<PROJECT><PAGES action=""list""/></PROJECT>";
293 XmlDocument xmlDoc = _project.ExecuteRQL(LIST_PAGES);
294 return (from XmlElement curPage in xmlDoc.GetElementsByTagName(
"PAGE")
295 select (IPage) new Page(_project, curPage.GetGuid(), _project.LanguageVariants.Current)
297 InitialHeadlineValue = curPage.GetAttributeValue(
"headline"),
298 Id = curPage.GetIntAttributeValue(
"id")
303 private IndexedRDList<int, IPage> GetPagesForLanguageVariant(
string language)
305 ILanguageVariant languageVariant = _project.LanguageVariants[language];
308 return _pagesByLanguage.GetOrAdd(
310 () =>
new IndexedRDList<int, IPage>(
323 private static string PageCreationString(
IContentClass cc,
string headline = null)
325 const string PAGE_CREATION_STRING =
@"<PAGE action=""addnew"" templateguid=""{0}"" {1}/>";
327 string headlineString = headline == null ?
"" :
string.Format(
@"headline=""{0}""", HttpUtility.HtmlEncode(headline));
328 return string.Format(PAGE_CREATION_STRING, cc.
Guid.ToRQLString(), headlineString);
334 internal class StartPagesValue : IStartPagesValue
336 private readonly IProject _project;
337 private readonly Dictionary<ILanguageVariant, IIndexedRDList<string, IPage>> _startPagesByLanguageVariant =
new Dictionary<ILanguageVariant, IIndexedRDList<string, IPage>>();
338 private Guid[] _startPageGuids;
340 public StartPagesValue(IProject project)
345 public IPartialRedDotProjectObject Parent
349 throw new InvalidOperationException(
"StartPage has no parent");
353 public IIndexedRDList<string, IPage> ForCurrentLanguage
355 get {
return ((ILanguageDependentReadValue < IIndexedRDList<string, IPage>>)(
this))[_project.LanguageVariants.Current]; }
358 public IIndexedRDList<string, IPage> ForMainLanguage
360 get {
return ((ILanguageDependentReadValue<IIndexedRDList<string, IPage>>)(
this))[_project.LanguageVariants.Main]; }
363 IIndexedRDList<string, IPage> ILanguageDependentReadValue<IIndexedRDList<string, IPage>>.this[ILanguageVariant languageVariant]
368 return _startPagesByLanguageVariant.GetOrAdd(languageVariant, ()=>GetStartPages(languageVariant));
372 IIndexedRDList<string, IPage> ILanguageDependentReadValue<IIndexedRDList<string, IPage>>.this[
string languageAbbreviation]
374 get {
return ((ILanguageDependentReadValue<IIndexedRDList<string, IPage>>)(
this))[_project.LanguageVariants[languageAbbreviation]]; }
377 private IIndexedRDList<string, IPage> GetStartPages(ILanguageVariant languageVariant)
379 return new IndexedRDList<string, IPage>(()=>Refreshed(languageVariant), x=>x.Headline,
Caching.Enabled);
382 private List<IPage> Refreshed(ILanguageVariant languageVariant)
385 return _startPageGuids.Select(x => _project.Pages.GetByGuid(x, languageVariant)).ToList();
388 private void EnsureInitialization()
390 if (_startPageGuids == null)
392 const string LOAD_DINGS =
@"<LINK guid=""{0}""><PAGES action=""list"" /></LINK>";
393 var doc = _project.ExecuteRQL(LOAD_DINGS.RQLFormat(
"00000000000000000000000000000001"));
395 var pageElements = doc.GetElementsByTagName(
"PAGE");
396 _startPageGuids = pageElements.Cast<XmlElement>()
397 .Select(XmlUtil.GetGuid)
402 public void InvalidateCache()
404 _startPageGuids = null;
405 _startPagesByLanguageVariant.Clear();
408 public void Refresh()
411 EnsureInitialization();