17 using System.Collections.Generic;
20 using erminas.SmartAPI.CMS.Project.ContentClasses;
21 using erminas.SmartAPI.CMS.Project.Folder;
22 using erminas.SmartAPI.CMS.Project.Pages;
23 using erminas.SmartAPI.CMS.Project.Pages.Elements;
24 using erminas.SmartAPI.Exceptions;
25 using erminas.SmartAPI.Utils;
26 using erminas.SmartAPI.Utils.CachedCollections;
28 namespace erminas.SmartAPI.CMS.Project
32 IEnumerable<ISessionObject> Content {
get; }
34 IEnumerable<IClipboardEntry> ClipboardEntries {
get; }
39 new IEnumerable<IProjectObject> Content {
get; }
41 IEnumerable<IPage>
Pages {
get; }
43 IEnumerable<IFolder> Folders {
get; }
45 IEnumerable<IPageElement> PageElements {
get; }
47 void Add(params
IPage[] page);
49 void Add(params
IFolder[] folder);
55 void Remove(params
IPage[] page);
57 void Remove(params
IFolder[] folder);
73 public Guid Guid {
get;
internal set; }
75 public String Type {
get;
internal set; }
78 internal class ProjectClipboard : IProjectClipboard,
ICached
80 private const string SINGLE_ENTRY =
@"<DATA guid=""{0}"" type=""{1}"" />";
81 private readonly IProject _project;
82 private List<ClipboardEntry> _clipboardEntries;
85 internal ProjectClipboard(IProject project)
90 public void InvalidateCache()
92 _clipboardEntries = null;
98 EnsureContentIsLoaded();
101 public ISession Session
103 get {
return _project.Session; }
106 public IEnumerable<IProjectObject> Content
110 EnsureContentIsLoaded();
115 public IEnumerable<IClipboardEntry> ClipboardEntries
119 EnsureContentIsLoaded();
120 return _clipboardEntries.AsReadOnly(); }
123 IEnumerable<ISessionObject> IClipboard.Content
125 get {
return _content; }
128 public IProject Project
130 get {
return _project; }
133 public IEnumerable<IPage>
Pages
137 return Content.Where(x => x is
IPage)
142 public IEnumerable<IFolder> Folders
146 return Content.Where(x => x is
IFolder)
151 public IEnumerable<IPageElement> PageElements
156 .Cast<IPageElement>();
160 public void Add(params
IPage[] pages)
162 GenericAdd(pages,
"page");
165 public void Add(params
IFolder[] folders)
167 GenericAdd(folders.Where(IS_SUB_FOLDER).ToArray(),
"project.6055");
168 GenericAdd(folders.Where(x => !IS_SUB_FOLDER(x)).ToArray(),
"project.6050");
171 private static readonly Func<IFolder, bool> IS_SUB_FOLDER = x=>x.IsAssetManager && ((
IAssetManagerFolder)x).IsSubFolder;
176 GenericAdd(contentClasses,
"app.4015");
181 GenericAdd(pageElements,
"pageelement");
184 public void Remove(params
IPage[] pages)
186 GenericRemove(pages,
"page");
189 public void Remove(params
IFolder[] folders)
191 GenericRemove(folders.Where(IS_SUB_FOLDER).ToArray(),
"project.6055");
192 GenericRemove(folders.Where(x => !IS_SUB_FOLDER(x)).ToArray(),
"project.6050");
197 GenericRemove(contentClasses,
"app.4015");
202 GenericRemove(pageElements,
"pageelement");
205 private void EnsureContentIsLoaded()
207 if (_clipboardEntries != null)
212 const string LOAD_CLIPBOARD =
213 @"<ADMINISTRATION><USER guid=""{0}""><CLIPBOARDDATA action=""load"" projectguid=""{1}"" foraspx=""1"" /></USER></ADMINISTRATION>";
216 Project.ExecuteRQL(LOAD_CLIPBOARD.RQLFormat(Session.CurrentUser, Project, Project.LanguageVariants.Current.Abbreviation));
218 var entries = doc.GetElementsByTagName(
"DATA");
219 _clipboardEntries = entries.Cast<XmlElement>().Select(x =>
new ClipboardEntry{Guid = x.GetGuid(), Type = x.GetAttributeValue(
"type")}).ToList();
221 _content = _clipboardEntries.Select(CreateContentEntry)
222 .Where(x => x != null)
226 private IProjectObject CreateContentEntry(IClipboardEntry clipboardEntry)
228 switch (clipboardEntry.Type)
231 return new Page(Project, clipboardEntry.Guid, Project.LanguageVariants.Current);
235 return new ContentClass(Project, clipboardEntry.Guid);
238 return Project.Folders.TryGetByGuid(clipboardEntry.Guid, out folder) ? folder : null;
241 return Project.Folders.AllIncludingSubFolders.TryGetByGuid(clipboardEntry.Guid, out subFolder) ? subFolder : null;
247 private void GenericAdd(IRedDotObject[] entries,
string type)
249 if (entries == null || entries.Length == 0)
254 const string ADD_ENTRY =
255 @"<ADMINISTRATION><USER guid=""{0}""><CLIPBOARDDATA action=""add"" projectguid=""{1}"">{2}</CLIPBOARDDATA></USER></ADMINISTRATION>";
257 var entryStr =
string.Join(
"", entries.Select(x => SINGLE_ENTRY.RQLFormat(x, type)));
259 var doc = Session.ExecuteRQL(ADD_ENTRY.RQLFormat(Session.CurrentUser, Project, entryStr), RQL.IODataFormat.SessionKeyAndLogonGuid);
269 private void GenericRemove(IProjectObject[] entries,
string type)
271 if (entries == null || entries.Length == 0)
276 const string REMOVE_ENTRY =
277 @"<ADMINISTRATION><USER guid=""{0}""><CLIPBOARDDATA action=""remove"" projectguid=""{1}"">{2}</CLIPBOARDDATA></USER></ADMINISTRATION>";
279 var entryStr =
string.Join(
"", entries.Select(x => SINGLE_ENTRY.RQLFormat(x, type)));
281 var doc = Project.ExecuteRQL(REMOVE_ENTRY.RQLFormat(Session.CurrentUser, Project, entryStr));
283 if (!doc.InnerText.Contains(
"ok"))
285 throw new SmartAPIException(Session.ServerLogin,
"Error removing entries from clipboard");