SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
ExampleFileConverter.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.Xml;
19 using erminas.SmartAPI.CMS.Project;
20 using erminas.SmartAPI.CMS.Project.Folder;
21 using erminas.SmartAPI.Exceptions;
22 using erminas.SmartAPI.Utils;
23 
24 namespace erminas.SmartAPI.CMS.Converter
25 {
26  internal class ExampleFileConverter : IAttributeConverter<IFile>
27  {
28  private const string ELTSRC = "eltrdexample";
29  private const string ELTFOLDERGUID = "eltfolderguid";
30  private const string ELTSRCSUBDIRGUID = "eltrdexamplesubdirguid";
31 
32  public IFile ConvertFrom(IProjectObject parent, XmlElement readElement, RedDotAttribute attribute)
33  {
34  if (parent == null)
35  {
36  throw new SmartAPIInternalException("Parent project object may not be null for ExampleFileConverter");
37  }
38 
39  var fileName = readElement.GetAttributeValue(ELTSRC);
40 
41  if (string.IsNullOrEmpty(fileName))
42  {
43  return null;
44  }
45 
46  var folder = GetFolder(parent, readElement);
47 
48  return new File(folder, fileName);
49  }
50 
51  public bool IsReadOnly
52  {
53  get { return false; }
54  }
55 
56  public void WriteTo(IProjectObject parent, IXmlReadWriteWrapper element, RedDotAttribute attribute, IFile value)
57  {
58  if (parent == null)
59  {
60  throw new SmartAPIInternalException("Parent project object may not be null for ExampleFileConverter");
61  }
62 
63  if (value == null)
64  {
65  ClearFile(element);
66  return;
67  }
68 
69  if (!element.IsAttributeSet(parent, ELTFOLDERGUID))
70  {
71  throw new SmartAPIException(parent.Session.ServerLogin,
72  string.Format(
73  "Cannot set an example file ({0}) without an active folder in element {1}",
74  value, parent));
75  }
76 
77  if (ConverterHelper.AreFromTheSameProject(parent, value))
78  {
79  SetFromSameProject(parent, element, value);
80  }
81  else
82  {
83  if (value.Folder.IsAssetManager)
84  {
85  SetValuesFromAssetManagerFolder(parent, element, value);
86  }
87  else
88  {
89  var ownFolder = parent.Project.Folders.GetByName(value.Folder.Name);
90  SetValuesFromTopLevelFolder(parent, element, value, ownFolder);
91  }
92  }
93  }
94 
95  private static void ClearFile(IXmlReadWriteWrapper element)
96  {
97  element.SetAttributeValue(ELTSRCSUBDIRGUID, null);
98  element.SetAttributeValue(ELTSRC, null);
99  }
100 
101  private static IFolder GetFolder(IProjectObject parent, XmlElement readElement)
102  {
103  Guid folderGuid;
104  if (!readElement.TryGetGuid(ELTSRCSUBDIRGUID, out folderGuid))
105  {
106  folderGuid = readElement.GetGuid(ELTFOLDERGUID);
107  }
108 
109  var folder = parent.Project.Folders.AllIncludingSubFolders.GetByGuid(folderGuid);
110  return folder;
111  }
112 
113  private static void SetFilename(IProjectObject parent, IFile value, IXmlReadWriteWrapper element,
114  IFolder ownFolder)
115  {
116  var ownFile = ownFolder.Files.GetByNamePattern(value.Name).SingleOrDefault();
117  if (ownFile == null)
118  {
119  throw new SmartAPIException(parent.Session.ServerLogin,
120  string.Format("No file with name {0} found in folder {1} of project {2}",
121  value.Name, ownFolder.Name, parent.Project));
122  }
123  element.SetAttributeValue(ELTSRC, value.Name);
124  }
125 
126  private static void SetFromSameProject(IProjectObject parent, IXmlReadWriteWrapper element, IFile value)
127  {
128  var folderGuid = element.GetGuid(ELTFOLDERGUID);
129  var topLevelFolder = value.Folder;
130  if (value.Folder.IsAssetManager)
131  {
132  var assetFolder = (IAssetManagerFolder) value.Folder;
133  if (assetFolder.IsSubFolder)
134  {
135  topLevelFolder = assetFolder.ParentFolder;
136  }
137  }
138 
139  if (topLevelFolder.Guid != folderGuid)
140  {
141  throw new SmartAPIException(parent.Session.ServerLogin,
142  string.Format(
143  "Cannot set sample file '{0}', because it isn't in the current folder branch '{1}/'",
144  value, parent.Project.Folders.GetByGuid(folderGuid).Name));
145  }
146 
147  //TODO at least cms 7.5 stores undefined as value, maybe "" is allowed, too, try this out
148  element.SetAttributeValue(ELTSRCSUBDIRGUID,
149  value.Folder.Guid == folderGuid ? "undefined" : value.Folder.Guid.ToRQLString());
150 
151  element.SetAttributeValue(ELTSRC, value.Name);
152  }
153 
154  private static void SetValuesFromAssetManagerFolder(IProjectObject parent, IXmlReadWriteWrapper element,
155  IFile value)
156  {
157  var assetFolder = (IAssetManagerFolder) value.Folder;
158  var folderGuid = element.GetGuid(ELTFOLDERGUID);
159  if (assetFolder.IsSubFolder)
160  {
161  var folder = parent.Project.Folders.GetByGuid(folderGuid) as IAssetManagerFolder;
162  if (folder == null)
163  {
164  throw new SmartAPIException(parent.Session.ServerLogin,
165  string.Format(
166  "Setting a example file from a subfolder into a non asset manager folder is not possible"));
167  }
168  if (folder.Name != assetFolder.ParentFolder.Name)
169  {
170  throw new SmartAPIException(parent.Session.ServerLogin,
171  string.Format(
172  "Example file not from subfolder of the currently active folder is not allowed (active folder: {0}, file folder {1})",
173  folder, value.Folder));
174  }
175 
176  var ownSubFolder = folder.SubFolders.GetByName(value.Folder.Name);
177  element.SetAttributeValue(ELTSRCSUBDIRGUID, ownSubFolder.Guid.ToRQLString());
178  SetFilename(parent, value, element, ownSubFolder);
179  }
180  else
181  {
182  IFolder ownFolder;
183  if (!parent.Project.Folders.TryGetByName(value.Folder.Name, out ownFolder))
184  {
185  throw new SmartAPIException(parent.Session.ServerLogin,
186  string.Format("Missing folder {1} in project {0}", value.Folder.Name,
187  parent.Project));
188  }
189  SetValuesFromTopLevelFolder(parent, element, value, ownFolder);
190  }
191  }
192 
193  private static void SetValuesFromTopLevelFolder(IProjectObject parent, IXmlReadWriteWrapper element, IFile value,
194  IFolder ownFolder)
195  {
196  var folderGuid = element.GetGuid(ELTFOLDERGUID);
197 
198  if (folderGuid != ownFolder.Guid)
199  {
200  throw new SmartAPIException(parent.Session.ServerLogin,
201  string.Format(
202  "Example file not from the folder or one of its subfolders is not allowed"));
203  }
204 
205  element.SetAttributeValue(ELTSRCSUBDIRGUID, ownFolder.Guid.ToRQLString());
206  SetFilename(parent, value, element, ownFolder);
207  }
208  }
209 }