SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
SrcFileConverter.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 SrcFileConverter : IAttributeConverter<IFile>
27  {
28  private const string ELTSRC = "eltsrc";
29  private const string ELTFOLDERGUID = "eltfolderguid";
30  private const string ELTSRCSUBDIRGUID = "eltsrcsubdirguid";
31 
32  public IFile ConvertFrom(IProjectObject parent, XmlElement element, RedDotAttribute attribute)
33  {
34  if (parent == null)
35  {
36  throw new SmartAPIInternalException("Parent project object may not be null for SrcFileConverter");
37  }
38 
39  var fileName = element.GetAttributeValue(ELTSRC);
40 
41  if (string.IsNullOrEmpty(fileName))
42  {
43  return null;
44  }
45 
46  var folder = GetFolder(parent, element);
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 SrcFileConverter");
61  }
62 
63  if (value == null)
64  {
65  ClearFile(element);
66  return;
67  }
68 
69  if (ConverterHelper.AreFromTheSameProject(parent, value))
70  {
71  SetFromSameProject(parent, element, value);
72  }
73  else
74  {
75  if (value.Folder.IsAssetManager)
76  {
77  SetValuesFromAssetManagerFolder(parent, element, value);
78  }
79  else
80  {
81  var ownFolder = parent.Project.Folders.GetByName(value.Folder.Name);
82  SetValuesFromTopLevelFolder(parent, element, value, ownFolder);
83  }
84  }
85  }
86 
87  private static void ClearFile(IXmlReadWriteWrapper element)
88  {
89  element.SetAttributeValue(ELTSRCSUBDIRGUID, null);
90  element.SetAttributeValue(ELTSRC, null);
91  }
92 
93  private static IFolder GetFolder(IProjectObject parent, XmlElement element)
94  {
95  Guid folderGuid;
96  if (!element.TryGetGuid(ELTSRCSUBDIRGUID, out folderGuid))
97  {
98  folderGuid = element.GetGuid(ELTFOLDERGUID);
99  }
100 
101  var folder = parent.Project.Folders.AllIncludingSubFolders.GetByGuid(folderGuid);
102  return folder;
103  }
104 
105  private static IAssetManagerFolder GetTopLevelFolder(IProjectObject parent, IAssetManagerFolder value)
106  {
107  var ownTopLevelFolder = parent.Project.Folders.GetByName(value.ParentFolder.Name) as IAssetManagerFolder;
108  if (ownTopLevelFolder == null)
109  {
110  throw new SmartAPIException(parent.Session.ServerLogin,
111  string.Format("No asset folder with name {0} found in project {1}",
112  value.ParentFolder.Name, parent.Project));
113  }
114  return ownTopLevelFolder;
115  }
116 
117  private static void SetFilename(IProjectObject parent, IFile value, IFolder ownFolder,
118  IXmlReadWriteWrapper writeElement)
119  {
120  var ownFile = ownFolder.Files.GetByNamePattern(value.Name).SingleOrDefault();
121  if (ownFile == null)
122  {
123  throw new SmartAPIException(parent.Session.ServerLogin,
124  string.Format("No file with name {0} found in folder {1} of project {2}",
125  value.Name, ownFolder.Name, parent.Project));
126  }
127 
128  writeElement.SetAttributeValue(ELTSRC, ownFile.Name);
129  }
130 
131  private static void SetFromSameProject(IProjectObject parent, IXmlReadWriteWrapper element, IFile value)
132  {
133  var folderGuid = element.GetGuid(ELTFOLDERGUID);
134 
135  var topLevelFolder = value.Folder;
136  if (value.Folder.IsAssetManager)
137  {
138  var assetFolder = (IAssetManagerFolder) value.Folder;
139  if (assetFolder.IsSubFolder)
140  {
141  topLevelFolder = assetFolder.ParentFolder;
142  }
143  }
144  if (topLevelFolder.Guid != folderGuid)
145  {
146  throw new SmartAPIException(parent.Session.ServerLogin,
147  string.Format(
148  "Cannot set src file '{0}', because it isn't in the current folder branch '{1}/'",
149  value, parent.Project.Folders.GetByGuid(folderGuid).Name));
150  //element.SetAttributeValue(ELTFOLDERGUID, topLevelFolder.Guid.ToRQLString());
151  }
152 //TODO at least cms 7.5 stores undefined as value, maybe "" is allowed, too, try this out
153  element.SetAttributeValue(ELTSRCSUBDIRGUID,
154  topLevelFolder.Guid == folderGuid ? "undefined" : value.Folder.Guid.ToRQLString());
155 
156  element.SetAttributeValue(ELTSRC, value.Name);
157  }
158 
159  private static void SetValuesFromAssetManagerFolder(IProjectObject parent, IXmlReadWriteWrapper element,
160  IFile value)
161  {
162  var assetFolder = (IAssetManagerFolder) value.Folder;
163  var ownFolder = GetTopLevelFolder(parent, assetFolder);
164 
165  if (assetFolder.IsSubFolder)
166  {
167  SetValuesFromSubFolder(parent, element, value, ownFolder);
168  }
169  else
170  {
171  SetValuesFromTopLevelFolder(parent, element, value, ownFolder);
172  }
173  }
174 
175  private static void SetValuesFromSubFolder(IProjectObject parent, IXmlReadWriteWrapper writeElement, IFile value,
176  IAssetManagerFolder ownFolder)
177  {
178  ownFolder = ownFolder.SubFolders.GetByName(value.Folder.Name);
179  writeElement.SetAttributeValue(ELTSRCSUBDIRGUID, ownFolder.Guid.ToRQLString());
180  SetFilename(parent, value, ownFolder, writeElement);
181  }
182 
183  private static void SetValuesFromTopLevelFolder(IProjectObject parent, IXmlReadWriteWrapper element, IFile value,
184  IFolder ownFolder)
185  {
186  var folderGuid = element.GetGuid(ELTFOLDERGUID);
187  element.SetAttributeValue(ELTSRCSUBDIRGUID,
188  ownFolder.Guid == folderGuid ? "undefined" : ownFolder.Guid.ToRQLString());
189 
190  SetFilename(parent, value, ownFolder, element);
191  }
192  }
193 }