SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
AbstractMediaElement.cs
Go to the documentation of this file.
1 ï»¿// SmartAPI - .Net programmatic access to RedDot servers
2 //
3 // Copyright (C) 2013 erminas GbR
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free Software Foundation,
7 // either version 3 of the License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 // See the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program.
14 // If not, see <http://www.gnu.org/licenses/>.
15 
16 using System;
17 using System.Linq;
18 using System.Web;
19 using System.Xml;
20 using erminas.SmartAPI.CMS.Project.Folder;
21 using erminas.SmartAPI.Utils;
22 
23 namespace erminas.SmartAPI.CMS.Project.Pages.Elements
24 {
25  public interface IMediaElementBase : IValueElement<IFile>
26  {
27  }
28 
29  internal abstract class AbstractMediaElement : PageElement, IMediaElementBase
30  {
31  private IFile _file;
32 
33  protected AbstractMediaElement(IProject project, Guid guid, ILanguageVariant languageVariant)
34  : base(project, guid, languageVariant)
35  {
36  }
37 
38  protected AbstractMediaElement(IProject project, XmlElement xmlElement) : base(project, xmlElement)
39  {
40  LoadWholePageElement();
41  }
42 
43  public void Commit()
44  {
45  const string COMMIT =
46  @"<ELT action=""save"" reddotcacheguid="""" guid=""{0}"" value=""{1}"" folderguid=""{3}"" {2} extendedinfo=""""></ELT>";
47 
48 
49  if (Value == null)
50  {
51  //TODO evtl. folderguid setzen?
52  Project.ExecuteRQL(string.Format(COMMIT, Guid.ToRQLString(), RQL.SESSIONKEY_PLACEHOLDER, "", ""));
53  return;
54  }
55 
56  var isInSubFolder = !Project.Folders.ContainsGuid(Value.Folder.Guid);
57 
58  var rqlStr = COMMIT.RQLFormat(this, HttpUtility.HtmlEncode(Value.Name),
59  isInSubFolder ? "subdirguid=\"{0}\"".RQLFormat(Value.Folder) : "",
60  isInSubFolder ? ((IAssetManagerFolder)Value.Folder).ParentFolder : Value.Folder);
61 
62  Project.ExecuteRQL(rqlStr);
63  }
64 
65  public IFile Value
66  {
67  get { return LazyLoad(ref _file); }
68  set { _file = value; }
69  }
70 
71  protected override sealed void LoadWholePageElement()
72  {
73  var folder = GetFolder();
74  if (folder == null)
75  {
76  return;
77  }
78  InitFileValue(folder);
79  }
80 
81  private IFolder GetFolder()
82  {
83  Guid folderGuid;
84  if (!XmlElement.TryGetGuid("folderguid", out folderGuid))
85  {
86  _file = null;
87  return null;
88  }
89 
90  Guid subFolderGuid;
91  var folderList = Project.Folders.AllIncludingSubFolders;
92  return XmlElement.TryGetGuid("subdirguid", out subFolderGuid)
93  ? folderList.FirstOrDefault(folder => folder.Guid == subFolderGuid)
94  : folderList.FirstOrDefault(folder => folder.Guid == folderGuid);
95  }
96 
97  private void InitFileValue(IFolder folder)
98  {
99  var fileName = XmlElement.GetAttributeValue("value");
100  if (string.IsNullOrEmpty(fileName))
101  {
102  _file = null;
103  return;
104  }
105 
106  var files = folder.Files.GetByNamePattern(fileName);
107  _file = files.FirstOrDefault(file => file.Name == fileName);
108  }
109 
110  private bool IsFileInSubFolder
111  {
112  get
113  {
114  EnsureInitialization();
115  if (Value == null)
116  {
117  return false;
118  }
119  var folderGuid = XmlElement.GetGuid("folderguid");
120  return folderGuid != Value.Folder.Guid;
121  }
122  }
123  }
124 }